| 1 | "use client"; |
| 2 | |
| 3 | import { useState, useEffect, useMemo } from "react"; |
| 4 | import { GroveLogo } from "@/app/components/grove-logo"; |
| 5 | import { CanopyLogo } from "@/app/components/canopy-logo"; |
| 6 | import { RingLogo } from "@/app/components/ring-logo"; |
| 7 | import { CollabLogo } from "@/app/components/collab-logo"; |
| 8 | import type { AppSwitcherItem } from "@/app/components/ui/navbar"; |
| 9 | |
| 10 | function useSubdomainOrigin(subdomain: string) { |
| 11 | const [origin, setOrigin] = useState(""); |
| 12 | useEffect(() => { |
| 13 | const host = window.location.host; |
| 14 | // Strip existing subdomain prefix if present (e.g. canopy.grove.host → grove.host) |
| 15 | const parts = host.split("."); |
| 16 | const base = |
| 17 | parts[0] === "canopy" || parts[0] === "ring" || parts[0] === "collab" |
| 18 | ? parts.slice(1).join(".") |
| 19 | : host; |
| 20 | setOrigin(`${window.location.protocol}//${subdomain}.${base}`); |
| 21 | }, [subdomain]); |
| 22 | return origin; |
| 23 | } |
| 24 | |
| 25 | function useBaseOrigin() { |
| 26 | const [origin, setOrigin] = useState(""); |
| 27 | useEffect(() => { |
| 28 | const host = window.location.host; |
| 29 | const parts = host.split("."); |
| 30 | const base = |
| 31 | parts[0] === "canopy" || parts[0] === "ring" || parts[0] === "collab" |
| 32 | ? parts.slice(1).join(".") |
| 33 | : host; |
| 34 | setOrigin(`${window.location.protocol}//${base}`); |
| 35 | }, []); |
| 36 | return origin; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns app switcher items for all apps except the current one. |
| 41 | * Pass the current app name (e.g. "grove", "canopy") to exclude it. |
| 42 | */ |
| 43 | export function useAppSwitcherItems( |
| 44 | currentApp: "grove" | "canopy" | "ring" | "collab", |
| 45 | context?: { owner?: string; repo?: string }, |
| 46 | ): AppSwitcherItem[] { |
| 47 | const groveOrigin = useBaseOrigin(); |
| 48 | const canopyOrigin = useSubdomainOrigin("canopy"); |
| 49 | const ringOrigin = useSubdomainOrigin("ring"); |
| 50 | const collabOrigin = useSubdomainOrigin("collab"); |
| 51 | |
| 52 | return useMemo(() => { |
| 53 | const repoPath = context?.owner && context?.repo ? `/${context.owner}/${context.repo}` : ""; |
| 54 | const all: (AppSwitcherItem & { key: string })[] = [ |
| 55 | { key: "grove", name: "Grove", logo: <GroveLogo size={28} />, href: `${groveOrigin}${repoPath}` }, |
| 56 | { key: "canopy", name: "Canopy", logo: <CanopyLogo size={28} />, href: `${canopyOrigin}${repoPath}${repoPath ? "/builds" : ""}` }, |
| 57 | { key: "ring", name: "Ring", logo: <RingLogo size={28} />, href: `${ringOrigin}${repoPath}` }, |
| 58 | { key: "collab", name: "Collab", logo: <CollabLogo size={28} />, href: `${collabOrigin}${repoPath}` }, |
| 59 | ]; |
| 60 | return all.filter((item) => item.key !== currentApp); |
| 61 | }, [currentApp, context?.owner, context?.repo, groveOrigin, canopyOrigin, ringOrigin, collabOrigin]); |
| 62 | } |
| 63 | |