[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
153
application/state/sftp/sftpTreeSelectionStore.ts
Normal file
153
application/state/sftp/sftpTreeSelectionStore.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { useCallback, useSyncExternalStore } from "react";
|
||||
|
||||
export interface SftpTreeSelectionItem {
|
||||
path: string;
|
||||
name: string;
|
||||
isDirectory: boolean;
|
||||
sourcePath: string;
|
||||
}
|
||||
|
||||
interface SftpTreeSelectionState {
|
||||
visibleItems: SftpTreeSelectionItem[];
|
||||
visibleItemsByPath: Map<string, SftpTreeSelectionItem>;
|
||||
visibleIndexByPath: Map<string, number>;
|
||||
visiblePathsSet: Set<string>;
|
||||
selectedPaths: Set<string>;
|
||||
}
|
||||
|
||||
const EMPTY_PATHS = new Set<string>();
|
||||
|
||||
const EMPTY_STATE: SftpTreeSelectionState = {
|
||||
visibleItems: [],
|
||||
visibleItemsByPath: new Map(),
|
||||
visibleIndexByPath: new Map(),
|
||||
visiblePathsSet: new Set(),
|
||||
selectedPaths: EMPTY_PATHS,
|
||||
};
|
||||
|
||||
type Listener = () => void;
|
||||
|
||||
const paneStates = new Map<string, SftpTreeSelectionState>();
|
||||
const paneListeners = new Map<string, Set<Listener>>();
|
||||
|
||||
const notifyPaneListeners = (paneId: string) => {
|
||||
paneListeners.get(paneId)?.forEach((listener) => listener());
|
||||
};
|
||||
|
||||
const getPaneState = (paneId: string): SftpTreeSelectionState =>
|
||||
paneStates.get(paneId) ?? EMPTY_STATE;
|
||||
|
||||
const setPaneState = (
|
||||
paneId: string,
|
||||
updater: (state: SftpTreeSelectionState) => SftpTreeSelectionState,
|
||||
) => {
|
||||
const prev = getPaneState(paneId);
|
||||
const next = updater(prev);
|
||||
if (next === prev) return;
|
||||
if (next.visibleItems.length === 0 && next.selectedPaths.size === 0) {
|
||||
paneStates.delete(paneId);
|
||||
} else {
|
||||
paneStates.set(paneId, next);
|
||||
}
|
||||
notifyPaneListeners(paneId);
|
||||
};
|
||||
|
||||
export const sftpTreeSelectionStore = {
|
||||
getPaneState,
|
||||
|
||||
getSelectedItems: (paneId: string): SftpTreeSelectionItem[] => {
|
||||
const state = getPaneState(paneId);
|
||||
const result: SftpTreeSelectionItem[] = [];
|
||||
for (const path of state.selectedPaths) {
|
||||
const item = state.visibleItemsByPath.get(path);
|
||||
if (item) result.push(item);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
setVisibleItems: (paneId: string, visibleItems: SftpTreeSelectionItem[]) => {
|
||||
const visibleItemsByPath = new Map<string, SftpTreeSelectionItem>();
|
||||
const visibleIndexByPath = new Map<string, number>();
|
||||
const visiblePathsSet = new Set(visibleItems.map((item) => item.path));
|
||||
visibleItems.forEach((item, index) => {
|
||||
visibleItemsByPath.set(item.path, item);
|
||||
visibleIndexByPath.set(item.path, index);
|
||||
});
|
||||
setPaneState(paneId, (state) => {
|
||||
const newSelected = new Set([...state.selectedPaths].filter((p) => visiblePathsSet.has(p)));
|
||||
const changed =
|
||||
newSelected.size !== state.selectedPaths.size ||
|
||||
[...newSelected].some((p) => !state.selectedPaths.has(p));
|
||||
return {
|
||||
visibleItems,
|
||||
visibleItemsByPath,
|
||||
visibleIndexByPath,
|
||||
visiblePathsSet,
|
||||
selectedPaths: changed ? newSelected : state.selectedPaths,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
setSelection: (paneId: string, selectedPaths: Iterable<string>) => {
|
||||
setPaneState(paneId, (state) => ({
|
||||
...state,
|
||||
selectedPaths: new Set(Array.from(selectedPaths).filter((path) => state.visiblePathsSet.has(path))),
|
||||
}));
|
||||
},
|
||||
|
||||
clearSelection: (paneId: string) => {
|
||||
setPaneState(paneId, (state) => ({ ...state, selectedPaths: EMPTY_PATHS }));
|
||||
},
|
||||
|
||||
clearAllExcept: (paneIdsToKeep?: Iterable<string>) => {
|
||||
const keep = new Set(paneIdsToKeep ?? []);
|
||||
Array.from(paneStates.keys()).forEach((paneId) => {
|
||||
if (keep.has(paneId)) return;
|
||||
setPaneState(paneId, (state) => {
|
||||
if (state.selectedPaths.size === 0) return state;
|
||||
return { ...state, selectedPaths: EMPTY_PATHS };
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
selectAllVisible: (paneId: string) => {
|
||||
setPaneState(paneId, (state) => ({
|
||||
...state,
|
||||
selectedPaths: new Set(
|
||||
state.visibleItems.map((item) => item.path),
|
||||
),
|
||||
}));
|
||||
},
|
||||
|
||||
clearPane: (paneId: string) => {
|
||||
if (!paneStates.has(paneId)) return;
|
||||
paneStates.delete(paneId);
|
||||
notifyPaneListeners(paneId);
|
||||
},
|
||||
|
||||
subscribe: (paneId: string, listener: Listener) => {
|
||||
const listeners = paneListeners.get(paneId) ?? new Set<Listener>();
|
||||
listeners.add(listener);
|
||||
paneListeners.set(paneId, listeners);
|
||||
return () => {
|
||||
const current = paneListeners.get(paneId);
|
||||
if (!current) return;
|
||||
current.delete(listener);
|
||||
if (current.size === 0) {
|
||||
paneListeners.delete(paneId);
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export const useSftpTreeSelectionState = (paneId: string): SftpTreeSelectionState => {
|
||||
const subscribe = useCallback(
|
||||
(listener: () => void) => sftpTreeSelectionStore.subscribe(paneId, listener),
|
||||
[paneId],
|
||||
);
|
||||
return useSyncExternalStore(
|
||||
subscribe,
|
||||
() => sftpTreeSelectionStore.getPaneState(paneId),
|
||||
() => sftpTreeSelectionStore.getPaneState(paneId),
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user