Open History to see the last commit
1import {
2 type ClientSettings,
3 type EnvironmentId,
4 type ProjectId,
5 resolveEnvironmentMachineKind,
6} from "@t3tools/contracts";
7
8import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
9import { useClientSettings } from "../hooks/useSettings";
10import { getProjectOrderKey } from "../logicalProject";
11import { useProjects, useThreadShells } from "../state/entities";
12import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
13import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping";
14import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
15import { 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.
20export function useEnvironmentLookups() {
21 const { environments } = useEnvironments();
22 const environmentLabelById = new Map(
23 environments.map((environment) => [environment.environmentId, environment.label] as const),
24 );
25 const projectEnvironmentLocationById = new Map(
26 environments.map((environment) => {
27 const isPrimary = environment.entry.target._tag === "PrimaryConnectionTarget";
28 const isLocal = isPrimary || isDesktopLocalConnectionTarget(environment.entry.target);
29 return [
30 environment.environmentId,
31 {
32 kind: isLocal ? "local" : "remote",
33 label: isPrimary ? "Local" : isLocal ? `${environment.label} (Local)` : environment.label,
34 machine: resolveEnvironmentMachineKind(environment.serverConfig),
35 },
36 ] as const;
37 }),
38 );
39 return { environmentLabelById, projectEnvironmentLocationById };
40}
41
42export function useProjectLookups() {
43 const projects = useProjects();
44 const projectCwdById = new Map<ProjectId, string>(
45 projects.map((project) => [project.id, project.workspaceRoot]),
46 );
47 const projectByKey = new Map(
48 projects.map((project) => [`${project.environmentId}:${project.id}`, project]),
49 );
50 const projectTitleById = new Map<ProjectId, string>(
51 projects.map((project) => [project.id, project.title]),
52 );
53 return { projectCwdById, projectByKey, projectTitleById };
54}
55
56export const selectCommandPaletteClientSettings = (settings: ClientSettings) => ({
57 sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
58 sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
59 sidebarProjectSortOrder: settings.sidebarProjectSortOrder,
60 sidebarThreadSortOrder: settings.sidebarThreadSortOrder,
61});
62
63export function useSortedProjectGroups(environmentLabelById: ReadonlyMap<EnvironmentId, string>) {
64 const projects = useProjects();
65 const threads = useThreadShells();
66 const projectOrder = useUiStateStore((store) => store.projectOrder);
67 const clientSettings = useClientSettings(selectCommandPaletteClientSettings);
68 const primaryEnvironmentId = usePrimaryEnvironmentId();
69 const projectGroupingSettings = {
70 sidebarProjectGroupingMode: clientSettings.sidebarProjectGroupingMode,
71 sidebarProjectGroupingOverrides: clientSettings.sidebarProjectGroupingOverrides,
72 };
73 const orderedProjects = orderItemsByPreferredIds({
74 items: projects,
75 preferredIds: projectOrder,
76 getId: getProjectOrderKey,
77 getPreferenceIds: (project) => [
78 getProjectOrderKey(project),
79 legacyProjectCwdPreferenceKey(project.workspaceRoot),
80 ],
81 });
82 const unsortedProjectGroups = buildSidebarProjectSnapshots({
83 projects: clientSettings.sidebarProjectSortOrder === "manual" ? orderedProjects : projects,
84 settings: projectGroupingSettings,
85 primaryEnvironmentId,
86 resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null,
87 });
88 return sortLogicalProjectsForSidebar(
89 unsortedProjectGroups,
90 threads,
91 clientSettings.sidebarProjectSortOrder,
92 );
93}