import { Settings, RefreshCw, Trash2, XCircle, Loader2, RotateCcw, Cog, Globe, Image, Code, Monitor, Award, Bug, BarChart3, FolderTree, Terminal as TerminalIcon, GitBranch } from "lucide-react"; import { useTranslation } from 'react-i18next'; import { useState, useEffect, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { SettingsPageLayout } from '@/components/SettingsPageLayout'; import { Input } from "@/components/ui/input"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'; import { useAppContext } from "@/hooks/useAppContext"; import { useOffline } from "@/hooks/useOffline"; import { useToast } from "@/hooks/useToast"; import { Terminal } from "@/components/Terminal"; export function SystemSettings() { const { t } = useTranslation(); const { config, defaultConfig, updateConfig } = useAppContext(); const { toast } = useToast(); const { serviceWorkerRegistration, updateServiceWorker, clearCache } = useOffline(); const [esmUrlInput, setEsmUrlInput] = useState(config.esmUrl); const [corsProxyInput, setCorsProxyInput] = useState(config.corsProxy); const [faviconUrlInput, setFaviconUrlInput] = useState(config.faviconUrl); const [ngitWebUrlInput, setNgitWebUrlInput] = useState(config.ngitWebUrl); const [previewDomainInput, setPreviewDomainInput] = useState(config.previewDomain); const [showcaseCuratorInput, setShowcaseCuratorInput] = useState(config.showcaseCurator); const [fsPathProjectsInput, setFsPathProjectsInput] = useState(config.fsPathProjects); const [fsPathConfigInput, setFsPathConfigInput] = useState(config.fsPathConfig); const [fsPathTmpInput, setFsPathTmpInput] = useState(config.fsPathTmp); const [fsPathPluginsInput, setFsPathPluginsInput] = useState(config.fsPathPlugins); const [fsPathTemplatesInput, setFsPathTemplatesInput] = useState(config.fsPathTemplates); const [sentryDsnInput, setSentryDsnInput] = useState(config.sentryDsn); const [plausibleDomainInput, setPlausibleDomainInput] = useState(config.plausibleDomain); const [plausibleEndpointInput, setPlausibleEndpointInput] = useState(config.plausibleEndpoint); // Check which settings differ from defaults const isModified = useMemo(() => ({ esmUrl: config.esmUrl !== defaultConfig.esmUrl, corsProxy: config.corsProxy !== defaultConfig.corsProxy, faviconUrl: config.faviconUrl !== defaultConfig.faviconUrl, ngitWebUrl: config.ngitWebUrl !== defaultConfig.ngitWebUrl, previewDomain: config.previewDomain !== defaultConfig.previewDomain, showcaseEnabled: config.showcaseEnabled !== defaultConfig.showcaseEnabled, showcaseCurator: config.showcaseCurator !== defaultConfig.showcaseCurator, fsPathProjects: config.fsPathProjects !== defaultConfig.fsPathProjects, fsPathConfig: config.fsPathConfig !== defaultConfig.fsPathConfig, fsPathTmp: config.fsPathTmp !== defaultConfig.fsPathTmp, fsPathPlugins: config.fsPathPlugins !== defaultConfig.fsPathPlugins, fsPathTemplates: config.fsPathTemplates !== defaultConfig.fsPathTemplates, sentryDsn: config.sentryDsn !== defaultConfig.sentryDsn, plausibleDomain: config.plausibleDomain !== defaultConfig.plausibleDomain, plausibleEndpoint: config.plausibleEndpoint !== defaultConfig.plausibleEndpoint, }), [config, defaultConfig]); // Restore functions - clear the value from config to use the default const restoreEsmUrl = () => { const defaultValue = defaultConfig.esmUrl; setEsmUrlInput(defaultValue); updateConfig((current) => { const { esmUrl, ...rest } = current; return rest; }); }; const restoreCorsProxy = () => { const defaultValue = defaultConfig.corsProxy; setCorsProxyInput(defaultValue); updateConfig((current) => { const { corsProxy, ...rest } = current; return rest; }); }; const restoreFaviconUrl = () => { const defaultValue = defaultConfig.faviconUrl; setFaviconUrlInput(defaultValue); updateConfig((current) => { const { faviconUrl, ...rest } = current; return rest; }); }; const restoreNgitWebUrl = () => { const defaultValue = defaultConfig.ngitWebUrl; setNgitWebUrlInput(defaultValue); updateConfig((current) => { const { ngitWebUrl, ...rest } = current; return rest; }); }; const restorePreviewDomain = () => { const defaultValue = defaultConfig.previewDomain; setPreviewDomainInput(defaultValue); updateConfig((current) => { const { previewDomain, ...rest } = current; return rest; }); }; const restoreShowcaseEnabled = () => { updateConfig((current) => { const { showcaseEnabled, ...rest } = current; return rest; }); }; const restoreShowcaseCurator = () => { const defaultValue = defaultConfig.showcaseCurator; setShowcaseCuratorInput(defaultValue); updateConfig((current) => { const { showcaseCurator, ...rest } = current; return rest; }); }; const restoreFsPathProjects = () => { const defaultValue = defaultConfig.fsPathProjects; setFsPathProjectsInput(defaultValue); updateConfig((current) => { const { fsPathProjects, ...rest } = current; return rest; }); }; const restoreFsPathConfig = () => { const defaultValue = defaultConfig.fsPathConfig; setFsPathConfigInput(defaultValue); updateConfig((current) => { const { fsPathConfig, ...rest } = current; return rest; }); }; const restoreFsPathTmp = () => { const defaultValue = defaultConfig.fsPathTmp; setFsPathTmpInput(defaultValue); updateConfig((current) => { const { fsPathTmp, ...rest } = current; return rest; }); }; const restoreFsPathPlugins = () => { const defaultValue = defaultConfig.fsPathPlugins; setFsPathPluginsInput(defaultValue); updateConfig((current) => { const { fsPathPlugins, ...rest } = current; return rest; }); }; const restoreFsPathTemplates = () => { const defaultValue = defaultConfig.fsPathTemplates; setFsPathTemplatesInput(defaultValue); updateConfig((current) => { const { fsPathTemplates, ...rest } = current; return rest; }); }; const restoreSentryDsn = () => { const defaultValue = defaultConfig.sentryDsn; setSentryDsnInput(defaultValue); updateConfig((current) => { const { sentryDsn, ...rest } = current; return rest; }); }; const restorePlausibleDomain = () => { const defaultValue = defaultConfig.plausibleDomain; setPlausibleDomainInput(defaultValue); updateConfig((current) => { const { plausibleDomain, ...rest } = current; return rest; }); }; const restorePlausibleEndpoint = () => { const defaultValue = defaultConfig.plausibleEndpoint; setPlausibleEndpointInput(defaultValue); updateConfig((current) => { const { plausibleEndpoint, ...rest } = current; return rest; }); }; // Service Worker state const [swState, setSwState] = useState(''); const [isUpdatingSW, setIsUpdatingSW] = useState(false); const [isUnregisteringSW, setIsUnregisteringSW] = useState(false); const [isClearingCache, setIsClearingCache] = useState(false); // Monitor Service Worker state useEffect(() => { if (serviceWorkerRegistration?.active) { setSwState(serviceWorkerRegistration.active.state); const handleStateChange = () => { if (serviceWorkerRegistration.active) { setSwState(serviceWorkerRegistration.active.state); } }; serviceWorkerRegistration.active.addEventListener('statechange', handleStateChange); return () => { serviceWorkerRegistration.active?.removeEventListener('statechange', handleStateChange); }; } }, [serviceWorkerRegistration]); const handleUpdateSW = async () => { setIsUpdatingSW(true); try { await updateServiceWorker(); toast({ title: t('serviceWorkerUpdated'), description: t('serviceWorkerUpdatedDescription'), }); } catch (error) { toast({ title: t('serviceWorkerUpdateFailed'), description: error instanceof Error ? error.message : "Failed to update service worker", variant: "destructive", }); } finally { setIsUpdatingSW(false); } }; const handleUnregisterSW = async () => { setIsUnregisteringSW(true); try { if (serviceWorkerRegistration) { const success = await serviceWorkerRegistration.unregister(); if (success) { toast({ title: t('serviceWorkerUnregistered'), description: t('serviceWorkerUnregisteredDescription'), }); } else { throw new Error("Failed to unregister service worker"); } } } catch (error) { toast({ title: t('serviceWorkerUnregisterFailed'), description: error instanceof Error ? error.message : "Failed to unregister service worker", variant: "destructive", }); } finally { setIsUnregisteringSW(false); } }; const handleClearCache = async () => { setIsClearingCache(true); try { await clearCache(); toast({ title: t('serviceWorkerCacheCleared'), description: t('serviceWorkerCacheClearedDescription'), }); } catch (error) { toast({ title: t('serviceWorkerClearCacheFailed'), description: error instanceof Error ? error.message : "Failed to clear cache", variant: "destructive", }); } finally { setIsClearingCache(false); } }; return ( {/* Service Worker Configuration */}

{t('serviceWorker')}

{serviceWorkerRegistration ? ( <> {/* Service Worker Status */}
{t('serviceWorkerStatus')} {swState || 'Unknown'}
{t('serviceWorkerScope')} {serviceWorkerRegistration.scope}
{serviceWorkerRegistration.active && (
{t('serviceWorkerScriptUrl')} {serviceWorkerRegistration.active.scriptURL.split('/').pop()}
)}
{/* Service Worker Controls */}
) : (
{t('serviceWorkerNotRegistered')}
)}
{/* CORS Proxy Configuration */}

{t('corsProxy')}

{isModified.corsProxy && (
)}
{ const value = e.target.value; setCorsProxyInput(value); updateConfig((current) => ({ ...current, corsProxy: value, })); }} className="flex-1" /> {isModified.corsProxy && ( )}

