Skip to content

Commit d4d3df7

Browse files
committed
fix(web): refresh branch rows when a ref changes worktrees
1 parent 1d87f8d commit d4d3df7

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎apps/web/src/components/BranchToolbarBranchSelector.test.tsx‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
RunId,
99
type VcsRef,
1010
} from "@t3tools/contracts";
11-
import { act, createRef, type ReactNode } from "react";
11+
import { act, createRef, type ComponentProps } from "react";
1212
import { createRoot } from "react-dom/client";
1313
import { expect, it, vi } from "vite-plus/test";
1414

1515
const state = vi.hoisted(() => ({
1616
worktreePath: null as string | null,
1717
started: true,
18+
refs: null as VcsRef[] | null,
1819
latestWorktreePath: undefined as string | null | undefined,
1920
latestStarted: undefined as boolean | undefined,
2021
updateMetadata: vi.fn().mockResolvedValue({ _tag: "Success" }),
@@ -90,7 +91,7 @@ vi.mock("../state/query", () => ({
9091
}));
9192
vi.mock("../state/queries", () => ({
9293
usePaginatedBranches: () => ({
93-
refs: [mainRef, topicBaseRef],
94+
refs: state.refs ?? [mainRef, topicBaseRef],
9495
data: { nextCursor: null, totalCount: 2 },
9596
refresh: vi.fn(),
9697
isPending: false,
@@ -110,27 +111,29 @@ vi.mock("./ThreadStatusIndicators", () => ({
110111
useLinkedThreadPullRequest: () => null,
111112
}));
112113
// jsdom has no layout measurements for the virtualized list.
113-
vi.mock("@legendapp/list/react", () => ({
114-
LegendList: ({
115-
data,
116-
renderItem,
117-
}: {
118-
data: string[];
119-
renderItem: (row: { item: string; index: number }) => ReactNode;
120-
}) => data.map((item, index) => <div key={item}>{renderItem({ item, index })}</div>),
121-
}));
114+
vi.mock("@legendapp/list/react", async (importOriginal) => {
115+
const { LegendList } = await importOriginal<typeof import("@legendapp/list/react")>();
116+
return {
117+
LegendList: (props: ComponentProps<typeof LegendList<string>>) => (
118+
<LegendList {...props} estimatedListSize={{ width: 300, height: 224 }} />
119+
),
120+
};
121+
});
122122

123123
import {
124124
BranchToolbarBranchSelector,
125125
type BranchToolbarBranchSelectorHandle,
126126
} from "./BranchToolbarBranchSelector";
127127

128128
it.each(["toolbar", "panel"] as const)(
129-
"keeps the main checkout selectable in %s with a running server thread and retained worktree draft",
129+
"refreshes branch rows in %s when the thread or a ref changes worktrees",
130130
async (displayMode) => {
131131
vi.clearAllMocks();
132132
state.worktreePath = "/repo/worktree";
133133
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
134+
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue(
135+
new DOMRect(0, 0, 300, 224),
136+
);
134137
const container = document.createElement("div");
135138
document.body.append(container);
136139
const root = createRoot(container);
@@ -158,6 +161,12 @@ it.each(["toolbar", "panel"] as const)(
158161
expect(main?.textContent).toContain("main");
159162
expect(main?.getAttribute("aria-disabled")).not.toBe("true");
160163
expect(document.body.textContent).not.toContain("Stop the current turn to switch worktrees.");
164+
state.refs = [{ ...mainRef, worktreePath: "/repo/another-worktree" }, topicBaseRef];
165+
await act(async () => renderSelector());
166+
expect(main?.getAttribute("aria-disabled")).toBe("true");
167+
state.refs = null;
168+
await act(async () => renderSelector());
169+
expect(main?.getAttribute("aria-disabled")).not.toBe("true");
161170
await act(async () => main?.click());
162171
expect(state.updateMetadata).toHaveBeenCalledWith({
163172
environmentId: "local",
@@ -169,6 +178,8 @@ it.each(["toolbar", "panel"] as const)(
169178
container.remove();
170179
state.worktreePath = null;
171180
vi.unstubAllGlobals();
181+
vi.restoreAllMocks();
182+
state.refs = null;
172183
}
173184
},
174185
);
@@ -185,6 +196,9 @@ it.each([
185196
state.started = false;
186197
state.worktreePath = null;
187198
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
199+
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue(
200+
new DOMRect(0, 0, 300, 224),
201+
);
188202
const container = document.createElement("div");
189203
document.body.append(container);
190204
const root = createRoot(container);
@@ -230,5 +244,7 @@ it.each([
230244
state.latestStarted = undefined;
231245
state.latestWorktreePath = undefined;
232246
vi.unstubAllGlobals();
247+
vi.restoreAllMocks();
248+
state.refs = null;
233249
}
234250
});

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,8 @@ export function BranchToolbarBranchSelector({
744744
? WORKTREE_CHANGE_BLOCKED_MESSAGE
745745
: null
746746
}
747-
// A row's disabled state depends on the run status and the current worktree.
748-
extraData={`${serverSession?.status ?? ""}:${serverSession?.activeRunId ?? ""}:${activeWorktreePath ?? ""}`}
747+
// Row availability depends on the run, current worktree, and each ref's worktree.
748+
extraData={`${serverSession?.status ?? ""}:${serverSession?.activeRunId ?? ""}:${activeWorktreePath ?? ""}:${refs.map((ref) => `${ref.name}:${ref.worktreePath ?? ""}`).join("|")}`}
749749
renderItem={renderPickerItem}
750750
getItemType={(item) =>
751751
item === checkoutPullRequestItemValue

0 commit comments

Comments
 (0)