Skip to content

refactor(web): move Home Screen saves into one hook - #1901

Merged
Quick104 merged 5 commits into
feat/collections-revampfrom
refactor/home-screen-save-hook
Oct 4, 2026
Merged

Quick104 merged 5 commits into
feat/collections-revampfrom
refactor/home-screen-save-hook

Conversation

@Quick104

@Quick104 Quick104 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: #1137
Validation tasks: changes #1142 C2

#1142 C2 (reorder, reload, switch profiles and back) now runs through the new save queue instead of the page's separate PUT calls, so it needs a re-test after merge. The order should still persist: a single reorder sends the same payload as before, and a reorder made during another save is sent after it, so the newest order is the one that stays saved. Each queued save is now sent as the profile that made it, which keeps the profile switch in C2 from carrying a pending save onto the other profile. C2 has no passed result yet.

Stacked on #1900, which is stacked on #1898. Merge those first; this PR targets #1900's branch. Its own changes are web-only and do not read the field #1900 adds.

Settings > Home Screen can save a profile's rows out of order. Every toggle, drag, add, edit or delete sends PUT /api/v2/profile/sections with the page's whole override set, and the page sends each one without waiting for the one before. The route checks no version, so when two quick changes land in the wrong order, the older state is what stays saved.

This PR moves the page's reads and saves into one hook, useProfileHomeRows. It sends one save at a time. Changes made while a save is in flight merge into a single next save, and the newest state wins. The Home rows redesign builds the new Settings > Home Screen on this hook, so the save rules get tested here, separately from the UI change.

Approach

  • The hook keeps at most one PUT or reset in flight. A merged save carries every row id its changes touched. It needs them because the override builder leaves out a legacy Trakt row that has no saved override unless the save names that row. The override set is built when the save is sent, so it reuses the override ids the previous save stored.
  • A failed save shows the same toast as before and puts the last server state back, unless a newer change is waiting; that change is kept and sent, and it also names the failed save's rows so a legacy Trakt row edited in the failed save is still written.
  • Each save or reset records the household profile that made it and is sent with that profile's authority. A save still queued when another profile is picked is dropped instead of being written to the newly selected profile. The page refetches after every attempt, failed ones included, because the PUT is not retry-safe and may have landed.
  • Nothing writes until the saved overrides have loaded, as before.
  • Visible changes are small:
    • The page picker is disabled while a save, a reset or the refetch after it is in flight. The hook also refuses a page switch in that state.
    • After confirming Reset, editing stays off until the reset and its refetch land. Otherwise an edit made in that window would be built on the overrides the reset drops and would save them again.
    • Everything else in the page JSX is unchanged.
  • buildSectionOverrides now accepts changedSectionIds. The single changedSectionId still works and gives the same output. What a save stores is unchanged. Storing only the fields a profile changed is planned for the next PR, which replaces this page's UI.
  • The page's pure helpers moved to web/src/lib/homeRows/profileOverrides.ts. The page re-exports them, so HomeScreenSettings.test.tsx and the payload golden tests run unedited. Two of those helpers, shouldRestoreSelectionState and shouldRestoreLatestSaveFailure, are now used only by that test file. They will be removed together with their tests when the page is rebuilt.
  • useSaveProfileOverrides and useResetProfileOverrides become plain request functions, because the hook has to await the refetch itself. Only this page used them.
  • Out of scope: the Home layout import still sends its own PUT outside the hook, and saves from other tabs or devices are still last-write-wins, since the profile route has no ETag.
  • This PR has no new UI, so none of the redesign's deviations from the approved mockups apply. There is no API change and no silo-apple, silo-android or jellycompat follow-up: the requests the page sends are the same.

Validation

  • Hook tests (renderHook against a mocked v2 server) cover save ordering, merging, a merged change to a legacy Trakt row reaching the PUT, page-switch refusal, rollback after a failure, a stale failure not undoing a newer save, a failed save's Trakt row edit reaching the save queued behind it, a queued save being dropped after a profile switch, pending lasting until the refetch, no writes before the overrides load, and a reset waiting behind an in-flight save. Breaking the changed-id union, the rollback condition or the one-at-a-time guard makes them fail.
  • A page render test covers a toggle, a delete and a reset going through the hook, including the disabled picker and the loading note staying hidden during a reset. Drag and gallery add are covered only at the hook level.
  • buildSectionOverrides unit tests cover several changed ids and confirm a one-id set matches changedSectionId.
  • HomeScreenSettings.test.tsx is unedited and passes.
  • Web lint (no errors; the warnings are all in files this PR does not touch), format:check, tsc -b, build, budget:check and make test-web: pass. make verify-local-paths: pass.
  • The page was not run in a browser, and Curated home sections — Web #1142 C2 was not walked on a running build. A single reorder sends the same payload as before.