{t('corsProxyDescription')}

{/* Favicon URL Configuration */}

{t('faviconUrl')}

{isModified.faviconUrl && (
)}
{ const value = e.target.value; setFaviconUrlInput(value); updateConfig((current) => ({ ...current, faviconUrl: value, })); }} className="flex-1" /> {isModified.faviconUrl && ( )}

{t('faviconUrlDescription')}

{/* Nostr Git Web URL Configuration */}

{t('ngitWebUrl')}

{isModified.ngitWebUrl && (
)}
{ const value = e.target.value; setNgitWebUrlInput(value); updateConfig((current) => ({ ...current, ngitWebUrl: value, })); }} className="flex-1" /> {isModified.ngitWebUrl && ( )}

{t('ngitWebUrlDescription')}

{/* JavaScript CDN Configuration */}

{t('esmUrl')}

{isModified.esmUrl && (
)}
{ // Strip trailing slash on save. const value = e.target.value; setEsmUrlInput(value); updateConfig((current) => ({ ...current, esmUrl: value.replace(/\/+$/, ''), })); }} className="flex-1" /> {isModified.esmUrl && ( )}

{t('esmUrlDescription')}

{/* Preview Domain Configuration */}

{t('previewDomain')}

{isModified.previewDomain && (
)}
{ const value = e.target.value; setPreviewDomainInput(value); updateConfig((current) => ({ ...current, previewDomain: value, })); }} className="flex-1" /> {isModified.previewDomain && ( )}

