Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useSidebarProjectGroups.ts

40 lines2.0 KB

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