mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 08:31:49 +00:00
* chore: ignore local .worktrees/ directory Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): editorTabStore scaffold with single-tab ops Implements the EditorTabStore class singleton (matching activeTabStore pattern) with updateContent, markSaved, setWordWrap, setSavingState, close, and subscribe. Includes useSyncExternalStore hooks and 6 passing unit tests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): editorTabStore promoteFromModal with per-session path dedup * feat(editor): confirmCloseBySession for session teardown Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(sftp): writeTextFileByConnection for pane-agnostic saves Adds a new `writeTextFileByConnection(connectionId, expectedHostId, filePath, content, filenameEncoding?)` method to `useSftpExternalOperations` that looks up the SFTP pane by connection ID (with a hostId safety check) instead of the left/right-side coupling used by `writeTextFile`. Threads the existing `getPaneByConnectionId` callback through the call site and re-exports the new method via `SftpStateApi`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(editor): editorSftpBridge singleton for out-of-React saves Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(editor): extract TextEditorPane from TextEditorModal Lift Monaco editor body + toolbar + theme sync + paste fallback into a pure TextEditorPane component. Adds sftp.editor.maximize i18n key to en.ts and zh-CN.ts locale files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(editor): drop unused getLanguageId import in TextEditorPane Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(editor): TextEditorModal delegates to TextEditorPane Replace the monolithic modal (560 lines including full Monaco setup) with a thin Dialog shell (~150 lines) that owns content/saving/saveError/ languageId state, save orchestration, and dirty-check on close, then delegates all editor chrome to <TextEditorPane chrome="modal" />. Exports TextEditorModalSnapshot for the optional onPromoteToTab callback so callers can later wire tab promotion (Task 12) without breaking the existing interface — the new prop is optional and existing callers (SftpOverlays.tsx) are source-compatible with zero changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(editor): include fileName and wordWrap in TextEditorModalSnapshot Task 12 will populate the promoted tab with these fields, so the snapshot must carry them from the modal at maximize time. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): UnsavedChangesDialog three-button confirm * fix(editor): resolve UnsavedChangesDialog re-entrance and unmount leaks - Re-entrance: if prompt() is called while a prior prompt is still pending, cancel the prior one so its caller doesn't hang forever. - Unmount: resolve any in-flight prompt as "cancel" in the effect cleanup so awaiters don't leak when the provider unmounts. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): TextEditorTabView tab-form shell Add TextEditorTabView component that binds an editorTabStore entry to TextEditorPane, with CSS display:none toggling for inactive tabs so the Monaco instance persists across tab switches. Also adds setLanguage public method to EditorTabStore (lands Task 15's intent early — Task 15 can be a no-op). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(editor): read live store state in TextEditorTabView handlers React state snapshot lags the store by a microtask. Closing over `tab` meant a keystroke between Monaco's onChange and a Ctrl+S would write stale content and mark a stale baseline. Read via editorTabStore.getTab at call time instead. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): dispatch editor:* tab ids in App and activeTabStore - Add EDITOR_PREFIX, isEditorTabId, toEditorTabId, fromEditorTabId helpers - Add useIsEditorTabActive hook to activeTabStore - Update useIsTerminalLayerVisible to exclude editor tabs - Import useEditorTabs and TextEditorTabView into App.tsx - Append editor tab ids (editor:<id>) to allTabs in hotkey handler - Mount TextEditorTabView per editorTab with CSS visibility toggling - Add editorTabs to executeHotkeyAction useCallback dependency array Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(editor): render editor tabs in TopTabs with icon/dirty/tooltip - Add `fromEditorTabId`, `isEditorTabId` imports to TopTabs.tsx - Add `FileCode`, `FileText` icons; use FileCode for code-like extensions - Extend `TopTabsProps` with `editorTabs`, `onRequestCloseEditorTab`, `hostById` - Build `editorTabMap` for O(1) lookup; add `editor` branch in `orderedTabItems` - Render editor tab chrome matching terminal tab style: file icon, dirty dot (●), filename with disambiguation suffix for duplicate filenames, close button - In App.tsx: add stub `handleRequestCloseEditorTab`, `orderedTabsWithEditors`, pass new props to `<TopTabs>` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(editor): hoist editor-tab code-extension regex and use onSelectTab - Move CODE_EXTENSIONS_RE to module scope so it isn't recompiled per render. - Call onSelectTab(tabId) for consistency with other tab types, instead of reaching into activeTabStore directly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): maximize modal to tab and dirty-confirm tab close Wire onPromoteToTab from TextEditorModal through SftpOverlays and useSftpViewFileOps so clicking the maximize button snapshots editor state into editorTabStore and activates the new editor tab. Replace the stub handleRequestCloseEditorTab in App.tsx with a real dirty-confirm flow using UnsavedChangesProvider render-prop: clean tabs close immediately, dirty tabs prompt save/discard/cancel, and save routes through editorSftpBridge with markSaved on success. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(editor): register SFTP bridge and gate session close on dirty editor tabs Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(editor): make onDisconnect async so host-picker waits for dirty check The session-close dirty gate added in Task 13 made onDisconnect async, but the host-picker in SftpPaneDialogs still called it synchronously before kicking off onConnect — a fire-and-forget that raced past the dirty prompt and let unsaved editor tabs slip through. Propagate the Promise return type through SftpPaneCallbacks / SftpPaneDialogs / useSftpViewPaneActionsResult and await it at the host-picker call sites. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): block app quit while editor tabs are dirty Add a before-quit IPC guard that asks the renderer whether any editor tab has unsaved changes. If dirty tabs exist, preventDefault() blocks the quit and a warning toast is shown. The app quits normally once editors are clean. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(editor): add 5s timeout fallback to quit-guard IPC check If the renderer crashes or throws before reporting back, the quitGuard would stay busy forever and the app could not be quit. Fall back to force-quit after 5 s if no reply arrives. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): quit-guard uses quitConfirmed flag to prevent re-entry loop The prior flow reset quitGuardChannelBusy before calling app.quit(), which on macOS re-fires before-quit and re-entered the dirty check with the flag cleared — creating an infinite IPC loop. Introduce a separate quitConfirmed flag that commits to quitting before app.quit() fires, so the re-entry takes the fast path. Also extract QUIT_GUARD_TIMEOUT_MS and clarify that a concurrent quit while a check is in flight is swallowed (preventDefault) rather than letting the second event through. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): use absolute inset-0 for tab panel and add sr-only DialogTitle Two bugs surfaced during the first dev-server smoke test: 1. Editor tab content was blank because TextEditorTabView used only className="h-full", while its sibling panels (VaultView, SftpView, TerminalLayerMount, LogView) all fill their flex-1 parent via `absolute inset-0`. In normal flow the editor tab collapsed to zero height. Match the sibling convention. 2. Radix printed an accessibility warning because the Task 7 refactor pulled the DialogTitle out of DialogContent and into the Pane header (now a plain span). Add a visually hidden DialogTitle that mirrors the filename, so screen readers have a title without showing it twice. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): raise tab panel z-index to 20 so it sits above TerminalLayer TerminalLayer's root is visibility:hidden when the active tab is an editor tab, but its inner panels set `absolute inset-0 z-10` on their own and those still paint. Without an explicit z on the editor tab panel, TerminalLayer's inner bg-background div was covering the Monaco content, producing a blank screen. Also add bg-background to the wrapper so the editor tab paints an opaque surface (matches the pattern VaultViewContainer / TerminalLayer follow). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): show host label and remote path next to filename in tab header The editor tab form previously only showed the bare filename in its header, which is ambiguous when the same filename is open against multiple hosts. Add an optional subtitle prop on TextEditorPane and populate it from the tab form with `<hostLabel>:<remotePath>` rendered in muted text beside the filename. The modal keeps its existing filename-only header. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): bridge supports multiple useSftpState instances useSftpState is instantiated in both the top-level SftpView and the terminal's SftpSidePanel, each owning its own pane registry. The editor bridge previously stored only one writer, so maximizing a file opened from the terminal side panel registered nothing (bridge was owned by SftpView which may never have mounted) and save failed with "bridge not registered". Change the bridge to track a Set of writers and dispatch by trying each until one owns the connectionId (signalled by its specific "connection no longer available" error). Add registerEditorSftpWriterScoped that returns an unregister fn so each instance's cleanup removes only its own entry. Register in both SftpView and SftpSidePanel. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(editor): Cmd+W closes editor tab + terminal close forces tab close Two behaviors added after user feedback from dev-server smoke-test: 1. Cmd/Ctrl+W (the closeTab hotkey) previously did nothing on editor tabs because executeHotkeyAction had no branch for editor:* ids. Add one that reaches into the UnsavedChangesProvider render-prop's close flow via a ref, routing through the existing dirty-confirm path. 2. Closing a terminal tab unmounts its SftpSidePanel which destroys the useSftpState instance that owned the connection. Any editor tab promoted from that panel would then be stuck — bridge gone, save channel dead. On SftpSidePanel unmount, gather the connection ids it owned and call a new editorTabStore.forceCloseBySessions to drop matching editor tabs. Dirty state is dropped because the user closed the terminal knowing the file was open — there is no save channel left anyway. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): Cmd/Ctrl+W works when focus is inside Monaco Monaco's internal key-event dispatcher swallows keydown before the capture-phase handler on the Pane's root div can see it, so the global hotkey dispatcher never got the chance to close the editor tab when the editor had focus. Register a Monaco editor command for the close-tab keybinding and route it through a handleCloseRef — mirrors the same pattern used for Cmd/Ctrl+S. Also drop the modal-only guard in the capture-phase handler so the outer-chrome path works in tab mode too. TextEditorTabView now receives an onRequestClose(tabId) prop that App.tsx wires via the render-prop-exposed handleRequestCloseEditorTabRef, same mechanism as the hotkey-dispatcher path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(editor): fall back to Vaults when forceCloseBySessions removes the active tab Closing a terminal tab triggers SftpSidePanel unmount which force-closes its editor tabs. If the editor tab being removed happened to be the active tab (user maximized → then closed the owning terminal from another path), the app ended up on a stale activeTabId with no selected tab and blank content. Inside forceCloseBySessions, if the active tab was one of the removed editor ids, redirect to 'vault'. Picking a more sophisticated neighbor would need the full orderedTabs list which isn't reachable from this layer; Vaults is always valid. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
541 lines
18 KiB
TypeScript
541 lines
18 KiB
TypeScript
import { useCallback, useMemo, useRef } from "react";
|
|
import {
|
|
Host,
|
|
Identity,
|
|
SftpFilenameEncoding,
|
|
SftpFileEntry,
|
|
SSHKey,
|
|
} from "../../domain/models";
|
|
import {
|
|
createEmptyPane,
|
|
SftpStateOptions,
|
|
} from "./sftp/types";
|
|
import {
|
|
formatDate,
|
|
formatFileSize,
|
|
getFileExtension,
|
|
getFileName,
|
|
getParentPath,
|
|
joinPath,
|
|
} from "./sftp/utils";
|
|
import { useSftpTabsState } from "./sftp/useSftpTabsState";
|
|
import { isSessionError } from "./sftp/errors";
|
|
import { useSftpExternalOperations } from "./sftp/useSftpExternalOperations";
|
|
import { useSftpTransfers } from "./sftp/useSftpTransfers";
|
|
import { useSftpPaneActions } from "./sftp/useSftpPaneActions";
|
|
import { useSftpConnections } from "./sftp/useSftpConnections";
|
|
import { useSftpFileWatch } from "./sftp/useSftpFileWatch";
|
|
import { useSftpSessionCleanup } from "./sftp/useSftpSessionCleanup";
|
|
import { useSftpSessionErrors } from "./sftp/useSftpSessionErrors";
|
|
|
|
// types + utils now live in ./sftp/*
|
|
|
|
export const useSftpState = (
|
|
hosts: Host[],
|
|
keys: SSHKey[],
|
|
identities: Identity[],
|
|
options?: SftpStateOptions
|
|
) => {
|
|
const createPane = useCallback(
|
|
(id?: string, showHiddenFiles = options?.defaultShowHiddenFiles ?? false) =>
|
|
createEmptyPane(id, showHiddenFiles),
|
|
[options?.defaultShowHiddenFiles],
|
|
);
|
|
|
|
const tabsState = useSftpTabsState({
|
|
defaultShowHiddenFiles: options?.defaultShowHiddenFiles,
|
|
});
|
|
const {
|
|
leftTabs,
|
|
rightTabs,
|
|
leftTabsRef,
|
|
rightTabsRef,
|
|
setLeftTabs,
|
|
setRightTabs,
|
|
leftPane,
|
|
rightPane,
|
|
getActivePane,
|
|
updateTab,
|
|
updateActiveTab,
|
|
clearSelectionsExcept,
|
|
setTabShowHiddenFiles,
|
|
addTab,
|
|
closeTab,
|
|
selectTab,
|
|
reorderTabs,
|
|
moveTabToOtherSide,
|
|
getTabsInfo,
|
|
getActiveTabId,
|
|
} = tabsState;
|
|
|
|
// SFTP session refs
|
|
const sftpSessionsRef = useRef<Map<string, string>>(new Map()); // connectionId -> sftpId
|
|
|
|
// Getter for sftpId from connectionId (for stream transfers)
|
|
const getSftpIdForConnection = useCallback((connectionId: string) => {
|
|
return sftpSessionsRef.current.get(connectionId);
|
|
}, []);
|
|
|
|
// Directory listing cache (connectionId + path)
|
|
const DIR_CACHE_TTL_MS = 10_000;
|
|
const dirCacheRef = useRef<
|
|
Map<string, { files: SftpFileEntry[]; timestamp: number }>
|
|
>(new Map());
|
|
|
|
// Navigation sequence per pane, used to ignore stale async results
|
|
const navSeqRef = useRef<{ left: number; right: number }>({
|
|
left: 0,
|
|
right: 0,
|
|
});
|
|
|
|
const makeCacheKey = useCallback(
|
|
(connectionId: string, path: string, encoding?: SftpFilenameEncoding) =>
|
|
`${connectionId}::${encoding || "auto"}::${path}`,
|
|
[],
|
|
);
|
|
|
|
const clearCacheForConnection = useCallback((connectionId: string) => {
|
|
for (const key of dirCacheRef.current.keys()) {
|
|
if (key.startsWith(`${connectionId}::`)) {
|
|
dirCacheRef.current.delete(key);
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
const clearDirCacheEntry = useCallback((connectionId: string, path: string) => {
|
|
// Remove all encoding variants of this path from the cache
|
|
for (const key of dirCacheRef.current.keys()) {
|
|
if (key.startsWith(`${connectionId}::`) && key.endsWith(`::${path}`)) {
|
|
dirCacheRef.current.delete(key);
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
const getPaneByConnectionId = useCallback((connectionId: string) => {
|
|
for (const tab of leftTabsRef.current.tabs) {
|
|
if (tab.connection?.id === connectionId) return tab;
|
|
}
|
|
for (const tab of rightTabsRef.current.tabs) {
|
|
if (tab.connection?.id === connectionId) return tab;
|
|
}
|
|
return null;
|
|
}, [leftTabsRef, rightTabsRef]);
|
|
|
|
const getTabByConnectionId = useCallback((connectionId: string) => {
|
|
for (const tab of leftTabsRef.current.tabs) {
|
|
if (tab.connection?.id === connectionId) {
|
|
return { side: "left" as const, tabId: tab.id, pane: tab };
|
|
}
|
|
}
|
|
for (const tab of rightTabsRef.current.tabs) {
|
|
if (tab.connection?.id === connectionId) {
|
|
return { side: "right" as const, tabId: tab.id, pane: tab };
|
|
}
|
|
}
|
|
return null;
|
|
}, [leftTabsRef, rightTabsRef]);
|
|
|
|
// Ref to track pending reconnections to avoid multiple reconnect attempts
|
|
const reconnectingRef = useRef<{ left: boolean; right: boolean }>({
|
|
left: false,
|
|
right: false,
|
|
});
|
|
|
|
// Map connectionId → cache key, set at connect time so each tab's
|
|
// navigateTo can use the correct cache key even when multiple tabs
|
|
// share the same hostId with different session-time overrides.
|
|
const connectionCacheKeyMapRef = useRef<Map<string, string>>(new Map());
|
|
|
|
// Store last connected host info for reconnection
|
|
const lastConnectedHostRef = useRef<{
|
|
left: Host | "local" | null;
|
|
right: Host | "local" | null;
|
|
}>({
|
|
left: null,
|
|
right: null,
|
|
});
|
|
|
|
const handleSessionError = useSftpSessionErrors({
|
|
getActivePane,
|
|
leftTabsRef,
|
|
rightTabsRef,
|
|
updateActiveTab,
|
|
sftpSessionsRef,
|
|
clearCacheForConnection,
|
|
navSeqRef,
|
|
lastConnectedHostRef,
|
|
reconnectingRef,
|
|
});
|
|
|
|
useSftpSessionCleanup(sftpSessionsRef);
|
|
useSftpFileWatch(options);
|
|
|
|
const { connect, disconnect, listLocalFiles, listRemoteFiles } = useSftpConnections({
|
|
hosts,
|
|
keys,
|
|
identities,
|
|
leftTabsRef,
|
|
rightTabsRef,
|
|
leftTabs,
|
|
rightTabs,
|
|
leftPane,
|
|
rightPane,
|
|
setLeftTabs,
|
|
setRightTabs,
|
|
getActivePane,
|
|
updateTab,
|
|
navSeqRef,
|
|
dirCacheRef,
|
|
sftpSessionsRef,
|
|
lastConnectedHostRef,
|
|
connectionCacheKeyMapRef,
|
|
reconnectingRef,
|
|
makeCacheKey,
|
|
clearCacheForConnection,
|
|
createEmptyPane: createPane,
|
|
autoConnectLocalOnMount: options?.autoConnectLocalOnMount,
|
|
});
|
|
|
|
const {
|
|
navigateTo,
|
|
refresh,
|
|
navigateUp,
|
|
openEntry,
|
|
setFilter,
|
|
toggleSelection,
|
|
rangeSelect,
|
|
clearSelection,
|
|
selectAll,
|
|
getFilteredFiles,
|
|
createDirectory,
|
|
createDirectoryAtPath,
|
|
createFile,
|
|
createFileAtPath,
|
|
deleteFiles,
|
|
deleteFilesAtPath,
|
|
renameFile,
|
|
renameFileAtPath,
|
|
moveEntriesToPath,
|
|
changePermissions,
|
|
} = useSftpPaneActions({
|
|
hosts,
|
|
getActivePane,
|
|
updateTab,
|
|
updateActiveTab,
|
|
leftTabsRef,
|
|
rightTabsRef,
|
|
navSeqRef,
|
|
dirCacheRef,
|
|
sftpSessionsRef,
|
|
lastConnectedHostRef,
|
|
connectionCacheKeyMapRef,
|
|
reconnectingRef,
|
|
makeCacheKey,
|
|
clearCacheForConnection,
|
|
listLocalFiles,
|
|
listRemoteFiles,
|
|
handleSessionError,
|
|
isSessionError,
|
|
clearSelectionsExcept,
|
|
dirCacheTtlMs: DIR_CACHE_TTL_MS,
|
|
});
|
|
|
|
const setFilenameEncoding = useCallback(
|
|
(side: "left" | "right", encoding: SftpFilenameEncoding) => {
|
|
updateActiveTab(side, (prev) => ({
|
|
...prev,
|
|
filenameEncoding: encoding,
|
|
}));
|
|
|
|
const pane = getActivePane(side);
|
|
if (pane?.connection && !pane.connection.isLocal) {
|
|
clearCacheForConnection(pane.connection.id);
|
|
// Defer refresh so state update lands before we read filenameEncoding in navigateTo.
|
|
setTimeout(() => {
|
|
const refreshedPane = getActivePane(side);
|
|
if (refreshedPane?.connection) {
|
|
navigateTo(side, refreshedPane.connection.currentPath, { force: true });
|
|
}
|
|
}, 0);
|
|
}
|
|
},
|
|
[clearCacheForConnection, getActivePane, navigateTo, updateActiveTab],
|
|
);
|
|
|
|
const setShowHiddenFiles = useCallback(
|
|
(side: "left" | "right", tabId: string, showHiddenFiles: boolean) => {
|
|
setTabShowHiddenFiles(side, tabId, showHiddenFiles);
|
|
},
|
|
[setTabShowHiddenFiles],
|
|
);
|
|
|
|
const {
|
|
transfers,
|
|
conflicts,
|
|
activeTransfersCount,
|
|
startTransfer,
|
|
downloadToLocal,
|
|
addExternalUpload,
|
|
updateExternalUpload,
|
|
cancelTransfer,
|
|
isTransferCancelled,
|
|
retryTransfer,
|
|
clearCompletedTransfers,
|
|
dismissTransfer,
|
|
resolveConflict,
|
|
} = useSftpTransfers({
|
|
getActivePane,
|
|
getPaneByConnectionId,
|
|
getTabByConnectionId,
|
|
updateTab,
|
|
refresh,
|
|
clearCacheForConnection,
|
|
sftpSessionsRef,
|
|
connectionCacheKeyMapRef,
|
|
listLocalFiles,
|
|
listRemoteFiles,
|
|
handleSessionError,
|
|
});
|
|
|
|
const {
|
|
readTextFile,
|
|
readBinaryFile,
|
|
writeTextFile,
|
|
writeTextFileByConnection,
|
|
downloadToTempAndOpen,
|
|
uploadExternalFiles,
|
|
uploadExternalEntries,
|
|
cancelExternalUpload,
|
|
selectApplication,
|
|
activeFileWatchCountRef,
|
|
} = useSftpExternalOperations({
|
|
getActivePane,
|
|
getPaneByConnectionId,
|
|
refresh,
|
|
sftpSessionsRef,
|
|
connectionCacheKeyMapRef,
|
|
clearDirCacheEntry,
|
|
useCompressedUpload: options?.useCompressedUpload,
|
|
addExternalUpload,
|
|
updateExternalUpload,
|
|
isTransferCancelled,
|
|
dismissExternalUpload: dismissTransfer,
|
|
});
|
|
|
|
// Store methods in a ref to create stable wrapper functions
|
|
// This prevents callback reference changes from causing re-renders in consumers
|
|
const methodsRef = useRef({
|
|
getFilteredFiles,
|
|
addTab,
|
|
closeTab,
|
|
selectTab,
|
|
reorderTabs,
|
|
moveTabToOtherSide,
|
|
getTabsInfo,
|
|
getActiveTabId,
|
|
getActivePane,
|
|
connect,
|
|
disconnect,
|
|
navigateTo,
|
|
navigateUp,
|
|
refresh,
|
|
openEntry,
|
|
toggleSelection,
|
|
rangeSelect,
|
|
clearSelection,
|
|
clearSelectionsExcept,
|
|
selectAll,
|
|
setFilter,
|
|
setFilenameEncoding,
|
|
setShowHiddenFiles,
|
|
createDirectory,
|
|
createDirectoryAtPath,
|
|
createFile,
|
|
createFileAtPath,
|
|
deleteFiles,
|
|
deleteFilesAtPath,
|
|
renameFile,
|
|
renameFileAtPath,
|
|
moveEntriesToPath,
|
|
changePermissions,
|
|
readTextFile,
|
|
readBinaryFile,
|
|
writeTextFile,
|
|
writeTextFileByConnection,
|
|
downloadToTempAndOpen,
|
|
uploadExternalFiles,
|
|
uploadExternalEntries,
|
|
cancelExternalUpload,
|
|
selectApplication,
|
|
startTransfer,
|
|
downloadToLocal,
|
|
addExternalUpload,
|
|
updateExternalUpload,
|
|
cancelTransfer,
|
|
retryTransfer,
|
|
clearCompletedTransfers,
|
|
dismissTransfer,
|
|
resolveConflict,
|
|
getSftpIdForConnection,
|
|
reportSessionError: handleSessionError,
|
|
});
|
|
methodsRef.current = {
|
|
getFilteredFiles,
|
|
addTab,
|
|
closeTab,
|
|
selectTab,
|
|
reorderTabs,
|
|
moveTabToOtherSide,
|
|
getTabsInfo,
|
|
getActiveTabId,
|
|
getActivePane,
|
|
connect,
|
|
disconnect,
|
|
navigateTo,
|
|
navigateUp,
|
|
refresh,
|
|
openEntry,
|
|
toggleSelection,
|
|
rangeSelect,
|
|
clearSelection,
|
|
clearSelectionsExcept,
|
|
selectAll,
|
|
setFilter,
|
|
setFilenameEncoding,
|
|
setShowHiddenFiles,
|
|
createDirectory,
|
|
createDirectoryAtPath,
|
|
createFile,
|
|
createFileAtPath,
|
|
deleteFiles,
|
|
deleteFilesAtPath,
|
|
renameFile,
|
|
renameFileAtPath,
|
|
moveEntriesToPath,
|
|
changePermissions,
|
|
readTextFile,
|
|
readBinaryFile,
|
|
writeTextFile,
|
|
writeTextFileByConnection,
|
|
downloadToTempAndOpen,
|
|
uploadExternalFiles,
|
|
uploadExternalEntries,
|
|
cancelExternalUpload,
|
|
selectApplication,
|
|
startTransfer,
|
|
downloadToLocal,
|
|
addExternalUpload,
|
|
updateExternalUpload,
|
|
cancelTransfer,
|
|
retryTransfer,
|
|
clearCompletedTransfers,
|
|
dismissTransfer,
|
|
resolveConflict,
|
|
getSftpIdForConnection,
|
|
reportSessionError: handleSessionError,
|
|
};
|
|
|
|
// Create stable method wrappers that call through methodsRef
|
|
// These are created once and never change reference
|
|
const stableMethods = useMemo(() => ({
|
|
getFilteredFiles: (...args: Parameters<typeof getFilteredFiles>) => methodsRef.current.getFilteredFiles(...args),
|
|
addTab: (...args: Parameters<typeof addTab>) => methodsRef.current.addTab(...args),
|
|
closeTab: (...args: Parameters<typeof closeTab>) => methodsRef.current.closeTab(...args),
|
|
selectTab: (...args: Parameters<typeof selectTab>) => methodsRef.current.selectTab(...args),
|
|
reorderTabs: (...args: Parameters<typeof reorderTabs>) => methodsRef.current.reorderTabs(...args),
|
|
moveTabToOtherSide: (...args: Parameters<typeof moveTabToOtherSide>) => methodsRef.current.moveTabToOtherSide(...args),
|
|
getTabsInfo: (...args: Parameters<typeof getTabsInfo>) => methodsRef.current.getTabsInfo(...args),
|
|
getActiveTabId: (...args: Parameters<typeof getActiveTabId>) => methodsRef.current.getActiveTabId(...args),
|
|
getActivePane: (...args: Parameters<typeof getActivePane>) => methodsRef.current.getActivePane(...args),
|
|
connect: (...args: Parameters<typeof connect>) => methodsRef.current.connect(...args),
|
|
disconnect: (...args: Parameters<typeof disconnect>) => methodsRef.current.disconnect(...args),
|
|
navigateTo: (...args: Parameters<typeof navigateTo>) => methodsRef.current.navigateTo(...args),
|
|
navigateUp: (...args: Parameters<typeof navigateUp>) => methodsRef.current.navigateUp(...args),
|
|
refresh: (...args: Parameters<typeof refresh>) => methodsRef.current.refresh(...args),
|
|
openEntry: (...args: Parameters<typeof openEntry>) => methodsRef.current.openEntry(...args),
|
|
toggleSelection: (...args: Parameters<typeof toggleSelection>) => methodsRef.current.toggleSelection(...args),
|
|
rangeSelect: (...args: Parameters<typeof rangeSelect>) => methodsRef.current.rangeSelect(...args),
|
|
clearSelection: (...args: Parameters<typeof clearSelection>) => methodsRef.current.clearSelection(...args),
|
|
clearSelectionsExcept: (...args: Parameters<typeof clearSelectionsExcept>) =>
|
|
methodsRef.current.clearSelectionsExcept(...args),
|
|
selectAll: (...args: Parameters<typeof selectAll>) => methodsRef.current.selectAll(...args),
|
|
setFilter: (...args: Parameters<typeof setFilter>) => methodsRef.current.setFilter(...args),
|
|
setFilenameEncoding: (...args: Parameters<typeof setFilenameEncoding>) =>
|
|
methodsRef.current.setFilenameEncoding(...args),
|
|
setShowHiddenFiles: (...args: Parameters<typeof setShowHiddenFiles>) =>
|
|
methodsRef.current.setShowHiddenFiles(...args),
|
|
createDirectory: (...args: Parameters<typeof createDirectory>) => methodsRef.current.createDirectory(...args),
|
|
createDirectoryAtPath: (...args: Parameters<typeof createDirectoryAtPath>) =>
|
|
methodsRef.current.createDirectoryAtPath(...args),
|
|
createFile: (...args: Parameters<typeof createFile>) => methodsRef.current.createFile(...args),
|
|
createFileAtPath: (...args: Parameters<typeof createFileAtPath>) =>
|
|
methodsRef.current.createFileAtPath(...args),
|
|
deleteFiles: (...args: Parameters<typeof deleteFiles>) => methodsRef.current.deleteFiles(...args),
|
|
deleteFilesAtPath: (...args: Parameters<typeof deleteFilesAtPath>) =>
|
|
methodsRef.current.deleteFilesAtPath(...args),
|
|
renameFile: (...args: Parameters<typeof renameFile>) => methodsRef.current.renameFile(...args),
|
|
renameFileAtPath: (...args: Parameters<typeof renameFileAtPath>) => methodsRef.current.renameFileAtPath(...args),
|
|
moveEntriesToPath: (...args: Parameters<typeof moveEntriesToPath>) => methodsRef.current.moveEntriesToPath(...args),
|
|
changePermissions: (...args: Parameters<typeof changePermissions>) => methodsRef.current.changePermissions(...args),
|
|
readTextFile: (...args: Parameters<typeof readTextFile>) => methodsRef.current.readTextFile(...args),
|
|
readBinaryFile: (...args: Parameters<typeof readBinaryFile>) => methodsRef.current.readBinaryFile(...args),
|
|
writeTextFile: (...args: Parameters<typeof writeTextFile>) => methodsRef.current.writeTextFile(...args),
|
|
writeTextFileByConnection: (...args: Parameters<typeof writeTextFileByConnection>) =>
|
|
methodsRef.current.writeTextFileByConnection(...args),
|
|
downloadToTempAndOpen: (...args: Parameters<typeof downloadToTempAndOpen>) => methodsRef.current.downloadToTempAndOpen(...args),
|
|
uploadExternalFiles: (...args: Parameters<typeof uploadExternalFiles>) => methodsRef.current.uploadExternalFiles(...args),
|
|
uploadExternalEntries: (...args: Parameters<typeof uploadExternalEntries>) =>
|
|
methodsRef.current.uploadExternalEntries(...args),
|
|
cancelExternalUpload: () => methodsRef.current.cancelExternalUpload(),
|
|
selectApplication: () => methodsRef.current.selectApplication(),
|
|
startTransfer: (...args: Parameters<typeof startTransfer>) => methodsRef.current.startTransfer(...args),
|
|
downloadToLocal: (...args: Parameters<typeof downloadToLocal>) => methodsRef.current.downloadToLocal(...args),
|
|
addExternalUpload: (...args: Parameters<typeof addExternalUpload>) => methodsRef.current.addExternalUpload(...args),
|
|
updateExternalUpload: (...args: Parameters<typeof updateExternalUpload>) => methodsRef.current.updateExternalUpload(...args),
|
|
cancelTransfer: (...args: Parameters<typeof cancelTransfer>) => methodsRef.current.cancelTransfer(...args),
|
|
retryTransfer: (...args: Parameters<typeof retryTransfer>) => methodsRef.current.retryTransfer(...args),
|
|
clearCompletedTransfers: () => methodsRef.current.clearCompletedTransfers(),
|
|
dismissTransfer: (...args: Parameters<typeof dismissTransfer>) => methodsRef.current.dismissTransfer(...args),
|
|
resolveConflict: (...args: Parameters<typeof resolveConflict>) => methodsRef.current.resolveConflict(...args),
|
|
getSftpIdForConnection: (...args: Parameters<typeof getSftpIdForConnection>) => methodsRef.current.getSftpIdForConnection(...args),
|
|
reportSessionError: (...args: Parameters<typeof handleSessionError>) => methodsRef.current.reportSessionError(...args),
|
|
activeFileWatchCountRef,
|
|
}), [activeFileWatchCountRef]); // activeFileWatchCountRef is a stable ref
|
|
|
|
// Return object with stable method references but reactive state
|
|
// State changes will cause re-renders, but method references stay stable
|
|
return useMemo(() => ({
|
|
// Reactive state - changes trigger re-renders
|
|
leftPane,
|
|
rightPane,
|
|
leftTabs,
|
|
rightTabs,
|
|
transfers,
|
|
activeTransfersCount,
|
|
conflicts,
|
|
|
|
// Stable methods - never change reference
|
|
...stableMethods,
|
|
|
|
// Pure helper functions (these are defined at module level, always stable)
|
|
formatFileSize,
|
|
formatDate,
|
|
getFileExtension,
|
|
joinPath,
|
|
getParentPath,
|
|
getFileName,
|
|
}), [
|
|
// Only state in deps - methods come from stableMethods which is stable
|
|
leftPane,
|
|
rightPane,
|
|
leftTabs,
|
|
rightTabs,
|
|
transfers,
|
|
activeTransfersCount,
|
|
conflicts,
|
|
stableMethods,
|
|
]);
|
|
};
|
|
|
|
export type SftpStateApi = ReturnType<typeof useSftpState>;
|