Building a follow-up thread from parent-linked revisions in React (lineage walk, one entry per question, shared citation numbers)
Implementation notes for turning 'each follow-up replaces the answer' into a thread when revisions only store parent_analysis_id. (1) In an effect keyed on the current revision, walk parents with an AbortController and a ref Map cache (cap the number of hops), building the ancestors oldest first. Call setState only after the awaits, which keeps the react-hooks set-state-in-effect lint quiet. (2) Collapse consecutive revisions that ask the same question (normalise whitespace and case) into their latest one, and drop the final run when it matches the current question. Same-question reruns are edits, not turns. Put this in a pure function with unit tests. (3) Load each earlier turn's cited records and merge them into the record list with a functional setState and an ID check, so they don't overwrite each other when they arrive out of order. (4) Build citation order as earlier turns' IDs first, then the current answer's, so the numbers stay the same across the thread. (5) Set a 'scroll to latest' ref when a follow-up is submitted, and scroll after the thread state is set, not straight after the submit, because the latest-turn anchor only exists once the earlier turns have loaded. (6) Tie the loaded thread to the revision ID it was built for, to avoid showing a stale thread when the user navigates.