The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useMessages } from "gt-react"; |
| 2 | |
| 3 | import { orderItemsByPreferredIds, sortSidebarV2ProjectGroups } from "../components/Sidebar.logic"; |
| 4 | import { |
| 5 | getProjectOrderKey, |
| 6 | selectProjectGroupingSettings, |
| 7 | } from "@t3tools/client-runtime/state/project-grouping"; |
| 8 | import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping"; |
| 9 | import { useEnvironmentIdentities, usePrimaryEnvironmentId } from "../state/environments"; |
| 10 | import { useDelegatedUnderParent, useProjects, useQuietThreadShells } from "../state/entities"; |
| 11 | import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"; |
| 12 | import { useClientSettings } from "./useSettings"; |
| 13 | |
| 14 | export function useSidebarProjectGroups() { |
| 15 | const projects = useProjects(); |
| 16 | // Activity order lags a running thread's streaming ticks until its next |
| 17 | // real change; reading every tick re-rendered the whole sidebar with it. |
| 18 | const threads = useQuietThreadShells(); |
| 19 | const delegatedUnderParent = useDelegatedUnderParent(); |
| 20 | const projectOrder = useUiStateStore((store) => store.projectOrder); |
| 21 | const sortOrder = useClientSettings((s) => s.sidebarProjectSortOrder); |
| 22 | const groupingSettings = useClientSettings(selectProjectGroupingSettings); |
| 23 | const environments = useEnvironmentIdentities(); |
| 24 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 25 | const resolve = useMessages(); |
| 26 | const orderedProjects = orderItemsByPreferredIds({ |
| 27 | items: projects, |
| 28 | preferredIds: projectOrder, |
| 29 | getId: getProjectOrderKey, |
| 30 | getPreferenceIds: (project) => [ |
| 31 | getProjectOrderKey(project), |
| 32 | legacyProjectCwdPreferenceKey(project.workspaceRoot), |
| 33 | ], |
| 34 | }); |
| 35 | const labels = new Map( |
| 36 | environments.map((environment) => [environment.environmentId, environment.label] as const), |
| 37 | ); |
| 38 | const unsortedGroups = buildSidebarProjectSnapshots( |
| 39 | { |
| 40 | projects: sortOrder === "manual" ? orderedProjects : projects, |
| 41 | groupScratchProjects: true, |
| 42 | settings: groupingSettings, |
| 43 | primaryEnvironmentId, |
| 44 | resolveEnvironmentLabel: (environmentId) => labels.get(environmentId) ?? null, |
| 45 | }, |
| 46 | resolve, |
| 47 | ); |
| 48 | return sortSidebarV2ProjectGroups(unsortedGroups, threads, sortOrder, delegatedUnderParent); |
| 49 | } |