Skip to content

Commit b4aaaee

Browse files
fix(pull-requests): a saved maintainer-edits choice yields to the host's next reading
The saved choice stayed for as long as the host value matched the value it was made against. If maintainer edits were turned back on elsewhere before T3 read the disabled state, every later read matched, and the menu showed "off" until remount. The choice now ends at the first detail with a different observedAt after the save, then follows the host. While saving it tracks the latest reading, so a read that was already in flight and lands during the save still counts as stale. The post-save refresh rebuilds the detail atom, which interrupts any read still in flight. The server keys detail reads by a per-PR epoch that update bumps, so the next reading started after the write. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent affe7e2 commit b4aaaee

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎apps/mobile/src/features/pull-requests/usePullRequestMaintainerEdits.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ export function usePullRequestMaintainerEdits(
2323
const pending = useRef(false);
2424
const [saving, setSaving] = useState(false);
2525
const [choice, setChoice] = useState<MaintainerEditsChoice | null>(null);
26-
const observed = pr?.maintainerCanModify;
27-
const kept = maintainerEditsChoiceInForce(choice, pr?.url ?? "", observed, saving);
26+
const kept = maintainerEditsChoiceInForce(choice, pr?.url ?? "", pr?.observedAt, saving);
2827
if (kept !== choice) {
2928
setChoice(kept);
3029
}
31-
const value = kept?.value ?? observed === true;
30+
const value = kept?.value ?? pr?.maintainerCanModify === true;
3231
const save = async (next: boolean) => {
3332
if (pending.current || pr === null || !canEdit) return;
3433
pending.current = true;
3534
const previousChoice = choice;
3635
setSaving(true);
37-
setChoice({ key: pr.url, observed, value: next });
36+
setChoice({ key: pr.url, observedAt: pr.observedAt, value: next });
3837
const response = await update({
3938
...target,
4039
input: { ...target.input, maintainerCanModify: next },

‎apps/web/src/components/pullRequest/PullRequestActionsMenu.browser.test.tsx‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const openDetail: Detail = {
4444
isDraft: false,
4545
headBranch: "feature",
4646
maintainerCanModify: true,
47+
observedAt: 1,
4748
capabilities: {
4849
diff: true,
4950
comment: true,
@@ -156,13 +157,33 @@ it("keeps a saved choice through a stale refresh, then follows the host", async
156157
await using screen = await render(control());
157158
await screen.getByRole("button", { name: "More pull request actions" }).click();
158159
const checkbox = page.getByRole("menuitemcheckbox", { name: "Allow edits from maintainers" });
160+
let settle!: (value: { _tag: "Success" }) => void;
161+
mocks.update.mockReturnValueOnce(
162+
new Promise((resolve) => {
163+
settle = resolve;
164+
}),
165+
);
159166
await checkbox.click();
167+
await screen.rerender(control({ ...openDetail, observedAt: 2 }));
168+
settle({ _tag: "Success" });
160169
await expect.element(page.getByRole("status")).not.toBeInTheDocument();
161-
await screen.rerender(control({ ...openDetail }));
170+
await screen.rerender(control({ ...openDetail, observedAt: 2 }));
171+
await expect.element(checkbox).not.toBeChecked();
172+
await screen.rerender(control({ ...openDetail, maintainerCanModify: false, observedAt: 3 }));
162173
await expect.element(checkbox).not.toBeChecked();
163-
await screen.rerender(control({ ...openDetail, maintainerCanModify: false }));
174+
await screen.rerender(control({ ...openDetail, maintainerCanModify: true, observedAt: 4 }));
175+
await expect.element(checkbox).toBeChecked();
176+
});
177+
178+
it("follows the host's first reading after a save, even when it matches the old value", async () => {
179+
const control = setup();
180+
await using screen = await render(control());
181+
await screen.getByRole("button", { name: "More pull request actions" }).click();
182+
const checkbox = page.getByRole("menuitemcheckbox", { name: "Allow edits from maintainers" });
183+
await checkbox.click();
184+
await expect.element(page.getByRole("status")).not.toBeInTheDocument();
164185
await expect.element(checkbox).not.toBeChecked();
165-
await screen.rerender(control({ ...openDetail, maintainerCanModify: true }));
186+
await screen.rerender(control({ ...openDetail, observedAt: 2 }));
166187
await expect.element(checkbox).toBeChecked();
167188
});
168189

‎apps/web/src/components/pullRequest/PullRequestActionsMenu.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function PullRequestActionsMenu({
115115
| "capabilities"
116116
| "viewerPermissions"
117117
| "maintainerCanModify"
118+
| "observedAt"
118119
>;
119120
terminology: ChangeRequestTerminology;
120121
linkThreadRef: ScopedThreadRef | null;
@@ -168,7 +169,7 @@ export function PullRequestActionsMenu({
168169
const maintainerEditsChoiceKept = maintainerEditsChoiceInForce(
169170
maintainerEditsChoice,
170171
pullRequestKey,
171-
detail.maintainerCanModify,
172+
detail.observedAt,
172173
maintainerEditsSaving,
173174
);
174175
if (maintainerEditsChoiceKept !== maintainerEditsChoice) {
@@ -180,7 +181,7 @@ export function PullRequestActionsMenu({
180181
const saveMaintainerEdits = async (value: boolean) => {
181182
if (!canSaveMaintainerEdits) return;
182183
const previousChoice = maintainerEditsChoice;
183-
setMaintainerEditsChoice({ key: pullRequestKey, observed: detail.maintainerCanModify, value });
184+
setMaintainerEditsChoice({ key: pullRequestKey, observedAt: detail.observedAt, value });
184185
setMaintainerEditsSaving(true);
185186
const result = await updatePullRequest({
186187
environmentId,

‎packages/client-runtime/src/pullRequestEditing.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,23 @@ export function canEditPullRequestMaintainerEdits(
4141

4242
export interface MaintainerEditsChoice {
4343
readonly key: string;
44-
readonly observed: boolean | undefined;
44+
readonly observedAt: number | undefined;
4545
readonly value: boolean;
4646
}
4747

48-
/** A choice ends at the first host value that differs from the one it was made against. */
48+
/**
49+
* A saved choice ends at the first host reading delivered after the save. Readers refresh once it
50+
* succeeds, which drops reads still in flight, so that reading started after the write.
51+
*/
4952
export function maintainerEditsChoiceInForce(
5053
choice: MaintainerEditsChoice | null,
5154
key: string,
52-
observed: boolean | undefined,
55+
observedAt: number | undefined,
5356
saving: boolean,
5457
): MaintainerEditsChoice | null {
5558
if (choice === null || choice.key !== key) return null;
56-
return saving || choice.observed === observed ? choice : null;
59+
if (saving) return choice.observedAt === observedAt ? choice : { ...choice, observedAt };
60+
return observedAt !== undefined && choice.observedAt === observedAt ? choice : null;
5761
}
5862

5963
/** Hosts do not support rewriting review summaries through comment mutations. */

0 commit comments

Comments
 (0)