Files
t3code/apps/mobile/src/features/terminal/nativeTerminalModule.ts
T
b3e8c0334b T3 Code Mobile [WIP] (#2013)
Co-authored-by: codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
2026-05-29 17:00:54 -07:00

66 lines
1.8 KiB
TypeScript

import type { ComponentType } from "react";
import type { NativeSyntheticEvent, ViewProps } from "react-native";
import { requireNativeView } from "expo";
const NATIVE_TERMINAL_MODULE_NAME = "T3TerminalSurface";
interface ExpoGlobalWithViewConfig {
readonly expo?: {
getViewConfig?: (moduleName: string, viewName?: string) => unknown;
};
}
interface TerminalInputEvent {
readonly data: string;
}
interface TerminalResizeEvent {
readonly cols: number;
readonly rows: number;
}
export interface NativeTerminalSurfaceProps extends ViewProps {
readonly appearanceScheme?: "light" | "dark";
readonly themeConfig?: string;
readonly backgroundColor?: string;
readonly foregroundColor?: string;
readonly mutedForegroundColor?: string;
readonly terminalKey: string;
readonly initialBuffer: string;
readonly fontSize: number;
readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
}
let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
function getExpoViewConfig(moduleName: string) {
return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
moduleName,
);
}
export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
if (cachedNativeTerminalSurfaceView) {
return cachedNativeTerminalSurfaceView;
}
if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
return null;
}
try {
cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
NATIVE_TERMINAL_MODULE_NAME,
);
} catch {
return null;
}
return cachedNativeTerminalSurfaceView ?? null;
}
export function hasNativeTerminalSurface() {
return resolveNativeTerminalSurfaceView() !== null;
}