Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeTerminalModule.ts

100 lines3.0 KB

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