Skip to content

Commit eebd40f

Browse files
committed
refactor(web): subscribe terminal links to metadata updates
1 parent 82a9f8d commit eebd40f

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from "@t3tools/contracts";
3434
import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
3535
import * as Schema from "effect/Schema";
36+
import { AsyncResult } from "effect/reactivity";
3637
import {
3738
type PointerEvent as ReactPointerEvent,
3839
type ReactNode,
@@ -81,7 +82,7 @@ import {
8182
import { readLocalApi } from "~/localApi";
8283
import { confirmTerminalClose } from "~/lib/terminalCloseConfirm";
8384
import { useClientSettings } from "../hooks/useSettings";
84-
import { getFileMetadataQueryAtom, useFileMetadata } from "../hooks/useFileMetadata";
85+
import { getFileMetadataQueryAtom } from "../hooks/useFileMetadata";
8586
import { FileMetadataThreadContext } from "../hooks/FileMetadataThreadProvider";
8687
import { useLocalStorage } from "../hooks/useLocalStorage";
8788
import { attachedTerminalSessionAtom } from "../state/terminalSessions";
@@ -530,20 +531,20 @@ function TerminalSessionViewport({
530531
readEnvironmentScope(environmentId, AuthTerminalOperateScope),
531532
);
532533
const canOpenHostEditor = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
534+
const canReadFilesystem = useEnvironmentScope(environmentId, AuthFilesystemReadScope);
533535
const [linkPath, setLinkPath] = useState<string | null>(null);
534-
const linkMetadata = useFileMetadata(
535-
linkPath === null ? null : environmentId,
536-
linkPath ?? "",
537-
cwd,
538-
);
536+
const metadataThreadRef = useContext(FileMetadataThreadContext);
537+
const linkMetadataQuery =
538+
linkPath !== null && canOpenHostEditor && canReadFilesystem
539+
? getFileMetadataQueryAtom(environmentId, linkPath, cwd, metadataThreadRef)
540+
: null;
539541
const canActivateTerminalLink = useEffectEvent((text: string) => {
540542
if (isTerminalUrl(text)) return true;
541543
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
542-
return (
543-
text === linkPath && (linkMetadata?.kind === "file" || linkMetadata?.kind === "directory")
544-
);
544+
if (text !== linkPath || !linkMetadataQuery) return false;
545+
const metadata = AsyncResult.getOrElse(appAtomRegistry.get(linkMetadataQuery), () => null);
546+
return metadata?.kind === "file" || metadata?.kind === "directory";
545547
});
546-
const metadataThreadRef = useContext(FileMetadataThreadContext);
547548
const validateTerminalLink = useEffectEvent(async (text: string) => {
548549
if (isTerminalUrl(text)) return true;
549550
if (
@@ -668,9 +669,16 @@ function TerminalSessionViewport({
668669

669670
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- refreshes the imperative terminal's hovered link after filesystem metadata or editor permissions change
670671
useEffect(() => {
671-
terminalRef.current?.refreshLinkActivation();
672-
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- The surface re-reads these values through canActivateTerminalLink's Effect Event.
673-
}, [canOpenHostEditor, linkPath, linkMetadata]);
672+
if (!linkMetadataQuery) {
673+
terminalRef.current?.refreshLinkActivation();
674+
return;
675+
}
676+
return appAtomRegistry.subscribe(
677+
linkMetadataQuery,
678+
() => terminalRef.current?.refreshLinkActivation(),
679+
{ immediate: true },
680+
);
681+
}, [linkMetadataQuery]);
674682

675683
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes visibility into the terminal surface before paint and consumes focus requests made while hidden
676684
useLayoutEffect(() => {

0 commit comments

Comments
 (0)