Skip to content

Commit 0dd1a0a

Browse files
refactor(web): the command palette's thread items come from their own hook
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 159fdb5 commit 0dd1a0a

3 files changed

Lines changed: 257 additions & 208 deletions

File tree

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

Lines changed: 14 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
} from "@t3tools/client-runtime/operations/projects";
2525
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2626
import { connectionStatusText } from "@t3tools/client-runtime/connection";
27-
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
2827
import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference";
2928
import {
3029
canPreloadBrowsePath,
@@ -42,11 +41,9 @@ import {
4241
AuthSourceControlWriteScope,
4342
AuthFilesystemReadScope,
4443
type DesktopWslState,
45-
type ClientSettings,
4644
type EnvironmentId,
4745
type EnvironmentMachineKind,
4846
type FilesystemBrowseResult,
49-
type ProjectId,
5047
type SourceControlDiscoveryResult,
5148
SourceControlProviderKind,
5249
type SourceControlRepositoryInfo,
@@ -121,7 +118,6 @@ import {
121118
usePrimaryEnvironmentId,
122119
} from "../state/environments";
123120
import { useProjects, useServerConfigs, useThreadShells, waitForProject } from "../state/entities";
124-
import { useThreadSearch } from "../state/queries";
125121
import { resolveThreadActionProjectRef, startNewThreadFromContext } from "../lib/chatThreadActions";
126122
import {
127123
appendBrowsePathSegment,
@@ -166,7 +162,6 @@ import {
166162
buildCommandPaletteProjectMetadata,
167163
buildProjectActionItems,
168164
buildRootGroups,
169-
buildThreadActionItems,
170165
buildLinkedThreadActionItems,
171166
buildCommandPaletteRows,
172167
enumerateCommandPaletteItems,
@@ -188,9 +183,15 @@ import {
188183
reduceCommandPaletteSearchState,
189184
type SearchOverlayMode,
190185
} from "./CommandPalette.logic";
191-
import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic";
192186
import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
193187
import { CommandPaletteContent } from "./CommandPaletteContent";
188+
import {
189+
selectCommandPaletteClientSettings,
190+
useEnvironmentLookups,
191+
useProjectLookups,
192+
useSortedProjectGroups,
193+
} from "./CommandPaletteLookups";
194+
import { useCommandPaletteThreadItems } from "./CommandPaletteThreadItems";
194195
import {
195196
APPEARANCE_OPTIONS,
196197
notifyThemeSaveFailure,
@@ -210,22 +211,8 @@ import { openLinkPullRequestDialog } from "./pullRequest/LinkPullRequestDialog";
210211
import { ProjectContentSearchDialog } from "./search/ProjectContentSearchDialog";
211212
import { toggleThemeEditorForTheme } from "./settings/themeEditorStore";
212213
import { searchSettings, SETTINGS_SECTION_LABELS } from "./settings/settingsSearch";
213-
import {
214-
COMMAND_PALETTE_META_ICON_CLASS,
215-
CommandPaletteMetaDot,
216-
ThreadCommandSubtitle,
217-
} from "./ThreadCommandSubtitle";
218-
import {
219-
ThreadRowLeadingStatus,
220-
ThreadRowTrailingStatus,
221-
ThreadSearchPullRequestNumber,
222-
} from "./ThreadStatusIndicators";
223-
import { primaryServerKeybindingsAtom, primaryServerProvidersAtom } from "../state/server";
224-
import {
225-
applyProviderInstanceSettings,
226-
deriveProviderInstanceEntries,
227-
type ProviderInstanceEntry,
228-
} from "../providerInstances";
214+
import { COMMAND_PALETTE_META_ICON_CLASS, CommandPaletteMetaDot } from "./ThreadCommandSubtitle";
215+
import { primaryServerKeybindingsAtom } from "../state/server";
229216
import { resolveShortcutCommand, threadJumpIndexFromCommand } from "../keybindings";
230217
import { CommandDialog, CommandDialogPopup, CommandFooterAction } from "./ui/command";
231218
import { Button } from "./ui/button";
@@ -235,12 +222,7 @@ import { stackedThreadToast, toastManager } from "./ui/toast";
235222
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
236223
import { ComposerHandleContext, useComposerHandleContext } from "../composerHandleContext";
237224
import type { ChatComposerHandle } from "./chat/ChatComposer";
238-
import { getProjectOrderKey } from "../logicalProject";
239-
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
240-
import {
241-
buildSidebarProjectPickerEntries,
242-
buildSidebarProjectSnapshots,
243-
} from "../sidebarProjectGrouping";
225+
import { buildSidebarProjectPickerEntries } from "../sidebarProjectGrouping";
244226
import type { Project } from "../types";
245227
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
246228
import { readPullRequestListPreferences } from "~/components/pullRequest/pullRequestListPreferences";
@@ -350,99 +332,6 @@ function overlayModeForCommand(command: string | null): SearchOverlayMode | null
350332
: null;
351333
}
352334

353-
// Each helper reads its own source hook so the compiler caches the derived value on that
354-
// source. Taking the source as an argument instead leaves the helper hook-free, and the
355-
// compiler then treats it as a plain function and caches nothing.
356-
function useThreadSearchWithContentMatches(
357-
environmentIds: ReadonlyArray<EnvironmentId>,
358-
query: string,
359-
) {
360-
const threadSearch = useThreadSearch(environmentIds, query);
361-
const contentMatchByKey = new Map(
362-
threadSearch.matches.flatMap((match) =>
363-
match.source === "user" || match.source === "assistant"
364-
? [[threadSearchMatchKey(match), match] as const]
365-
: [],
366-
),
367-
);
368-
return { threadSearch, contentMatchByKey };
369-
}
370-
371-
function useEnvironmentLookups() {
372-
const { environments } = useEnvironments();
373-
const environmentLabelById = new Map(
374-
environments.map((environment) => [environment.environmentId, environment.label] as const),
375-
);
376-
const projectEnvironmentLocationById = new Map(
377-
environments.map((environment) => {
378-
const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget";
379-
const isLocal = isPrimary || isDesktopLocalConnectionTarget(environment.entry.target);
380-
return [
381-
environment.environmentId,
382-
{
383-
kind: isLocal ? "local" : "remote",
384-
label: isPrimary ? "Local" : isLocal ? `${environment.label} (Local)` : environment.label,
385-
machine: resolveEnvironmentMachineKind(environment.serverConfig),
386-
},
387-
] as const;
388-
}),
389-
);
390-
return { environmentLabelById, projectEnvironmentLocationById };
391-
}
392-
393-
function useProjectLookups() {
394-
const projects = useProjects();
395-
const projectCwdById = new Map<ProjectId, string>(
396-
projects.map((project) => [project.id, project.workspaceRoot]),
397-
);
398-
const projectByKey = new Map(
399-
projects.map((project) => [`${project.environmentId}:${project.id}`, project]),
400-
);
401-
const projectTitleById = new Map<ProjectId, string>(
402-
projects.map((project) => [project.id, project.title]),
403-
);
404-
return { projectCwdById, projectByKey, projectTitleById };
405-
}
406-
407-
const selectCommandPaletteClientSettings = (settings: ClientSettings) => ({
408-
sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
409-
sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
410-
sidebarProjectSortOrder: settings.sidebarProjectSortOrder,
411-
sidebarThreadSortOrder: settings.sidebarThreadSortOrder,
412-
});
413-
414-
function useSortedProjectGroups(environmentLabelById: ReadonlyMap<EnvironmentId, string>) {
415-
const projects = useProjects();
416-
const threads = useThreadShells();
417-
const projectOrder = useUiStateStore((store) => store.projectOrder);
418-
const clientSettings = useClientSettings(selectCommandPaletteClientSettings);
419-
const primaryEnvironmentId = usePrimaryEnvironmentId();
420-
const projectGroupingSettings = {
421-
sidebarProjectGroupingMode: clientSettings.sidebarProjectGroupingMode,
422-
sidebarProjectGroupingOverrides: clientSettings.sidebarProjectGroupingOverrides,
423-
};
424-
const orderedProjects = orderItemsByPreferredIds({
425-
items: projects,
426-
preferredIds: projectOrder,
427-
getId: getProjectOrderKey,
428-
getPreferenceIds: (project) => [
429-
getProjectOrderKey(project),
430-
legacyProjectCwdPreferenceKey(project.workspaceRoot),
431-
],
432-
});
433-
const unsortedProjectGroups = buildSidebarProjectSnapshots({
434-
projects: clientSettings.sidebarProjectSortOrder === "manual" ? orderedProjects : projects,
435-
settings: projectGroupingSettings,
436-
primaryEnvironmentId,
437-
resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null,
438-
});
439-
return sortLogicalProjectsForSidebar(
440-
unsortedProjectGroups,
441-
threads,
442-
clientSettings.sidebarProjectSortOrder,
443-
);
444-
}
445-
446335
export function CommandPalette({ children }: { children: ReactNode }) {
447336
const navigate = useNavigate();
448337
const [state, dispatch] = useReducer(reduceCommandPaletteUiState, {
@@ -778,31 +667,10 @@ function OpenCommandPaletteDialog(props: {
778667
};
779668
const threads = useThreadShells();
780669
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
781-
const { changeThemeItem, changeAppearanceItem, themeEditorItem } = useCommandPaletteThemeItems();
782-
const providers = useAtomValue(primaryServerProvidersAtom);
783670
const providersByEnvironment = useEnvironmentProviders();
784-
const providerEntryByEnvironmentAndInstanceId = new Map<string, ProviderInstanceEntry>(
785-
environments.flatMap((environment) => {
786-
const serverConfig = environment.serverConfig;
787-
const derived = deriveProviderInstanceEntries(
788-
providersByEnvironment.get(environment.environmentId) ??
789-
(environment.environmentId === primaryEnvironmentId ? providers : []),
790-
);
791-
const entries = serverConfig
792-
? applyProviderInstanceSettings(derived, serverConfig.settings, null)
793-
: derived;
794-
return entries.map(
795-
(entry) => [`${environment.environmentId}:${entry.instanceId}`, entry] as const,
796-
);
797-
}),
798-
);
671+
const { changeThemeItem, changeAppearanceItem, themeEditorItem } = useCommandPaletteThemeItems();
799672
const currentView = viewStack.at(-1)?.view ?? null;
800-
const environmentIds = environments
801-
.filter((environment) => environment.connection.phase === "connected")
802-
.map((environment) => environment.environmentId);
803673
const threadSearchQuery = currentView === null && !isActionsOnly ? deferredQuery : "";
804-
const { threadSearch, contentMatchByKey: threadContentMatchByKey } =
805-
useThreadSearchWithContentMatches(environmentIds, threadSearchQuery);
806674
const [browseGeneration, setBrowseGeneration] = useState(0);
807675
const [browseNavigation] = useState(createBrowseNavigationCoordinator);
808676
const [addProjectEnvironmentId, setAddProjectEnvironmentId] = useState<EnvironmentId | null>(
@@ -1183,70 +1051,8 @@ function OpenCommandPaletteDialog(props: {
11831051
];
11841052
})();
11851053

1186-
const allThreadItems = buildThreadActionItems({
1187-
threads,
1188-
...(activeThreadId ? { activeThreadId } : {}),
1189-
projectTitleById,
1190-
sortOrder: clientSettings.sidebarThreadSortOrder,
1191-
icon: <MessageSquareIcon className={ITEM_ICON_CLASS} />,
1192-
renderLeadingContent: (thread) => <ThreadRowLeadingStatus thread={thread} />,
1193-
renderTrailingContent: (thread) => (
1194-
<>
1195-
<ThreadSearchPullRequestNumber
1196-
thread={thread}
1197-
query={threadSearchQuery}
1198-
settled={thread.settledOverride === "settled"}
1199-
/>
1200-
<ThreadRowTrailingStatus thread={thread} />
1201-
</>
1202-
),
1203-
renderDescription: (thread, { projectTitle }) => {
1204-
const modelInstanceId =
1205-
thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId;
1206-
const providerEntry =
1207-
providerEntryByEnvironmentAndInstanceId.get(`${thread.environmentId}:${modelInstanceId}`) ??
1208-
null;
1209-
return (
1210-
<ThreadCommandSubtitle
1211-
project={projectByKey.get(`${thread.environmentId}:${thread.projectId}`) ?? null}
1212-
projectTitle={projectTitle ?? null}
1213-
environmentLabel={
1214-
projectEnvironmentLocationById.get(thread.environmentId)?.label ?? "Remote"
1215-
}
1216-
branch={thread.branch}
1217-
worktreePath={thread.worktreePath}
1218-
isCurrent={thread.id === activeThreadId}
1219-
driverKind={providerEntry?.driverKind ?? null}
1220-
providerDisplayName={
1221-
thread.runtime?.providerName ?? providerEntry?.displayName ?? modelInstanceId
1222-
}
1223-
acpRegistryAgentId={providerEntry?.acpRegistryAgentId}
1224-
acpRegistryIconUrl={providerEntry?.acpRegistryIconUrl}
1225-
/>
1226-
);
1227-
},
1228-
getContentMatch: (thread) => {
1229-
const match = threadContentMatchByKey.get(
1230-
threadSearchMatchKey({
1231-
environmentId: thread.environmentId,
1232-
threadId: thread.id,
1233-
}),
1234-
);
1235-
return match && (match.source === "user" || match.source === "assistant")
1236-
? {
1237-
source: match.source,
1238-
snippet: match.snippet,
1239-
query: threadSearch.query,
1240-
}
1241-
: undefined;
1242-
},
1243-
runThread: async (thread) => {
1244-
await navigate({
1245-
to: "/$environmentId/$threadId",
1246-
params: buildThreadRouteParams(scopeThreadRef(thread.environmentId, thread.id)),
1247-
});
1248-
},
1249-
});
1054+
const { threadItems: allThreadItems, isSearchPending: isThreadSearchPending } =
1055+
useCommandPaletteThreadItems({ activeThreadId, threadSearchQuery });
12501056
const recentThreadItems = allThreadItems.slice(0, RECENT_THREAD_LIMIT);
12511057

12521058
// Stable so the item builders that capture it during render never see the
@@ -3208,7 +3014,7 @@ function OpenCommandPaletteDialog(props: {
32083014
? {
32093015
emptyStateMessage: "Press Enter to create this folder and add it as a project.",
32103016
}
3211-
: threadSearch.isPending
3017+
: isThreadSearchPending
32123018
? { emptyStateMessage: "Searching thread messages…" }
32133019
: {})}
32143020
/>

0 commit comments

Comments
 (0)