Skip to content

refactor(app): move the access state into a hook - #1771

Merged
devswha merged 1 commit into
mainfrom
refactor/app-access-hook
Oct 23, 2026
Merged

devswha merged 1 commit into
mainfrom
refactor/app-access-hook

Conversation

@devswha

@devswha devswha commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Wave 7 of the App.tsx split. The plan is .omo/plans/app-stores-split.md in the maintainer's workspace, and plan-reviewer approved it. It follows PR #1770.

This wave moves the access state out of src/App.tsx into src/lib/useAccess.ts, as two hooks. Each is called at its own block's line, per the plan's rule R1:

  • useAccessState(), where the access state stood: locked, lockReason, pairCode, auth, canSignOut and pairedFromAddress, with their comments.
    • pairCode's initializer still runs takePairCode() at the same point in App's render.
    • It also returns readBridgeHealth: the bridge-health read that opened loadHealth, now a useCallback(..., []). Its body is the original two lines (the fetchBridgeHealth() try and its catch), and it uses only stable setters.
  • useLockedRef({ locked }), where lockedRef stood. It returns the same ref object, with the render-time assignment.

Not purely moved code: in loadHealth, the two bridge-health lines become await readBridgeHealth();. loadHealth's dependency list stays []. readBridgeHealth is stable, so the closure loadHealth holds from the first render runs the same code as before. loadHealth's identity, which the polling effect and unlock/lock depend on, is unchanged.

Stays in App, as the plan says:

  • the pairing effect;
  • unlock;
  • the hashchange effect;
  • lock.

Hook ceiling: APP_HOOK_CEILING drops from 113 to 107, after moving four useState and two useRef calls.

Checks

  • --color-moved: git diff --color-moved=plain --color-moved-ws=allow-indentation-change origin/main...HEAD shows 30 lines as moved. The other 26 lines are:
    • App's narrowed imports and the new import;
    • the two calls and the await readBridgeHealth(); line;
    • the new file's imports, signatures, the readBridgeHealth wrapper lines and the returns;
    • the ceiling.
  • Moved lines are exact: the moved blocks match App.tsx on 705dcb8ff byte for byte, at lines 257-267, 403-404 and 413-414.
  • Positions: the line before and after each call is the line that surrounded its block.
  • Typecheck: bun run typecheck passes.
  • Unit tests: HERDR_TEST_MODE=unit bun test src/app-hooks.test.ts src/conventions.test.ts src/lib/authLink.test.ts src/lib/phone.test.ts src/lib/i18n.test.ts: 41 pass, 0 fail.
  • Ceiling test: adding const zz = useState(false); to App.tsx makes src/app-hooks.test.ts fail.
  • bun run check fast: passes (unit 6253 pass, 0 fail, 13 skip in 443 files, 8 at a time, 57.9s)
  • bun run check integration browser: the browser lane exits 0 (every browser script passed, 1795 s), including the access gate and sign-out checks in ui-regression, herdr profiles, and every script that loads through the gate. The integration lane's only failure is server/api.contract.test.ts "creates, edits, searches, inspects, and closes an owned workspace" (line 1540), which fails the same way on unchanged origin/main on this PC; CI runs that lane in its own environment.

Outside PRs that touch the same files

No other maintainer PR touches these files. One outside contributor's PR touches src/App.tsx: #1427. It already conflicts with main, and needs a rebase either way.

No changelog fragment: users see no change.

The access state and lockedRef move into src/lib/useAccess.ts, each called at its own line.
loadHealth's bridge-health read becomes readBridgeHealth, a []-callback, with loadHealth's deps unchanged.
The hook-call ceiling is lowered to 107.
Plan .omo/plans/app-stores-split.md wave 7.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 705dcb8 and f1a06a4.

📒 Files selected for processing: 3
  • src/App.tsx
  • src/app-hooks.test.ts
  • src/lib/useAccess.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Improvements
    • Access status is preserved when a bridge health check fails, avoiding unexpected changes to the access gate.
    • Sign-out availability now reflects whether the current session is an eligible authenticated session.
  • Refactor
    • Access-state handling has been consolidated, with no changes to the app’s public interfaces.

