import type { SitePublication, SitePublicationLink } from "@opengeni/contracts"; import type { OpenGeniBrowserClient } from "@opengeni/sdk/browser"; import type { DesktopBridge } from "@opengeni/sdk/desktop-bridge"; import { ArrowUpRightIcon, CheckIcon, ChevronDownIcon, GlobeIcon, LinkIcon, Settings2Icon, } from "lucide-react"; import { useCallback, useEffect, useRef, useState, type MouseEvent } from "react"; import { copyTextToClipboard } from "../../../lib/clipboard"; import { apiErrorFacts } from "../../../ui/api-error"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "../../../ui/dropdown-menu"; import { desktopCapability } from "../../lib/desktop"; import { cn } from "../../lib/utils"; /* ---------------------------------------------------------------------------- The Site view's one publication control. A Site on the web shows "Published": its menu names the address and opens or copies it. One that isn't shows Publish to people who can publish, and nothing to anyone else. Publishing and its settings open the same publish form as the Site's Settings, through `onOpenPublishing`. Access never changes the link. -------------------------------------------------------------------------- */ /** Where the Site's published address stands, from the API's answer. */ export type SitePublicationLinkLoad = | { state: "loading" } | { state: "unavailable" } | { state: "forbidden" } | { state: "none" } | { state: "error"; error: unknown } | { state: "ready"; link: SitePublicationLink }; /** * The Site's published address, for anyone who can open the Site. `update` * shows a publication this page just changed without waiting for a read. */ export function useSitePublicationLink({ client, workspaceId, siteId, }: { client: Pick; workspaceId: string; siteId: string; }): { load: SitePublicationLinkLoad; update: (publication: Pick) => void; } { // Keyed by Site, so moving to another Site never shows the last one's link. const key = `${workspaceId}/${siteId}`; const [answer, setAnswer] = useState<{ key: string; load: SitePublicationLinkLoad } | null>(null); // Read again when the person comes back to the window, so publishing from // the chat beside the Site or another tab shows here without a reload. const [revision, setRevision] = useState(0); useEffect(() => { const refresh = () => { if (document.visibilityState === "visible") setRevision((value) => value + 1); }; window.addEventListener("focus", refresh); document.addEventListener("visibilitychange", refresh); return () => { window.removeEventListener("focus", refresh); document.removeEventListener("visibilitychange", refresh); }; }, []); // A change made here wins over a read already on its way. const request = useRef(null); useEffect(() => { const abort = new AbortController(); request.current = abort; client .getSitePublicationLink(workspaceId, siteId, { signal: abort.signal }) .then((link) => { if (!abort.signal.aborted) setAnswer({ key, load: { state: "ready", link } }); }) .catch((error: unknown) => { if (!abort.signal.aborted) setAnswer({ key, load: sitePublicationLinkLoadFromError(error) }); }); return () => abort.abort(); }, [client, key, revision, siteId, workspaceId]); const update = useCallback( ({ siteId: publishedSiteId, hostname, url, status, }: Pick) => { request.current?.abort(); setAnswer({ key, load: { state: "ready", link: { siteId: publishedSiteId, hostname, url, status } }, }); }, [key], ); return { load: answer?.key === key ? answer.load : { state: "loading" }, update }; } /** * Only the API's own "not published" answer means not published. Any other * 404 (an older API without this route, publishing off) shows nothing rather * than offering Publish for a Site that may be on the web. */ export function sitePublicationLinkLoadFromError(error: unknown): SitePublicationLinkLoad { const { status, code } = apiErrorFacts(error); if (code === "site_not_published") return { state: "none" }; if (status === 404 || status === 503 || code === "sites_publishing_disabled") return { state: "unavailable" }; if (status === 403) return { state: "forbidden" }; return { state: "error", error }; } /** The published address when the Site is on the web now, else null. */ export function publishedSiteLink( load: SitePublicationLinkLoad, ): { url: string; hostname: string } | null { if (load.state !== "ready" || load.link.status !== "active") return null; return { url: load.link.url, hostname: publishedHostname(load.link) }; } /** The hostname people see: the one in the URL the API reports, which wins. */ function publishedHostname(link: Pick): string { try { return new URL(link.url).hostname || link.hostname; } catch { return link.hostname; } } /** * Open the published Site. In the desktop app it opens in the default * browser through the shell and returns true; elsewhere it returns false so * the link opens its new tab itself. */ export function openPublishedSite( url: string, bridge: Pick | null = desktopCapability("openExternal"), ): boolean { if (!bridge) return false; void bridge.openExternal(url).catch(() => { window.open(url, "_blank", "noopener,noreferrer"); }); return true; } /** The header's quiet control: the version chip's height, its type and hover. */ const CONTROL = "mr-1 inline-flex h-7 shrink-0 items-center gap-1.5 rounded-md px-1.5 text-2xs font-medium text-fg-muted outline-hidden transition-colors hover:bg-hover hover:text-fg focus-visible:ring-2 focus-visible:ring-ring/40 pointer-coarse:h-8"; export function SitePublicationControl({ load, canPublish, onOpenPublishing, openExternal = openPublishedSite, }: { load: SitePublicationLinkLoad; /** May publish the Site and change how it is published. */ canPublish: boolean; /** Open the publish form: to publish, or for a published Site's settings. */ onOpenPublishing: () => void; /** The desktop path; returns true when it handled the open. */ openExternal?: ((url: string) => boolean) | undefined; }) { const link = publishedSiteLink(load); if (link) return ( ); const unpublished = load.state === "none" || (load.state === "ready" && load.link.status === "unpublished"); if (unpublished) return canPublish ? ( ) : null; if (load.state === "ready" && load.link.status === "suspended") { const content = ( <>