Risks

  • This rewrites how the page saves. Until now no test rendered the page itself; the new page test and the hook tests are the coverage.
  • If a save succeeds and the refetch right after it fails, the list shows the last fetched state until the next successful fetch. Editing is off in that state because the settings query is in error.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code (in T3 Code)
  • Tool(s): Claude Code subagents (implementer, independent reviewer)
  • Model(s): claude-opus-5-5[1m]
  • Involvement: Fully AI-generated, human review pending
  • Adversarial review: An independent reviewer subagent checked the diff against the planned scope, confirmed the saved payload is unchanged, and reran the hook, helper and page tests and tsc -b. It approved with three minor findings, all fixed in the second commit. First, a reset showed "Loading saved section state"; the hook now reports readiness separately from editability. Second, a page switch during a save snapped back silently; the picker is now disabled. Third, the page had no render test; one was added. Two nits were not fixed. The failed-refetch case is recorded under Risks. The two helpers with no production callers stay until the unedited test file that imports them is replaced.

🤖 Generated with Claude Code

Note

Move Home Screen section saves into useProfileHomeRows hook

  • Adds a page-scoped useProfileHomeRows hook that holds home-screen edits as local drafts and sends saves one operation at a time (useProfileHomeRows.ts).
  • Changes made during an in-flight save are merged into a follow-up save; failed saves restore server state unless a newer save is queued.
  • Moves override helpers out of HomeScreenSettings into a reusable module and lets buildSectionOverrides accept a set of changed section IDs, so legacy Trakt rows changed in a merged save are no longer dropped (profileOverrides.ts).
  • Rewrites HomeScreenSettings to delegate queries, drafts, saves, resets, and scope handling to the hook; the scope picker is disabled while work is pending (HomeScreenSettings.tsx).
  • Risk: replaceProfileSectionOverrides and resetProfileSectionOverrides in sections.ts no longer invalidate queries themselves; invalidation now happens only in the hook, so any other callers of these functions lose automatic cache refresh. Editing is also blocked until both section queries load, and page changes are refused while saves or resets drain.

Macroscope summarized 99bb457.

Quick104 and others added 2 commits October 4, 2026 06:39
Settings > Home Screen fired a full override PUT on every change without
waiting for the one before, so two quick edits could land out of order,
and a merged save could only name one changed row, dropping a change to a
legacy Trakt row with no saved override. useProfileHomeRows now owns the
page's reads and writes: one save in flight, later changes merged into a
single next save that carries every row they touched, the page switch
refused until saves and their refetch land, and the last saved state put
back when the newest save fails. The page's JSX is unchanged; its pure
helpers move to lib/homeRows/profileOverrides.ts and are re-exported.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ring saves

The save hook refuses a page switch while a save or its refetch is in
flight, but the picker stayed enabled and snapped back with no feedback;
it is now disabled while saves are pending, as the plan specifies. A
reset also turned off editing, which the page reported as "Loading saved
section state"; the hook now exposes `ready` apart from `canEdit` so the
note shows only while the page is actually loading. A page render test
covers a toggle, a delete and a reset going through the hook.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0b46abfe-981b-404b-ab7c-ef5977585d93

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-04T18:23:20.265308Z 99bb457 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ae0385f97

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +31 to +33
type QueueEntry =
| { kind: "save"; page: PageRef; state: PageState; changedIds: Set<string> }
| { kind: "reset"; page: PageRef };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Bind queued writes to the originating profile

Capture the profile request context when each queue entry is created. If a user queues another save or reset behind an in-flight PUT and then switches household profiles, the delayed send() runs after the switch and the request helper uses the newly active profile because QueueEntry retains no authority snapshot. Clearing the query client does not cancel this local queue, so the old profile's layout can be written to—or reset on—the newly selected profile.

AGENTS.md reference: AGENTS.md:L105-L107

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 760e202. Each queued save or reset now captures the profile request context when it is queued, is sent with that profileContext, and is dropped if another profile is active by the time it would go out (its cached overrides would belong to the other profile). New hook test: a save queued behind an in-flight one is not sent after a profile switch, and the in-flight one goes out as the original profile.

Comment on lines +178 to +180
const changedIds = new Set(q.next?.kind === "save" ? q.next.changedIds : []);
changedIds.add(id);
q.next = { kind: "save", page, state: next, changedIds };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain in-flight IDs in the successor save

