import React, { useCallback, useMemo, useState } from 'react'; import { RefreshCw, Settings } from 'lucide-react'; import { useI18n } from '../../../application/i18n/I18nProvider'; import type { usePluginUpdateCheck } from '../../../application/state/usePluginUpdateCheck'; import type { usePluginManager } from '../../../application/state/usePluginManager'; import { Button } from '../../ui/button'; import { Input } from '../../ui/input'; import { PluginBrandIcon } from '../../plugins/PluginBrandIcon'; import { PluginAuthorRow } from '../../plugins/PluginAuthorRow'; import { SettingCard, SettingRow, SettingsSection, Toggle } from '../settings-ui'; import { PluginLogSettings } from './PluginLogSettings'; type Manager = ReturnType; export function PluginSettingsLanding({ manager, settingsAvailable, settingsError, managerButtonRef, updates, onManage }: { manager: Manager; settingsAvailable: ReadonlySet; settingsError?: string; managerButtonRef?: React.Ref; updates?: ReturnType; onManage: (pluginId: string | null, opener: HTMLElement) => void; }) { const { t, resolvedLocale } = useI18n(); const [search, setSearch] = useState(''); const localized = useCallback((value: string | Record | undefined) => typeof value === 'string' ? value : value?.[resolvedLocale] ?? value?.en ?? '', [resolvedLocale]); const nameOf = (plugin: NetcattyInstalledPlugin) => localized(plugin.manifest?.displayName) || plugin.manifest?.name || plugin.id; const visible = useMemo(() => { const term = search.trim().toLocaleLowerCase(); return manager.plugins.filter((plugin) => [localized(plugin.manifest?.displayName), plugin.manifest?.name, plugin.id, plugin.manifest?.publisher, localized(plugin.manifest?.description), plugin.source?.name].some((value) => String(value ?? '').toLocaleLowerCase().includes(term))); }, [manager.plugins, search, localized]); const statusOf = (plugin: NetcattyInstalledPlugin) => { if (!manager.statusKnown) return t('settings.plugins.permissions.statusUnknown'); const state = plugin.runtime.status; if (['starting', 'stopping', 'error', 'quarantined'].includes(state)) return t(`settings.plugins.landing.status.${state}`); if (state !== 'stopped' && state !== 'running') return t('settings.plugins.landing.status.unknown'); return null; }; return <> {updates && } }> {updates?.error &&

{updates.error}

} {updates?.result &&

{t('settings.plugins.landing.updateSummary').replace('{checked}', String(updates.result.checked)).replace('{updates}', String(updates.result.updates.length)).replace('{skipped}', String(updates.result.skipped))}{updates.result.failedSources > 0 && ` ${t('settings.plugins.landing.updateSourcesFailed').replace('{count}', String(updates.result.failedSources))}`} {t('settings.plugins.landing.updateReview')}

} {settingsError &&

{settingsError}

} {manager.error &&

{manager.error}

} {!manager.statusKnown && !manager.error &&

{t('settings.plugins.landing.loading')}

} {manager.statusKnown && !manager.available &&

{t('settings.plugins.landing.unavailable')}

} {manager.statusKnown && manager.available && manager.plugins.length === 0 &&

{t('settings.plugins.landing.empty')}

} {manager.plugins.length > 0 && <> setSearch(event.target.value)} /> {visible.length === 0 ?

{t('settings.plugins.market.noResults')}

: {visible.map((plugin) => { const name = nameOf(plugin); const description = localized(plugin.manifest?.description); const status = statusOf(plugin); const hasError = manager.statusKnown && ['error', 'quarantined'].includes(plugin.runtime.status); const availableUpdate = updates?.result?.updates.find((item) => item.pluginId === plugin.id && item.sourceId === plugin.source?.id); const declaredSettings = Boolean(plugin.manifest?.contributes?.settings?.length); return } description={
{name} {status && {status}} {plugin.activeVersion && {plugin.activeVersion}} {availableUpdate && {t('settings.plugins.landing.updateAvailable').replace('{version}', availableUpdate.availableVersion)}} {description && {description}}
}>
{declaredSettings && } void manager.setEnabled(plugin.id, enabled)} />
; })}
} }
; }