@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 113 lines 3.43 KB
Raw
1 import { requireOptionalNativeModule } from "expo";
2
3 import { MOBILE_THEME_IDS, type MobileThemeId } from "../../lib/mobileTheme";
4
5 const SHOWCASE_SCENES = [
6 "threads",
7 "thread",
8 "terminal",
9 "review",
10 "environments",
11 "agent-activity",
12 ] as const;
13 export type ShowcaseScene = (typeof SHOWCASE_SCENES)[number];
14
15 export type ShowcaseOrientation = "portrait" | "landscape";
16
17 interface NativeShowcaseControls {
18 readonly getShowcasePairingUrl?: () => string | null;
19 readonly getShowcaseScene?: () => string | null;
20 readonly getShowcaseTheme?: () => string | null;
21 readonly getShowcaseOrientation?: () => string | null;
22 readonly applyShowcaseOrientation?: (orientation: ShowcaseOrientation) => Promise<void>;
23 readonly getInterfaceOrientation?: () => Promise<string>;
24 readonly prepareShowcaseCapture?: () => void;
25 readonly markShowcaseReady?: (scene: ShowcaseScene) => void;
26 }
27
28 function nativeShowcaseControls(): NativeShowcaseControls | null {
29 return requireOptionalNativeModule<NativeShowcaseControls>("T3NativeControls");
30 }
31
32 export function getNativeShowcasePairingUrls(): ReadonlyArray<string> {
33 try {
34 let raw = nativeShowcaseControls()?.getShowcasePairingUrl?.()?.trim();
35 if (!raw) return [];
36 if (raw.startsWith("json-uri:")) {
37 try {
38 raw = decodeURIComponent(raw.slice("json-uri:".length));
39 } catch {
40 return [];
41 }
42 }
43 try {
44 const parsed: unknown = JSON.parse(raw);
45 if (Array.isArray(parsed)) {
46 return parsed.filter(
47 (candidate): candidate is string =>
48 typeof candidate === "string" && candidate.trim().length > 0,
49 );
50 }
51 } catch {}
52 return [raw];
53 } catch {
54 return [];
55 }
56 }
57
58 export function getNativeShowcaseScene(): ShowcaseScene | null {
59 try {
60 const scene = nativeShowcaseControls()?.getShowcaseScene?.()?.trim();
61 return SHOWCASE_SCENES.find((candidate) => candidate === scene) ?? null;
62 } catch {
63 return null;
64 }
65 }
66
67 export 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
76 export function prepareNativeShowcaseCapture(): void {
77 try {
78 nativeShowcaseControls()?.prepareShowcaseCapture?.();
79 } catch {}
80 }
81
82 export function getNativeShowcaseOrientation(): ShowcaseOrientation | null {
83 try {
84 const orientation = nativeShowcaseControls()?.getShowcaseOrientation?.()?.trim();
85 return orientation === "portrait" || orientation === "landscape" ? orientation : null;
86 } catch {
87 return null;
88 }
89 }
90
91 export async function applyNativeShowcaseOrientation(
92 orientation: ShowcaseOrientation,
93 ): Promise<boolean> {
94 const controls = nativeShowcaseControls();
95 if (!controls?.applyShowcaseOrientation || !controls.getInterfaceOrientation) {
96 // Older binaries cannot change orientation; report success to avoid endless retries.
97 return true;
98 }
99 try {
100 await controls.applyShowcaseOrientation(orientation);
101 // Confirm async geometry changes so requests made before foreground activation can retry.
102 await new Promise((resolve) => setTimeout(resolve, 500));
103 return (await controls.getInterfaceOrientation()) === orientation;
104 } catch {
105 return false;
106 }
107 }
108
109 export function markNativeShowcaseReady(scene: ShowcaseScene): void {
110 try {
111 nativeShowcaseControls()?.markShowcaseReady?.(scene);
112 } catch {}
113 }