Skip to content

Commit 9ff0fac

Browse files
wrymnLukas Tomondy
andauthored
fix(web): make Changes and PR Code file lists resizable (#28244)
Co-authored-by: Lukas Tomondy <[email protected]>
1 parent 6717d92 commit 9ff0fac

6 files changed

Lines changed: 194 additions & 21 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./Dif
6767
import { DiffStatLabel } from "./chat/DiffStatLabel";
6868
import { AnnotatableCodeView } from "./diffs/AnnotatableCodeView";
6969
import { DiffFileTree } from "./diffs/DiffFileTree";
70+
import { FileExplorerPane } from "./files/FileExplorerPane";
7071
import { diffFileTreeEntries } from "./diffs/diffFileTree.logic";
7172
import { Button } from "./ui/button";
7273
import { MorphIcon } from "~/components/MorphIcon";
@@ -1451,15 +1452,20 @@ export default function DiffPanel({
14511452
/>
14521453
</div>
14531454
{fileTreeOpen ? (
1454-
<aside className="flex w-[min(16rem,40%)] min-w-40 shrink-0 border-l border-border/60">
1455+
<FileExplorerPane
1456+
resizable
1457+
storageKey="t3code:diff-file-tree-width"
1458+
defaultWidthFraction={0.4}
1459+
resizeLabel="Resize Changes files"
1460+
>
14551461
<DiffFileTree
14561462
ariaLabel={`${reviewSectionTitle} files`}
14571463
entries={fileTreeEntries}
14581464
selectedPath={selectedFilePath}
14591465
revealRequestId={selectedFileRevealRequestId}
14601466
onSelectFile={revealDiffFile}
14611467
/>
1462-
</aside>
1468+
</FileExplorerPane>
14631469
) : null}
14641470
</div>
14651471
) : (
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { render } from "../../../test/render";
4+
import { FileExplorerPane } from "./FileExplorerPane";
5+
6+
const STORAGE_KEY = "test:file-explorer-default";
7+
8+
describe("FileExplorerPane", () => {
9+
it.each([
10+
{ view: "Changes", defaultWidth: 256, defaultWidthFraction: 0.4, minWidth: 160, expected: 168 },
11+
{ view: "PR Code", defaultWidth: 320, defaultWidthFraction: 0.4, minWidth: 192, expected: 192 },
12+
{
13+
view: "Files",
14+
defaultWidth: 256,
15+
minWidth: 160,
16+
expected: 164,
17+
},
18+
])(
19+
"preserves the $view initial width in a 420px row",
20+
async ({ expected, view: _, ...sizing }) => {
21+
const previous = localStorage.getItem(STORAGE_KEY);
22+
localStorage.removeItem(STORAGE_KEY);
23+
try {
24+
// This layout has no navigation or links.
25+
await using screen = await render(
26+
<div style={{ display: "flex", width: 420 }}>
27+
<FileExplorerPane resizable storageKey={STORAGE_KEY} {...sizing}>
28+
File tree
29+
</FileExplorerPane>
30+
</div>,
31+
{ router: false },
32+
);
33+
await expect
34+
.element(screen.getByRole("separator"))
35+
.toHaveAttribute("aria-valuenow", String(expected));
36+
expect(screen.container.querySelector("aside")!.getBoundingClientRect().width).toBe(
37+
expected,
38+
);
39+
} finally {
40+
if (previous === null) localStorage.removeItem(STORAGE_KEY);
41+
else localStorage.setItem(STORAGE_KEY, previous);
42+
}
43+
},
44+
);
45+
});

‎apps/web/src/components/files/FileExplorerPane.tsx‎

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@ const MIN_WIDTH = 160;
99
const MAX_WIDTH = 560;
1010
const FILE_CONTENT_MIN_WIDTH = 256;
1111

12+
/** A persisted, resizable file-tree column; fills the row when resizing is disabled.
13+
* Callers can retain a responsive initial width via defaultWidthFraction (a fraction of the row),
14+
* and use a separate saved width, minimum and resize label. A chosen width overrides the default.
15+
*/
1216
export function FileExplorerPane({
1317
resizable,
1418
children,
19+
storageKey = "t3code.fileExplorerWidth",
20+
defaultWidth = 256,
21+
defaultWidthFraction,
22+
minWidth = MIN_WIDTH,
23+
resizeLabel = "Resize file explorer",
1524
}: {
1625
resizable: boolean;
1726
children: ReactNode;
27+
storageKey?: string;
28+
defaultWidth?: number;
29+
defaultWidthFraction?: number;
30+
minWidth?: number;
31+
resizeLabel?: string;
1832
}) {
1933
const [maxWidth, setMaxWidth] = useState(MAX_WIDTH);
34+
const [responsiveDefaultWidth, setResponsiveDefaultWidth] = useState(defaultWidth);
2035
const { width, setWidth, handlers } = useResizableWidth({
21-
storageKey: "t3code.fileExplorerWidth",
22-
defaultWidth: 256,
23-
minWidth: MIN_WIDTH,
36+
storageKey,
37+
defaultWidth: responsiveDefaultWidth,
38+
minWidth,
2439
maxWidth,
2540
edge: "left",
2641
});
@@ -30,10 +45,19 @@ export function FileExplorerPane({
3045
ref={(host) => {
3146
const row = host?.parentElement;
3247
if (!row || !resizable) return;
33-
const measure = () =>
34-
setMaxWidth(
35-
Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, row.clientWidth - FILE_CONTENT_MIN_WIDTH)),
48+
const measure = () => {
49+
const initialWidth = Math.max(
50+
minWidth,
51+
Math.min(defaultWidth, row.clientWidth * (defaultWidthFraction ?? 1)),
3652
);
53+
// A responsive default must not shrink to make room for the content reserve.
54+
const availableWidth = Math.max(
55+
row.clientWidth - FILE_CONTENT_MIN_WIDTH,
56+
defaultWidthFraction === undefined ? minWidth : initialWidth,
57+
);
58+
setMaxWidth(Math.max(minWidth, Math.min(MAX_WIDTH, availableWidth)));
59+
setResponsiveDefaultWidth(initialWidth);
60+
};
3761
measure();
3862
return observeResize(row, measure);
3963
}}
@@ -54,10 +78,10 @@ export function FileExplorerPane({
5478
{resizable ? (
5579
<RightPanelResizeHandle
5680
handlers={handlers}
57-
label="Resize file explorer"
81+
label={resizeLabel}
5882
tabIndex={0}
5983
aria-valuenow={width}
60-
aria-valuemin={MIN_WIDTH}
84+
aria-valuemin={minWidth}
6185
aria-valuemax={maxWidth}
6286
onKeyDown={(event) => {
6387
if (event.altKey || event.ctrlKey || event.metaKey) return;
@@ -70,7 +94,7 @@ export function FileExplorerPane({
7094
nextWidth = width - 16;
7195
break;
7296
case "Home":
73-
nextWidth = MIN_WIDTH;
97+
nextWidth = minWidth;
7498
break;
7599
case "End":
76100
nextWidth = maxWidth;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import { DiffPanelLoadingState } from "../DiffPanelShell";
8181
import { useDiffWorkerPool } from "../DiffWorkerPoolProvider";
8282
import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";
8383
import { DiffFileTree } from "../diffs/DiffFileTree";
84+
import { FileExplorerPane } from "../files/FileExplorerPane";
8485
import { useCodeViewFileReveal } from "../diffs/useCodeViewFileReveal";
8586
import { useHideTestFiles } from "../diffs/useHideTestFiles";
8687
import { diffFileTreeEntries, orderDiffFilesLikeTree } from "../diffs/diffFileTree.logic";
@@ -1515,7 +1516,14 @@ function PullRequestCodeTab({
15151516
</FileViewedToggleContext>
15161517
</div>
15171518
{fileTreeOpen ? (
1518-
<aside className="flex w-[min(20rem,40%)] min-w-48 shrink-0 border-l border-border/60">
1519+
<FileExplorerPane
1520+
resizable
1521+
storageKey="t3code:diff-file-tree-width"
1522+
defaultWidth={320}
1523+
defaultWidthFraction={0.4}
1524+
minWidth={192}
1525+
resizeLabel="Resize PR Code files"
1526+
>
15191527
<DiffFileTree
15201528
ariaLabel={`${capitalizeTerm(term)} #${detail.number} files`}
15211529
entries={fileTreeEntries}
@@ -1541,7 +1549,7 @@ function PullRequestCodeTab({
15411549
)
15421550
}
15431551
/>
1544-
</aside>
1552+
</FileExplorerPane>
15451553
) : null}
15461554
</div>
15471555
{unstructured}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { act, render } from "../../test/render";
4+
import { RESIZABLE_WIDTH_PROPERTY, useResizableWidth } from "./useResizableWidth";
5+
6+
const STORAGE_KEY = "test:responsive-panel-width";
7+
8+
function Panel({ defaultWidth }: { defaultWidth: number }) {
9+
const { width, setWidth, handlers } = useResizableWidth({
10+
storageKey: STORAGE_KEY,
11+
defaultWidth,
12+
minWidth: 160,
13+
maxWidth: 560,
14+
edge: "left",
15+
});
16+
return (
17+
<>
18+
<button onClick={() => setWidth(350)}>{width}</button>
19+
<div role="separator" style={{ width: 8, height: 40 }} {...handlers} />
20+
</>
21+
);
22+
}
23+
24+
describe("useResizableWidth", () => {
25+
it("follows a responsive default until the user saves a width", async () => {
26+
const previous = localStorage.getItem(STORAGE_KEY);
27+
localStorage.removeItem(STORAGE_KEY);
28+
try {
29+
// This hook only needs browser storage and React; no navigation is involved.
30+
await using screen = await render(<Panel defaultWidth={320} />, { router: false });
31+
expect(screen.container.textContent).toBe("320");
32+
await screen.rerender(<Panel defaultWidth={200} />);
33+
expect(screen.container.textContent).toBe("200");
34+
35+
await act(() => screen.container.querySelector("button")!.click());
36+
await screen.rerender(<Panel defaultWidth={180} />);
37+
expect(screen.container.textContent).toBe("350");
38+
39+
await screen.unmount();
40+
await using restored = await render(<Panel defaultWidth={200} />, { router: false });
41+
expect(restored.container.textContent).toBe("350");
42+
} finally {
43+
if (previous === null) localStorage.removeItem(STORAGE_KEY);
44+
else localStorage.setItem(STORAGE_KEY, previous);
45+
}
46+
});
47+
48+
it("keeps the default responsive after a stationary click and persists an actual drag", async () => {
49+
const previous = localStorage.getItem(STORAGE_KEY);
50+
localStorage.removeItem(STORAGE_KEY);
51+
try {
52+
// This hook only needs browser storage and React; no navigation is involved.
53+
await using screen = await render(<Panel defaultWidth={240} />, { router: false });
54+
await screen.getByRole("separator").click();
55+
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
56+
await screen.rerender(<Panel defaultWidth={256} />);
57+
expect(screen.container.textContent).toBe("256");
58+
59+
const handle = screen.container.querySelector<HTMLElement>('[role="separator"]')!;
60+
// Synthetic drag events have no active browser pointer to capture.
61+
using _capture = vi.spyOn(handle, "setPointerCapture").mockImplementation(() => {});
62+
const pointer = { bubbles: true, pointerId: 1, button: 0 };
63+
await act(() => {
64+
handle.dispatchEvent(new PointerEvent("pointerdown", { ...pointer, clientX: 500 }));
65+
});
66+
await screen.rerender(<Panel defaultWidth={280} />);
67+
await act(() => {
68+
handle.dispatchEvent(new PointerEvent("pointerup", { ...pointer, clientX: 500 }));
69+
});
70+
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
71+
expect(screen.container.style.getPropertyValue(RESIZABLE_WIDTH_PROPERTY)).toBe("280px");
72+
73+
await act(() => {
74+
handle.dispatchEvent(new PointerEvent("pointerdown", { ...pointer, clientX: 500 }));
75+
handle.dispatchEvent(new PointerEvent("pointerup", { ...pointer, clientX: 450 }));
76+
});
77+
expect(localStorage.getItem(STORAGE_KEY)).toBe("330");
78+
await screen.rerender(<Panel defaultWidth={200} />);
79+
expect(screen.container.textContent).toBe("330");
80+
} finally {
81+
if (previous === null) localStorage.removeItem(STORAGE_KEY);
82+
else localStorage.setItem(STORAGE_KEY, previous);
83+
}
84+
});
85+
});

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useState } fro
33

44
import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
55
import { useResizeDrag } from "./useResizeDrag";
6+
import { useStableCallback } from "./useStableCallback";
67

78
const WidthSchema = Schema.Finite;
89

@@ -26,7 +27,7 @@ export interface ResizableWidthHandlers {
2627
readonly onLostPointerCapture: (event: ReactPointerEvent<HTMLElement>) => void;
2728
}
2829

29-
/** Persist on drag end to avoid a storage write on every animation frame. */
30+
/** Uses the current default until a width is chosen; persists on drag end, not every frame. */
3031
export function useResizableWidth(options: UseResizableWidthOptions): {
3132
readonly width: number;
3233
readonly setWidth: (width: number) => void;
@@ -39,22 +40,25 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
3940
return Math.max(minWidth, Math.min(maxWidth, value));
4041
};
4142

42-
const readWidth = () => {
43-
if (typeof window === "undefined") return defaultWidth;
43+
const readWidth = (): number | null => {
44+
if (typeof window === "undefined") return null;
4445
try {
4546
const stored = getLocalStorageItem(storageKey, WidthSchema);
46-
return clamp(stored ?? defaultWidth);
47+
return stored === null ? null : clamp(stored);
4748
} catch (error) {
4849
console.error("Could not read persisted panel width.", error);
49-
return defaultWidth;
50+
return null;
5051
}
5152
};
5253
const [widthState, setWidthState] = useState(() => ({ storageKey, width: readWidth() }));
5354
if (widthState.storageKey !== storageKey) {
5455
setWidthState({ storageKey, width: readWidth() });
5556
}
5657

57-
const clampedWidth = clamp(widthState.width);
58+
const clampedWidth = clamp(widthState.width ?? defaultWidth);
59+
const restoreWidth = useStableCallback((host: HTMLElement | null) => {
60+
host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${clampedWidth}px`);
61+
});
5862

5963
const commitWidth = (nextStorageKey: string, nextWidth: number) => {
6064
// Resizing remains usable for this session when browser storage is unavailable.
@@ -82,8 +86,9 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
8286
host?.style.setProperty(RESIZABLE_WIDTH_PROPERTY, `${nextWidth}px`);
8387
return nextWidth;
8488
},
85-
finish(finalWidth) {
86-
commitWidth(startStorageKey, finalWidth);
89+
finish(finalWidth, moved) {
90+
if (moved) commitWidth(startStorageKey, finalWidth);
91+
else restoreWidth(host);
8792
},
8893
cleanup(committed) {
8994
host?.style.removeProperty("transition-property");

0 commit comments

Comments
 (0)