import { Settings, ArrowLeft, RefreshCw, Trash2, XCircle, Loader2, RotateCcw } from "lucide-react"; import { useTranslation } from 'react-i18next'; import { useState, useEffect, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'; import { useIsMobile } from "@/hooks/useIsMobile"; import { useNavigate } from "react-router-dom"; import { useAppContext } from "@/hooks/useAppContext"; import { useOffline } from "@/hooks/useOffline"; import { useToast } from "@/hooks/useToast"; import { defaultSystemPrompt } from "@/lib/system"; export function SystemSettings() { const { t } = useTranslation(); const isMobile = useIsMobile(); const navigate = useNavigate(); const { config, defaultConfig, updateConfig } = useAppContext(); const { toast } = useToast(); const { serviceWorkerRegistration, updateServiceWorker, clearCache } = useOffline(); const [projectTemplateInput, setProjectTemplateInput] = useState(config.projectTemplate); const [esmUrlInput, setEsmUrlInput] = useState(config.esmUrl); const [corsProxyInput, setCorsProxyInput] = useState(config.corsProxy); const [faviconUrlInput, setFaviconUrlInput] = useState(config.faviconUrl); const [previewDomainInput, setPreviewDomainInput] = useState(config.previewDomain); const [showcaseModeratorInput, setShowcaseModeratorInput] = useState(config.showcaseModerator); 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 [sentryDsnInput, setSentryDsnInput] = useState(config.sentryDsn); const [systemPromptInput, setSystemPromptInput] = useState(config.systemPrompt || defaultSystemPrompt); // Check which settings differ from defaults const isModified = useMemo(() => ({ projectTemplate: config.projectTemplate !== defaultConfig.projectTemplate, esmUrl: config.esmUrl !== defaultConfig.esmUrl, corsProxy: config.corsProxy !== defaultConfig.corsProxy, faviconUrl: config.faviconUrl !== defaultConfig.faviconUrl, previewDomain: config.previewDomain !== defaultConfig.previewDomain, showcaseEnabled: config.showcaseEnabled !== defaultConfig.showcaseEnabled, showcaseModerator: config.showcaseModerator !== defaultConfig.showcaseModerator, fsPathProjects: config.fsPathProjects !== defaultConfig.fsPathProjects, fsPathConfig: config.fsPathConfig !== defaultConfig.fsPathConfig, fsPathTmp: config.fsPathTmp !== defaultConfig.fsPathTmp, fsPathPlugins: config.fsPathPlugins !== defaultConfig.fsPathPlugins, sentryDsn: config.sentryDsn !== defaultConfig.sentryDsn, systemPrompt: (config.systemPrompt || defaultSystemPrompt) !== (defaultConfig.systemPrompt || defaultSystemPrompt), }), [config, defaultConfig]); // Restore functions const restoreProjectTemplate = () => { const defaultValue = defaultConfig.projectTemplate; setProjectTemplateInput(defaultValue); updateConfig((current) => ({ ...current, projectTemplate: defaultValue })); }; const restoreEsmUrl = () => { const defaultValue = defaultConfig.esmUrl; setEsmUrlInput(defaultValue); updateConfig((current) => ({ ...current, esmUrl: defaultValue })); }; const restoreCorsProxy = () => { const defaultValue = defaultConfig.corsProxy; setCorsProxyInput(defaultValue); updateConfig((current) => ({ ...current, corsProxy: defaultValue })); }; const restoreFaviconUrl = () => { const defaultValue = defaultConfig.faviconUrl; setFaviconUrlInput(defaultValue); updateConfig((current) => ({ ...current, faviconUrl: defaultValue })); }; const restorePreviewDomain = () => { const defaultValue = defaultConfig.previewDomain; setPreviewDomainInput(defaultValue); updateConfig((current) => ({ ...current, previewDomain: defaultValue })); }; const restoreShowcaseEnabled = () => { const defaultValue = defaultConfig.showcaseEnabled; updateConfig((current) => ({ ...current, showcaseEnabled: defaultValue })); }; const restoreShowcaseModerator = () => { const defaultValue = defaultConfig.showcaseModerator; setShowcaseModeratorInput(defaultValue); updateConfig((current) => ({ ...current, showcaseModerator: defaultValue })); }; const restoreFsPathProjects = () => { const defaultValue = defaultConfig.fsPathProjects; setFsPathProjectsInput(defaultValue); updateConfig((current) => ({ ...current, fsPathProjects: defaultValue })); }; const restoreFsPathConfig = () => { const defaultValue = defaultConfig.fsPathConfig; setFsPathConfigInput(defaultValue); updateConfig((current) => ({ ...current, fsPathConfig: defaultValue })); }; const restoreFsPathTmp = () => { const defaultValue = defaultConfig.fsPathTmp; setFsPathTmpInput(defaultValue); updateConfig((current) => ({ ...current, fsPathTmp: defaultValue })); }; const restoreFsPathPlugins = () => { const defaultValue = defaultConfig.fsPathPlugins; setFsPathPluginsInput(defaultValue); updateConfig((current) => ({ ...current, fsPathPlugins: defaultValue })); }; const restoreSentryDsn = () => { const defaultValue = defaultConfig.sentryDsn; setSentryDsnInput(defaultValue); updateConfig((current) => ({ ...current, sentryDsn: defaultValue })); }; const restoreSystemPrompt = () => { const defaultValue = defaultSystemPrompt; setSystemPromptInput(defaultValue); updateConfig((current) => ({ ...current, systemPrompt: defaultValue })); }; // 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 (
{isMobile && (

{t('systemSettings')}

{t('systemSettingsDescription')}

)} {!isMobile && (

{t('systemSettings')}

{t('systemSettingsDescription')}

)}
{/* 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')}
)}
{/* Project Template Configuration */}

{t('projectTemplate')}

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

{t('projectTemplateDescription')}

{/* 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')}

{/* 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.showcaseModerator) && (
)}
{isModified.showcaseEnabled && (
)}
{ updateConfig((current) => ({ ...current, showcaseEnabled: checked, })); }} /> {isModified.showcaseEnabled && ( )}

{t('showcaseEnabledDescription')}

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

{t('showcaseModeratorDescription')}

{/* 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')}

{/* Filesystem Paths Configuration */}

{t('filesystemPaths')}

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

{t('projectsDirectoryDescription')}

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

{t('configDirectoryDescription')}

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

{t('temporaryDirectoryDescription')}

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

{t('pluginsDirectoryDescription')}

{/* System Prompt Configuration */}

{t('systemPrompt')}

{isModified.systemPrompt && (
)}