mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-24 17:32:18 +00:00
- Added agents overview documentation outlining the project structure and roles. - Implemented `useSessionState` for managing terminal sessions and workspaces. - Developed `useSettingsState` for handling user settings and theme management. - Created `useVaultState` for managing hosts, SSH keys, snippets, and custom groups. - Introduced domain logic for host normalization and sanitization. - Defined models for Host, SSHKey, Snippet, TerminalSession, and Workspace. - Implemented workspace management functions including creation, insertion, and pruning. - Established local storage adapter for persistent data management. - Integrated Gemini AI service for terminal simulation and command generation. - Developed sync service for backing up and restoring configuration to/from GitHub Gists.
121 lines
3.5 KiB
TypeScript
121 lines
3.5 KiB
TypeScript
import { Workspace, WorkspaceNode } from './models';
|
|
|
|
export type SplitDirection = 'horizontal' | 'vertical';
|
|
export type SplitPosition = 'left' | 'right' | 'top' | 'bottom';
|
|
|
|
export type SplitHint = {
|
|
direction: SplitDirection;
|
|
position: SplitPosition;
|
|
targetSessionId?: string;
|
|
};
|
|
|
|
export const pruneWorkspaceNode = (node: WorkspaceNode, targetSessionId: string): WorkspaceNode | null => {
|
|
if (node.type === 'pane') {
|
|
return node.sessionId === targetSessionId ? null : node;
|
|
}
|
|
|
|
const nextChildren: WorkspaceNode[] = [];
|
|
const nextSizes: number[] = [];
|
|
const sizeList = node.sizes && node.sizes.length === node.children.length ? node.sizes : node.children.map(() => 1);
|
|
|
|
node.children.forEach((child, idx) => {
|
|
const pruned = pruneWorkspaceNode(child, targetSessionId);
|
|
if (pruned) {
|
|
nextChildren.push(pruned);
|
|
nextSizes.push(sizeList[idx] ?? 1);
|
|
}
|
|
});
|
|
|
|
if (nextChildren.length === 0) return null;
|
|
if (nextChildren.length === 1) return nextChildren[0];
|
|
|
|
const total = nextSizes.reduce((acc, n) => acc + n, 0) || 1;
|
|
const normalized = nextSizes.map(n => n / total);
|
|
return { ...node, children: nextChildren, sizes: normalized };
|
|
};
|
|
|
|
const createSplitFromPane = (
|
|
existingPane: WorkspaceNode,
|
|
newPane: WorkspaceNode,
|
|
hint: SplitHint
|
|
): WorkspaceNode => {
|
|
const children = (hint.position === 'left' || hint.position === 'top') ? [newPane, existingPane] : [existingPane, newPane];
|
|
return {
|
|
id: crypto.randomUUID(),
|
|
type: 'split',
|
|
direction: hint.direction,
|
|
children,
|
|
sizes: [1, 1],
|
|
};
|
|
};
|
|
|
|
export const insertPaneIntoWorkspace = (
|
|
root: WorkspaceNode,
|
|
sessionId: string,
|
|
hint: SplitHint
|
|
): WorkspaceNode => {
|
|
const pane: WorkspaceNode = { id: crypto.randomUUID(), type: 'pane', sessionId };
|
|
|
|
if (!hint.targetSessionId) {
|
|
const children = (hint.position === 'left' || hint.position === 'top') ? [pane, root] : [root, pane];
|
|
return {
|
|
id: crypto.randomUUID(),
|
|
type: 'split',
|
|
direction: hint.direction,
|
|
children,
|
|
sizes: [1, 1],
|
|
};
|
|
}
|
|
|
|
const insertPane = (node: WorkspaceNode): WorkspaceNode => {
|
|
if (node.type === 'pane' && node.sessionId === hint.targetSessionId) {
|
|
return createSplitFromPane(node, pane, hint);
|
|
}
|
|
if (node.type === 'split') {
|
|
return { ...node, children: node.children.map(child => insertPane(child)) };
|
|
}
|
|
return node;
|
|
};
|
|
|
|
return insertPane(root);
|
|
};
|
|
|
|
export const createWorkspaceFromSessions = (
|
|
baseSessionId: string,
|
|
joiningSessionId: string,
|
|
hint: SplitHint
|
|
): Workspace => {
|
|
const basePane: WorkspaceNode = { id: crypto.randomUUID(), type: 'pane', sessionId: baseSessionId };
|
|
const newPane: WorkspaceNode = { id: crypto.randomUUID(), type: 'pane', sessionId: joiningSessionId };
|
|
const children = (hint.position === 'left' || hint.position === 'top') ? [newPane, basePane] : [basePane, newPane];
|
|
|
|
return {
|
|
id: `ws-${crypto.randomUUID()}`,
|
|
title: 'Workspace',
|
|
root: {
|
|
id: crypto.randomUUID(),
|
|
type: 'split',
|
|
direction: hint.direction,
|
|
children,
|
|
sizes: [1, 1],
|
|
},
|
|
};
|
|
};
|
|
|
|
export const updateWorkspaceSplitSizes = (
|
|
root: WorkspaceNode,
|
|
splitId: string,
|
|
sizes: number[]
|
|
): WorkspaceNode => {
|
|
const patch = (node: WorkspaceNode): WorkspaceNode => {
|
|
if (node.type === 'split') {
|
|
if (node.id === splitId) {
|
|
return { ...node, sizes };
|
|
}
|
|
return { ...node, children: node.children.map(child => patch(child)) };
|
|
}
|
|
return node;
|
|
};
|
|
return patch(root);
|
|
};
|