import { Fragment } from 'react'; import { History, Loader2 } from 'lucide-react'; import { useFieldHistory } from './FieldHistoryProvider'; import { formatDateTime } from '../../utils/dateUtils'; import type { RecordCreation } from '../../utils/fieldHistory/recordCreation'; // One sentence per distinct answer, so a view of several records created together reads once. const describeCreations = (creations: RecordCreation[]): string => { const sentences = creations.map((creation) => { if (creation.who) { return creation.whenIso ? `Created by ${creation.who} on ${formatDateTime(creation.whenIso)}.` : `Created by ${creation.who}.`; } return creation.unreadable ? 'We could not read who created it.' : 'No record names who created it.'; }); const distinct = Array.from(new Set(sentences)); return distinct.length > 0 ? ` ${distinct.join(' ')}` : ''; }; /** * Activates/deactivates the field-history feature for the enclosing `FieldHistoryProvider`. * Activating fetches `_history`+`Provenance` for every currently-registered resource (Task 4, * bounded concurrency) before flipping active -- the button shows a spinner and disables itself * for that window so it can't be double-clicked mid-fetch. Deactivating is instant (no fetch). * * A utility action, so the shared small blue ghost; it reads as pressed while history shows. */ export function ShowFieldHistoryToggle() { const { isActive, loading, nothingEverEdited, creations, historyUnavailable, historyRefused, historyComplete, verifiedOnly, shownFields, activate, deactivate } = useFieldHistory(); // Only once every read succeeded and every value on screen is bound; otherwise the claim is unproven. const changedOutOfView = isActive && !loading && historyComplete && !nothingEverEdited && shownFields.count > 0 && shownFields.changed === 0; return ( {isActive && nothingEverEdited && ( Nothing here has been edited yet.{describeCreations(creations ?? [])} )} {changedOutOfView && ( This record has been updated, but there are no changes to the fields shown here. )} {isActive && !loading && verifiedOnly && ( Showing only changes your practice made that the record can confirm. A field with no history here does not mean nothing changed. )} {isActive && historyUnavailable && ( {historyRefused ? 'Your access to this patient does not include its edit history.' : <>We could not read this record's history just now.} )} ); } export default ShowFieldHistoryToggle;