| 1 | import { useMemo } from "react"; |
| 2 | |
| 3 | import { useClientSettings } from "../../hooks/useSettings"; |
| 4 | import { selectProjectGroupingSettings } from "../../logicalProject"; |
| 5 | import { buildSidebarProjectSnapshots } from "../../sidebarProjectGrouping"; |
| 6 | import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments"; |
| 7 | import { useProjects } from "../../state/entities"; |
| 8 | |
| 9 | /** Settings uses the same logical projects as the sidebar, sorted by display name. */ |
| 10 | export function useSettingsProjectGroups() { |
| 11 | const projects = useProjects(); |
| 12 | const settings = useClientSettings(selectProjectGroupingSettings); |
| 13 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 14 | const { environments } = useEnvironments(); |
| 15 | return useMemo(() => { |
| 16 | const labels = new Map(environments.map((entry) => [entry.environmentId, entry.label])); |
| 17 | return buildSidebarProjectSnapshots({ |
| 18 | projects, |
| 19 | settings, |
| 20 | primaryEnvironmentId, |
| 21 | resolveEnvironmentLabel: (id) => labels.get(id) ?? null, |
| 22 | }).sort((a, b) => a.displayName.localeCompare(b.displayName)); |
| 23 | }, [environments, primaryEnvironmentId, projects, settings]); |
| 24 | } |