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