|
| 1 | +import { PluginStatus } from '@jellyfin/sdk/lib/generated-client/models/plugin-status'; |
| 2 | +import { useMemo } from 'react'; |
| 3 | + |
| 4 | +import { useApi } from 'hooks/useApi'; |
| 5 | + |
| 6 | +import { PluginCategory } from '../constants/pluginCategory'; |
| 7 | +import type { PluginDetails } from '../types/PluginDetails'; |
| 8 | + |
| 9 | +import { findBestConfigurationPage } from './configurationPage'; |
| 10 | +import { findBestPluginInfo } from './pluginInfo'; |
| 11 | +import { useConfigurationPages } from './useConfigurationPages'; |
| 12 | +import { usePackages } from './usePackages'; |
| 13 | +import { usePlugins } from './usePlugins'; |
| 14 | + |
| 15 | +export const usePluginDetails = () => { |
| 16 | + const { api } = useApi(); |
| 17 | + |
| 18 | + const { |
| 19 | + data: configurationPages, |
| 20 | + isError: isConfigurationPagesError, |
| 21 | + isPending: isConfigurationPagesPending |
| 22 | + } = useConfigurationPages(); |
| 23 | + |
| 24 | + const { |
| 25 | + data: packages, |
| 26 | + isError: isPackagesError, |
| 27 | + isPending: isPackagesPending |
| 28 | + } = usePackages(); |
| 29 | + |
| 30 | + const { |
| 31 | + data: plugins, |
| 32 | + isError: isPluginsError, |
| 33 | + isPending: isPluginsPending |
| 34 | + } = usePlugins(); |
| 35 | + |
| 36 | + const pluginDetails = useMemo<PluginDetails[]>(() => { |
| 37 | + if (!isPackagesPending && !isPluginsPending) { |
| 38 | + const pluginIds = new Set<string>(); |
| 39 | + packages?.forEach(({ guid }) => { |
| 40 | + if (guid) pluginIds.add(guid); |
| 41 | + }); |
| 42 | + plugins?.forEach(({ Id }) => { |
| 43 | + if (Id) pluginIds.add(Id); |
| 44 | + }); |
| 45 | + |
| 46 | + return Array.from(pluginIds) |
| 47 | + .map(id => { |
| 48 | + const packageInfo = packages?.find(pkg => pkg.guid === id); |
| 49 | + const pluginInfo = findBestPluginInfo(id, plugins); |
| 50 | + |
| 51 | + let version; |
| 52 | + if (pluginInfo) { |
| 53 | + // Find the installed version |
| 54 | + const repoVersion = packageInfo?.versions?.find(v => v.version === pluginInfo.Version); |
| 55 | + version = repoVersion || { |
| 56 | + version: pluginInfo.Version, |
| 57 | + VersionNumber: pluginInfo.Version |
| 58 | + }; |
| 59 | + } else { |
| 60 | + // Use the latest version |
| 61 | + version = packageInfo?.versions?.[0]; |
| 62 | + } |
| 63 | + |
| 64 | + let imageUrl; |
| 65 | + if (pluginInfo?.HasImage) { |
| 66 | + imageUrl = api?.getUri(`/Plugins/${pluginInfo.Id}/${pluginInfo.Version}/Image`); |
| 67 | + } |
| 68 | + |
| 69 | + let category = packageInfo?.category; |
| 70 | + if (!packageInfo) { |
| 71 | + switch (id) { |
| 72 | + case 'a629c0dafac54c7e931a7174223f14c8': // AudioDB |
| 73 | + case '8c95c4d2e50c4fb0a4f36c06ff0f9a1a': // MusicBrainz |
| 74 | + category = PluginCategory.Music; |
| 75 | + break; |
| 76 | + case 'a628c0dafac54c7e9d1a7134223f14c8': // OMDb |
| 77 | + case 'b8715ed16c4745289ad3f72deb539cd4': // TMDb |
| 78 | + category = PluginCategory.MoviesAndShows; |
| 79 | + break; |
| 80 | + case '872a78491171458da6fb3de3d442ad30': // Studio Images |
| 81 | + category = PluginCategory.General; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return { |
| 86 | + canUninstall: !!pluginInfo?.CanUninstall, |
| 87 | + category, |
| 88 | + description: pluginInfo?.Description || packageInfo?.description || packageInfo?.overview, |
| 89 | + id, |
| 90 | + imageUrl: imageUrl || packageInfo?.imageUrl || undefined, |
| 91 | + isEnabled: pluginInfo?.Status !== PluginStatus.Disabled, |
| 92 | + name: pluginInfo?.Name || packageInfo?.name, |
| 93 | + owner: packageInfo?.owner, |
| 94 | + status: pluginInfo?.Status, |
| 95 | + configurationPage: findBestConfigurationPage(configurationPages || [], id), |
| 96 | + version, |
| 97 | + versions: packageInfo?.versions || [] |
| 98 | + }; |
| 99 | + }) |
| 100 | + .sort(({ name: nameA }, { name: nameB }) => ( |
| 101 | + (nameA || '').localeCompare(nameB || '') |
| 102 | + )); |
| 103 | + } |
| 104 | + |
| 105 | + return []; |
| 106 | + }, [ |
| 107 | + api, |
| 108 | + configurationPages, |
| 109 | + isPluginsPending, |
| 110 | + packages, |
| 111 | + plugins |
| 112 | + ]); |
| 113 | + |
| 114 | + return { |
| 115 | + data: pluginDetails, |
| 116 | + isError: isConfigurationPagesError || isPackagesError || isPluginsError, |
| 117 | + isPending: isConfigurationPagesPending || isPackagesPending || isPluginsPending |
| 118 | + }; |
| 119 | +}; |
0 commit comments