Skip to content

Commit 0ca5fbf

Browse files
author
Techno911
committed
feat: open the root composer beside a restored layout instead of over it
The app opens on the root route. When a saved layout comes back from storage it usually has no composer pane, so `reconcileLayoutForContent` replaced the focused pane with the composer: whatever the person left in that pane was gone after every restart, without them closing it. The first reconcile after mount now uses `reconcileLayoutForContentOnLoad`, which keeps every restored pane and splits the composer in to the right of the rightmost pane (bounded by MAX_PANES). Later navigation and every other content kind keep the usual rules, so archiving or losing a thread still shows the composer in that thread's pane. Unit test covers the split-right and the unchanged path; the surface test restores a two-pane layout on the root route and expects three panes.
1 parent 8ca3861 commit 0ca5fbf

4 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎apps/app/src/views/thread-detail/SplitThreadArea.test.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,20 @@ describe("SplitThreadArea", () => {
17191719
expect(screen.getByTestId("pane-thr-b")).toBeTruthy();
17201720
});
17211721

1722+
it("keeps a restored layout intact on the root route and opens the composer beside it", async () => {
1723+
renderSplitArea({
1724+
path: "/",
1725+
layout: twoPaneLayout("pane-1"),
1726+
routeContent: { kind: "new-thread" },
1727+
});
1728+
1729+
expect(await screen.findByTestId("pane-thr-a")).toBeTruthy();
1730+
expect(screen.getByTestId("pane-thr-b")).toBeTruthy();
1731+
await waitFor(() => {
1732+
expect(document.querySelectorAll("[data-split-pane-id]")).toHaveLength(3);
1733+
});
1734+
});
1735+
17221736
it("restores eight successive default-right opens, then focuses and closes with valid URL state", async () => {
17231737
const layout = eightPaneThreadLayout();
17241738
expect(layout.root).toMatchObject({

‎apps/app/src/views/thread-detail/SplitThreadArea.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import {
9696
focusedPaneRoute,
9797
paneContentRoute,
9898
reconcileLayoutForContent,
99+
reconcileLayoutForContentOnLoad,
99100
threadPaneContent,
100101
} from "./splitThreadNavigation";
101102
import { ThreadDetailWorkerPoolProvider } from "./ThreadDetailWorkerPoolProvider";
@@ -280,12 +281,19 @@ function SplitThreadAreaContent({ routeContent }: SplitThreadAreaProps) {
280281
[routeContent, routeThread],
281282
);
282283

284+
// The first reconcile after mount meets the layout restored from storage;
285+
// it must not close a restored pane to make room for the root composer.
286+
const hasReconciledOnceRef = useRef(false);
283287
useEffect(() => {
284288
if (currentContent === null) {
285289
return;
286290
}
291+
const isInitialReconcile = !hasReconciledOnceRef.current;
292+
hasReconciledOnceRef.current = true;
287293
setLayout((previous) =>
288-
reconcileLayoutForContent(previous, currentContent),
294+
isInitialReconcile
295+
? reconcileLayoutForContentOnLoad(previous, currentContent)
296+
: reconcileLayoutForContent(previous, currentContent),
289297
);
290298
}, [currentContent, setLayout]);
291299

‎apps/app/src/views/thread-detail/splitThreadNavigation.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
createSinglePaneLayout,
1414
focusedPaneRoute,
1515
reconcileLayoutForContent,
16+
reconcileLayoutForContentOnLoad,
1617
} from "./splitThreadNavigation";
1718

1819
function twoPaneLayout(): SplitLayout {
@@ -57,6 +58,40 @@ describe("mixed page navigation", () => {
5758
expect(focusedPaneRoute(after)).toBe("/");
5859
});
5960

61+
it("opens the root composer beside a restored layout instead of over its focused pane", () => {
62+
const restored = twoPaneLayout();
63+
64+
const after = reconcileLayoutForContentOnLoad(restored, {
65+
kind: "new-thread",
66+
});
67+
68+
expect(listPanes(after.root)).toHaveLength(3);
69+
expect(
70+
listPanes(after.root).filter((pane) => pane.content.kind === "thread"),
71+
).toHaveLength(2);
72+
expect(after.focusedPaneId).toBe(
73+
findPaneByContent(after.root, { kind: "new-thread" })?.paneId,
74+
);
75+
// Rightmost pane, whichever pane was focused when the layout was saved.
76+
const order = listPanes(after.root).map((pane) => pane.content.kind);
77+
expect(order).toEqual(["thread", "thread", "new-thread"]);
78+
});
79+
80+
it("keeps the usual replace rule on load for content other than the composer", () => {
81+
const restored = twoPaneLayout();
82+
const plugin = {
83+
kind: "plugin-panel",
84+
pluginId: "notes",
85+
panelPath: "notes",
86+
subPath: "",
87+
} as const;
88+
89+
const after = reconcileLayoutForContentOnLoad(restored, plugin);
90+
91+
expect(listPanes(after.root)).toHaveLength(2);
92+
expect(findPaneByContent(after.root, plugin)).not.toBeNull();
93+
});
94+
6095
it("updates a plugin pane's subpath without duplicating the panel", () => {
6196
const plugin = {
6297
kind: "plugin-panel",

‎apps/app/src/views/thread-detail/splitThreadNavigation.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ThreadRoutePathArgs } from "@/lib/route-paths";
22
import type { ThreadOpenSplit, ThreadPaneAction } from "@bb/server-contract";
33
import {
4+
listPanes,
45
countPanes,
56
findPane,
67
findPaneByContent,
@@ -124,6 +125,39 @@ export function reconcileLayoutForContent(
124125
return replacePaneContent(layout, layout.focusedPaneId, content);
125126
}
126127

128+
/**
129+
* Reconciles the route with a layout restored from storage on load.
130+
*
131+
* The app opens on the root route, and a restored layout usually has no
132+
* composer pane. Replacing the focused pane would silently close whatever
133+
* the person left there, so on load the composer opens beside it instead:
134+
* the restored panes stay, the composer takes a new pane on the right.
135+
* Any other content, and any later navigation, keeps the usual rules.
136+
*/
137+
export function reconcileLayoutForContentOnLoad(
138+
layout: SplitLayout | null,
139+
content: PaneContent,
140+
): SplitLayout {
141+
if (
142+
layout === null ||
143+
content.kind !== "new-thread" ||
144+
findPaneByContent(layout.root, content) !== null ||
145+
countPanes(layout.root) >= MAX_PANES
146+
) {
147+
return reconcileLayoutForContent(layout, content);
148+
}
149+
// Rightmost, not beside the focused pane: the person asked for the composer
150+
// to always land to the right of everything that was open.
151+
const panes = listPanes(layout.root);
152+
const rightmost = panes[panes.length - 1] ?? null;
153+
return splitPane(
154+
layout,
155+
rightmost?.paneId ?? layout.focusedPaneId,
156+
"right",
157+
content,
158+
);
159+
}
160+
127161
export function focusedPaneRoute(layout: SplitLayout): string | null {
128162
const focused = findPane(layout.root, layout.focusedPaneId);
129163
return focused === null ? null : paneContentRoute(focused.content);

0 commit comments

Comments
 (0)