import { useEffect, useRef, useState } from "react"; import { habitRequest, scheduleLabel, type TodayHabit } from "../lib/dashboard"; import { Button } from "./design-system/primitives"; import { Archive, ChevronDown, RotateCcw } from "lucide-react"; import { FormMessage, WorkspacePanel } from "./design-system/WorkspacePanel"; import { HabitHistory } from "./HabitHistory"; export function ArchivedHabits({ date, revision, onChanged, onExpired, onClose }: { date: string; revision: number; onChanged: () => void; onExpired: () => void; onClose?: () => void; }) { const [habits, setHabits] = useState([]); const [selected, setSelected] = useState(); const [error, setError] = useState(""); const [notice, setNotice] = useState(""); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const [attempt, setAttempt] = useState(0); const saving = useRef(false); const expired = useRef(onExpired); expired.current = onExpired; useEffect(() => { const controller = new AbortController(); setLoading(true); setError(""); habitRequest<{ habits: TodayHabit[] }>(`/days/${date}`, { signal: controller.signal }) .then(data => { if (!controller.signal.aborted) setHabits(data.habits.filter(habit => habit.requirements?.archived)); }) .catch(error => { if (!controller.signal.aborted) { setError(error.message); if (error.message.includes("session has expired")) expired.current(); } }) .finally(() => { if (!controller.signal.aborted) setLoading(false); }); return () => controller.abort(); }, [date, revision, attempt]); async function restore(habit: TodayHabit) { if (saving.current) return; saving.current = true; setBusy(true); setError(""); setNotice(""); try { await habitRequest(`/habits/${habit.habitId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ archived: false }) }); setHabits(current => current.filter(item => item.habitId !== habit.habitId)); setNotice(`${habit.name} restored. Check-ins resume today.`); onChanged(); } catch (error) { const message = error instanceof Error ? error.message : "Could not restore. Try again."; setError(message); if (message.includes("session has expired")) expired.current(); } finally { saving.current = false; setBusy(false); } } return {error} {error && } {loading ?

Loading archive…

: !habits.length &&
} {notice}
{habits.map(habit =>

{habit.name ?? "Habit"}

{scheduleLabel(habit.requirements?.schedule)} · Archived

{selected === habit.habitId &&
setAttempt(value => value + 1)} />
}
)}
; }