Open History to see the last commit
1import type { ComponentType } from "react";
2import type { NativeSyntheticEvent, ViewProps } from "react-native";
3import { requireNativeView } 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 themeConfig?: string;
27 readonly backgroundColor?: string;
28 readonly foregroundColor?: string;
29 readonly mutedForegroundColor?: string;
30 readonly terminalKey: string;
31 readonly initialBuffer: string;
32 readonly fontSize: number;
33 readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
34 readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
35}
36
37let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
38let nativeTerminalSurfaceViewResolutionFailed = false;
39
40function getExpoViewConfig(moduleName: string) {
41 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
42 moduleName,
43 );
44}
45
46export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
47 if (cachedNativeTerminalSurfaceView) {
48 return cachedNativeTerminalSurfaceView;
49 }
50
51 if (nativeTerminalSurfaceViewResolutionFailed) {
52 return null;
53 }
54
55 if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
56 return null;
57 }
58
59 try {
60 cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
61 NATIVE_TERMINAL_MODULE_NAME,
62 );
63 } catch (cause) {
64 nativeTerminalSurfaceViewResolutionFailed = true;
65 console.error(
66 new NativeViewResolutionError({
67 nativeModuleName: NATIVE_TERMINAL_MODULE_NAME,
68 cause,
69 }),
70 );
71 return null;
72 }
73
74 return cachedNativeTerminalSurfaceView ?? null;
75}
76
77export function hasNativeTerminalSurface() {
78 return resolveNativeTerminalSurfaceView() !== null;
79}