({ project, active, source }: AdrWorkspaceProps)
| 33 | } |
| 34 | |
| 35 | function ProjectAdrWorkspace({ project, active, source }: AdrWorkspaceProps): JSX.Element { |
| 36 | const [state, setState] = useState<EditorState>(() => { |
| 37 | const draft = readAdrDraft(project); |
| 38 | return { base: draft?.base, content: draft?.content ?? '', editing: !!draft, preview: false, |
| 39 | phase: 'idle', error: '', notice: draft ? s.restored : '', reviewing: false, reads: 0 }; |
| 40 | }); |
| 41 | const [saving, setSaving] = useState(false); |
| 42 | const [storageAvailable, setStorageAvailable] = useState(true); |
| 43 | const [reload, setReload] = useState(0); |
| 44 | const stateRef = useRef(state); |
| 45 | stateRef.current = state; |
| 46 | const mounted = useRef(true); |
| 47 | const savingRef = useRef(false); |
| 48 | const loadTicket = useRef(0); |
| 49 | const lastReload = useRef(0); |
| 50 | const hasLoaded = useRef(false); |
| 51 | const editor = useRef<HTMLTextAreaElement>(null); |
| 52 | const document = useRef<HTMLDivElement>(null); |
| 53 | const headingPrefix = useId(); |
| 54 | const [outline, setOutline] = useState<{ id: string; label: string; level: number }[]>([]); |
| 55 | const dirty = !!state.base && state.content !== state.base.content; |
| 56 | const size = adrSize(project, state.content); |
| 57 | |
| 58 | useEffect(() => { mounted.current = true; return () => { mounted.current = false; loadTicket.current += 1; }; }, []); |
| 59 | |
| 60 | useEffect(() => { |
| 61 | const current = stateRef.current; |
| 62 | const forced = reload !== lastReload.current; |
| 63 | lastReload.current = reload; |
| 64 | if (!active || savingRef.current || (hasLoaded.current && current.phase === 'ready' && current.base && current.content !== current.base.content && !forced)) return; |
| 65 | const ticket = ++loadTicket.current; |
| 66 | setState(value => ({ ...value, phase: 'loading', error: '', reads: value.reads + 1 })); |
| 67 | void source.adr(project).then(record => { |
| 68 | if (!mounted.current || ticket !== loadTicket.current) return; |
| 69 | hasLoaded.current = true; |
| 70 | setState(value => { |
| 71 | if (value.base && value.content !== value.base.content) { |
| 72 | return { ...value, phase: 'ready', error: '', conflict: sameAdr(value.base, record) ? undefined : record }; |
| 73 | } |
| 74 | return { ...value, base: record, content: record.content, phase: 'ready', error: '', conflict: undefined }; |
| 75 | }); |
| 76 | }, error => { |
| 77 | if (mounted.current && ticket === loadTicket.current) setState(value => ({ ...value, phase: 'error', error: busyError(error) ? s.busy : s.readFailed })); |
| 78 | }); |
| 79 | return () => { if (ticket === loadTicket.current) loadTicket.current += 1; }; |
| 80 | }, [active, project, source, reload]); |
| 81 | |
| 82 | useEffect(() => { |
| 83 | if (state.base && dirty) setStorageAvailable(storeAdrDraft(project, { version: 1, base: state.base, content: state.content })); |
| 84 | else if (state.base) clearAdrDraft(project); |
| 85 | }, [project, state.base, state.content, dirty]); |
| 86 | |
| 87 | useEffect(() => { |
| 88 | if (!dirty) return; |
| 89 | const warn = (event: BeforeUnloadEvent) => { event.preventDefault(); event.returnValue = ''; }; |
| 90 | window.addEventListener('beforeunload', warn); |
| 91 | return () => window.removeEventListener('beforeunload', warn); |
| 92 | }, [dirty]); |
nothing calls this directly
no test coverage detected