forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionTree.tsx
More file actions
173 lines (155 loc) · 6.98 KB
/
Copy pathCollectionTree.tsx
File metadata and controls
173 lines (155 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { useState, useEffect, useRef } from 'react';
import { Folder, Plus, ChevronRight, ChevronDown, Play, Trash2 } from 'lucide-react';
import { CollectionNode } from '../../types';
import { appStore } from '@/lib/store';
interface CollectionTreeProps {
collections: CollectionNode[];
filterQuery?: string;
activeTabId: string | null;
onToggleExpand: (nodeId: string) => void;
onSelectCollectionRequest: (node: CollectionNode) => void;
onCreateCollection: (name: string) => void;
}
const nodeMatchesQuery = (node: CollectionNode, query: string) => {
const searchableValues = [
node.name,
node.requestData?.name,
node.requestData?.rpcParams?.method
];
return searchableValues.some((value) =>
value?.toLowerCase().includes(query)
);
};
export const filterCollectionTree = (
nodes: CollectionNode[],
filterQuery: string
): CollectionNode[] => {
const query = filterQuery.trim().toLowerCase();
if (!query) {
return nodes;
}
return nodes.flatMap((node) => {
const selfMatches = nodeMatchesQuery(node, query);
const filteredChildren = filterCollectionTree(node.children ?? [], query);
if (!selfMatches && filteredChildren.length === 0) {
return [];
}
return [{
...node,
children: selfMatches ? node.children : filteredChildren
}];
});
};
export const CollectionTree: React.FC<CollectionTreeProps> = ({
collections,
filterQuery = '',
activeTabId,
onToggleExpand,
onSelectCollectionRequest,
onCreateCollection
}) => {
const isFiltering = filterQuery.trim().length > 0;
const visibleCollections = filterCollectionTree(collections, filterQuery);
const renderTree = (nodes: CollectionNode[], depth = 0) => {
return nodes.map((node, index) => {
const isLast = index === nodes.length - 1;
const isActive = node.type === 'request' && node.requestData?.id === activeTabId;
return (
<div key={node.id} className="relative select-none">
{depth > 0 && (
<div
className="absolute left-0 w-px bg-white/10"
style={{ left: `${(depth * 12) + 7}px`, top: '-4px', height: isLast ? '18px' : '100%' }}
/>
)}
<button
className={`
group flex items-center gap-2 px-2 py-1.5 cursor-pointer rounded-lg transition-all duration-200 text-left w-full
${node.type !== 'request' ? 'hover:bg-slate-100 dark:bg-white/5 text-slate-400 hover:text-slate-700 dark:text-slate-200' :
isActive ? 'bg-sui-900/20 text-sui-300 shadow-[inset_2px_0_0_0_#0ea5e9]' : 'hover:bg-slate-100 dark:bg-white/5 text-slate-400 hover:text-slate-700 dark:text-slate-200'}
`}
style={{ paddingLeft: `${depth * 12 + 4}px` }}
onClick={() => node.type !== 'request' ? onToggleExpand(node.id) : onSelectCollectionRequest(node)}
>
<div className="shrink-0 flex items-center justify-center w-4 h-4">
{node.type === 'collection' || node.type === 'folder' ? (
node.children && node.children.length > 0 ? (
node.isExpanded ? <ChevronDown size={10} className="text-slate-500 group-hover:text-slate-400" /> : <ChevronRight size={10} className="text-slate-500 group-hover:text-slate-400" />
) : <div className="w-1 h-1 rounded-full bg-slate-700" />
) : (
<div className={`w-1.5 h-1.5 rounded-full ${node.name.includes('RPC') ? 'bg-emerald-500 shadow-[0_0_5px_rgba(16,185,129,0.4)]' : 'bg-amber-500 shadow-[0_0_5px_rgba(245,158,11,0.4)]'}`} />
)}
</div>
{node.type === 'collection' && <Folder size={13} className={`${node.isShared ? 'text-blue-400' : 'text-amber-500/80'} fill-current opacity-90`} />}
{node.type === 'folder' && <Folder size={13} className="text-slate-600 fill-white/5" />}
<span className={`truncate flex-1 font-sans text-[11px] font-medium leading-none pt-0.5 ${isActive ? 'font-bold' : ''}`}>
{node.name}
</span>
{(node.type === 'collection' || node.type === 'folder') && (
<div className="opacity-0 group-hover:opacity-100 flex items-center gap-1">
{node.type === 'collection' && (
<button
onClick={(e) => { e.stopPropagation(); appStore.openTab('runner', { collectionId: node.id }); }}
className="p-1 hover:bg-slate-100 dark:hover:bg-white/10 rounded text-slate-500 hover:text-green-400 transition-colors"
title="Run Collection"
>
<Play size={10} />
</button>
)}
<button
onClick={(e) => { e.stopPropagation(); appStore.openTab('new_request', { collectionId: node.id }); }}
className="p-1 hover:bg-slate-100 dark:hover:bg-white/10 rounded text-slate-500 hover:text-slate-900 dark:hover:text-slate-900 dark:text-white transition-colors"
title="Add Request"
>
<Plus size={10} />
</button>
{node.type === 'collection' && (
<button
onClick={(e) => {
// Confirm before deleting, since this permanently
// removes the collection and all of its requests.
e.stopPropagation();
if (window.confirm(`Delete collection "${node.name}"? This cannot be undone.`)) {
appStore.deleteCollection(node.id);
}
}}
className="p-1 hover:bg-slate-100 dark:hover:bg-white/10 rounded text-slate-500 hover:text-red-400 transition-colors"
title="Delete Collection"
>
<Trash2 size={10} />
</button>
)}
</div>
)}
</button>
{(isFiltering || node.isExpanded) && node.children && (
<div className="relative animate-in slide-in-from-left-1 duration-200">
{renderTree(node.children, depth + 1)}
</div>
)}
</div>
);
});
};
return (
<div className="flex-1 pb-4 px-2">
<button
type="button"
onClick={() => appStore.openTab('new_collection')}
className="w-full flex items-center gap-1.5 px-2 py-1 mb-1 text-[11px] text-slate-500 hover:text-slate-700 dark:text-slate-200 hover:bg-slate-100 dark:bg-white/5 rounded-lg transition-colors"
>
<Plus size={11} />
<span>New Collection</span>
</button>
<div className="pl-1 space-y-0.5">
{visibleCollections.length > 0 ? (
renderTree(visibleCollections)
) : isFiltering ? (
<p className="px-3 py-6 text-center text-[11px] text-slate-600">
No collections or requests match “{filterQuery.trim()}”.
</p>
) : null}
</div>
</div>
);
};