"use client"; import { useState } from "react"; import { SettingsSection, SettingsRow, SettingsToggle } from "../ui"; import { SystemSettings } from "../../types"; import { api } from "@/lib/api"; import { useQueryClient, useQuery } from "@tanstack/react-query"; import { CheckCircle, Loader2, User, Heart, Activity } from "lucide-react"; interface CacheSectionProps { settings: SystemSettings; onUpdate: (updates: Partial) => void; } // Progress bar component function ProgressBar({ progress, color = "bg-[#ecb200]", showPercentage = true }: { progress: number; color?: string; showPercentage?: boolean; }) { return (
{showPercentage && ( {progress}% )}
); } // Enrichment stage component function EnrichmentStage({ icon: Icon, label, description, completed, total, progress, isBackground = false, failed = 0, processing = 0, }: { icon: React.ElementType; label: string; description: string; completed: number; total: number; progress: number; isBackground?: boolean; failed?: number; processing?: number; }) { const isComplete = progress === 100; const hasActivity = processing > 0; return (
{isComplete ? ( ) : hasActivity ? ( ) : ( )}
{label} {isBackground && !isComplete && ( background )}

{description}

{completed} / {total} {processing > 0 && {processing} processing} {failed > 0 && {failed} failed}
); } export function CacheSection({ settings, onUpdate }: CacheSectionProps) { const [syncing, setSyncing] = useState(false); const [clearingCaches, setClearingCaches] = useState(false); const [reEnriching, setReEnriching] = useState(false); const [error, setError] = useState(null); const queryClient = useQueryClient(); // Fetch enrichment progress const { data: enrichmentProgress, refetch: refetchProgress } = useQuery({ queryKey: ["enrichment-progress"], queryFn: () => api.getEnrichmentProgress(), refetchInterval: 5000, // Refresh every 5 seconds staleTime: 2000, }); const refreshNotifications = () => { queryClient.invalidateQueries({ queryKey: ["notifications"] }); queryClient.invalidateQueries({ queryKey: ["unread-notification-count"] }); window.dispatchEvent(new CustomEvent("notifications-changed")); }; const handleSyncAndEnrich = async () => { setSyncing(true); setError(null); try { if (settings.autoEnrichMetadata) { await api.post("/audiobooks/sync", {}); } await api.post("/podcasts/sync-covers", {}); await api.startLibraryEnrichment(); refreshNotifications(); refetchProgress(); } catch (err) { console.error("Sync error:", err); setError("Failed to sync"); } finally { setSyncing(false); } }; const handleFullEnrichment = async () => { setReEnriching(true); setError(null); try { await api.triggerFullEnrichment(); refreshNotifications(); refetchProgress(); } catch (err) { console.error("Full enrichment error:", err); setError("Failed to start full enrichment"); } finally { setReEnriching(false); } }; const handleClearCaches = async () => { setClearingCaches(true); setError(null); try { await api.clearAllCaches(); refreshNotifications(); } catch (err) { setError("Failed to clear caches"); } finally { setClearingCaches(false); } }; return ( {/* Enrichment Progress */} {enrichmentProgress && (

Library Enrichment

{enrichmentProgress.coreComplete && !enrichmentProgress.isFullyComplete && ( Audio analysis running )} {enrichmentProgress.isFullyComplete && ( Complete )}
)} {/* Cache Sizes */}
onUpdate({ maxCacheSizeMb: parseInt(e.target.value) })} className="w-32 h-1 bg-[#404040] rounded-lg appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-white" /> {(settings.maxCacheSizeMb / 1024).toFixed(1)} GB
onUpdate({ transcodeCacheMaxGb: parseInt(e.target.value) })} className="w-32 h-1 bg-[#404040] rounded-lg appearance-none cursor-pointer [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-3 [&::-webkit-slider-thumb]:h-3 [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-white" /> {settings.transcodeCacheMaxGb} GB
{/* Automation */} onUpdate({ autoSync: checked })} /> onUpdate({ autoEnrichMetadata: checked })} /> {/* Cache Actions */}
{error && (

{error}

)}
); }