App now delegates access-state management, bridge-health reads, and lock-ref synchronization to hooks in useAccess.ts. The hook-call ceiling test uses a lower limit.

Changes: Access state integration

Layer / File(s) Summary
Add access state hooks
src/lib/useAccess.ts
useAccessState manages access, pairing, and authentication state, including sign-out eligibility and bridge-health updates. useLockedRef synchronizes a ref with the lock state.
Wire hooks into App
src/App.tsx, src/app-hooks.test.ts
App uses the new hooks instead of managing access state and bridge-health updates inline. The hook-call ceiling decreases from 113 to 107.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to f1a06

No actionable risk introduced by this change remains; it is ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: moving access state from App.tsx into a hook.
Description check Passed The description explains the change, retained behavior, affected hooks, scope, validation results, known unrelated integration failure, and changelog status. It uses a "## Checks" heading instead of t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the change, retained behavior, affected hooks, scope, validation results, known unrelated integration failure, and changelog status. It uses a "## Checks" heading instead of the template's "## Validation" heading, but the required validation information is present.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 23, 2026

Copy link
Copy Markdown
Owner Author

Merge pass: this PR touches authentication state, so per AGENTS.md this comment records the review that stands in for a full one. Reviews: a gate reviewer on a different model than the author's, plus my own checks.

Findings: none.

  • Reconstruction. Putting both hooks back at their call sites and inlining readBridgeHealth gives an App body byte-identical to 705dcb8. Every existing effect and listener stays in order.
  • readBridgeHealth. Its body is origin/main's two loadHealth lines, byte for byte: the fetchBridgeHealth try and its catch that keeps the gate up. There is no early return in them, so loadHealth's control flow is unchanged.
  • Stable identities. loadHealth keeps its [] deps and identity. readBridgeHealth is stable. lockedRef is the same ref and is still assigned at render time.
  • Gate behaviour unchanged. locked starts null, so the shell and the WebSocket wait for it. The gate is still retained while the server restarts, and sign-out availability is the same.
  • Pair code. takePairCode() still runs at the same render point. Under StrictMode only the first invocation strips ?pair=, and React keeps that result, as before.
  • Tests at this head. 41 unit tests pass (app-hooks, conventions, authLink, phone, i18n), and the typecheck passes.
  • CI. The access gate and sign-out checks inside ui-regression run in CI's browser lane.

All required checks are green and no review thread is open.

@devswha
devswha merged commit 58fcc3a into main Oct 23, 2026
9 checks passed
@devswha
devswha deleted the refactor/app-access-hook branch October 23, 2026 18:27
devswha added a commit that referenced this pull request Oct 23, 2026
Wave 8 of the App.tsx split. The plan is
`.omo/plans/app-stores-split.md` in the maintainer's workspace, and
plan-reviewer approved it. It follows PR #1771.

This wave moves the PC roster and this PC's health state out of
`src/App.tsx` into two files. Each contiguous block becomes one hook
called at that block's own line (rule R1), so effect order is unchanged.
In order, that is the recent-folders effect, then polling, then the
event stream, as before.

## What moves

| Hook (file) | Block in App.tsx on `58fcc3ae4` |
|---|---|
| `useRosterState()` (`useMachineRoster.ts`) | 213-214: `machines`,
`machineRosterLoaded` |
| `useRecentFolderMemory({ machines })` | 224-236: the recent-folders
effect |
| `useMachinesRef({ machines })` | 243: `machinesRef` and its
render-time assignment |
| `useLocalHealth()` (`useLocalHealth.ts`) | 247-255: the health book,
reads, `localHealth`, `health`, `localFreshness`, `shownHealth` |
| `useRosterError()` | 256: `error` |
| `useRefetchTimer()` | 398: `refetchTimer` |
| `useRosterLoader({ … })` | 412-478: `snapshotRequests`, `loadRef`,
`reorderCoordinator`, `load`, the polling effect, `scheduleRefetch`, the
refetch cleanup |
| `useRosterEvents({ … })` | 482-547: the event-stream effect |

