useFlyDeprecationProjects.ts62 lines · main
| 1 | import { useOrgProjectsInfiniteQuery } from '@/data/projects/org-projects-infinite-query' |
| 2 | import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' |
| 3 | import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' |
| 4 | import { PROVIDERS } from '@/lib/constants/infrastructure' |
| 5 | |
| 6 | export interface FlyDeprecationProject { |
| 7 | ref: string |
| 8 | name: string |
| 9 | orgSlug: string |
| 10 | orgName: string |
| 11 | } |
| 12 | |
| 13 | export function useFlyDeprecationProjects({ enabled }: { enabled: boolean }) { |
| 14 | const { data: selectedProject } = useSelectedProjectQuery() |
| 15 | const { data: selectedOrg } = useSelectedOrganizationQuery() |
| 16 | |
| 17 | const orgSlug = selectedOrg?.slug |
| 18 | const orgName = selectedOrg?.name ?? '' |
| 19 | |
| 20 | const { data: orgProjectsData, isFetched } = useOrgProjectsInfiniteQuery( |
| 21 | { slug: orgSlug }, |
| 22 | { enabled: enabled && Boolean(orgSlug), staleTime: 30 * 60 * 1000 } |
| 23 | ) |
| 24 | |
| 25 | if (!enabled || !orgSlug) { |
| 26 | return { isReady: false, primaries: [], branches: [] } |
| 27 | } |
| 28 | |
| 29 | const byRef = new Map<string, FlyDeprecationProject & { isBranch: boolean }>() |
| 30 | |
| 31 | if (selectedProject?.cloud_provider === PROVIDERS.FLY.id) { |
| 32 | byRef.set(selectedProject.ref, { |
| 33 | ref: selectedProject.ref, |
| 34 | name: selectedProject.name, |
| 35 | orgSlug, |
| 36 | orgName, |
| 37 | isBranch: Boolean(selectedProject.parent_project_ref), |
| 38 | }) |
| 39 | } |
| 40 | |
| 41 | const orgProjects = orgProjectsData?.pages.flatMap((page) => page.projects) ?? [] |
| 42 | for (const p of orgProjects) { |
| 43 | if (p.cloud_provider !== PROVIDERS.FLY.id) continue |
| 44 | if (byRef.has(p.ref)) continue |
| 45 | byRef.set(p.ref, { |
| 46 | ref: p.ref, |
| 47 | name: p.name, |
| 48 | orgSlug, |
| 49 | orgName, |
| 50 | isBranch: Boolean(p.is_branch), |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | const all = Array.from(byRef.values()) |
| 55 | const primaries: FlyDeprecationProject[] = [] |
| 56 | const branches: FlyDeprecationProject[] = [] |
| 57 | for (const { isBranch, ...rest } of all) { |
| 58 | ;(isBranch ? branches : primaries).push(rest) |
| 59 | } |
| 60 | |
| 61 | return { isReady: isFetched, primaries, branches } |
| 62 | } |