497 lines
18 KiB
TypeScript
497 lines
18 KiB
TypeScript
import { AlertTriangle, FolderOpen, PackagePlus, Palette, Settings2, ShieldAlert, Terminal, Trash2 } from 'lucide-react'
|
|
import React, { useEffect, useState } from 'react'
|
|
import { I18nKey, t } from '../i18n'
|
|
import {
|
|
ActiveThemePayload, CustomThemeScheme, Locale, PluginInfo, PluginUIContributions,
|
|
} from '../types'
|
|
import { Button, IconButton } from '../ui/controls'
|
|
import { Modal } from '../ui/dialog'
|
|
import { PopupSelect, Switch } from '../ui/settings'
|
|
import { api } from '../wails'
|
|
import { PluginPanelFrame } from './PluginPanelFrame'
|
|
import { SchemaForm, locText } from './SchemaForm'
|
|
|
|
interface Props {
|
|
locale: Locale
|
|
appearance: 'light' | 'dark'
|
|
safeMode?: boolean
|
|
onRefresh: () => void
|
|
onError: (title: string, e: unknown) => void
|
|
onThemeChanged?: () => void
|
|
}
|
|
|
|
/**
|
|
* Lazy-loaded Plugin & Theme Management section inside App Settings (§5.1, §8.1, §10).
|
|
* Keeps plugin management + SchemaForm out of the startup bundle.
|
|
*/
|
|
export const PluginsSection: React.FC<Props> = ({
|
|
locale, appearance, safeMode, onRefresh, onError, onThemeChanged,
|
|
}) => {
|
|
const tr = (k: I18nKey, v?: Record<string, string | number>) => t(locale, k, v)
|
|
const [plugins, setPlugins] = useState<PluginInfo[]>([])
|
|
const [contribs, setContribs] = useState<PluginUIContributions | null>(null)
|
|
const [activeTheme, setActiveTheme] = useState<ActiveThemePayload | null>(null)
|
|
const [busyID, setBusyID] = useState('')
|
|
const [approveTarget, setApproveTarget] = useState<PluginInfo | null>(null)
|
|
const [approveChecked, setApproveChecked] = useState(false)
|
|
const [settingsTarget, setSettingsTarget] = useState<PluginInfo | null>(null)
|
|
const [settingsValues, setSettingsValues] = useState<Record<string, unknown>>({})
|
|
const [logTarget, setLogTarget] = useState<{ id: string; text: string } | null>(null)
|
|
|
|
// Theme scheme editor state (§5.1)
|
|
const [schemeEditorOpen, setSchemeEditorOpen] = useState(false)
|
|
const [schemeName, setSchemeName] = useState('')
|
|
const [seedAccent, setSeedAccent] = useState('#007aff')
|
|
const [seedSurface, setSeedSurface] = useState('')
|
|
|
|
const reload = async () => {
|
|
try {
|
|
const [list, ui, th] = await Promise.all([
|
|
api.ListPlugins(),
|
|
api.GetPluginUIContributions(),
|
|
api.GetActiveTheme(),
|
|
])
|
|
setPlugins(list ?? [])
|
|
setContribs(ui)
|
|
setActiveTheme(th)
|
|
} catch (e) {
|
|
onError(tr('pluginFailLoad'), e)
|
|
}
|
|
}
|
|
|
|
useEffect(() => { void reload() }, [])
|
|
|
|
const togglePlugin = async (p: PluginInfo, enable: boolean) => {
|
|
if (enable && p.needs_approval) {
|
|
setApproveChecked(false)
|
|
setApproveTarget(p)
|
|
return
|
|
}
|
|
setBusyID(p.id)
|
|
try {
|
|
if (enable) await api.EnablePlugin(p.id, false)
|
|
else await api.DisablePlugin(p.id)
|
|
await reload()
|
|
onRefresh()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailToggle'), e)
|
|
} finally {
|
|
setBusyID('')
|
|
}
|
|
}
|
|
|
|
const confirmApprove = async () => {
|
|
if (!approveTarget || !approveChecked) return
|
|
const target = approveTarget
|
|
setApproveTarget(null)
|
|
setBusyID(target.id)
|
|
try {
|
|
await api.EnablePlugin(target.id, true)
|
|
await reload()
|
|
onRefresh()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailEnable'), e)
|
|
} finally {
|
|
setBusyID('')
|
|
}
|
|
}
|
|
|
|
const importLocalZip = async () => {
|
|
try {
|
|
const path = await api.PickFile(tr('pluginImportTitle'), '*.zip')
|
|
if (!path) return
|
|
await api.InstallPluginArchive(path)
|
|
await reload()
|
|
onRefresh()
|
|
} catch (e) {
|
|
onError(tr('pluginFailImport'), e)
|
|
}
|
|
}
|
|
|
|
const linkDevDir = async () => {
|
|
try {
|
|
const dir = await api.PickDirectory(tr('pluginLinkDevTitle'))
|
|
if (!dir) return
|
|
await api.LinkPluginDev(dir)
|
|
await reload()
|
|
onRefresh()
|
|
} catch (e) {
|
|
onError(tr('pluginFailLinkDev'), e)
|
|
}
|
|
}
|
|
|
|
const removePlugin = async (p: PluginInfo) => {
|
|
setBusyID(p.id)
|
|
try {
|
|
await api.RemovePlugin(p.id, false)
|
|
await reload()
|
|
onRefresh()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailRemove'), e)
|
|
} finally {
|
|
setBusyID('')
|
|
}
|
|
}
|
|
|
|
const openPluginSettings = async (p: PluginInfo) => {
|
|
try {
|
|
const vals = await api.GetPluginSettings(p.id, '')
|
|
setSettingsValues((vals ?? {}) as Record<string, unknown>)
|
|
setSettingsTarget(p)
|
|
} catch (e) {
|
|
onError(tr('pluginFailLoadSettings'), e)
|
|
}
|
|
}
|
|
|
|
const savePluginSettings = async (next: Record<string, unknown>) => {
|
|
setSettingsValues(next)
|
|
if (!settingsTarget) return
|
|
try {
|
|
await api.SetPluginSettings(settingsTarget.id, '', next)
|
|
onRefresh()
|
|
} catch (e) {
|
|
onError(tr('pluginFailSaveSettings'), e)
|
|
}
|
|
}
|
|
|
|
const openPluginLog = async (p: PluginInfo) => {
|
|
try {
|
|
const txt = await api.ReadPluginLog(p.id)
|
|
setLogTarget({ id: p.id, text: txt || tr('pluginLogEmpty') })
|
|
} catch (e) {
|
|
onError(tr('pluginFailReadLog'), e)
|
|
}
|
|
}
|
|
|
|
const selectTheme = async (themeID: string, schemeID = '') => {
|
|
try {
|
|
await api.SetActiveTheme(themeID, schemeID)
|
|
await reload()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailSwitchTheme'), e)
|
|
}
|
|
}
|
|
|
|
const saveScheme = async () => {
|
|
if (!activeTheme || !schemeName.trim()) return
|
|
const id = `custom-${Date.now()}`
|
|
const scheme: CustomThemeScheme = {
|
|
id,
|
|
theme_id: activeTheme.theme_id,
|
|
name: schemeName.trim(),
|
|
appearance,
|
|
seeds: {
|
|
accent: seedAccent,
|
|
...(seedSurface ? { surface: seedSurface } : {}),
|
|
},
|
|
}
|
|
try {
|
|
await api.SaveCustomThemeScheme(scheme)
|
|
await api.SetActiveTheme(activeTheme.theme_id, id)
|
|
setSchemeEditorOpen(false)
|
|
setSchemeName('')
|
|
await reload()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailSaveScheme'), e)
|
|
}
|
|
}
|
|
|
|
const deleteScheme = async (schemeID: string) => {
|
|
try {
|
|
await api.DeleteCustomThemeScheme(schemeID)
|
|
await reload()
|
|
onThemeChanged?.()
|
|
} catch (e) {
|
|
onError(tr('pluginFailDeleteScheme'), e)
|
|
}
|
|
}
|
|
|
|
const builtins = plugins.filter((p) => p.source === 'builtin')
|
|
const external = plugins.filter((p) => p.source !== 'builtin')
|
|
const themes = contribs?.themes ?? []
|
|
const settingsPanels = (contribs?.panels ?? []).filter((p) => p.slot === 'settings.plugin')
|
|
|
|
const trustLabel = (trLevel: string) => {
|
|
if (trLevel === 'official') return tr('pluginTrustOfficial')
|
|
if (trLevel === 'verified') return tr('pluginTrustVerified')
|
|
if (trLevel === 'dev') return tr('pluginTrustDev')
|
|
return tr('pluginTrustCommunity')
|
|
}
|
|
|
|
const stateBadge = (p: PluginInfo) => {
|
|
if (p.state === 'active') return <span className="badge badge-ok">{tr('pluginStateActive')}</span>
|
|
if (p.state === 'degraded') return <span className="badge badge-warn">{tr('pluginStateDegraded')}</span>
|
|
if (p.state === 'failed') return <span className="badge badge-error">{tr('pluginStateFailed')}</span>
|
|
if (p.state === 'pending') return <span className="badge">{tr('pluginStatePending')}</span>
|
|
return <span className="badge">{tr('pluginStateOff')}</span>
|
|
}
|
|
|
|
const renderRow = (p: PluginInfo) => {
|
|
const highPerms = (p.permissions ?? []).filter((x) => x.level === 'high')
|
|
return (
|
|
<div className="row" key={p.id}>
|
|
<div className="row-main">
|
|
<div className="row-title">
|
|
{locText(p.name, locale, p.id)}
|
|
<span className="mono muted">v{p.version}</span>
|
|
<span className="badge">{trustLabel(p.trust)}</span>
|
|
{p.tier !== 'builtin' && <span className="badge">{p.tier}</span>}
|
|
{stateBadge(p)}
|
|
</div>
|
|
<div className="row-sub">
|
|
{locText(p.description, locale, p.id)}
|
|
{p.license ? ` · ${p.license}` : ''}
|
|
{p.permissions && p.permissions.length > 0
|
|
? ` · ${tr('pluginPermissionsLabel', { list: p.permissions.map((x) => x.name).join(', ') })}`
|
|
: ''}
|
|
</div>
|
|
{(p.error || p.degraded_reason) && (
|
|
<div className="row-sub warn-text">
|
|
<AlertTriangle size={12} style={{ verticalAlign: '-2px', marginRight: 4 }} />
|
|
{p.error || p.degraded_reason}
|
|
</div>
|
|
)}
|
|
{p.needs_approval && highPerms.length > 0 && (
|
|
<div className="row-sub warn-text">
|
|
{tr('pluginHighRiskNotice')}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="row-right hstack">
|
|
{p.settings && (
|
|
<IconButton small label={tr('pluginSettings')} icon={<Settings2 size={14} />} onClick={() => void openPluginSettings(p)} />
|
|
)}
|
|
{p.log_path && (
|
|
<IconButton small label={tr('pluginViewLog')} icon={<Terminal size={14} />} onClick={() => void openPluginLog(p)} />
|
|
)}
|
|
{p.removable && (
|
|
<IconButton
|
|
small
|
|
disabled={p.in_use || busyID === p.id}
|
|
label={p.in_use ? tr('pluginInUse') : tr('pluginRemove')}
|
|
icon={<Trash2 size={14} />}
|
|
onClick={() => void removePlugin(p)}
|
|
/>
|
|
)}
|
|
<Switch
|
|
label={locText(p.name, locale, p.id)}
|
|
disabled={busyID === p.id || (p.enabled && p.in_use) || (!!safeMode && p.source !== 'builtin')}
|
|
checked={p.enabled}
|
|
onChange={(checked) => void togglePlugin(p, checked)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{themes.length > 1 && (
|
|
<section className="section">
|
|
<div className="section-title">{tr('pluginThemeSection')}</div>
|
|
<div className="list">
|
|
<div className="row">
|
|
<div className="row-main">
|
|
<div className="row-title">{tr('pluginActiveTheme')}</div>
|
|
<div className="row-sub">
|
|
{safeMode ? tr('pluginThemeSafeModeHint') : tr('pluginThemeHint')}
|
|
</div>
|
|
</div>
|
|
<div className="row-right hstack">
|
|
<PopupSelect
|
|
label={tr('pluginThemePackLabel')}
|
|
value={activeTheme?.theme_id || 'builtin.theme.default'}
|
|
options={themes.map((th) => ({
|
|
value: th.id,
|
|
label: locText(th.name, locale, th.id),
|
|
disabled: !!safeMode && th.id !== 'builtin.theme.default',
|
|
}))}
|
|
onChange={(id) => { if (!safeMode) void selectTheme(id) }}
|
|
/>
|
|
{activeTheme && activeTheme.theme_id !== 'builtin.theme.default' && (
|
|
<Button icon={<Palette size={14} />} onClick={() => setSchemeEditorOpen(true)}>
|
|
{tr('pluginNewScheme')}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
{activeTheme && activeTheme.theme_id !== 'builtin.theme.default' && (
|
|
<div className="row">
|
|
<div className="row-main">
|
|
<div className="row-title">{tr('pluginColorSchemes')}</div>
|
|
<div className="row-sub">{tr('pluginColorSchemesHint')}</div>
|
|
</div>
|
|
<div className="row-right hstack">
|
|
{(activeTheme.custom_schemes ?? []).filter((s) => s.theme_id === activeTheme.theme_id).map((s) => (
|
|
<span className="hstack" key={s.id}>
|
|
<Button
|
|
variant={activeTheme.active_scheme === s.id ? 'primary' : undefined}
|
|
onClick={() => void selectTheme(activeTheme.theme_id, s.id)}
|
|
>
|
|
{s.name}
|
|
</Button>
|
|
<IconButton small label={tr('pluginDeleteScheme')} icon={<Trash2 size={13} />} onClick={() => void deleteScheme(s.id)} />
|
|
</span>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
<section className="section">
|
|
<div className="section-title hstack" style={{ justifyContent: 'space-between' }}>
|
|
<span>{tr('pluginExternalSection')}</span>
|
|
<span className="hstack">
|
|
<Button icon={<FolderOpen size={14} />} onClick={linkDevDir}>
|
|
{tr('pluginLinkDevBtn')}
|
|
</Button>
|
|
<Button icon={<PackagePlus size={14} />} onClick={importLocalZip}>
|
|
{tr('pluginImportBtn')}
|
|
</Button>
|
|
</span>
|
|
</div>
|
|
<div className="list">
|
|
{external.length === 0 ? (
|
|
<div className="row"><div className="row-sub">{tr('pluginExternalEmpty')}</div></div>
|
|
) : external.map(renderRow)}
|
|
</div>
|
|
</section>
|
|
|
|
<section className="section">
|
|
<div className="section-title">{tr('pluginBuiltinSection')}</div>
|
|
<div className="list">{builtins.map(renderRow)}</div>
|
|
</section>
|
|
|
|
{settingsPanels.length > 0 && contribs?.asset_token ? (
|
|
<section className="section" data-rg-slot="settings.plugin">
|
|
<div className="section-title">{tr('pluginPanelsSection')}</div>
|
|
{settingsPanels.map((panel) => (
|
|
<PluginPanelFrame
|
|
key={`${panel.pluginId}:${panel.id}`}
|
|
locale={locale}
|
|
panel={panel}
|
|
assetToken={contribs.asset_token!}
|
|
appearance={appearance}
|
|
/>
|
|
))}
|
|
</section>
|
|
) : (
|
|
<div className="rg-slot-anchor" data-rg-slot="settings.plugin" />
|
|
)}
|
|
|
|
{approveTarget && (
|
|
<Modal
|
|
title={tr('pluginApproveTitle')}
|
|
subtitle={locText(approveTarget.name, locale, approveTarget.id)}
|
|
closeLabel={tr('close')}
|
|
onClose={() => setApproveTarget(null)}
|
|
>
|
|
<div className="stack-3" style={{ padding: 'var(--sp-4)' }}>
|
|
{(approveTarget.tier === 'L3' || (approveTarget.permissions ?? []).some((p) => p.name === 'ui:script')) && (
|
|
<div className="notice notice-warn">
|
|
<ShieldAlert size={15} aria-hidden="true" style={{ marginRight: 6, verticalAlign: '-2px' }} />
|
|
{tr('pluginApproveL3Warn')}
|
|
</div>
|
|
)}
|
|
<div className="list">
|
|
<div className="row"><div className="row-main"><div className="row-title">{tr('pluginPublisherLicense')}</div><div className="row-sub">{approveTarget.publisher || '—'} · {approveTarget.license || '—'}</div></div></div>
|
|
{approveTarget.entry_path && (
|
|
<div className="row"><div className="row-main"><div className="row-title">{tr('pluginEntryPath')}</div><div className="row-sub mono selectable">{approveTarget.entry_path}</div></div></div>
|
|
)}
|
|
{approveTarget.entry_hash && (
|
|
<div className="row"><div className="row-main"><div className="row-title">SHA-256</div><div className="row-sub mono selectable">{approveTarget.entry_hash}</div></div></div>
|
|
)}
|
|
<div className="row"><div className="row-main"><div className="row-title">{tr('pluginRequestedPerms')}</div><div className="row-sub mono">{(approveTarget.permissions ?? []).length > 0 ? (approveTarget.permissions ?? []).map((p) => `${p.name} (${p.level})`).join(', ') : '—'}</div></div></div>
|
|
</div>
|
|
<label className="hstack" style={{ cursor: 'pointer' }}>
|
|
<input type="checkbox" checked={approveChecked} onChange={(e) => setApproveChecked(e.target.checked)} />
|
|
<span>{tr('pluginApproveCheckbox')}</span>
|
|
</label>
|
|
</div>
|
|
<div className="dialog-foot">
|
|
<span />
|
|
<span className="hstack">
|
|
<Button variant="ghost" onClick={() => setApproveTarget(null)}>{tr('cancel')}</Button>
|
|
<Button variant="primary" disabled={!approveChecked} onClick={() => void confirmApprove()}>
|
|
{tr('pluginApproveConfirm')}
|
|
</Button>
|
|
</span>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
|
|
{settingsTarget && settingsTarget.settings && (
|
|
<Modal
|
|
title={locText(settingsTarget.name, locale, settingsTarget.id)}
|
|
subtitle={tr('pluginGlobalSettings')}
|
|
closeLabel={tr('close')}
|
|
onClose={() => setSettingsTarget(null)}
|
|
>
|
|
<div style={{ padding: 'var(--sp-4)' }}>
|
|
<SchemaForm
|
|
locale={locale}
|
|
schema={settingsTarget.settings}
|
|
values={settingsValues}
|
|
onChange={(next) => void savePluginSettings(next)}
|
|
/>
|
|
</div>
|
|
<div className="dialog-foot">
|
|
<span />
|
|
<Button variant="primary" onClick={() => setSettingsTarget(null)}>{tr('pluginDone')}</Button>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
|
|
{logTarget && (
|
|
<Modal
|
|
title={tr('pluginLogTitle', { id: logTarget.id })}
|
|
closeLabel={tr('close')}
|
|
onClose={() => setLogTarget(null)}
|
|
>
|
|
<pre className="mono selectable" style={{ padding: 'var(--sp-4)', maxHeight: 360, overflow: 'auto', margin: 0 }}>
|
|
{logTarget.text}
|
|
</pre>
|
|
</Modal>
|
|
)}
|
|
|
|
{schemeEditorOpen && (
|
|
<Modal
|
|
title={tr('pluginSchemeModalTitle')}
|
|
closeLabel={tr('close')}
|
|
onClose={() => setSchemeEditorOpen(false)}
|
|
>
|
|
<div className="stack-3" style={{ padding: 'var(--sp-4)' }}>
|
|
<label className="stack-1">
|
|
<span>{tr('pluginSchemeName')}</span>
|
|
<input className="input" value={schemeName} onChange={(e) => setSchemeName(e.target.value)} placeholder={tr('pluginSchemePlaceholder')} />
|
|
</label>
|
|
<label className="hstack">
|
|
<span>{tr('pluginAccentSeed')}</span>
|
|
<input type="color" value={seedAccent} onChange={(e) => setSeedAccent(e.target.value)} />
|
|
<span className="mono">{seedAccent}</span>
|
|
</label>
|
|
<label className="stack-1">
|
|
<span>{tr('pluginSurfaceSeed')}</span>
|
|
<input className="input mono" value={seedSurface} onChange={(e) => setSeedSurface(e.target.value)} placeholder="#1d1e23" />
|
|
</label>
|
|
</div>
|
|
<div className="dialog-foot">
|
|
<span />
|
|
<span className="hstack">
|
|
<Button variant="ghost" onClick={() => setSchemeEditorOpen(false)}>{tr('cancel')}</Button>
|
|
<Button variant="primary" disabled={!schemeName.trim()} onClick={() => void saveScheme()}>{tr('save')}</Button>
|
|
</span>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
</>
|
|
)
|
|
}
|