The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | type ClientSettings, |
| 3 | type EnvironmentId, |
| 4 | resolveEnvironmentMachineKind, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { useGT } from "gt-react"; |
| 7 | |
| 8 | import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal"; |
| 9 | import { useClientSettings } from "../hooks/useSettings"; |
| 10 | import { getProjectOrderKey } from "@t3tools/client-runtime/state/project-grouping"; |
| 11 | import { useProjects, useVisibleThreadShells } from "../state/entities"; |
| 12 | import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments"; |
| 13 | import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping"; |
| 14 | import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"; |
| 15 | import { orderItemsByPreferredIds, sortLogicalProjectsForSidebar } from "./Sidebar.logic"; |
| 16 | |
| 17 | // Each helper reads its own source hook so the compiler caches the derived value on that |
| 18 | // source. Taking the source as an argument instead leaves the helper hook-free, and the |
| 19 | // compiler then treats it as a plain function and caches nothing. |
| 20 | export function useEnvironmentLookups() { |
| 21 | const gt = useGT(); |
| 22 | const { environments } = useEnvironments(); |
| 23 | const environmentLabelById = new Map( |
| 24 | environments.map((environment) => [environment.environmentId, environment.label] as const), |
| 25 | ); |
| 26 | const projectEnvironmentLocationById = new Map( |
| 27 | environments.map((environment) => { |
| 28 | const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget"; |
| 29 | const isLocal = isPrimary || isDesktopLocalConnectionTarget(environment.entry.target); |
| 30 | return [ |
| 31 | environment.environmentId, |
| 32 | { |
| 33 | kind: isLocal ? "local" : "remote", |
| 34 | label: isPrimary |
| 35 | ? gt("Local", { $context: "machine location" }) |
| 36 | : isLocal |
| 37 | ? gt("{label} (Local)", { label: environment.label }) |
| 38 | : environment.label, |
| 39 | machine: resolveEnvironmentMachineKind(environment.serverConfig), |
| 40 | }, |
| 41 | ] as const; |
| 42 | }), |
| 43 | ); |
| 44 | return { environmentLabelById, projectEnvironmentLocationById }; |
| 45 | } |
| 46 | |
| 47 | export function useProjectLookups() { |
| 48 | const projects = useProjects(); |
| 49 | const projectByKey = new Map( |
| 50 | projects.map((project) => [`${project.environmentId}:${project.id}`, project]), |
| 51 | ); |
| 52 | return { projectByKey }; |
| 53 | } |
| 54 | |
| 55 | export const selectCommandPaletteClientSettings = (settings: ClientSettings) => ({ |
| 56 | sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode, |
| 57 | sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides, |
| 58 | sidebarProjectSortOrder: settings.sidebarProjectSortOrder, |
| 59 | sidebarThreadSortOrder: settings.sidebarThreadSortOrder, |
| 60 | }); |
| 61 | |
| 62 | export function useSortedProjectGroups(environmentLabelById: ReadonlyMap<EnvironmentId, string>) { |
| 63 | const projects = useProjects(); |
| 64 | const threads = useVisibleThreadShells(); |
| 65 | const projectOrder = useUiStateStore((store) => store.projectOrder); |
| 66 | const clientSettings = useClientSettings(selectCommandPaletteClientSettings); |
| 67 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 68 | const projectGroupingSettings = { |
| 69 | sidebarProjectGroupingMode: clientSettings.sidebarProjectGroupingMode, |
| 70 | sidebarProjectGroupingOverrides: clientSettings.sidebarProjectGroupingOverrides, |
| 71 | }; |
| 72 | const orderedProjects = orderItemsByPreferredIds({ |
| 73 | items: projects, |
| 74 | preferredIds: projectOrder, |
| 75 | getId: getProjectOrderKey, |
| 76 | getPreferenceIds: (project) => [ |
| 77 | getProjectOrderKey(project), |
| 78 | legacyProjectCwdPreferenceKey(project.workspaceRoot), |
| 79 | ], |
| 80 | }); |
| 81 | const unsortedProjectGroups = buildSidebarProjectSnapshots({ |
| 82 | projects: clientSettings.sidebarProjectSortOrder === "manual" ? orderedProjects : projects, |
| 83 | settings: projectGroupingSettings, |
| 84 | primaryEnvironmentId, |
| 85 | resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null, |
| 86 | }); |
| 87 | return sortLogicalProjectsForSidebar( |
| 88 | unsortedProjectGroups, |
| 89 | threads, |
| 90 | clientSettings.sidebarProjectSortOrder, |
| 91 | ); |
| 92 | } |