Open History to see the last commit
1import type { ComponentType } from "react";
2import type { NativeSyntheticEvent, ViewProps } from "react-native";
3import { requireNativeView, requireOptionalNativeModule } from "expo";
4
5import { NativeViewResolutionError } from "../../native/nativeViewResolutionError";
6
7const NATIVE_TERMINAL_MODULE_NAME = "T3TerminalSurface";
8
9interface ExpoGlobalWithViewConfig {
10 readonly expo?: {
11 getViewConfig?: (moduleName: string, viewName?: string) => unknown;
12 };
13}
14
15interface TerminalInputEvent {
16 readonly data: string;
17}
18
19interface TerminalResizeEvent {
20 readonly cols: number;
21 readonly rows: number;
22}
23
24export interface NativeTerminalSurfaceProps extends ViewProps {
25 readonly appearanceScheme?: "light" | "dark";
26 readonly autoFocus?: boolean;
27 readonly focusRequest?: number;
28 readonly captureRequest?: number;
29 readonly onCapture?: (event: NativeSyntheticEvent<{ readonly text: string }>) => void;
30 readonly themeConfig?: string;
31 readonly backgroundColor?: string;
32 readonly foregroundColor?: string;
33 readonly mutedForegroundColor?: string;
34 readonly terminalKey: string;
35 readonly initialBuffer: string;
36 readonly fontSize: number;
37 readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
38 readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
39}
40
41let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
42let nativeTerminalSurfaceViewResolutionFailed = false;
43
44function getExpoViewConfig(moduleName: string) {
45 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
46 moduleName,
47 );
48}
49
50export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
51 if (cachedNativeTerminalSurfaceView) {
52 return cachedNativeTerminalSurfaceView;
53 }
54
55 if (nativeTerminalSurfaceViewResolutionFailed) {
56 return null;
57 }
58
59 if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
60 return null;
61 }
62
63 try {
64 cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
65 NATIVE_TERMINAL_MODULE_NAME,
66 );
67 } catch (cause) {
68 nativeTerminalSurfaceViewResolutionFailed = true;
69 console.error(
70 new NativeViewResolutionError({
71 nativeModuleName: NATIVE_TERMINAL_MODULE_NAME,
72 cause,
73 }),
74 );
75 return null;
76 }
77
78 return cachedNativeTerminalSurfaceView ?? null;
79}
80
81/**
82 * Revision of the native hardware-keyboard handling compiled into the installed binary,
83 * or `null` when the binary predates the revision constant (or the module is missing).
84 * Used in terminal debug logs to detect stale native builds.
85 */
86export function getNativeTerminalHardwareKeyRevision(): number | null {
87 try {
88 if (typeof requireOptionalNativeModule !== "function") {
89 return null;
90 }
91 const module = requireOptionalNativeModule<{ readonly hardwareKeyRevision?: number }>(
92 NATIVE_TERMINAL_MODULE_NAME,
93 );
94 return module?.hardwareKeyRevision ?? null;
95 } catch {
96 return null;
97 }
98}
99
100export function hasNativeTerminalSurface() {
101 return resolveNativeTerminalSurfaceView() !== null;
102}