import { Fragment, type ReactNode } from 'react' import { Save, Loader2, Link2, Unlink, CheckCircle, Puzzle, Zap } from 'lucide-react' import { resolvePluginIcon } from '../shared/PluginIcon' import CustomSelect from '../shared/CustomSelect' import ConfirmDialog from '../shared/ConfirmDialog' import { fs } from '../shared/DialogShell' import { EditorField, INPUT } from '../shared/dialogParts' import { SettingRow, SettingRows, SettingsCard, SettingsHint, SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER, SETTINGS_BUTTON_PRIMARY } from './settingsKit' import PluginFrame from '../Plugins/PluginFrame' import type { PluginUserSettingField } from '../../api/client' import { usePluginStore } from '../../store/pluginStore' import { useTranslation } from '../../i18n' import PluginActivityPanel from './PluginActivityPanel' import { usePluginOAuth, usePluginUserSettings, type PluginOAuthState } from '../Plugins/usePluginUserSettings' type FieldGroup = { kind: 'switches' | 'fields'; fields: PluginUserSettingField[] } /** Runs of on/off fields become one box of switch rows; everything else stays a field. */ function groupFields(fields: PluginUserSettingField[]): FieldGroup[] { const groups: FieldGroup[] = [] for (const f of fields) { const kind = f.input_type === 'checkbox' ? 'switches' : 'fields' const last = groups[groups.length - 1] if (last && last.kind === kind) last.fields.push(f) else groups.push({ kind, fields: [f] }) } return groups } /** * A declared checkbox in the look of ToggleSwitch. It stays a real checkbox * underneath, so a label points at it and assistive tech reads it as one. */ function CheckSwitch({ id, checked, onChange }: { id: string; checked: boolean; onChange: (checked: boolean) => void }) { return ( onChange(e.target.checked)} className="peer absolute inset-0 z-10 m-0 h-full w-full cursor-pointer opacity-0" /> : t('settings.plugins.oauth.notConnected')} control={state.connected ? : } /> ) } /** * A user's own per-plugin settings (#plugins). The host renders the plugin's * declared `scope:'user'` fields as an editable form — a plugin never ships markup * here; the field list is trusted, validated manifest data. Secrets stay write-only * (masked, never echoed back). One form per active plugin that declares user fields. */ function PluginSettingsForm({ id, name, icon }: { id: string; name: string; icon: string | null }) { const { t } = useTranslation() const { fields, values, setValue, hasFields, visible, saving, save, actions, running, actionResult, runAction, performAction, pendingAction, setPendingAction, oauth, setOauth, } = usePluginUserSettings(id) if (!visible || fields === null) return null const fieldId = (key: string) => `plugin-setting-${id}-${key}` const labelOf = (f: PluginUserSettingField): ReactNode => ( <>{f.label || f.key}{f.required && *} ) return ( {hasFields && (
{groupFields(fields ?? []).map(group => group.kind === 'switches' ? ( // Consecutive on/off fields share one box of rows, like the rest of settings. {group.fields.map(f => ( setValue(f.key, checked)} /> } /> ))} ) : ( {group.fields.map(f => ( {f.input_type === 'select' && f.options ? ( setValue(f.key, String(v))} options={[{ value: '', label: '—' }, ...f.options.map(o => ({ value: o.value, label: o.label }))]} /> ) : ( setValue(f.key, e.target.value)} className={INPUT} /> )} ))} ))}
)} {actions.length > 0 && (

{t('settings.plugins.actions')}

{actions.map(a => { const res = actionResult[a.key] return (
{a.hint && {a.hint}} {res && ( {res.message || (res.ok ? t('common.success') : t('common.error'))} )}
) })}
)} setPendingAction(null)} onConfirm={() => { if (pendingAction) void performAction(pendingAction) }} message={t('settings.plugins.actions.confirm')} confirmLabel={pendingAction?.label} danger />
) } /** * A plugin's OWN settings surface (capabilities.settingsUi): its sandboxed * client/settings.html framed inside the familiar settings card. Unlike the * declared-fields form above, the plugin renders this itself — same opaque-origin * sandbox and postMessage bridge as its widget, so it can only reach its own * routes. The frame auto-sizes via trek:resize like a dashboard widget. */ function PluginSettingsUiCard({ id, name, icon }: { id: string; name: string; icon: string | null }) { return ( {/* min-height covers the beat before the frame's first trek:resize lands. color-scheme light on the frame element matches the sandboxed document's default ("normal"): mismatched schemes make Chromium paint an opaque white canvas behind the transparent frame (same trap .hero-overlay-frame guards against), which glares in dark mode. */}
) } export default function PluginSettingsTab() { const { t } = useTranslation() const plugins = usePluginStore(s => s.plugins) return (
{plugins.length === 0 ? {t('settings.plugins.empty')} : null} {plugins.map(p => ( {p.settingsUi && } ))}
) }