Skip to content

Commit 66ffa76

Browse files
committed
fix(web): a promoted draft pane counts as its thread when deduping panes
1 parent 408105a commit 66ffa76

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎apps/web/src/components/chat/SplitChatWorkspace.tsx‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
12
import { useNavigate } from "@tanstack/react-router";
23
import { Columns2, Maximize2, Minimize2, Rows2, X } from "lucide-react";
34
import { memo, type CSSProperties, useRef, useState, useEffect, useLayoutEffect } from "react";
@@ -28,6 +29,12 @@ interface SplitChatWorkspaceProps {
2829
readonly modalOpen: boolean;
2930
}
3031

32+
// Stays the draft's key after promotion so the pane keeps its ChatView mounted.
33+
function paneKey(target: ThreadRouteTarget | undefined): string {
34+
if (!target) return "";
35+
return target.kind === "server" ? scopedThreadKey(target.threadRef) : `draft:${target.draftId}`;
36+
}
37+
3138
export const SplitChatWorkspace = memo(function SplitChatWorkspace({
3239
primaryTarget,
3340
modalOpen,
@@ -144,7 +151,7 @@ export const SplitChatWorkspace = memo(function SplitChatWorkspace({
144151
? Math.min(index, next.length - 1)
145152
: activeIndex;
146153
if (index === 0) navigatePrimary(nextPrimary);
147-
if (activeIndex === index) focusTargetRef.current = threadTargetKey(next[active]);
154+
if (activeIndex === index) focusTargetRef.current = paneKey(next[active]);
148155
store.replaceSecondaryPanes(next.slice(1), active);
149156
};
150157
const resolveDrop = (
@@ -190,7 +197,7 @@ export const SplitChatWorkspace = memo(function SplitChatWorkspace({
190197
if (!nextPrimary) return false;
191198
if (threadTargetKey(nextPrimary) !== threadTargetKey(effectivePrimary))
192199
navigatePrimary(nextPrimary);
193-
focusTargetRef.current = threadTargetKey(target);
200+
focusTargetRef.current = paneKey(target);
194201
store.replaceSecondaryPanes(next.slice(1), zone.index);
195202
setDropZone(null);
196203
return true;
@@ -298,9 +305,9 @@ export const SplitChatWorkspace = memo(function SplitChatWorkspace({
298305
{panes.map((target, index) => (
299306
<div
300307
// The primary pane keeps one key so draft promotion keeps its ChatView mounted.
301-
key={index === 0 ? "primary" : threadTargetKey(target)}
308+
key={index === 0 ? "primary" : paneKey(target)}
302309
data-split-pane-index={index}
303-
data-split-pane-key={threadTargetKey(target)}
310+
data-split-pane-key={paneKey(target)}
304311
tabIndex={-1}
305312
hidden={maximized !== null && maximized !== index}
306313
style={{

‎apps/web/src/splitChatStore.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it, beforeEach } from "vite-plus/test";
22
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
33
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
44

5+
import { type DraftId, useComposerDraftStore } from "./composerDraftStore";
56
import { useSplitChatStore, MAX_SPLIT_CHATS } from "./splitChatStore";
67
import type { ThreadRouteTarget } from "./threadRoutes";
78

@@ -59,6 +60,12 @@ describe("splitChatStore", () => {
5960
expect(useSplitChatStore.getState().openToSide(thread3, thread3)).toBe(true);
6061
expect(useSplitChatStore.getState().secondaryPanes).toHaveLength(2);
6162
expect(useSplitChatStore.getState().activePaneIndex).toBe(0);
63+
64+
useComposerDraftStore.setState({ promotedDraftRedirects: { "draft-4": thread4.threadRef } });
65+
useSplitChatStore.getState().openToSide({ kind: "draft", draftId: "draft-4" as DraftId });
66+
expect(useSplitChatStore.getState().openToSide(thread4)).toBe(true);
67+
expect(useSplitChatStore.getState().secondaryPanes).toHaveLength(3);
68+
expect(useSplitChatStore.getState().activePaneIndex).toBe(3);
6269
});
6370

6471
it("enforces the maximum limit of 4 chats total (3 secondary)", () => {

‎apps/web/src/splitChatStore.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { scopedThreadKey } from "@t3tools/client-runtime/environment";
22
import { create } from "zustand";
33
import { createJSONStorage, persist } from "zustand/middleware";
44

5+
import { useComposerDraftStore } from "./composerDraftStore";
56
import { resolveStorage } from "./lib/storage";
67
import type { ThreadRouteTarget } from "./threadRoutes";
78

@@ -18,7 +19,10 @@ interface SplitChatPersisted {
1819

1920
export function threadTargetKey(target: ThreadRouteTarget | null | undefined): string {
2021
if (!target) return "";
21-
return target.kind === "server" ? scopedThreadKey(target.threadRef) : `draft:${target.draftId}`;
22+
if (target.kind === "server") return scopedThreadKey(target.threadRef);
23+
// A promoted draft pane shows its server thread, so it dedupes as that thread.
24+
const promoted = useComposerDraftStore.getState().getPromotedDraftRedirect(target.draftId);
25+
return promoted ? scopedThreadKey(promoted) : `draft:${target.draftId}`;
2226
}
2327

2428
interface SplitChatState {

0 commit comments

Comments
 (0)