Front end engineering

Component decomposition plan for the largest front end components

Audit of the largest components in pack app-campus.site.1 and a concrete plan to split each one into focused reusable pieces with clear inputs while preserving existing behavior, storage keys, limits, and timing.

What the audit found

The largest components are the ones that mix several jobs in one file. The plan below keeps each page address and each saved item format unchanged and moves shared logic into small pieces that a second page can reuse.

  • public/site.js mixes site navigation, request form handling with draft saving and retries, speech dictation, the terminology filter, and in memory analytics on every public page.
  • public/my-answers.html holds its own saved answers store, draft handling, collection handling, search and sort, list rendering with a node cache, history, and import and export in one inline script.
  • public/offline.html holds a question draft, a personal answer pack list, search and filters, export, and a chunked import with resume in one inline script.
  • public/my-essentials.html, public/print-answers.html, public/my-follows.html, and public/search.html each repeat saved item loading, word matching, and list rendering in their own form.
  • public/reports.html, public/kpi-dashboard.html, and public/answer-insight.html each repeat count parsing, rate calculation, bar rendering, and CSV download for a calculator on the page.

Plan for public/site.js

Split the file into five focused pieces with the same loaded behavior. Each piece receives its elements as inputs and returns no global state.

Piece, inputs, and behavior kept
PieceInputsBehavior kept
NavMenubutton element, nav element, breakpoint 900Open and close from the button, close on Escape and on link choice, move focus back to the button, close above the breakpoint.
RequestFormform element, endpoint, timeout 30000, attempts 3, retry delays 600 and 1800, draft debounce 250Validation with the same field limits, draft saving in the same session key, retry with the same request reference, confirmation replaces the form.
DictationButtonfield element, form kind, status elementOne session at a time, stop on Escape, text added at the cursor, status text for listening, added, stopped, and each error, no button on excluded field types.
TerminologyFiltersearch input, entry list elements, count element, empty state elementFilter debounce 120, every word must match, partial and one letter off matching, count shown, clear search restores the full list.
Analyticspage path, link element, placementIn memory only, cap 100, no network request, opt out by Do Not Track, Global Privacy Control, and CAMPUS_ANALYTICS_OPT_OUT, allowlisted paths only.

Plan for saved answers pages

Align public/my-answers.html with the shared pieces already used by public/my-essentials.html and public/print-answers.html, then split the remaining inline script.

Piece, inputs, and behavior kept
PieceInputsBehavior kept
SavedItemsStorestorage key campus-answers:my-answers:v1, field limits, history limit 20Load, clean, and save answers, collections, and history in the same format, duplicate check on title, question, source link, workspace, and assistant.
SavedAnswerListanswers, collections, filters for search, collection, workspace, assistant, and sort, page size 20Search, sort, and filter with the same relevance order, Show more adds 20, empty state with count shown and clear action.
SavedAnswerItemone answer, its collection name, callbacks for open, re-ask, copy, edit, delete, and move collectionOne card per answer, source link and last checked date shown, history recorded on open and re-ask, no second copy saved on duplicate.
LibraryImportExportstate object, file input, import limits 2 MB, 500 answers, 100 collections, 100 history entriesExport as my-answers.json, import adds new items only, skipped counts shown, saved library unchanged for skipped entries.

The same SavedAnswerList and SavedAnswerItem pieces serve public/my-essentials.html and public/print-answers.html by passing different callbacks, such as mark essential and select for print, instead of repeating the list code.

Plan for public/offline.html and public/search.html

Split offline tools into draft, pack list, and import pieces, and split public search into index, scoring, and results pieces.

Piece, inputs, and behavior kept
PieceInputsBehavior kept
OfflineDraftworkspace field, assistant field, draft text field, storage key campus-answers:offline-tools:v1Save and copy the draft on this device only, clear the draft, restore on load.
PackListpack entries, search input, workspace and assistant filters, render window 50Search and filter with the same word matching, Show more adds 50, empty states for no entries and for no match.
PackImporterfile input, chunk size 50, import limit 500 entries and 2 MBImport in batches, resume or discard an incomplete import, count as imported only when every entry is saved and checked.
SearchIndex and Scorerindex entries with path, title, and description, query wordsTitle matches rank first, every query word must match, partial and one letter off matching, no result shows suggested entries.
SearchResultssearch input, results list, count element, empty state elementFilter debounce 120, count shown as showing X of Y entries, clear search restores the full list and the page address.

Plan for calculators on reports pages

Extract the shared calculator logic from public/reports.html, public/kpi-dashboard.html, and public/answer-insight.html into one pure piece and one rendering piece.

Piece, inputs, and behavior kept
PieceInputsBehavior kept
RateCalculatornumerator count, denominator count, zero denominator textZero denominator shows words such as no questions in this range, never zero percent, numerator larger than denominator shows check counts, rates show one decimal place.
RateTablemeasure rows, numerator and denominator cells, rate cells, bar elementsRates update as counts are typed, bars match the rates, unrated count shown where the page states it.
CountsCsvheader row, data rows, download nameCSV holds scope, counts, and rates only, with no question text, download works or shows the fallback message.

Migration order and checks

Ship the pieces in this order so each step keeps the build and existing behavior intact.

  1. Extract pure helpers first

    Move word matching, count parsing, rate calculation, and CSV building into pure functions that a test can call with no network call. Keep the same limits and the same wording.

  2. Extract rendering pieces second

    Move list and item rendering into pieces that receive data and callbacks as inputs. Keep the same element ids, the same empty states, the same counts, and the same focus order.

  3. Wire one page at a time

    Wire one page to the new pieces, check it on phone and desktop, then move to the next page. Keep storage keys, import formats, and page addresses unchanged in each step.

  4. Remove duplicated code last

    Remove a duplicated inline copy only after the shared piece is used by every page that needs it and the page check passes.

  • Every page keeps its existing headings, labels, required marking, and primary action placement
  • Every list keeps its count shown, its empty state, and its clear action
  • Every form keeps its draft saving, retry with the same request reference, and confirmation behavior
  • No piece adds a new field, a new limit, or a new tracking event

Start with the pure helpers

Extract word matching and rate calculation first, then split list rendering page by page.