| 1 | import { lazy, Suspense, useEffect, useRef, useState } from "react"; |
| 2 | import { app } from "../lib/bridge"; |
| 3 | import { desktopHost } from "../lib/desktopHost"; |
| 4 | import { claimStudioIntroduction, isStableOneVersion, studioIntroductionPending } from "../lib/studioDiscovery"; |
| 5 | import { useUpdater } from "../lib/useUpdater"; |
| 6 | |
| 7 | const StudioIntroduction = lazy(() => import("../components/StudioIntroduction").then((module) => ({ default: module.StudioIntroduction }))); |
| 8 | |
| 9 | /** Optional campaign, subordinate to setup, user input, tasks and updates. */ |
| 10 | export function StudioDiscoveryLifecycle({ safeToShow, stableRelease }: { safeToShow: boolean; stableRelease: boolean }) { |
| 11 | const [eligible, setEligible] = useState(false); |
| 12 | const [open, setOpen] = useState(false); |
| 13 | const interacted = useRef(false); |
| 14 | const { status } = useUpdater(); |
| 15 | const safe = safeToShow && (status.kind === "idle" || status.kind === "upToDate"); |
| 16 | |
| 17 | useEffect(() => { |
| 18 | if (!stableRelease || desktopHost().kind === "none" || !studioIntroductionPending()) return; |
| 19 | let cancelled = false; |
| 20 | void Promise.all([app.Version(), app.NeedsOnboarding()]).then(([version, needsSetup]) => { |
| 21 | if (!cancelled) setEligible(isStableOneVersion(version) && !needsSetup); |
| 22 | }).catch(() => { /* Promotion must never block startup. */ }); |
| 23 | return () => { cancelled = true; }; |
| 24 | }, [stableRelease]); |
| 25 | |
| 26 | useEffect(() => { |
| 27 | const markInteraction = () => { interacted.current = true; }; |
| 28 | document.addEventListener("pointerdown", markInteraction, true); |
| 29 | document.addEventListener("keydown", markInteraction, true); |
| 30 | return () => { |
| 31 | document.removeEventListener("pointerdown", markInteraction, true); |
| 32 | document.removeEventListener("keydown", markInteraction, true); |
| 33 | }; |
| 34 | }, []); |
| 35 | |
| 36 | useEffect(() => { |
| 37 | if (!eligible || !stableRelease || !safe || open) return; |
| 38 | const timer = window.setTimeout(() => { |
| 39 | if (interacted.current || document.visibilityState === "hidden" || document.querySelector('[data-app-overlay], [role="dialog"], [aria-modal="true"]')) return; |
| 40 | if (claimStudioIntroduction()) setOpen(true); |
| 41 | }, 1500); |
| 42 | return () => window.clearTimeout(timer); |
| 43 | }, [eligible, stableRelease, safe, open]); |
| 44 | |
| 45 | // New work or approvals take precedence even if they arrive after it opens. |
| 46 | useEffect(() => { if (!safe) setOpen(false); }, [safe]); |
| 47 | return open ? <Suspense fallback={null}><StudioIntroduction onClose={() => setOpen(false)} /></Suspense> : null; |
| 48 | } |
| 49 |