"use client"; import { useState, useEffect } from "react"; import { useTranslate } from "@/hooks/useTranslate"; import { usePreferences } from "@/contexts/PreferencesContext"; import { Label } from "@/components/ui/label"; import { useToast } from "@/components/ui/use-toast"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { GRADIENT_PRESETS } from "@/types/preferences"; import type { BackgroundType } from "@/types/preferences"; import { Check } from "lucide-react"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { Checkbox } from "@/components/ui/checkbox"; import { SliderControl } from "@/components/ui/slider-control"; import type { KomgaLibrary } from "@/types/komga"; import logger from "@/lib/logger"; interface BackgroundSettingsProps { initialLibraries: KomgaLibrary[]; } export function BackgroundSettings({ initialLibraries }: BackgroundSettingsProps) { const { t } = useTranslate(); const { toast } = useToast(); const { preferences, updatePreferences } = usePreferences(); const [customImageUrl, setCustomImageUrl] = useState(preferences.background.imageUrl || ""); const [komgaConfigValid, setKomgaConfigValid] = useState(false); const [libraries, setLibraries] = useState(initialLibraries || []); const [selectedLibraries, setSelectedLibraries] = useState( preferences.background.komgaLibraries || [] ); useEffect(() => { setLibraries(initialLibraries || []); }, [initialLibraries]); useEffect(() => { setKomgaConfigValid(libraries.length > 0); }, [libraries]); const handleBackgroundTypeChange = async (type: BackgroundType) => { try { await updatePreferences({ background: { ...preferences.background, type, }, }); toast({ title: t("settings.title"), description: t("settings.komga.messages.configSaved"), }); } catch (error) { logger.error({ err: error }, "Erreur:"); toast({ variant: "destructive", title: t("settings.error.title"), description: t("settings.error.message"), }); } }; const handleGradientSelect = async (gradient: string) => { try { await updatePreferences({ background: { ...preferences.background, type: "gradient", gradient, }, }); toast({ title: t("settings.title"), description: t("settings.komga.messages.configSaved"), }); } catch (error) { logger.error({ err: error }, "Erreur:"); toast({ variant: "destructive", title: t("settings.error.title"), description: t("settings.error.message"), }); } }; const handleCustomImageSave = async () => { if (!customImageUrl.trim()) { toast({ variant: "destructive", title: t("settings.error.title"), description: "Veuillez entrer une URL valide", }); return; } try { await updatePreferences({ background: { ...preferences.background, type: "image", imageUrl: customImageUrl, }, }); toast({ title: t("settings.title"), description: t("settings.komga.messages.configSaved"), }); } catch (error) { logger.error({ err: error }, "Erreur:"); toast({ variant: "destructive", title: t("settings.error.title"), description: t("settings.error.message"), }); } }; const handleOpacityChange = async (value: number[]) => { try { await updatePreferences({ background: { ...preferences.background, opacity: value[0], }, }); } catch (error) { logger.error({ err: error }, "Erreur:"); } }; const handleBlurChange = async (value: number[]) => { try { await updatePreferences({ background: { ...preferences.background, blur: value[0], }, }); } catch (error) { logger.error({ err: error }, "Erreur:"); } }; const handleLibraryToggle = async (libraryId: string) => { const newSelection = selectedLibraries.includes(libraryId) ? selectedLibraries.filter((id) => id !== libraryId) : [...selectedLibraries, libraryId]; setSelectedLibraries(newSelection); try { await updatePreferences({ background: { ...preferences.background, komgaLibraries: newSelection, }, }); } catch (error) { logger.error({ err: error }, "Erreur:"); } }; return ( {t("settings.background.title")} {t("settings.background.description")}
{/* Type de background */}
handleBackgroundTypeChange(value as BackgroundType)} className="space-y-2" >
{komgaConfigValid && (
)}
{/* Sélection de dégradé */} {preferences.background.type === "gradient" && (
{GRADIENT_PRESETS.map((preset) => ( ))}
)} {/* URL d'image personnalisée */} {preferences.background.type === "image" && (
setCustomImageUrl(e.target.value)} className="flex-1" />

{t("settings.background.image.description")}

)} {/* Sélection des bibliothèques Komga */} {preferences.background.type === "komga-random" && (
{libraries.map((library) => (
handleLibraryToggle(library.id)} />
))}

Sélectionnez une ou plusieurs bibliothèques pour choisir une cover aléatoire

)} {/* Contrôles d'opacité et de flou */} {(preferences.background.type === "gradient" || preferences.background.type === "image" || preferences.background.type === "komga-random") && ( <> handleOpacityChange([value])} /> handleBlurChange([value])} /> )}
); }