Files
Netcatty/components/terminal/hooks/useTerminalFilePaste.ts
T
陈大猫andryan-wong-coder 67f8108713 feat(plugins): add reviewed privileged terminal data pipeline (#2402)
* feat(plugins): add privileged terminal data pipeline

* fix(plugins): harden terminal interceptor lifecycle

* fix(plugins): close terminal interceptor lifecycle gaps

* fix(plugins): preserve terminal interceptor state transitions

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): harden terminal interceptor startup

* fix(plugin): isolate terminal output route attempts

* fix(plugin): isolate reused terminal sessions

* fix(plugin): harden terminal interceptor boundaries

* fix(plugin): harden terminal replay metadata

* fix(plugin): preserve terminal replay provenance

* fix(plugin): close terminal pipeline lifecycle races

* fix(plugin): harden terminal route handoff

* fix(plugin): contain terminal handoff failures

* fix(plugin): route terminal attachment through RPC

* fix(plugin): validate terminal attachment RPC

* fix(plugin): close unexpected runtime ports

* fix(plugin): canonicalize terminal data frames

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): close terminal interceptor review gaps

* fix(plugin): harden terminal interceptor protocol handling

* fix(plugin): preserve terminal flow compatibility

* fix(plugin): preserve terminal protocol accounting

* fix(plugin): bound terminal interceptor lifecycle

* fix(plugin): close terminal interceptor review gaps

---------

Co-authored-by: ryan-wong-coder <wangsiyuan19990317@gmail.com>
2026-07-23 09:42:47 +08:00

93 lines
2.8 KiB
TypeScript

import type { Terminal as XTerm } from "@xterm/xterm";
import type React from "react";
import { useEffect } from "react";
import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge";
import { logger } from "../../../lib/logger";
import type { TerminalSession } from "../../../types";
import { handleTerminalClipboardPaste } from "../terminalClipboardPaste";
interface UseTerminalFilePasteOptions {
isLocalConnection: boolean;
status: TerminalSession["status"];
termRef: React.MutableRefObject<XTerm | null>;
sessionRef: React.MutableRefObject<string | null>;
terminalBackend: {
writeToSession: (sessionId: string, data: string, options?: { automated?: boolean; sensitive?: boolean }) => void;
};
isSensitiveInput?: () => boolean;
scrollOnPasteRef?: React.RefObject<boolean>;
onPasteData?: (data: string) => boolean | void;
scrollToBottomAfterProgrammaticInput: (data: string) => void;
containerRef: React.RefObject<HTMLDivElement | null>;
}
export function useTerminalFilePaste({
isLocalConnection,
status,
termRef,
sessionRef,
terminalBackend,
isSensitiveInput,
scrollOnPasteRef,
onPasteData,
scrollToBottomAfterProgrammaticInput,
containerRef,
}: UseTerminalFilePasteOptions) {
useEffect(() => {
const container = containerRef.current;
if (!container) return;
const handlePaste = (event: ClipboardEvent) => {
if (status !== "connected") return;
const bridge = netcattyBridge.get();
if (!isLocalConnection || !bridge?.readClipboardFiles) return;
// ⚡ Must call preventDefault SYNCHRONOUSLY — the event lifecycle
// is synchronous; calling it after an await is too late and the
// browser will have already performed the default paste action.
event.preventDefault();
event.stopPropagation();
void (async () => {
try {
const term = termRef.current;
if (!term) return;
await handleTerminalClipboardPaste({
bridge,
isLocalConnection,
isSensitiveInput,
readClipboardText: () => navigator.clipboard.readText(),
scrollOnPaste: scrollOnPasteRef?.current ?? false,
onPasteData,
sessionId: sessionRef.current,
terminalBackend,
term,
scrollToBottomAfterProgrammaticInput,
});
} catch (error) {
logger.error("Failed to handle file paste", error);
}
})();
};
container.addEventListener("paste", handlePaste, true);
return () => {
container.removeEventListener("paste", handlePaste, true);
};
}, [
containerRef,
isLocalConnection,
isSensitiveInput,
onPasteData,
scrollOnPasteRef,
scrollToBottomAfterProgrammaticInput,
sessionRef,
status,
terminalBackend,
termRef,
]);
}