import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Separator } from "@/components/ui/separator"; import { useUpdateEntry, useUpdateEntryRatings } from "@/hooks/useEntries"; import { StarRating } from "@/components/ui/star-rating"; import { useLibraryStore } from "@/stores/useLibraryStore"; import type { Entry } from "@/types"; interface EntrySettingsDialogProps { open: boolean; onOpenChange: (open: boolean) => void; entry: Entry; } export function EntrySettingsDialog({ open, onOpenChange, entry: entryProp, }: EntrySettingsDialogProps) { // The entry prop can be a stale snapshot (e.g. from a collection query), so // read the live copy from the library store, which updates on every mutation. const storeEntry = useLibraryStore((s) => s.entries.get(entryProp.id)); const entry = storeEntry ? { ...entryProp, ...storeEntry } : entryProp; const updateEntry = useUpdateEntry(); const updateRatings = useUpdateEntryRatings(); const handleToggle = (field: keyof Entry, value: boolean) => { updateEntry.mutate({ id: entry.id, data: { [field]: value } }); }; const boolSettings = [ { field: "favorit" as const, label: "Favorite" }, { field: "hidden" as const, label: "Hidden" }, { field: "watchlist" as const, label: "Watchlist" }, { field: "show_in_calendar" as const, label: "Show in Calendar" }, { field: "auto_sync" as const, label: "Auto Sync" }, ]; const userRating = entry.entryRatings?.user_rating ?? 0; const handleRatingChange = (rating: number) => { updateRatings.mutate({ id: entry.id, ratings: { id: entry.entryRatings?.id, user_rating: rating || null }, }); }; return ( Entry Settings

{entry.title}

{boolSettings.map(({ field, label }) => (
handleToggle(field, checked)} />
))}
{userRating > 0 ? `${userRating}/10` : "—"}
); }