import { useSyncExternalStore } from "react"; import { resolveTabId } from "../../telemetry/tabId"; import { trackStudioEvent } from "../../utils/studioTelemetry"; import type { DesktopRoute } from "../DesktopGuideCard"; export const DESKTOP_CARD_LAST_AUTO_KEY = "hf_desktop_card_last_auto_at"; export const ONE_WEEK_MS = 7 * 24 * 60 * 60 * 1000; export type DesktopCardSceneIndex = 0 | 1 | 2 | 3 | 4; export type DesktopCardSource = | "header" | "auto_watch" | "auto_export" | "empty_inspector" | "catalog"; export interface DesktopCardSnapshot { route: DesktopRoute | null; open: boolean; scene: DesktopCardSceneIndex; openSeq: number; source: DesktopCardSource; downloaded: boolean; } let state: DesktopCardSnapshot = { route: null, open: false, scene: 0, openSeq: 0, source: "header", downloaded: false, }; const listeners = new Set<() => void>(); function emit(): void { for (const listener of listeners) listener(); } function subscribe(listener: () => void): () => void { listeners.add(listener); return () => listeners.delete(listener); } export function getDesktopCardSnapshot(): DesktopCardSnapshot { return state; } export function useDesktopCardStore(): DesktopCardSnapshot { return useSyncExternalStore(subscribe, getDesktopCardSnapshot, getDesktopCardSnapshot); } /** Update the shared `/api/open-in-desktop` route discovered by OpenInDesktopButton. */ export function setDesktopCardRoute(route: DesktopRoute | null): void { if (state.route === route) return; const open = canShowDesktopUpsell(route) ? state.open : false; state = { ...state, route, open }; emit(); } export function openDesktopCard( scene: DesktopCardSceneIndex = 0, source: DesktopCardSource = "header", ): void { state = { ...state, open: true, scene, openSeq: state.openSeq + 1, source, }; emit(); } export function closeDesktopCard(): void { if (!state.open) return; state = { ...state, open: false }; emit(); } function setDesktopCardDownloaded(downloaded: boolean): void { if (state.downloaded === downloaded) return; state = { ...state, downloaded }; emit(); } export function resetDesktopCardStore(): void { state = { route: null, open: false, scene: 0, openSeq: 0, source: "header", downloaded: false, }; emit(); } /** True when Desktop is not installed yet and a real download URL is offered by the preview server. */ export function canShowDesktopUpsell(route: DesktopRoute | null): boolean { return ( route !== null && !route.context.app_installed && !route.handoff && route.downloadUrl !== null ); } export function resolveDownloadOs(os: string): { key: "mac" | "win" | "linux"; label: string; } { if (os === "win32") return { key: "win", label: "Download free for Windows" }; if (os === "linux") return { key: "linux", label: "Download free for Linux" }; return { key: "mac", label: "Download free for Mac" }; } /** Tag the download URL with this page's telemetry tab id so installs are attributable. */ function withDesktopCta(url: string): string { const tagged = new URL(url); tagged.searchParams.set("hf_cta", resolveTabId()); return tagged.toString(); } export function launchDesktopDownload( route: DesktopRoute, source?: DesktopCardSource, showToast?: (message: string, tone?: "error" | "info") => void, ): boolean { if (!route.downloadUrl) return false; const ctx = source ? { ...route.context, source } : route.context; trackStudioEvent("toolbar_action", { action: "get_desktop_app", ...ctx }); const tab = window.open(withDesktopCta(route.downloadUrl), "_blank"); if (!tab) { showToast?.(`Get the HyperFrames desktop app at ${route.downloadUrl}`, "info"); trackStudioEvent("desktop_cta_outcome", { outcome: "failed", reason: "download_tab_blocked", ...ctx, }); return false; } tab.opener = null; setDesktopCardDownloaded(true); trackStudioEvent("desktop_cta_outcome", { outcome: "sent_to_download", ...ctx }); trackStudioEvent("desktop_cta_guide", { step: "download_opened", ...ctx }); return true; } function defaultStorage(): Pick | null { try { return typeof localStorage !== "undefined" ? localStorage : null; } catch { return null; } } /** At most once per week: true when no auto-open was recorded within the last 7 days. */ export function canAutoOpenDesktopCard( now = Date.now(), storage: Pick | null = defaultStorage(), ): boolean { if (!storage) return true; try { const raw = storage.getItem(DESKTOP_CARD_LAST_AUTO_KEY); if (!raw) return true; const last = Number(raw); if (!Number.isFinite(last)) return true; return now - last >= ONE_WEEK_MS; } catch { return true; } } function markDesktopCardAutoOpened( now = Date.now(), storage: Pick | null = defaultStorage(), ): void { if (!storage) return; try { storage.setItem(DESKTOP_CARD_LAST_AUTO_KEY, String(now)); } catch { // Storage quota or private browsing; skip persistence without failing the open. } } /** Attempt an automatic open (playback ended / export complete), obeying the once-a-week cap. */ export function maybeAutoOpenDesktopCard( source: "auto_watch" | "auto_export", now = Date.now(), storage: Pick | null = defaultStorage(), ): boolean { if (!canShowDesktopUpsell(state.route)) return false; if (state.open) return false; if (!canAutoOpenDesktopCard(now, storage)) return false; markDesktopCardAutoOpened(now, storage); openDesktopCard(0, source); return true; }