Files
Netcatty/application/state/useTerminalDirectorySession.ts
T
182531d7a7 feat(terminal): support Directory Picker in Windows PowerShell (#4177)
* feat(terminal): support Directory Picker in Windows PowerShell

* fix(terminal): handle qualified PowerShell paths and CI fixtures

* test(plugins): await dialog focus restoration in log settings

* test(terminal): normalize Windows fixture paths with native realpath

* fix(terminal): retry stale listings and recognize spaced UNC shares

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-11-24 10:50:14 +08:00

64 lines
3.2 KiB
TypeScript

import { useEffect, useRef } from 'react';
import type { Host } from '../../domain/models';
import { classifyDistroId, hostRestrictsExtraSshChannels } from '../../domain/host';
import { netcattyBridge } from '../../infrastructure/services/netcattyBridge';
import { createTerminalDirectoryIO } from '../../infrastructure/services/terminalDirectory';
import { createTerminalDirectoryController } from './terminalDirectoryController';
import { registerTerminalDirectory } from './terminalDirectoryRegistry';
import { directoryChangeCommand, COMPLETE_DIRECTORY_CAPABILITIES } from '../../domain/directoryPicker';
import type { DirectoryPathFlavor } from '../../domain/directoryPath';
export function useTerminalDirectorySession(options: {
sessionId: string;
status: string;
host: Host;
shellType?: string;
getBackendId: () => string | null;
isReady: () => boolean;
publish: (cwd: string) => void;
}) {
const live = useRef(options);
live.current = options;
const { sessionId, status, shellType, host } = options;
const local = host.protocol === 'local';
const supported = (local || ((host.protocol ?? 'ssh') === 'ssh' && !host.moshEnabled && !host.etEnabled))
&& (shellType !== 'powershell' || local) && shellType !== 'cmd'
&& host.deviceType !== 'network' && classifyDistroId(host.distro) !== 'network-device'
&& !hostRestrictsExtraSshChannels(host);
useEffect(() => {
const bridge = netcattyBridge.get();
const backendId = live.current.getBackendId();
if (!bridge || !backendId || status !== 'connected' || !supported) return;
let active = true;
const isCurrent = () => active && live.current.status === 'connected' && live.current.getBackendId() === backendId;
let unregister = () => {};
const register = (pathFlavor: DirectoryPathFlavor = 'posix') => {
if (!isCurrent()) return;
const controller = createTerminalDirectoryController({
sessionId,
io: createTerminalDirectoryIO(bridge, backendId, {
local, hostname: host.hostname, username: host.username || 'root', port: host.port,
encoding: host.sftpEncoding,
pathFlavor,
}, isCurrent),
target: { authority: `${local ? 'local' : 'ssh'}:${backendId}`, pathFlavor },
capabilities: COMPLETE_DIRECTORY_CAPABILITIES,
buildChangeCommand: path => directoryChangeCommand(path, pathFlavor === 'win32' ? 'powershell' : 'posix', pathFlavor),
isCurrent,
isReady: () => live.current.isReady(),
publish: cwd => live.current.publish(cwd),
});
unregister = registerTerminalDirectory(sessionId, controller);
};
if (local) {
// POSIX wrappers on Windows still use win32 PTYs. Neither their cwd nor
// their path namespace is supported by readLocalSessionCwd/local FS.
void bridge.getAppInfo?.().then(info => {
if ((info.platform === 'darwin' || info.platform === 'linux') && shellType !== 'powershell') register();
else if (info.platform === 'win32' && shellType === 'powershell') register('win32');
}).catch(() => {});
} else register();
return () => { active = false; unregister(); };
}, [sessionId, status, supported, local, host.hostname, host.username, host.port, host.sftpEncoding, shellType]);
}