import { useState, useRef, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Skeleton } from '@/components/ui/skeleton'; import { toast } from '@/components/ui/toast-store'; import { apiFetch } from '@/lib/api'; import { RefreshCw } from 'lucide-react'; import { SettingsSection } from './SettingsSection'; import { SettingsField } from './SettingsField'; import { SettingsActions, SettingsPrimaryButton } from './SettingsActions'; function SectionSkeleton() { return (
); } export function AppStoreSection() { const [templateRegistryUrl, setTemplateRegistryUrl] = useState(''); const serverUrl = useRef(''); const [isLoading, setIsLoading] = useState(false); const [isSavingRegistry, setIsSavingRegistry] = useState(false); useEffect(() => { const fetchSettings = async () => { setIsLoading(true); try { const res = await apiFetch('/settings'); if (res.ok) { const data: Record = await res.json(); const url = data.template_registry_url ?? ''; setTemplateRegistryUrl(url); serverUrl.current = url; } } catch (e) { console.error('Failed to fetch app store settings', e); } finally { setIsLoading(false); } }; fetchSettings(); }, []); const saveRegistrySettings = async () => { const trimmedUrl = templateRegistryUrl.trim(); if (trimmedUrl && !/^https?:\/\/./.test(trimmedUrl)) { toast.error('Registry URL must start with http:// or https://'); return; } setIsSavingRegistry(true); try { const res = await apiFetch('/settings', { method: 'PATCH', body: JSON.stringify({ template_registry_url: trimmedUrl }), }); if (!res.ok) { const err = await res.json().catch(() => ({})); toast.error(err?.error || err?.message || 'Failed to save registry settings.'); return; } serverUrl.current = templateRegistryUrl; await apiFetch('/templates/refresh-cache', { method: 'POST' }); toast.success('Registry saved. App Store will reload from the new source.'); } catch (e: unknown) { toast.error((e as Error)?.message || 'Failed to save registry settings.'); } finally { setIsSavingRegistry(false); } }; if (isLoading) return ; return (
api.linuxserver.io/api/v1/images setTemplateRegistryUrl(e.target.value)} />
{isSavingRegistry ? ( <> Saving ) : ( 'Save & refresh' )}
); }