{t('previewDomainDescription')}

{/* Showcase Configuration */}

{t('showcase')}

{(isModified.showcaseEnabled || isModified.showcaseCurator) && (
)}
{isModified.showcaseEnabled && (
)}
{ updateConfig((current) => ({ ...current, showcaseEnabled: checked, })); }} /> {isModified.showcaseEnabled && ( )}

{t('showcaseEnabledDescription')}

{isModified.showcaseCurator && (
)}
{ const value = e.target.value; setShowcaseCuratorInput(value); updateConfig((current) => ({ ...current, showcaseCurator: value, })); }} className="flex-1" /> {isModified.showcaseCurator && ( )}

{t('showcaseCuratorDescription')}

{/* Sentry DSN Configuration */}

{t('sentryDsn')}

{isModified.sentryDsn && (
)}
{ const value = e.target.value; setSentryDsnInput(value); updateConfig((current) => ({ ...current, sentryDsn: value, })); }} className="flex-1" /> {isModified.sentryDsn && ( )}

{t('sentryDsnDescription')}

{/* Plausible Analytics Configuration */}

{t('plausibleAnalytics')}

{(isModified.plausibleDomain || isModified.plausibleEndpoint) && (
)}
{isModified.plausibleDomain && (
)}
{ const value = e.target.value; setPlausibleDomainInput(value); updateConfig((current) => ({ ...current, plausibleDomain: value, })); }} className="flex-1" /> {isModified.plausibleDomain && ( )}

{t('plausibleDomainDescription')}

{isModified.plausibleEndpoint && (
)}
{ const value = e.target.value; setPlausibleEndpointInput(value); updateConfig((current) => ({ ...current, plausibleEndpoint: value, })); }} className="flex-1" /> {isModified.plausibleEndpoint && ( )}

{t('plausibleEndpointDescription')}

{/* Filesystem Paths Configuration */}

{t('filesystemPaths')}

{(isModified.fsPathProjects || isModified.fsPathConfig || isModified.fsPathTmp || isModified.fsPathPlugins || isModified.fsPathTemplates) && (
)}
{isModified.fsPathProjects && (
)}
{ const value = e.target.value; setFsPathProjectsInput(value); updateConfig((current) => ({ ...current, fsPathProjects: value, })); }} className="flex-1" /> {isModified.fsPathProjects && ( )}
{isModified.fsPathConfig && (
)}
{ const value = e.target.value; setFsPathConfigInput(value); updateConfig((current) => ({ ...current, fsPathConfig: value, })); }} className="flex-1" /> {isModified.fsPathConfig && ( )}
{isModified.fsPathTmp && (
)}
{ const value = e.target.value; setFsPathTmpInput(value); updateConfig((current) => ({ ...current, fsPathTmp: value, })); }} className="flex-1" /> {isModified.fsPathTmp && ( )}
{isModified.fsPathPlugins && (
)}
{ const value = e.target.value; setFsPathPluginsInput(value); updateConfig((current) => ({ ...current, fsPathPlugins: value, })); }} className="flex-1" /> {isModified.fsPathPlugins && ( )}
{isModified.fsPathTemplates && (
)}
{ const value = e.target.value; setFsPathTemplatesInput(value); updateConfig((current) => ({ ...current, fsPathTemplates: value, })); }} className="flex-1" /> {isModified.fsPathTemplates && ( )}
{/* Terminal */}

{t('terminal')}

); } export default SystemSettings;