Map of state flows and single ownership for Campus Answers frontend
Use this page to find every local, lifted and global state flow in the public pages, to see where state is duplicated or prop drilled, and to build the next state change with one owner per state item.
How to read this map
Global state is kept across pages or across reloads. Lifted state is held above a list so a search input, a select control, the page address and the rendered list stay in step. Local state is held in one page or one form and is not shared.
Global
Stored in localStorage, sessionStorage, the page address, or in memory on the page. The owner decides the shape, the limits, and the save rule.
Lifted
Shared by controls on one page. The owner is one object on that page. Controls and the address are views of that object.
Local
Kept in one form or one list render. It is not read by another page and it is not stored across reloads unless stated.
Global state inventory
Each row names the stored state, its owner today, its readers and writers, and the risk when more than one loader parses the same key.
| State | Where stored | Owner today | Readers and writers | Risk |
|---|---|---|---|---|
| Saved answers, collections and history, key campus-answers:my-answers:v1 | localStorage | My Answers page | My Answers, Saved Essentials, Print answers, Answer library, Sample start, return start on the home page, My Data | Each page parses the key with its own loader. Answer library uses a 2-field duplicate check against a 5-field check in My Answers, and saves a relative source link that the My Answers loader drops |
| Essentials, key campus-answers:essentials:v1 | localStorage | Saved Essentials page | Saved Essentials, return start on the home page | Low today. Keep the owner and read through the shared loader only |
| Followed questions, key campus-answers:followed-questions:v1 | localStorage | My followed questions page | My followed questions, return start on the home page | Low today. The last visit time and last change time need the same date rule on every reader |
| Offline draft and answer pack, key campus-answers:offline-tools:v1 | localStorage | Offline page | Offline page, My Data snapshot | Import progress is a second key with its own owner. Keep pack entries and import progress as two stated owners |
| Language choice, user id and translations | localStorage, keys campus-answers:language-preferences:v1, campus-answers:user-id:v1, campus-answers:translations:v1 | Sample start page | Sample start page only | Low. This is the colocated pattern to copy. No other page reads or writes these keys |
| Public form drafts, keys campus-answers:form-draft: | sessionStorage | public/site.js | Request a demo forms only | Low. One owner, one store, one debounce of 250 milliseconds. Device tool drafts use a different store and must state it |
| My Answers form draft, key campus-answers:my-answers:draft:v1 | localStorage | My Answers page | My Answers page only | Different store from public form drafts for the same idea of a draft. State the store on the page so a reader does not expect tab only drafts |
| List filters in the page address, q, collection, sort, mode, workspace, assistant | Page address query | Each list page, separately | My Answers, Saved Essentials, Print answers, Offline pack, Search, Answer library | Each page writes and reads the address with its own code, defaults, and debounce. A shared link can restore a different list on each page |
| Marketing analytics queue | In memory on the page, capped at 100 | public/site.js | public/site.js only. No network request, cookie, or storage | Low. Keep in memory only. Do not persist this queue |
| Service worker registration | Browser registration, idle timeout 2000 milliseconds | public/site.js and the app shell | Public pages and the signed-in app shell | Two call sites with the same rule. Keep the timeout and the failure caught rule in one place |
Lifted and local state
Lifted state is the main source of prop drilling in this pack. A value travels from an input to a select to the address to the list render, with a pending variable in between.
| State | Where it travels | Symptom today | Fix in the ownership design |
|---|---|---|---|
| Filter state, q, collection, workspace, assistant, sort, mode | Search input, select controls, page address, filtered list, count text, empty state | pendingCollection, pendingWorkspace and pendingAssistant sit beside the DOM selects and the address. Two timer variables hold one timer in My Answers | One filterState object per page owns every filter value. DOM controls and the address are views. One debounce timer. One write to the address |
| Pagination, shown limit and Show more | Filtered list, Show more button, count text | shownLimit is reset in render in some pages and in filter handlers in others | filterState owns page size and shown limit. Render derives the shown slice and the remaining count from filterState only |
| Selection in Print answers | Selected Set, checkbox checked, is-selected class, Print selected and Download selected | Three places hold the same selection. A node cache update can leave the checkbox and the Set apart | One selected Set owns selection. Render sets checkbox checked and is-selected class from the Set on every render |
| Editing a saved answer in My Answers | editingId, form title, Save button text, Cancel button, draft values | editingId is held outside the form state and the draft stores it separately | The form owner holds editingId with the field values. Title, button text and Cancel visibility are derived in render |
| Node cache and signature | Node cache map, signature string, DOM list | My Answers, Saved Essentials, Print answers and Offline each copy build and update node code with a signature | Keep node cache local to one list render. Do not lift it. Share only the signature rule for fields that affect the node |
| Status text | Save result, import result, filter count, empty state | Status elements are set from several handlers with different wording for the same result | One setStatus function per page owns status text. Handlers call it with a stated result, they do not write the status element directly |
Duplication findings
Five copies of the same logic create the risk. Each copy can change without the others.
Search matching
A synonym map and close match rule appear in site.js with an inner shadow copy, in My Answers, in Offline, in Search, and through components/search.js in Saved Essentials and Print answers. Change a synonym in one copy and the lists disagree.
Saved answer loading
The same storage key is parsed in My Answers, Print answers, Answer library, Sample start, return start on the home page, and My Data. Limits, link rules, and duplicate checks differ per copy.
Filter to address
My Answers, Saved Essentials, Print answers and Search each write q, collection, sort and related values to the address with their own defaults and their own restore code.
Draft saving
site.js, My Answers, Offline and My followed questions each save a draft after 250 milliseconds, in sessionStorage or in localStorage, under different keys, with no shared owner.
List node rendering
Build node, update node, node cache and signature are copied in My Answers, Saved Essentials, Print answers and Offline. A field that affects the node must be added in four places.
Single ownership design
One owner per state item. Every other page reads through the owner or keeps its own local copy with a stated scope.
| State | Single owner | Allowed readers | Rule |
|---|---|---|---|
| Saved answers, collections, history, essentials | components/saved-items.js load and save helpers | My Answers, Saved Essentials, Print answers, Answer library, Sample start, My Data | No page parses the storage key directly. No page writes with its own loader. Duplicate check uses title, question, source link, workspace and assistant in every writer |
| Search matching | components/search.js word, token, synonym and relevance helpers | Every list page and the terminology filter | No page copies the synonym map. No inner shadow copy. A change to matching ships in one file |
| Filter state per list page | One filterState object colocated on that page | Controls, address, list render and count on that page only | No pending variables beside the DOM. One debounce timer. One address write per filter change. Defaults omitted from the address |
| Public form drafts | public/site.js sessionStorage draft | Request a demo forms only | Device tool drafts state their store on the page. No page mixes session and local drafts for the same form |
| Selection in Print answers | One selected Set on the Print answers page | Checkbox, list class, Print and Download on that page only | Checkbox and class are derived in render. No handler writes the checkbox as the source of truth |
| Marketing analytics | public/site.js in-memory queue | Console read only through the stated getter | No persistence, no cookie, no network request. Cap stays at 100 |
| Form validation on public forms | public/site.js validation helpers and the page validation script | Request a demo forms only | Field limits stay in one table per form. A limit change updates the page and the script in the same change |
Migration steps in order
Each step ships alone through the normal build. Keep every existing test passing before the next step starts.
Step 1: Freeze the owners on this page
Use the ownership table above for every new state change. Do not add a new loader, a new synonym map, or a new address writer.
Step 2: Route Print answers and Answer library through the shared loader
Replace their own loaders with the saved-items loader. Align the duplicate check to five fields. Decide once whether a relative source link is allowed, and state it in the loader, not per page.
Step 3: Remove the inner synonym copy in site.js
Route the terminology filter, My Answers and Offline through components/search.js. Add a pure test for token matching with a partial word, a spelling one letter off, and a synonym.
Step 4: Colocate filterState on one page at a time
Start with Print answers, the smallest list. Replace pending variables and two timers with one filterState object and one timer. Keep the address parameter names unchanged.
Step 5: State the draft store per page
Keep public forms in sessionStorage through site.js. Keep device tool drafts in localStorage only where the page states it. Do not change a store in the same change as a field change.
Step 6: Add pure tests for owners
Test the saved answer loader, the duplicate check, search token matching, and filterState address round trip with node test and no network call.
- One owner per state item, stated in code at the top of the owner file
- No page parses a storage key owned by another page
- No page copies the synonym map or the filter to address code
- DOM controls and the page address are views of filterState, never a second source of truth
- Selection, editing and node cache stay local to one page render
Build the next state change from this map
Find the state item in the tables above, use its single owner, and keep controls and the address as views.