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