From a499f30f7ea1ddaabc49b365961a0785714f5b9e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 23 Jul 2026 00:16:06 +0200 Subject: [PATCH] group projects in new-thread pickers Co-authored-by: codex --- apps/web/src/components/CommandPalette.tsx | 143 +++++++++++++++++++-- apps/web/src/routes/_chat.tsx | 23 +++- 2 files changed, 150 insertions(+), 16 deletions(-) diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index 4f330baf66..ee7a79a1fc 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -59,6 +59,7 @@ import { useAtomQueryRunner } from "../state/use-atom-query-runner"; import { useEnvironments, usePrimaryEnvironment } from "../state/environments"; import { useProjects, useThreadShells } from "../state/entities"; import { + resolveThreadActionProjectRef, startNewThreadInProjectFromContext, startNewThreadFromContext, } from "../lib/chatThreadActions"; @@ -79,7 +80,7 @@ import { } from "../lib/projectPaths"; import { onOpenCommandPalette } from "../commandPaletteBus"; import { isTerminalFocused } from "../lib/terminalFocus"; -import { getLatestThreadForProject } from "../lib/threadSort"; +import { getLatestThreadForProject, sortThreads } from "../lib/threadSort"; import { cn, isMacPlatform, isWindowsPlatform, newProjectId } from "../lib/utils"; import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; import { buildThreadRouteParams, resolveThreadRouteTarget } from "../threadRoutes"; @@ -106,6 +107,7 @@ import { ITEM_ICON_CLASS, RECENT_THREAD_LIMIT, } from "./CommandPalette.logic"; +import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic"; import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic"; import { CommandPaletteResults } from "./CommandPaletteResults"; import { AzureDevOpsIcon, BitbucketIcon, GitHubIcon, GitLabIcon } from "./Icons"; @@ -128,6 +130,12 @@ import { stackedThreadToast, toastManager } from "./ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { ComposerHandleContext, useComposerHandleContext } from "../composerHandleContext"; import type { ChatComposerHandle } from "./chat/ChatComposer"; +import { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject"; +import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"; +import { + buildSidebarProjectPickerEntries, + buildSidebarProjectSnapshots, +} from "../sidebarProjectGrouping"; const EMPTY_BROWSE_ENTRIES: FilesystemBrowseResult["entries"] = []; @@ -491,6 +499,7 @@ function OpenCommandPaletteDialog(props: { const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread } = useHandleNewThread(); const projects = useProjects(); + const projectOrder = useUiStateStore((store) => store.projectOrder); const threads = useThreadShells(); const keybindings = useAtomValue(primaryServerKeybindingsAtom); const providers = useAtomValue(primaryServerProvidersAtom); @@ -505,6 +514,93 @@ function OpenCommandPaletteDialog(props: { const [isRemoteProjectLookingUp, setIsRemoteProjectLookingUp] = useState(false); const [isRemoteProjectCloning, setIsRemoteProjectCloning] = useState(false); const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null; + const projectGroupingSettings = useMemo( + () => selectProjectGroupingSettings(clientSettings), + [clientSettings], + ); + + const environmentLabelById = useMemo( + () => + new Map( + environments.map((environment) => [environment.environmentId, environment.label] as const), + ), + [environments], + ); + const orderedProjects = useMemo( + () => + orderItemsByPreferredIds({ + items: projects, + preferredIds: projectOrder, + getId: getProjectOrderKey, + getPreferenceIds: (project) => [ + getProjectOrderKey(project), + legacyProjectCwdPreferenceKey(project.workspaceRoot), + ], + }), + [projectOrder, projects], + ); + const unsortedProjectGroups = useMemo( + () => + buildSidebarProjectSnapshots({ + projects: clientSettings.sidebarProjectSortOrder === "manual" ? orderedProjects : projects, + settings: projectGroupingSettings, + primaryEnvironmentId, + resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null, + }), + [ + clientSettings.sidebarProjectSortOrder, + environmentLabelById, + orderedProjects, + primaryEnvironmentId, + projectGroupingSettings, + projects, + ], + ); + const projectGroups = useMemo( + () => + sortLogicalProjectsForSidebar( + unsortedProjectGroups, + threads, + clientSettings.sidebarProjectSortOrder, + ), + [clientSettings.sidebarProjectSortOrder, threads, unsortedProjectGroups], + ); + const contextualProjectRef = useMemo( + () => + resolveThreadActionProjectRef({ + activeDraftThread, + activeThread: activeThread ?? undefined, + defaultProjectRef, + handleNewThread, + }), + [activeDraftThread, activeThread, defaultProjectRef, handleNewThread], + ); + const projectPickerEntries = useMemo( + () => + buildSidebarProjectPickerEntries({ + groups: projectGroups, + preferredProjectRef: contextualProjectRef, + }), + [contextualProjectRef, projectGroups], + ); + const pickerProjects = useMemo( + () => + projectPickerEntries.map(({ group, targetProject }) => ({ + ...targetProject, + title: group.displayName, + })), + [projectPickerEntries], + ); + const projectGroupByTargetKey = useMemo( + () => + new Map( + projectPickerEntries.map(({ group, targetProject }) => [ + `${targetProject.environmentId}:${targetProject.id}`, + group, + ]), + ), + [projectPickerEntries], + ); const addProjectEnvironmentOptions = useMemo(() => { const options = environments.map((environment): AddProjectEnvironmentOption => { @@ -648,11 +744,28 @@ function OpenCommandPaletteDialog(props: { const openProjectFromSearch = useMemo( () => async (project: (typeof projects)[number]) => { - const latestThread = getLatestThreadForProject( - threads.filter((thread) => thread.environmentId === project.environmentId), - project.id, - clientSettings.sidebarThreadSortOrder, - ); + const group = projectGroupByTargetKey.get(`${project.environmentId}:${project.id}`); + const groupedProjectKeys = group + ? new Set( + group.memberProjectRefs.map( + (projectRef) => `${projectRef.environmentId}:${projectRef.projectId}`, + ), + ) + : null; + const latestThread = groupedProjectKeys + ? (sortThreads( + threads.filter( + (thread) => + thread.archivedAt === null && + groupedProjectKeys.has(`${thread.environmentId}:${thread.projectId}`), + ), + clientSettings.sidebarThreadSortOrder, + )[0] ?? null) + : getLatestThreadForProject( + threads.filter((thread) => thread.environmentId === project.environmentId), + project.id, + clientSettings.sidebarThreadSortOrder, + ); if (latestThread) { await navigate({ to: "/$environmentId/$threadId", @@ -665,13 +778,19 @@ function OpenCommandPaletteDialog(props: { await handleNewThread(scopeProjectRef(project.environmentId, project.id)); }, - [handleNewThread, navigate, clientSettings.sidebarThreadSortOrder, threads], + [ + clientSettings.sidebarThreadSortOrder, + handleNewThread, + navigate, + projectGroupByTargetKey, + threads, + ], ); const projectSearchItems = useMemo( () => buildProjectActionItems({ - projects, + projects: pickerProjects, valuePrefix: "project", icon: (project) => ( = []; if (projects.length > 0) { - const activeProjectTitle = currentProjectId - ? (projectTitleById.get(currentProjectId) ?? null) - : null; + const activeProjectTitle = + projectPickerEntries.find((entry) => entry.isPreferred)?.group.displayName ?? + (currentProjectId ? (projectTitleById.get(currentProjectId) ?? null) : null); if (activeProjectTitle) { actionItems.push({ diff --git a/apps/web/src/routes/_chat.tsx b/apps/web/src/routes/_chat.tsx index 57824fe518..2e29d222b5 100644 --- a/apps/web/src/routes/_chat.tsx +++ b/apps/web/src/routes/_chat.tsx @@ -1,11 +1,14 @@ import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"; import { useAtomValue } from "@effect/atom-react"; -import { useEffect } from "react"; +import { useEffect, useMemo } from "react"; import { isCommandPaletteOpen } from "../commandPaletteBus"; import { useClientSettings } from "../hooks/useSettings"; import { openCommandPalette } from "../commandPaletteBus"; import { useProjects } from "../state/entities"; +import { usePrimaryEnvironmentId } from "../state/environments"; +import { selectProjectGroupingSettings } from "../logicalProject"; +import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping"; import { dispatchPreviewAction } from "../components/preview/previewActionBus"; import { useHandleNewThread } from "../hooks/useHandleNewThread"; import { @@ -29,7 +32,19 @@ function ChatRouteGlobalShortcuts() { useHandleNewThread(); const keybindings = useAtomValue(primaryServerKeybindingsAtom); const sidebarV2Enabled = useClientSettings((settings) => settings.sidebarV2Enabled); - const projectCount = useProjects().length; + const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings); + const projects = useProjects(); + const primaryEnvironmentId = usePrimaryEnvironmentId(); + const projectGroupCount = useMemo( + () => + buildSidebarProjectSnapshots({ + projects, + settings: projectGroupingSettings, + primaryEnvironmentId, + resolveEnvironmentLabel: () => null, + }).length, + [primaryEnvironmentId, projectGroupingSettings, projects], + ); const terminalOpen = useTerminalUiStateStore((state) => routeThreadRef ? selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef).terminalOpen @@ -83,7 +98,7 @@ function ChatRouteGlobalShortcuts() { // Sidebar v2 routes creation through the command palette whenever // there is a real choice to make; v1 (and single-project setups) // keep the immediate contextual create. - if (sidebarV2Enabled && projectCount > 1) { + if (sidebarV2Enabled && projectGroupCount > 1) { openCommandPalette({ open: "new-thread-in" }); return; } @@ -152,7 +167,7 @@ function ChatRouteGlobalShortcuts() { keybindings, defaultProjectRef, previewOpen, - projectCount, + projectGroupCount, routeThreadRef, selectedThreadKeysSize, sidebarV2Enabled,