Skip to content
Source
Sign inSign up

useSidebarProjectGroups.ts

43 lines2.0 KB

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