Open History to see the last commit
1import { requireOptionalNativeModule } from "expo";
2
3import { MOBILE_THEME_IDS, type MobileThemeId } from "../../lib/mobileTheme";
4
5export const SHOWCASE_SCENES = ["threads", "thread", "terminal", "review", "environments"] as const;
6export type ShowcaseScene = (typeof SHOWCASE_SCENES)[number];
7
8export type ShowcaseOrientation = "portrait" | "landscape";
9
10interface NativeShowcaseControls {
11 readonly getShowcasePairingUrl?: () => string | null;
12 readonly getShowcaseScene?: () => string | null;
13 readonly getShowcaseTheme?: () => string | null;
14 readonly getShowcaseOrientation?: () => string | null;
15 readonly applyShowcaseOrientation?: (orientation: ShowcaseOrientation) => Promise<void>;
16 readonly getInterfaceOrientation?: () => Promise<string>;
17 readonly prepareShowcaseCapture?: () => void;
18 readonly markShowcaseReady?: (scene: ShowcaseScene) => void;
19}
20
21function nativeShowcaseControls(): NativeShowcaseControls | null {
22 return requireOptionalNativeModule<NativeShowcaseControls>("T3NativeControls");
23}
24
25export function getNativeShowcasePairingUrls(): ReadonlyArray<string> {
26 try {
27 let raw = nativeShowcaseControls()?.getShowcasePairingUrl?.()?.trim();
28 if (!raw) return [];
29 if (raw.startsWith("json-uri:")) {
30 try {
31 raw = decodeURIComponent(raw.slice("json-uri:".length));
32 } catch {
33 return [];
34 }
35 }
36 try {
37 const parsed: unknown = JSON.parse(raw);
38 if (Array.isArray(parsed)) {
39 return parsed.filter(
40 (candidate): candidate is string =>
41 typeof candidate === "string" && candidate.trim().length > 0,
42 );
43 }
44 } catch {
45 // Older runners pass a single URL rather than a JSON array.
46 }
47 return [raw];
48 } catch {
49 return [];
50 }
51}
52
53export function getNativeShowcaseScene(): ShowcaseScene | null {
54 try {
55 const scene = nativeShowcaseControls()?.getShowcaseScene?.()?.trim();
56 return SHOWCASE_SCENES.find((candidate) => candidate === scene) ?? null;
57 } catch {
58 return null;
59 }
60}
61
62/**
63 * Returns null when the runner requested no palette, which leaves the stored
64 * theme preference untouched. An unknown id also reads as null rather than
65 * silently falling back, so a capture never claims to show a theme it does not.
66 */
67export function getNativeShowcaseTheme(): MobileThemeId | null {
68 try {
69 const theme = nativeShowcaseControls()?.getShowcaseTheme?.()?.trim();
70 return MOBILE_THEME_IDS.find((candidate) => candidate === theme) ?? null;
71 } catch {
72 return null;
73 }
74}
75
76export function prepareNativeShowcaseCapture(): void {
77 try {
78 nativeShowcaseControls()?.prepareShowcaseCapture?.();
79 } catch {
80 // The harness still works when a development build predates this helper.
81 }
82}
83
84export function getNativeShowcaseOrientation(): ShowcaseOrientation | null {
85 try {
86 const orientation = nativeShowcaseControls()?.getShowcaseOrientation?.()?.trim();
87 return orientation === "portrait" || orientation === "landscape" ? orientation : null;
88 } catch {
89 return null;
90 }
91}
92
93export async function applyNativeShowcaseOrientation(
94 orientation: ShowcaseOrientation,
95): Promise<boolean> {
96 const controls = nativeShowcaseControls();
97 if (!controls?.applyShowcaseOrientation || !controls.getInterfaceOrientation) {
98 // A development build that predates this helper keeps its default
99 // orientation; report success so callers do not retry forever.
100 return true;
101 }
102 try {
103 await controls.applyShowcaseOrientation(orientation);
104 // The geometry request settles asynchronously; confirm it took effect so
105 // callers can retry attempts made before the scene was foreground-active.
106 await new Promise((resolve) => setTimeout(resolve, 500));
107 return (await controls.getInterfaceOrientation()) === orientation;
108 } catch {
109 return false;
110 }
111}
112
113export function markNativeShowcaseReady(scene: ShowcaseScene): void {
114 try {
115 nativeShowcaseControls()?.markShowcaseReady?.(scene);
116 } catch {
117 // The readiness marker is capture-runner metadata, never app functionality.
118 }
119}