mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 15:41:17 +00:00
Updates type annotations across terminal-related hooks and runtime modules to use RefObject instead of MutableRefObject for React refs. Aligns with React's convention for refs not directly assigned via useRef, improving type safety and clarity.
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import type { Terminal as XTerm } from "@xterm/xterm";
|
|
import { useCallback } from "react";
|
|
import type { RefObject } from "react";
|
|
import { logger } from "../../../lib/logger";
|
|
|
|
type TerminalBackendWriteApi = {
|
|
writeToSession: (sessionId: string, data: string) => void;
|
|
};
|
|
|
|
export const useTerminalContextActions = ({
|
|
termRef,
|
|
sessionRef,
|
|
terminalBackend,
|
|
onHasSelectionChange,
|
|
}: {
|
|
termRef: RefObject<XTerm | null>;
|
|
sessionRef: RefObject<string | null>;
|
|
terminalBackend: TerminalBackendWriteApi;
|
|
onHasSelectionChange?: (hasSelection: boolean) => void;
|
|
}) => {
|
|
const onCopy = useCallback(() => {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
const selection = term.getSelection();
|
|
if (selection) {
|
|
navigator.clipboard.writeText(selection);
|
|
}
|
|
}, [termRef]);
|
|
|
|
const onPaste = useCallback(async () => {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
try {
|
|
const text = await navigator.clipboard.readText();
|
|
if (text && sessionRef.current) terminalBackend.writeToSession(sessionRef.current, text);
|
|
} catch (err) {
|
|
logger.warn("Failed to paste from clipboard", err);
|
|
}
|
|
}, [sessionRef, termRef, terminalBackend]);
|
|
|
|
const onSelectAll = useCallback(() => {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
term.selectAll();
|
|
onHasSelectionChange?.(true);
|
|
}, [onHasSelectionChange, termRef]);
|
|
|
|
const onClear = useCallback(() => {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
term.clear();
|
|
}, [termRef]);
|
|
|
|
const onSelectWord = useCallback(() => {
|
|
const term = termRef.current;
|
|
if (!term) return;
|
|
term.selectAll();
|
|
onHasSelectionChange?.(true);
|
|
}, [onHasSelectionChange, termRef]);
|
|
|
|
return { onCopy, onPaste, onSelectAll, onClear, onSelectWord };
|
|
};
|