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