Skip to content

Commit f16ae12

Browse files
refactor(web): thread find state lives in one store instead of event buses and mirrors (pingdotgg#25073)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9f7b8ad commit f16ae12

15 files changed

Lines changed: 644 additions & 495 deletions

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useCh
1919
import { useComposerSend } from "./chat/useComposerSend";
2020
import { useComposerResume } from "./chat/useComposerResume";
2121
import { useImplementPlanInNewThread } from "./chat/useProposedPlanSend";
22-
import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";
22+
import { ThreadFind, ThreadFindCanvas } from "./chat/ThreadFindProvider";
23+
import { closeThreadFind, openThreadFind, useThreadFindOpen } from "./chat/threadFindStore";
2324
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
2425
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
2526
import { resolveDraftHeroState } from "./ChatView.logic";
@@ -2656,10 +2657,9 @@ export default function ChatView(props: ChatViewProps) {
26562657
const timelineSkills = activeProviderStatus
26572658
? resolveProviderSkillsForCwd(activeProviderStatus, gitCwd)
26582659
: EMPTY_PROVIDER_SKILLS;
2659-
const threadFindControlsRef = useRef<ThreadFindControls | null>(null);
2660-
const [isThreadFindActive, setIsThreadFindActive] = useState(false);
2661-
const openThreadFind = () => threadFindControlsRef.current?.open();
2662-
const closeThreadFind = () => threadFindControlsRef.current?.close();
2660+
const threadFindEnabled =
2661+
isServerThread && serverConfig?.threadFind === true && !paintOnlyDisplayedTimeline;
2662+
const isThreadFindActive = useThreadFindOpen(activeThreadRef, threadFindEnabled);
26632663

26642664
useChatShortcuts({
26652665
enabled: activeThreadId !== null && !backgroundThread,
@@ -2681,8 +2681,9 @@ export default function ChatView(props: ChatViewProps) {
26812681
if (!event.repeat) copyActiveThreadReference();
26822682
},
26832683
"chat.find": () => {
2684-
if (!isServerThread || serverConfig?.threadFind !== true) return false;
2685-
openThreadFind();
2684+
if (!isServerThread || serverConfig?.threadFind !== true || activeThreadKey === null)
2685+
return false;
2686+
openThreadFind(activeThreadKey);
26862687
},
26872688
"composer.cycleHost": ({ event }) => {
26882689
if (!canChangeEnvironment) return false;
@@ -3208,11 +3209,8 @@ export default function ChatView(props: ChatViewProps) {
32083209
skills: timelineSkills,
32093210
progressive: serverConfig?.threadFindProgressive === true,
32103211
thread: activeThreadRef,
3211-
enabled:
3212-
isServerThread && serverConfig?.threadFind === true && !paintOnlyDisplayedTimeline,
3212+
enabled: threadFindEnabled,
32133213
}}
3214-
controlsRef={threadFindControlsRef}
3215-
onOpenChange={setIsThreadFindActive}
32163214
detailsCardTopInset={isThreadFindActive ? THREAD_FIND_BAR_RESERVED_HEIGHT : 0}
32173215
composerOverlayElement={isDraftHeroState ? null : composerOverlayElement}
32183216
data-chat-workspace-drop-target="true"

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { threadPullRequestLinkMode } from "@t3tools/client-runtime/thread-pull-request-compatibility";
22
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
33

4-
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
4+
import {
5+
scopedThreadKey,
6+
scopeProjectRef,
7+
scopeThreadRef,
8+
} from "@t3tools/client-runtime/environment";
59
import {
610
canCreateProjectInEnvironment,
711
getCloneDestinationBrowsePath,
@@ -66,7 +70,7 @@ import {
6670
TextSearchIcon,
6771
UserPlusIcon,
6872
} from "lucide-react";
69-
import { requestThreadFindOpen } from "./chat/threadFindActionBus";
73+
import { openThreadFind } from "./chat/threadFindStore";
7074
import {
7175
useDeferredValue,
7276
useEffect,
@@ -729,7 +733,9 @@ function OpenCommandPaletteDialog(props: {
729733

730734
const { projectCwdById, projectByKey, projectTitleById } = useProjectLookups();
731735

732-
const activeThreadId = activeThread?.id;
736+
const activeThreadKey = activeThread
737+
? scopedThreadKey(scopeThreadRef(activeThread.environmentId, activeThread.id))
738+
: null;
733739
const supportsThreadFind =
734740
environments.find((environment) => environment.environmentId === activeThread?.environmentId)
735741
?.serverConfig?.threadFind === true;
@@ -1256,7 +1262,7 @@ function OpenCommandPaletteDialog(props: {
12561262
});
12571263
}
12581264

1259-
if (activeThreadId && supportsThreadFind) {
1265+
if (activeThreadKey !== null && supportsThreadFind) {
12601266
actionItems.push({
12611267
kind: "action",
12621268
value: "find-current-thread",
@@ -1265,7 +1271,7 @@ function OpenCommandPaletteDialog(props: {
12651271
icon: <TextSearchIcon className={ITEM_ICON_CLASS} />,
12661272
shortcutCommand: "chat.find",
12671273
run: async () => {
1268-
requestThreadFindOpen();
1274+
openThreadFind(activeThreadKey);
12691275
},
12701276
});
12711277
}
@@ -2411,11 +2417,17 @@ function OpenCommandPaletteDialog(props: {
24112417
return;
24122418
}
24132419
// ChatView ignores shortcuts while the palette is open, so intercept find here.
2414-
if (command === "chat.find" && activeThreadId && !event.nativeEvent.isComposing) {
2420+
if (command === "chat.find" && activeThreadKey !== null && !event.nativeEvent.isComposing) {
24152421
event.preventDefault();
24162422
event.stopPropagation();
24172423
setOpen(false);
2418-
requestThreadFindOpen();
2424+
if (supportsThreadFind) openThreadFind(activeThreadKey);
2425+
else
2426+
toastManager.add({
2427+
id: "thread-find-unavailable",
2428+
title: "Thread search is unavailable on this server.",
2429+
description: "Update the server to enable it.",
2430+
});
24192431
return;
24202432
}
24212433

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2-
import { findForMessageMatch, requestThreadFindQuery } from "./chat/threadFindActionBus";
2+
import { findForMessageMatch, openThreadFindOnQuery } from "./chat/threadFindStore";
33
import {
44
claimThreadOpen,
55
nextShownOrder,
@@ -218,7 +218,7 @@ export function useCommandPaletteThreadItems(input: {
218218
// Opens the thread with find on the word at the matched message, so it is in view.
219219
run: async () => {
220220
await item.run();
221-
requestThreadFindQuery(
221+
openThreadFindOnQuery(
222222
scopedThreadKey(scopeThreadRef(match.environmentId, match.threadId)),
223223
findForMessageMatch(match, messageSearch.query),
224224
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
type ShownOrder,
4141
} from "@t3tools/client-runtime/state/thread-search";
4242
import { foldSearchQuery, rankMessageMatches } from "@t3tools/shared/paletteSearch";
43-
import { findForMessageMatch, requestThreadFindQuery } from "./chat/threadFindActionBus";
43+
import { findForMessageMatch, openThreadFindOnQuery } from "./chat/threadFindStore";
4444
import {
4545
threadRuntimeCanArchive,
4646
type EnvironmentThreadShell,
@@ -1305,7 +1305,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
13051305
const query = threadSearch.query;
13061306
clearThreadSearch();
13071307
navigateToThread(ref);
1308-
if (match) requestThreadFindQuery(scopedThreadKey(ref), findForMessageMatch(match, query));
1308+
if (match) openThreadFindOnQuery(scopedThreadKey(ref), findForMessageMatch(match, query));
13091309
};
13101310
const handleThreadSearchKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => {
13111311
if (event.nativeEvent.isComposing || event.keyCode === 229) return;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ import {
7474
} from "./MessagesTimeline.logic";
7575
import { type ThreadFindMatch } from "./threadFind";
7676
import { useThreadFindNavigation } from "./useThreadFindNavigation";
77-
import type { ThreadFindPositionReader } from "./threadFind";
7877
import { TooltipScrollDismissArea } from "../ui/tooltip";
7978
import { WorktreeSetupCard } from "./WorktreeSetupCard";
8079
import { useOpenPrLink } from "~/lib/openPullRequestLink";
@@ -267,7 +266,6 @@ interface MessagesTimelineProps {
267266
topFadeEnabled?: boolean;
268267
historyControls?: MessagesTimelineHistoryControls | undefined;
269268
loadEarlier?: CitationHistoryPage | null;
270-
findPositionReaderRef?: React.RefObject<ThreadFindPositionReader | null>;
271269
findExpanded?: boolean;
272270
findQuery?: string;
273271
activeFindMatch?: ThreadFindMatch | null;
@@ -315,7 +313,6 @@ const ConversationTimeline = memo(function ConversationTimeline({
315313
findQuery = "",
316314
activeFindMatch = null,
317315
findNavigationId = 0,
318-
findPositionReaderRef,
319316
latestRun,
320317
announcedRun = null,
321318
detailBehindShell = false,
@@ -1024,7 +1021,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
10241021
};
10251022

10261023
useThreadFindPositionReader({
1027-
findPositionReaderRef,
1024+
threadKey: listIdentityKey,
10281025
timelineViewportElement,
10291026
findFoldRunIds,
10301027
timelineEntries,

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,31 @@
1-
import {
2-
createContext,
3-
use,
4-
useImperativeHandle,
5-
useLayoutEffect,
6-
type ReactNode,
7-
type ComponentProps,
8-
type Ref,
9-
} from "react";
1+
import { createContext, use, type ReactNode, type ComponentProps } from "react";
102
import { useThreadFind } from "./useThreadFind";
113
import { ThreadFindBar } from "./ThreadFindBar";
124
import { ChatCanvas } from "./ChatCanvas";
135

146
type Find = ReturnType<typeof useThreadFind>;
15-
export type ThreadFindControls = Pick<Find, "open" | "close">;
167
export const ThreadFindTimelineContext = createContext<Find["timelineProps"] | null>(null);
178
const ThreadFindBarContext = createContext<Find["barProps"] | null>(null);
189

1910
export function ThreadFindCanvas({
2011
findOptions,
21-
controlsRef,
22-
onOpenChange,
2312
...canvasProps
2413
}: ComponentProps<typeof ChatCanvas> & {
2514
findOptions: Parameters<typeof useThreadFind>[0];
26-
controlsRef: Ref<ThreadFindControls>;
27-
onOpenChange: (open: boolean) => void;
2815
}) {
2916
return (
30-
<ThreadFindProvider {...findOptions} controlsRef={controlsRef} onOpenChange={onOpenChange}>
17+
<ThreadFindProvider {...findOptions}>
3118
<ChatCanvas {...canvasProps} />
3219
</ThreadFindProvider>
3320
);
3421
}
3522

3623
/** Owns search updates below the chat view so cycling does not rerender the composer and panels. */
3724
function ThreadFindProvider({
38-
controlsRef,
39-
onOpenChange,
4025
children,
4126
...options
42-
}: Parameters<typeof useThreadFind>[0] & {
43-
controlsRef: Ref<ThreadFindControls>;
44-
onOpenChange: (open: boolean) => void;
45-
children: ReactNode;
46-
}) {
27+
}: Parameters<typeof useThreadFind>[0] & { children: ReactNode }) {
4728
const find = useThreadFind(options);
48-
useImperativeHandle(controlsRef, () => ({ open: find.open, close: find.close }), [
49-
find.open,
50-
find.close,
51-
]);
52-
useLayoutEffect(() => onOpenChange(find.isOpen), [find.isOpen, onOpenChange]);
5329
return (
5430
<ThreadFindBarContext value={find.barProps}>
5531
<ThreadFindTimelineContext value={find.timelineProps}>{children}</ThreadFindTimelineContext>

‎apps/web/src/components/chat/threadFindActionBus.test.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎apps/web/src/components/chat/threadFindActionBus.ts‎

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)