-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSpaceBaseDirsModal.tsx
More file actions
201 lines (188 loc) · 7.01 KB
/
Copy pathSpaceBaseDirsModal.tsx
File metadata and controls
201 lines (188 loc) · 7.01 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { useCallback, useEffect, useState } from 'react';
import { open as openDialog } from '@tauri-apps/plugin-dialog';
import { FolderPlus, FolderOpen, Loader2, Trash2, X } from 'lucide-react';
import { Button, useToast, ToastContainer } from '@mcpmux/ui';
import {
addSpaceBaseDir,
listSpaceBaseDirs,
removeSpaceBaseDir,
type Space,
type SpaceBaseDir,
} from '@/lib/api/spaces';
/**
* Manage a Space's base directories.
*
* A base dir scopes any workspace root opened at or under it to this Space:
* an unmapped folder there falls back to this Space's Starter set, and the
* self-optimize meta-tools + mapping popup restrict to this Space. Longest
* match wins when base dirs nest across Spaces, and a folder can belong to
* only one Space.
*/
export function SpaceBaseDirsModal({
space,
onClose,
}: {
space: Space | null;
onClose: () => void;
}) {
const [dirs, setDirs] = useState<SpaceBaseDir[]>([]);
const [loading, setLoading] = useState(false);
const [busy, setBusy] = useState(false);
const { toasts, success, error: showError, dismiss } = useToast();
const spaceId = space?.id ?? null;
const load = useCallback(async () => {
if (!spaceId) return;
setLoading(true);
try {
setDirs(await listSpaceBaseDirs(spaceId));
} catch (e) {
showError('Could not load base directories', e instanceof Error ? e.message : String(e));
} finally {
setLoading(false);
}
}, [spaceId, showError]);
useEffect(() => {
void load();
}, [load]);
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') onClose();
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [onClose]);
const handleAdd = async () => {
if (!spaceId || busy) return;
let picked: string | string[] | null;
try {
picked = await openDialog({ directory: true, multiple: true, title: 'Add base directory' });
} catch {
return;
}
const paths = Array.isArray(picked) ? picked : picked ? [picked] : [];
if (paths.length === 0) return;
setBusy(true);
let added = 0;
for (const p of paths) {
try {
await addSpaceBaseDir(spaceId, p);
added++;
} catch (e) {
showError('Could not add folder', e instanceof Error ? e.message : String(e));
}
}
await load();
setBusy(false);
if (added > 0) {
success(
added === 1 ? 'Base directory added' : `${added} base directories added`,
'Folders here are now scoped to this space.'
);
}
};
const handleRemove = async (dir: SpaceBaseDir) => {
if (busy) return;
setBusy(true);
try {
await removeSpaceBaseDir(dir.id);
setDirs((prev) => prev.filter((d) => d.id !== dir.id));
} catch (e) {
showError('Could not remove folder', e instanceof Error ? e.message : String(e));
} finally {
setBusy(false);
}
};
if (!space) return null;
return (
<div
className="animate-fade-in fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4 backdrop-blur-sm"
onClick={onClose}
>
<div
className="animate-slide-up flex max-h-[80vh] w-full max-w-lg flex-col rounded-2xl border border-[rgb(var(--border))] bg-[rgb(var(--background))] shadow-2xl"
onClick={(e) => e.stopPropagation()}
data-testid="space-base-dirs-modal"
>
<div className="flex items-start justify-between border-b border-[rgb(var(--border-subtle))] p-5">
<div className="flex items-center gap-3">
<div className="flex h-10 w-10 items-center justify-center rounded-lg border border-[rgb(var(--border-subtle))] bg-[rgb(var(--surface))] text-xl">
{space.icon || '🌐'}
</div>
<div>
<h2 className="text-lg font-semibold">Base directories</h2>
<p className="text-xs text-[rgb(var(--muted))]">
Folders scoped to <span className="font-medium">{space.name}</span>
</p>
</div>
</div>
<button
onClick={onClose}
className="rounded-lg p-1.5 text-[rgb(var(--muted))] transition-colors hover:bg-[rgb(var(--surface))] hover:text-[rgb(var(--foreground))]"
aria-label="Close"
>
<X className="h-5 w-5" />
</button>
</div>
<div className="min-h-0 flex-1 overflow-y-auto p-5">
<p className="mb-4 text-sm text-[rgb(var(--muted))]">
Any folder you open here (or under it) is scoped to this space — it uses this
space's tools by default, and self-optimize only sees this space. The most specific
base directory wins, and a folder can belong to only one space.
</p>
{loading ? (
<div className="flex items-center justify-center py-10 text-[rgb(var(--muted))]">
<Loader2 className="h-5 w-5 animate-spin" />
</div>
) : dirs.length === 0 ? (
<div className="rounded-xl border border-dashed border-[rgb(var(--border))] px-4 py-8 text-center text-sm text-[rgb(var(--muted))]">
No base directories yet. Add one to scope its folders to this space.
</div>
) : (
<ul className="space-y-2" data-testid="space-base-dirs-list">
{dirs.map((dir) => (
<li
key={dir.id}
className="flex items-center gap-3 rounded-xl border border-[rgb(var(--border-subtle))] bg-[rgb(var(--surface))] px-3 py-2.5"
>
<FolderOpen className="text-primary-500 h-4 w-4 flex-shrink-0" />
<span
className="min-w-0 flex-1 truncate font-mono text-xs text-[rgb(var(--foreground))]"
title={dir.path}
>
{dir.path}
</span>
<button
onClick={() => handleRemove(dir)}
disabled={busy}
className="flex-shrink-0 rounded-lg p-1.5 text-[rgb(var(--muted))] transition-colors hover:bg-red-50 hover:text-red-500 disabled:opacity-50 dark:hover:bg-red-900/20"
title="Remove base directory"
data-testid={`remove-base-dir-${dir.id}`}
>
<Trash2 className="h-4 w-4" />
</button>
</li>
))}
</ul>
)}
</div>
<div className="border-t border-[rgb(var(--border-subtle))] p-5">
<Button
variant="primary"
className="w-full"
onClick={handleAdd}
disabled={busy}
data-testid="add-base-dir-btn"
>
{busy ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<FolderPlus className="mr-2 h-4 w-4" />
)}
Add folder…
</Button>
</div>
</div>
<ToastContainer toasts={toasts} onClose={dismiss} />
</div>
);
}