Skip to content

Commit 8a31fca

Browse files
saphidclaude
andcommitted
refactor(web): pin and reorder just expire unpin Undo
Pin and pinned reorder only need to invalidate an outstanding Undo, not own one, so they drop the try/finally wrappers that tripped memo-dependency lint. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d04ece4 commit 8a31fca

3 files changed

Lines changed: 23 additions & 28 deletions

File tree

‎apps/web/src/hooks/threadPinAction.test.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { describe, expect, it } from "vite-plus/test";
33
import * as ThreadPinAction from "./threadPinAction";
44

55
describe("pin action ownership", () => {
6-
it("does not revive the first Undo after a later pin and unpin finish", () => {
6+
it("does not revive the first Undo after a later pin and unpin", () => {
77
const firstUnpin = ThreadPinAction.begin("env/thread");
8-
const pin = ThreadPinAction.begin("env/thread");
9-
pin.finish();
8+
ThreadPinAction.invalidate("env/thread");
109
const secondUnpin = ThreadPinAction.begin("env/thread");
1110
expect(firstUnpin.isCurrent()).toBe(false);
1211
expect(secondUnpin.isCurrent()).toBe(true);
@@ -16,12 +15,10 @@ describe("pin action ownership", () => {
1615
expect(firstUnpin.isCurrent()).toBe(false);
1716
});
1817

19-
it("rejects late completion after a newer action has already completed", () => {
18+
it("rejects a late unpin completion after a newer pin started", () => {
2019
const pendingUnpin = ThreadPinAction.begin("env/late");
21-
const newerPin = ThreadPinAction.begin("env/late");
22-
newerPin.finish();
20+
ThreadPinAction.invalidate("env/late");
2321
expect(pendingUnpin.isCurrent()).toBe(false);
24-
pendingUnpin.finish();
2522
});
2623

2724
it("expires an Undo without invalidating another environment or thread", () => {

‎apps/web/src/hooks/threadPinAction.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Shared across hook instances so sidebar, header and menu actions invalidate each other.
22
const currentActions = new Map<string, symbol>();
33

4+
/** Claims a thread's pin state for one unpin Undo; any later claim or invalidation expires it. */
45
export function begin(key: string) {
56
const token = Symbol();
67
currentActions.set(key, token);
@@ -12,3 +13,8 @@ export function begin(key: string) {
1213
},
1314
};
1415
}
16+
17+
/** Expires any outstanding Undo for the thread, e.g. when it is pinned or reordered. */
18+
export function invalidate(key: string) {
19+
currentActions.delete(key);
20+
}

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,14 @@ export function useThreadActions() {
578578
const orderKey = readEnvironmentSupportsPinReorder(target.environmentId)
579579
? (opts.orderKey ?? topOfPinnedRunOrderKey())
580580
: undefined;
581-
const action = ThreadPinAction.begin(scopedThreadKey(target));
582-
try {
583-
return await pinThreadMutation({
584-
environmentId: target.environmentId,
585-
input: {
586-
threadId: target.threadId,
587-
...(orderKey !== undefined ? { orderKey } : {}),
588-
},
589-
});
590-
} finally {
591-
action.finish();
592-
}
581+
ThreadPinAction.invalidate(scopedThreadKey(target));
582+
return pinThreadMutation({
583+
environmentId: target.environmentId,
584+
input: {
585+
threadId: target.threadId,
586+
...(orderKey !== undefined ? { orderKey } : {}),
587+
},
588+
});
593589
},
594590
[pinThreadMutation],
595591
);
@@ -691,15 +687,11 @@ export function useThreadActions() {
691687
),
692688
);
693689
}
694-
const action = ThreadPinAction.begin(scopedThreadKey(target));
695-
try {
696-
return await reorderPinnedThreadMutation({
697-
environmentId: target.environmentId,
698-
input: { threadId: target.threadId, orderKey },
699-
});
700-
} finally {
701-
action.finish();
702-
}
690+
ThreadPinAction.invalidate(scopedThreadKey(target));
691+
return reorderPinnedThreadMutation({
692+
environmentId: target.environmentId,
693+
input: { threadId: target.threadId, orderKey },
694+
});
703695
},
704696
[reorderPinnedThreadMutation],
705697
);

0 commit comments

Comments
 (0)