import { useEffect, useId, useState } from "react"; import { useI18n } from "../../../application/i18n/I18nProvider"; import { applyPluginThemeSelection, usePluginThemeRevision, } from "../../../application/state/usePluginThemeRuntime"; import { pluginThemeRegistry } from "../../../infrastructure/config/pluginThemeRegistry"; import { Button } from "../../ui/button"; export function PluginThemePicker({ mode, onRestore, }: { mode: "light" | "dark"; onRestore: () => void; }) { const { t } = useI18n(); usePluginThemeRevision(); const owner = useId(); const [selected, setSelected] = useState(""), [pending, setPending] = useState(false), [error, setError] = useState(""); const catalog = pluginThemeRegistry.getCatalog(); const entries = catalog.entries.filter((e) => e.palette.base === mode); const applied = catalog.selection[mode]; const appliedAvailable = applied && entries.some( (e) => e.ref.pluginId === applied.ref.pluginId && e.ref.themeId === applied.ref.themeId && e.ref.version === applied.ref.version && e.ref.digest === applied.ref.digest, ); const entry = entries.find( (e) => `${e.ref.pluginId}/${e.ref.themeId}` === selected, ); useEffect( () => () => pluginThemeRegistry.cancelPreview(owner), [owner, mode], ); async function apply(restore = false) { if (pending || (!restore && !entry)) return; setPending(true); setError(""); try { await applyPluginThemeSelection( mode, restore ? null : { ref: entry!.ref }, ); pluginThemeRegistry.cancelPreview(owner); if (restore) { setSelected(""); onRestore(); } } catch (e) { setError( e instanceof Error ? e.message : t("settings.plugins.themes.failed"), ); } finally { setPending(false); } } return (

{t("settings.plugins.themes.title")}

{t("settings.plugins.themes.description")}

{entries.length === 0 ? (

{t("settings.plugins.themes.empty")}

) : ( )} {catalog.selection[mode] && (

{t( appliedAvailable ? "settings.plugins.themes.applied" : "settings.plugins.themes.unavailable", )} : {catalog.selection[mode]!.ref.themeId} ·{" "} {catalog.selection[mode]!.ref.version}

)}
{catalog.diagnostics.length > 0 && (

{t("settings.plugins.themes.invalid")}

)} {error && (

{error}

)}
); }