2021-07-12 02:49:01 -05:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useAsync } from 'react-use';
|
2021-09-09 05:20:35 -05:00
|
|
|
import { CatalogPlugin, CatalogPluginsState } from '../types';
|
2021-07-08 10:50:42 -05:00
|
|
|
import { api } from '../api';
|
2021-09-09 05:20:35 -05:00
|
|
|
import { mapLocalToCatalog, mapRemoteToCatalog, mapToCatalogPlugin } from '../helpers';
|
2021-07-08 10:50:42 -05:00
|
|
|
|
2021-07-20 08:20:24 -05:00
|
|
|
export function usePlugins(): CatalogPluginsState {
|
|
|
|
const { loading, value, error } = useAsync(async () => {
|
|
|
|
const remote = await api.getRemotePlugins();
|
|
|
|
const installed = await api.getInstalledPlugins();
|
|
|
|
return { remote, installed };
|
|
|
|
}, []);
|
2021-07-08 10:50:42 -05:00
|
|
|
|
2021-07-20 08:20:24 -05:00
|
|
|
const plugins = useMemo(() => {
|
|
|
|
const installed = value?.installed || [];
|
|
|
|
const remote = value?.remote || [];
|
|
|
|
const unique: Record<string, CatalogPlugin> = {};
|
2021-07-08 10:50:42 -05:00
|
|
|
|
2021-07-20 08:20:24 -05:00
|
|
|
for (const plugin of installed) {
|
|
|
|
unique[plugin.id] = mapLocalToCatalog(plugin);
|
|
|
|
}
|
2021-07-08 10:50:42 -05:00
|
|
|
|
2021-07-20 08:20:24 -05:00
|
|
|
for (const plugin of remote) {
|
|
|
|
if (plugin.typeCode === 'renderer') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Boolean(plugin.versionSignatureType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-08-04 08:09:57 -05:00
|
|
|
if (unique[plugin.slug]) {
|
|
|
|
unique[plugin.slug] = mapToCatalogPlugin(
|
|
|
|
installed.find((installedPlugin) => installedPlugin.id === plugin.slug),
|
|
|
|
plugin
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
unique[plugin.slug] = mapRemoteToCatalog(plugin);
|
|
|
|
}
|
2021-07-20 08:20:24 -05:00
|
|
|
}
|
|
|
|
return Object.values(unique);
|
|
|
|
}, [value?.installed, value?.remote]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
plugins,
|
|
|
|
};
|
|
|
|
}
|