When the first save is already in flight, q.next is null, so a later edit starts changedIds from an empty set instead of inheriting the running entry's IDs. If the first edit targets a shown, unsaved legacy Trakt row and that request fails while another row edit is queued, the successor state still contains the Trakt edit, but buildSectionOverrides treats that row as unchanged and omits it; the second PUT then succeeds without the earlier edit even though the hook promises that the newest state wins. Carry the running entry's changed IDs forward until its save succeeds.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 760e202. When a save fails and a newer save is queued, the failed save's changed row IDs are added to the queued one, so a legacy Trakt row edited in the failed save is still named and written. Only the failure path carries them: after a successful save the refetched overrides already hold that row. New hook test covers the Trakt edit failing with another change queued behind it.

@macroscopeapp

macroscopeapp Bot commented Oct 4, 2026

Copy link
Copy Markdown

Major change — #1142 C2 (Web). Reordering now goes through the new useProfileHomeRows save queue (serialized PUTs followed by a refetch) instead of independent page-level mutation calls. This materially reworks the path C2 exercises. The PR must explain why the changed path is expected to preserve C2’s order-persistence behavior, and C2 will need re-validation after merge. The task currently says Not run, so there is no earlier passed build to invalidate.

The current Validation tasks: line says reaches, which does not state the impact clearly.

Suggested fix: No diff; update the PR description to:
Validation tasks: changes #1142 C2

Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed.

Posted via Macroscope — v1 validation impact

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6339ade6d2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

} else {
toast.error(sectionSaveErrorMessage(error));
// A newer change still to be sent carries the user's latest state; keep it.
if (!q.next) setDraft(null);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep the failed draft until the refetch finishes

When a PUT fails with no already-queued successor, this clears draft before the awaited invalidation, while canEdit remains true during that background refetch because the queries still hold successful data. If the response was lost after the server committed the PUT and the user edits another row before the GET completes, that edit captures the stale pre-PUT serverSections; its subsequent full-replacement PUT then undoes the first change even after the refetch discovers it. Retain the draft or disable edits until the authoritative refetch has completed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0d3a1f2 on #1913 (follow-up for late findings)

A save queued behind an in-flight one was sent with whatever household
profile was active when it ran, so switching profiles mid-save could write
or reset the old profile's layout on the new one. Each queued save now
carries the profile it was made under, is sent with that authority, and is
dropped if another profile is picked before it goes out.

A failed save also no longer loses its rows from the save queued behind
it: their IDs carry into that save, so an edit to a legacy Trakt row with
no saved override is still written.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104

Quick104 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR body: the validation line now reads Validation tasks: changes #1142 C2, with a short note on why reorder persistence should hold and that C2 needs a re-test after merge.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 760e2023a8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +199 to +202
const base = draftRef.current ?? {
sections: serverSections ?? [],
removed: hydrateRemovedSystemSections(savedOverrides),
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear the old profile draft before accepting new edits

If profile A has a PUT in flight and the user switches to profile B, clearing the query cache can let B's queries become ready before A's request finishes; editing is not blocked by pending, so B's next change uses A's still-populated draftRef as its base. That entry captures B's authority and therefore passes the active-authority check, causing A's unsaved layout state to be included in the PUT for B. The fresh evidence beyond the earlier queued-authority report is that the newly queued entry belongs to B—the stale state enters through draftRef, so the new profile snapshot does not prevent the cross-profile write. Clear or partition the draft when profile authority changes, or keep editing disabled until the prior profile's drain completes.

AGENTS.md reference: AGENTS.md:L105-L107

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0d3a1f2 on #1913 (follow-up for late findings)

Comment on lines +207 to +209
const changedIds = new Set(q.next?.kind === "save" ? q.next.changedIds : []);
changedIds.add(id);
q.next = { kind: "save", page, profile, state: next, changedIds };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain running IDs until the raw override refetch succeeds

When a successful in-flight save is followed by a failed raw-overrides refetch while a successor save is queued, the successor's changedIds excludes the running entry's IDs and getQueryData still returns the pre-save overrides. If the first edit was to a shown legacy Trakt row without an old override, buildSectionOverrides consequently omits that row from the successor's full-replacement PUT and erases the edit that just succeeded. Fresh evidence beyond the earlier failure-path report is that this occurs after a successful PUT when only the raw-overrides GET fails, so the catch-path union added for failed PUTs never runs.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0d3a1f2 on #1913 (follow-up for late findings)

Base automatically changed from feat/api-profile-section-default-title to feat/collections-revamp October 4, 2026 18:18
@Quick104
Quick104 merged commit 21fb32f into feat/collections-revamp Oct 4, 2026
15 checks passed
@Quick104
Quick104 deleted the refactor/home-screen-save-hook branch October 4, 2026 18:30
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