import { useCallback, useEffect, useId, useRef, useState } from 'react'; import type { ChangeEvent } from 'react'; import type { ChecklistItem } from '@cra-compliance/types'; import type { Page } from '../App'; import { navigateTo } from '../App'; import { useCompliance } from '../context/ComplianceContext'; import { useNotify } from '../hooks/useNotify'; import { errorMessage } from '../hooks/useApi'; import { APPLIES_NOW, CATEGORY_CITES, CHECKLIST, ITEM_TEMPLATES } from '../content/checklist'; import { Chip, Cite, ErrorState, Mark, Meter, PageHead, Panel, Skel } from '../components/ui'; type Filter = 'all' | 'open' | 'done'; export function Checklist({ onNavigate }: { onNavigate: (page: Page) => void }) { const { progress, loading, loadError, reload, toggleItem, saveNotes, savingItemId, savingNotesId, totalItems, completedItems } = useCompliance(); const { toast, NotifyPortal } = useNotify(); const [expanded, setExpanded] = useState>(new Set()); const [filter, setFilter] = useState('all'); const toggleExpanded = (id: string) => { setExpanded((current) => { const next = new Set(current); if (next.has(id)) next.delete(id); else next.add(id); return next; }); }; const onToggle = async (item: ChecklistItem) => { try { await toggleItem(item); } catch (error) { toast({ message: errorMessage(error, 'The item could not be saved.'), type: 'error' }); } }; if (loading) { return (
{[0, 1].map((i) => ( }>
{[0, 1, 2].map((j) => )}
))}
); } if (loadError) { return (
); } const visible = (item: ChecklistItem) => { const done = Boolean(progress[item.id]?.completed); return filter === 'all' || (filter === 'done' ? done : !done); }; return (
{(['all', 'open', 'done'] as Filter[]).map((value) => ( ))}
} />
{completedItems} of {totalItems} done {totalItems > 0 ? Math.round((completedItems / totalItems) * 100) : 0}%
{CHECKLIST.map((category) => { const items = category.items.filter(visible); const done = category.items.filter((item) => progress[item.id]?.completed).length; if (items.length === 0) return null; return ( {category.title} {CATEGORY_CITES[category.id] && {CATEGORY_CITES[category.id]}} {APPLIES_NOW.has(category.id) && Applies now} } description={category.description} actions={{done} of {category.items.length}} flush >
    {items.map((item) => ( void onToggle(item)} onExpand={() => toggleExpanded(item.id)} onSaveNotes={saveNotes} onNotesError={(message) => toast({ message, type: 'error' })} onNavigate={onNavigate} /> ))}
); })} {filter !== 'all' && CHECKLIST.every((category) => category.items.filter(visible).length === 0) && (

{filter === 'done' ? 'Nothing is marked as done yet.' : 'Everything is marked as done.'}

)} ); } function ChecklistRow({ item, done, notes, expanded, saving, savingNotes, onToggle, onExpand, onSaveNotes, onNotesError, onNavigate }: { item: ChecklistItem; done: boolean; notes: string; expanded: boolean; saving: boolean; savingNotes: boolean; onToggle: () => void; onExpand: () => void; onSaveNotes: (itemId: string, notes: string) => Promise; onNotesError: (message: string) => void; onNavigate: (page: Page) => void; }) { const checkboxId = useId(); const detailsId = useId(); const template = ITEM_TEMPLATES[item.id]; return (
  • {item.description}

    {!item.required && ( {item.cra_article.startsWith('Annex I') ? 'Good practice' : 'Optional'} )} {notes && !expanded && Note} {item.cra_article}
    {expanded && (

    How to do this

    {item.help_text}

    {(template || item.automated) && (
    {template && ( )} {item.automated && ( )}
    )}
    )}
  • ); } function EvidenceNote({ itemId, initialValue, onSave, saving, onError }: { itemId: string; initialValue: string; onSave: (itemId: string, notes: string) => Promise; saving: boolean; onError: (message: string) => void; }) { const id = useId(); const [value, setValue] = useState(initialValue); const [state, setState] = useState<'idle' | 'pending' | 'saved'>('idle'); const timer = useRef(null); useEffect(() => () => { if (timer.current) window.clearTimeout(timer.current); }, []); const handleChange = useCallback((event: ChangeEvent) => { const next = event.target.value; setValue(next); setState('pending'); if (timer.current) window.clearTimeout(timer.current); timer.current = window.setTimeout(async () => { try { await onSave(itemId, next); setState('saved'); } catch (error) { setState('idle'); onError(errorMessage(error, 'The note could not be saved.')); } }, 800); }, [itemId, onSave, onError]); return (