Module-level helpers move with the code that uses them, verbatim with
their comments:
- `sameData` (exported, because `useLocalHealth.ts` imports it);
- `POLL_MS`;
- `REFETCH_DEBOUNCE_MS`.

**Not purely moved code, three spots:**
- **`loadHealth`:** the local health read inside it (7 lines) becomes
`readLocalHealth`, a `useCallback(..., [])` in `useLocalHealth`. Its
body is those lines verbatim, and it uses only refs and a setter.
- `loadHealth` is now `await readBridgeHealth(); await
readLocalHealth();`, with its dependency list still `[]`.
- Both callbacks are stable, so `loadHealth` keeps its identity and runs
the same code. The polling effect, `unlock` and `lock` depend on that
identity.
- **The event stream:** it reads `takeNotificationTargetRef`, which App
declares further down. So `useRosterEvents` takes
`getTakeNotificationTargetRef: () => takeNotificationTargetRef`, and the
one added line, `const takeNotificationTargetRef =
getTakeNotificationTargetRef();`, opens the message handler. Every other
line of the effect is unchanged, and the ref is still read when an event
arrives.
- **The effect's dependency list:** `[locked, scheduleRefetch, dropIn,
chime, reorderCoordinator]` is unchanged, so the stream resubscribes
exactly when it did before.

**Departures from the plan's draft:**
- The follow-herdr-focus block stays inside the event-stream effect
verbatim, instead of becoming an injected `onPaneFocused`. Every value
it reads is declared above the effect, so it moves as-is.
- The plan's "probe forgetting" item already moved in wave 6, inside
`useSessionHistoryList`.

**Hook ceiling:** `APP_HOOK_CEILING` drops from 107 to 90.

## Checks

- **`--color-moved`:** `git diff --color-moved=plain
--color-moved-ws=allow-indentation-change origin/main...HEAD` shows 374
lines as moved. The other 105 lines are:
  - App's narrowed imports, the two new imports and the eight calls;
- the two lines of `loadHealth` and the `getTakeNotificationTargetRef`
argument;
- the new files' imports, params types, signatures, the
`readLocalHealth` wrapper, the getter line and the returns;
  - the ceiling.
- **Moved blocks are exact:** every block above, and the health-read
lines 404-410, appear byte for byte in the new files. The event-stream
block does too, apart from the one getter line.
- **Positions:** the line before and after each call is the line that
surrounded its block.
- **Typecheck:** `bun run typecheck` passes.
- **Unit tests:** **120 pass, 0 fail across 11 files**, from:

`HERDR_TEST_MODE=unit bun test src/app-hooks.test.ts
src/conventions.test.ts src/lib/healthPresentation.test.ts
src/lib/snapshotRequests.test.ts src/lib/workspaceOrder.test.ts
src/lib/eventStream.test.ts src/lib/snapshot.test.ts
src/lib/recent-directories.test.ts src/lib/preferencesSync.test.ts
src/lib/i18n.test.ts scripts/conventions.test.ts`
- `bun run check fast`: passes (unit 6253 pass, 0 fail, 13 skip in 443
files, 8 at a time, 57.9s)
- **`bun run check integration browser`:** the browser lane exits 0
(every browser script passed, 1800 s), including the machines, machine
conflict and dialog, sidebar machine layout/removal, workspace rows and
touch reorder, recent folders, preferences sync, sidebar activity,
session history, follow-focus, health-flap, herdr-restart and pending
pane switch scripts. The integration lane's only failure is
`server/api.contract.test.ts` "creates, edits, searches, inspects, and
closes an owned workspace" (line 1540), which fails the same way on
unchanged `origin/main` on this PC; CI runs that lane in its own
environment.

## Outside PRs that touch the same files

No other maintainer PR touches these files. One outside contributor's PR
touches `src/App.tsx`: #1427. It already conflicts with main, and needs
a rebase either way.

No changelog fragment: users see no change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant