Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 8 additions & 172 deletions packages/desktop/src/features/explorer/tree.ts
Original file line number Diff line number Diff line change
@@ -1,172 +1,8 @@
import type {
ExplorerExpandedState,
ExplorerFileEntry,
ExplorerFlatNode,
ExplorerNode
} from './types';
import { normalizeRelativePath } from './utils/path';

type MutableNode = ExplorerNode & {
childMap?: Map<string, MutableNode>;
};

function compareNodes(a: ExplorerNode, b: ExplorerNode): number {
if (a.isDir && !b.isDir) return -1;
if (!a.isDir && b.isDir) return 1;
return a.name.localeCompare(b.name, undefined, {
sensitivity: 'base',
numeric: true
});
}

function toExplorerNodes(map: Map<string, MutableNode>): ExplorerNode[] {
return Array.from(map.values())
.map((node): ExplorerNode => {
const children = node.childMap ? toExplorerNodes(node.childMap) : undefined;
return {
name: node.name,
path: node.path,
isDir: node.isDir,
depth: node.depth,
children,
requestCount: node.requestCount
};
})
.sort(compareNodes);
}

export function buildExplorerTree(files: ExplorerFileEntry[]): ExplorerNode[] {
const rootMap = new Map<string, MutableNode>();

for (const file of files) {
const normalizedPath = normalizeRelativePath(file.path);
if (!normalizedPath) continue;

const parts = normalizedPath.split('/');
let currentPath = '';
let cursorMap = rootMap;

for (let index = 0; index < parts.length; index += 1) {
const part = parts[index];
if (!part) continue;

currentPath = currentPath ? `${currentPath}/${part}` : part;
const isDir = index < parts.length - 1;
let node = cursorMap.get(part);

if (!node) {
node = {
name: part,
path: currentPath,
isDir,
depth: index,
children: isDir ? [] : undefined,
requestCount: isDir ? undefined : file.requestCount,
childMap: isDir ? new Map<string, MutableNode>() : undefined
};
cursorMap.set(part, node);
} else {
if (isDir && !node.isDir) {
node.isDir = true;
node.requestCount = undefined;
node.children = [];
node.childMap = new Map<string, MutableNode>();
}

if (!isDir) {
node.requestCount = file.requestCount;
}
}

if (isDir) {
if (!node.childMap) {
node.childMap = new Map<string, MutableNode>();
}
if (!node.children) {
node.children = [];
}
cursorMap = node.childMap;
}
}
}

return toExplorerNodes(rootMap);
}

export function flattenExplorerTree(
nodes: ExplorerNode[],
expandedDirs: ExplorerExpandedState
): ExplorerFlatNode[] {
const flattened: ExplorerFlatNode[] = [];

const visit = (node: ExplorerNode) => {
const isExpanded = Boolean(expandedDirs[node.path]);
flattened.push({
node,
isExpanded
});

if (!node.isDir || !isExpanded || !node.children) {
return;
}

for (const child of node.children) {
visit(child);
}
};

for (const node of nodes) {
visit(node);
}

return flattened;
}

export function createInitialExpandedDirs(nodes: ExplorerNode[]): ExplorerExpandedState {
const initial: ExplorerExpandedState = {};

for (const node of nodes) {
if (node.isDir) {
initial[node.path] = true;
}
}

return initial;
}

export function findExplorerNode(nodes: ExplorerNode[], path: string): ExplorerNode | undefined {
for (const node of nodes) {
if (node.path === path) {
return node;
}

if (node.children) {
const child = findExplorerNode(node.children, path);
if (child) {
return child;
}
}
}

return undefined;
}

export function hasExplorerPath(nodes: ExplorerNode[], path: string): boolean {
return Boolean(findExplorerNode(nodes, path));
}

export function pruneExpandedDirs(
expandedDirs: ExplorerExpandedState,
nodes: ExplorerNode[]
): ExplorerExpandedState {
const pruned: ExplorerExpandedState = {};

for (const [path, isExpanded] of Object.entries(expandedDirs)) {
const node = findExplorerNode(nodes, path);
if (node?.isDir) {
pruned[path] = Boolean(isExpanded);
}
}

return pruned;
}
export {
buildExplorerTree,
createInitialExpandedDirs,
findExplorerNode,
flattenExplorerTree,
hasExplorerPath,
pruneExpandedDirs
} from '@t-req/ui/explorer';
26 changes: 6 additions & 20 deletions packages/desktop/src/features/explorer/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
export type ExplorerExpandedState = Record<string, boolean>;

export interface ExplorerFileEntry {
path: string;
requestCount?: number;
}

export interface ExplorerNode {
name: string;
path: string;
isDir: boolean;
depth: number;
children?: ExplorerNode[];
requestCount?: number;
}

export interface ExplorerFlatNode {
node: ExplorerNode;
isExpanded: boolean;
}
export type {
ExplorerExpandedState,
ExplorerFileEntry,
ExplorerFlatNode,
ExplorerNode
} from '@t-req/ui/explorer';

export interface ExplorerFileDocument {
path: string;
Expand Down
48 changes: 7 additions & 41 deletions packages/desktop/src/features/explorer/utils/request-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
type RequestSummary = {
index: number;
name?: string;
method: string;
url: string;
protocol?: 'http' | 'sse' | 'ws';
};

export type RequestOption = {
index: number;
label: string;
protocol?: 'http' | 'sse' | 'ws';
};

export function isHttpProtocol(protocol: string | undefined): boolean {
return protocol === undefined || protocol === 'http';
}

export function toRequestOptionLabel(request: RequestSummary): string {
const prefix = `${request.index + 1}.`;
if (request.name) {
return `${prefix} ${request.name}`;
}
return `${prefix} ${request.method.toUpperCase()} ${request.url}`;
}

export function toRequestOption(request: RequestSummary): RequestOption {
return {
index: request.index,
label: toRequestOptionLabel(request),
protocol: request.protocol
};
}

export function toRequestIndex(value: string): number | undefined {
const parsed = Number.parseInt(value, 10);
if (Number.isNaN(parsed)) {
return undefined;
}
return parsed;
}
export type { RequestOption } from '@t-req/ui/explorer';
export {
isHttpProtocol,
toRequestIndex,
toRequestOption,
toRequestOptionLabel
} from '@t-req/ui/explorer';
20 changes: 1 addition & 19 deletions packages/desktop/src/features/explorer/workspace-files.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
import type { ExplorerFileEntry } from './types';

const HTTP_FILE_EXTENSION = '.http';

type WorkspaceFileLike = {
path: string;
requestCount?: number;
};

function isHttpWorkspaceFile(file: WorkspaceFileLike): boolean {
return file.path.toLowerCase().endsWith(HTTP_FILE_EXTENSION);
}

export function toExplorerFiles(files: WorkspaceFileLike[]): ExplorerFileEntry[] {
return files.filter(isHttpWorkspaceFile).map((file) => ({
path: file.path,
requestCount: file.requestCount
}));
}
export { toExplorerFiles } from '@t-req/ui/explorer';
4 changes: 4 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"types": "./src/index.ts",
"import": "./src/index.ts"
},
"./explorer": {
"types": "./src/explorer/index.ts",
"import": "./src/explorer/index.ts"
},
"./styles": {
"types": "./src/styles/styles.d.ts",
"default": "./src/styles/base.css"
Expand Down
22 changes: 22 additions & 0 deletions packages/ui/src/explorer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type { RequestOption } from './request-workspace.js';
export {
isHttpProtocol,
toRequestIndex,
toRequestOption,
toRequestOptionLabel
} from './request-workspace.js';
export {
buildExplorerTree,
createInitialExpandedDirs,
findExplorerNode,
flattenExplorerTree,
hasExplorerPath,
pruneExpandedDirs
} from './tree.js';
export type {
ExplorerExpandedState,
ExplorerFileEntry,
ExplorerFlatNode,
ExplorerNode
} from './types.js';
export { toExplorerFiles } from './workspace-files.js';
41 changes: 41 additions & 0 deletions packages/ui/src/explorer/request-workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type RequestSummary = {
index: number;
name?: string;
method: string;
url: string;
protocol?: 'http' | 'sse' | 'ws';
};

export type RequestOption = {
index: number;
label: string;
protocol?: 'http' | 'sse' | 'ws';
};

export function isHttpProtocol(protocol: string | undefined): boolean {
return protocol === undefined || protocol === 'http';
}

export function toRequestOptionLabel(request: RequestSummary): string {
const prefix = `${request.index + 1}.`;
if (request.name) {
return `${prefix} ${request.name}`;
}
return `${prefix} ${request.method.toUpperCase()} ${request.url}`;
}

export function toRequestOption(request: RequestSummary): RequestOption {
return {
index: request.index,
label: toRequestOptionLabel(request),
protocol: request.protocol
};
}

export function toRequestIndex(value: string): number | undefined {
const parsed = Number.parseInt(value, 10);
if (Number.isNaN(parsed)) {
return undefined;
}
return parsed;
}
Loading