Frontend state

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.

Global state, owner today, readers and writers, and risk
StateWhere storedOwner todayReaders and writersRisk
Saved answers, collections and history, key campus-answers:my-answers:v1localStorageMy Answers pageMy Answers, Saved Essentials, Print answers, Answer library, Sample start, return start on the home page, My DataEach 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:v1localStorageSaved Essentials pageSaved Essentials, return start on the home pageLow today. Keep the owner and read through the shared loader only
Followed questions, key campus-answers:followed-questions:v1localStorageMy followed questions pageMy followed questions, return start on the home pageLow 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:v1localStorageOffline pageOffline page, My Data snapshotImport progress is a second key with its own owner. Keep pack entries and import progress as two stated owners
Language choice, user id and translationslocalStorage, keys campus-answers:language-preferences:v1, campus-answers:user-id:v1, campus-answers:translations:v1Sample start pageSample start page onlyLow. This is the colocated pattern to copy. No other page reads or writes these keys
Public form drafts, keys campus-answers:form-draft:sessionStoragepublic/site.jsRequest a demo forms onlyLow. 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:v1localStorageMy Answers pageMy Answers page onlyDifferent 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, assistantPage address queryEach list page, separatelyMy Answers, Saved Essentials, Print answers, Offline pack, Search, Answer libraryEach 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 queueIn memory on the page, capped at 100public/site.jspublic/site.js only. No network request, cookie, or storageLow. Keep in memory only. Do not persist this queue
Service worker registrationBrowser registration, idle timeout 2000 millisecondspublic/site.js and the app shellPublic pages and the signed-in app shellTwo 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.

Lifted state, where it travels, and the colocated fix
StateWhere it travelsSymptom todayFix in the ownership design
Filter state, q, collection, workspace, assistant, sort, modeSearch input, select controls, page address, filtered list, count text, empty statependingCollection, pendingWorkspace and pendingAssistant sit beside the DOM selects and the address. Two timer variables hold one timer in My AnswersOne 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 moreFiltered list, Show more button, count textshownLimit is reset in render in some pages and in filter handlers in othersfilterState owns page size and shown limit. Render derives the shown slice and the remaining count from filterState only
Selection in Print answersSelected Set, checkbox checked, is-selected class, Print selected and Download selectedThree places hold the same selection. A node cache update can leave the checkbox and the Set apartOne selected Set owns selection. Render sets checkbox checked and is-selected class from the Set on every render
Editing a saved answer in My AnswerseditingId, form title, Save button text, Cancel button, draft valueseditingId is held outside the form state and the draft stores it separatelyThe form owner holds editingId with the field values. Title, button text and Cancel visibility are derived in render
Node cache and signatureNode cache map, signature string, DOM listMy Answers, Saved Essentials, Print answers and Offline each copy build and update node code with a signatureKeep node cache local to one list render. Do not lift it. Share only the signature rule for fields that affect the node
Status textSave result, import result, filter count, empty stateStatus elements are set from several handlers with different wording for the same resultOne 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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, and rule
StateSingle ownerAllowed readersRule
Saved answers, collections, history, essentialscomponents/saved-items.js load and save helpersMy Answers, Saved Essentials, Print answers, Answer library, Sample start, My DataNo 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 matchingcomponents/search.js word, token, synonym and relevance helpersEvery list page and the terminology filterNo page copies the synonym map. No inner shadow copy. A change to matching ships in one file
Filter state per list pageOne filterState object colocated on that pageControls, address, list render and count on that page onlyNo pending variables beside the DOM. One debounce timer. One address write per filter change. Defaults omitted from the address
Public form draftspublic/site.js sessionStorage draftRequest a demo forms onlyDevice tool drafts state their store on the page. No page mixes session and local drafts for the same form
Selection in Print answersOne selected Set on the Print answers pageCheckbox, list class, Print and Download on that page onlyCheckbox and class are derived in render. No handler writes the checkbox as the source of truth
Marketing analyticspublic/site.js in-memory queueConsole read only through the stated getterNo persistence, no cookie, no network request. Cap stays at 100
Form validation on public formspublic/site.js validation helpers and the page validation scriptRequest a demo forms onlyField 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.