Spaces:
Sleeping
Sleeping
File size: 28,677 Bytes
cc11e77 | 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | /**
* FileTreeSidebar.tsx — S611: file tree persistente nell'editor tab
*
* Sostituisce il VFS "cieco" (accessibile solo da More > File System VFS) con
* una sidebar sempre visibile che mostra la struttura del progetto in real-time.
*
* Desktop: sidebar collassabile a sinistra dell'editor (~180px).
* Mobile (iPhone): drawer a scomparsa dal basso, attivato da un FAB floating.
*
* Real-time: si aggiorna automaticamente via onVfsChanged ogni volta che
* l'agente scrive un file (write_file tool).
*
* S615: search input con debounce 250ms — quando attivo mostra lista flat
* dei file che corrispondono (path match), con highlight del termine trovato.
*/
import { useState, useEffect, useCallback, useRef, memo } from "react";
import { Z_INDEX } from "@/lib/zindex";
import {
Folder, FolderOpen, FileText, ChevronRight, ChevronDown,
PanelLeftClose, PanelLeftOpen, X, Search, FilePlus,
} from "lucide-react";
import { vfsAsync, onVfsChanged } from "@/lib/vfsDb";
import type { VFSFile } from "@/lib/vfsDb";
import { useWorkspaceStore } from "@/store/workspaceStore";
import { useUIStore } from "@/store/uiStore";
import { useLongPress } from "@/hooks/useLongPress"; // S800
import { MobileFileContextSheet } from "@/components/workspace/MobileFileContextSheet"; // S800
// ─── Extension → colour map (same as FileEditor) ─────────────────────────────
const EXT_COLORS: Record<string, string> = {
ts: "#3178c6", tsx: "#3178c6", js: "#f7df1e", jsx: "#61dafb",
py: "#3572a5", go: "#00add8", rs: "#dea584", json: "#ffa500",
md: "#83a598", css: "#563d7c", html: "#e34c26", sh: "#89d051",
txt: "#888", toml: "#9c4221", yaml: "#cc3e44", yml: "#cc3e44",
csv: "#3b82f6", xml: "#84cc16", sql: "#f472b6",
};
function fileColor(name: string): string {
const ext = name.split(".").pop()?.toLowerCase() ?? "";
return EXT_COLORS[ext] ?? "#9ca3af";
}
// ─── Tree node types ──────────────────────────────────────────────────────────
interface TreeNode {
name: string;
path: string;
isDir: boolean;
children: TreeNode[];
file?: VFSFile;
}
function buildTree(files: VFSFile[]): TreeNode[] {
const root: TreeNode[] = [];
const dirMap = new Map<string, TreeNode>();
const getOrCreateDir = (dirPath: string): TreeNode => {
if (dirMap.has(dirPath)) return dirMap.get(dirPath)!;
const parts = dirPath.split("/");
const name = parts.pop()!;
const parentPath = parts.join("/");
const node: TreeNode = { name, path: dirPath, isDir: true, children: [] };
dirMap.set(dirPath, node);
if (!parentPath) root.push(node);
else getOrCreateDir(parentPath).children.push(node);
return node;
};
const sorted = [...files].sort((a, b) => a.path.localeCompare(b.path));
for (const f of sorted) {
const parts = f.path.split("/");
if (parts.length === 1) {
root.push({ name: f.name, path: f.path, isDir: false, children: [], file: f });
} else {
parts.pop();
getOrCreateDir(parts.join("/")).children.push({
name: f.name, path: f.path, isDir: false, children: [], file: f,
});
}
}
const sort = (nodes: TreeNode[]) => {
nodes.sort((a, b) => {
if (a.isDir !== b.isDir) return a.isDir ? -1 : 1;
return a.name.localeCompare(b.name);
});
nodes.forEach(n => n.children.length && sort(n.children));
};
sort(root);
return root;
}
// ─── S615: debounce hook ──────────────────────────────────────────────────────
function useDebounce<T>(value: T, ms: number): T {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const t = setTimeout(() => setDebounced(value), ms);
return () => clearTimeout(t);
}, [value, ms]);
return debounced;
}
// ─── S615: highlight helper ───────────────────────────────────────────────────
function HighlightMatch({ text, query }: { text: string; query: string }) {
if (!query) return <span>{text}</span>;
const lc = text.toLowerCase();
const qi = lc.indexOf(query.toLowerCase());
if (qi === -1) return <span>{text}</span>;
return (
<span>
{text.slice(0, qi)}
<mark style={{ background: "rgba(251,191,36,0.30)", color: "#fbbf24", borderRadius: 3, padding: "0 1px" }}>
{text.slice(qi, qi + query.length)}
</mark>
{text.slice(qi + query.length)}
</span>
);
}
// ─── S615: flat search results ────────────────────────────────────────────────
function SearchResults({
files, query, activePath, onSelect, onLongPress,
}: {
files: VFSFile[];
query: string;
activePath: string | null;
onSelect: (f: VFSFile) => void;
onLongPress?: (f: VFSFile) => void;
}) {
const lq = query.toLowerCase();
const matches = files.filter(f =>
f.path.toLowerCase().includes(lq) || f.name.toLowerCase().includes(lq),
);
if (matches.length === 0) {
return (
<p style={{ fontSize: "0.66rem", color: "#3a3a6a", padding: "8px 10px", lineHeight: 1.4 }}>
Nessun file trovato per "{query}"
</p>
);
}
return (
<>
<p style={{ fontSize: "0.58rem", color: "#3a3a6a", padding: "4px 10px 2px", letterSpacing: "0.05em" }}>
{matches.length} risultat{matches.length === 1 ? "o" : "i"}
</p>
{matches.map(f => {
const isActive = activePath === f.path;
return (
<button
key={f.path}
onClick={() => onSelect(f)}
title={f.path}
style={{
display: "flex", alignItems: "center", gap: 4, width: "100%",
padding: "3px 8px",
background: isActive ? "rgba(59,130,246,0.15)" : "none",
border: "none", cursor: "pointer",
borderRadius: 6, margin: "0 2px",
color: isActive ? "#93c5fd" : "#8892b0",
fontSize: "0.70rem", textAlign: "left",
transition: "background 0.12s",
}}
onMouseEnter={e => { if (!isActive) e.currentTarget.style.background = "rgba(255,255,255,0.04)"; }}
onMouseLeave={e => { if (!isActive) e.currentTarget.style.background = "none"; }}
>
<FileText size={11} color={fileColor(f.name)} style={{ flexShrink: 0 }} />
<span style={{ flex: 1, minWidth: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
<HighlightMatch text={f.name} query={query} />
</span>
</button>
);
})}
</>
);
}
// ─── TreeRow ─────────────────────────────────────────────────────────────────
function TreeRow({
node, depth, activePath, onSelect, onLongPress,
}: {
node: TreeNode;
depth: number;
activePath: string | null;
onSelect: (f: VFSFile) => void;
/** S800: long-press su file leaf → context sheet mobile */
onLongPress?: (f: VFSFile) => void;
}) {
const [expanded, setExpanded] = useState(true);
const isActive = activePath === node.path;
const indent = 8 + depth * 12;
// S800: long-press detection per file (non cartelle)
const lp = useLongPress({
onLongPress: () => { if (node.file) onLongPress?.(node.file); },
enabled: !!onLongPress && !node.isDir,
});
if (node.isDir) {
return (
<>
<button
onClick={() => setExpanded(e => !e)}
style={{
display: "flex", alignItems: "center", gap: 4, width: "100%",
padding: `4px 6px 4px ${indent}px`,
background: "none", border: "none", cursor: "pointer",
color: "#8892b0", fontSize: "0.72rem", textAlign: "left",
}}
>
{expanded
? <ChevronDown size={10} color="#f59e0b" />
: <ChevronRight size={10} color="#f59e0b" />}
{expanded
? <FolderOpen size={12} color="#f59e0b" />
: <Folder size={12} color="#f59e0b" />}
<span style={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", flex: 1, fontWeight: 600 }}>
{node.name}
</span>
</button>
{expanded && node.children.map(child => (
<TreeRow key={child.path} node={child} depth={depth + 1}
activePath={activePath} onSelect={onSelect} onLongPress={onLongPress} />
))}
</>
);
}
return (
<button
onClick={() => node.file && onSelect(node.file)}
title={node.path}
{...(onLongPress ? lp : {})}
style={{
display: "flex", alignItems: "center", gap: 4, width: "100%",
padding: `3px 6px 3px ${indent}px`,
background: isActive ? "rgba(59,130,246,0.15)" : "none",
border: "none", cursor: "pointer",
borderRadius: 6, margin: "0 2px",
color: isActive ? "#93c5fd" : "#8892b0",
fontSize: "0.72rem", textAlign: "left",
transition: "background 0.12s",
touchAction: "manipulation",
WebkitTapHighlightColor: "transparent",
} as React.CSSProperties}
onMouseEnter={e => { if (!isActive) e.currentTarget.style.background = "rgba(255,255,255,0.04)"; }}
onMouseLeave={e => { if (!isActive) e.currentTarget.style.background = "none"; }}
>
<FileText size={11} color={fileColor(node.name)} style={{ flexShrink: 0 }} />
<span style={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", flex: 1 }}>
{node.name}
</span>
</button>
);
}
// ─── Props ────────────────────────────────────────────────────────────────────
interface FileTreeSidebarProps {
/** Called when user clicks a file — parent switches tab to editor */
onRequestOpen: (file: VFSFile) => void;
}
// ─── Desktop sidebar ──────────────────────────────────────────────────────────
export const FileTreeSidebar = memo(function FileTreeSidebar({ onRequestOpen }: FileTreeSidebarProps) {
const [files, setFiles] = useState<VFSFile[]>([]);
const [tree, setTree] = useState<TreeNode[]>([]);
// S615: search state
const [rawQuery, setRawQuery] = useState("");
const debouncedQuery = useDebounce(rawQuery, 250);
const searchInputRef = useRef<HTMLInputElement | null>(null);
// S620: nuovo file inline
const [newFileMode, setNewFileMode] = useState(false);
const [newFilePath, setNewFilePath] = useState("");
const newFileInputRef = useRef<HTMLInputElement | null>(null);
const openFile = useWorkspaceStore(s => s.openFile);
const showFileTree = useUIStore(s => s.showFileTree);
const toggleFileTree = useUIStore(s => s.toggleFileTree);
const reload = useCallback(async () => {
const all = await vfsAsync.list();
setFiles(all);
setTree(buildTree(all));
}, []);
useEffect(() => {
reload();
return onVfsChanged(reload);
}, [reload]);
// S723-GAP3.2: ascolta agent:vfs-update emesso da agentSSE quando il backend scrive file.
// reload() rilegge vfsAsync.list() — il workspace mostra file creati dall'agente in real-time.
useEffect(() => {
const _h = () => { void reload(); };
window.addEventListener("agent:vfs-update", _h);
return () => window.removeEventListener("agent:vfs-update", _h);
}, [reload]);
// S615: Ctrl+F / Cmd+F focalizza la search input quando la sidebar è aperta
// S619: Ctrl+B / Cmd+B toggle FileTreeSidebar (VS Code standard)
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.ctrlKey || e.metaKey) {
if (e.key === "b") {
e.preventDefault();
toggleFileTree();
return;
}
if (e.key === "f" && showFileTree && searchInputRef.current) {
const active = document.activeElement;
if (active?.tagName === "TEXTAREA" || (active as HTMLInputElement)?.dataset?.codemirror) return;
e.preventDefault();
searchInputRef.current.focus();
}
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [showFileTree, toggleFileTree]);
const clearSearch = () => { setRawQuery(""); searchInputRef.current?.focus(); };
const isSearching = debouncedQuery.trim().length > 0;
// S620: auto-focus sull'input "nuovo file" quando si entra in modalità di creazione
useEffect(() => {
if (newFileMode) {
setTimeout(() => newFileInputRef.current?.focus(), 60);
}
}, [newFileMode]);
// S620: crea il file nel VFS e aprilo nell'editor
const handleCreateFile = useCallback(async () => {
const p = newFilePath.trim();
if (!p) { setNewFileMode(false); setNewFilePath(""); return; }
const normPath = p.startsWith("/") ? p : "/" + p;
const exists = await vfsAsync.exists(normPath);
if (!exists) {
await vfsAsync.write(normPath, "", "text/plain");
}
setNewFileMode(false);
setNewFilePath("");
const _vfsFile = files.find(f => f.path === normPath) ?? {
name: normPath.split('/').pop() ?? normPath,
path: normPath, content: '', size: 0,
type: 'text/plain', createdAt: Date.now(), updatedAt: Date.now(),
};
onRequestOpen(_vfsFile);
}, [newFilePath, onRequestOpen]);
return (
<div style={{
width: showFileTree ? 180 : 28,
minWidth: showFileTree ? 180 : 28,
borderRight: "1px solid rgba(99,102,241,0.14)",
background: "rgba(4,5,16,0.96)",
display: "flex", flexDirection: "column",
overflow: "hidden",
transition: "width 0.22s cubic-bezier(0.4,0,0.2,1), min-width 0.22s cubic-bezier(0.4,0,0.2,1)",
flexShrink: 0,
}}>
{/* Header */}
<div style={{
display: "flex", alignItems: "center", justifyContent: "space-between",
padding: "6px 6px 6px 8px", borderBottom: "1px solid rgba(99,102,241,0.10)",
flexShrink: 0, minHeight: 32,
}}>
{showFileTree && (
<span style={{ fontSize: "0.62rem", fontWeight: 700, color: "#4a4a78",
textTransform: "uppercase", letterSpacing: "0.08em", whiteSpace: "nowrap", overflow: "hidden" }}>
FILE
{files.length > 0 && (
<span style={{ marginLeft: 5, color: "#3b82f6", fontWeight: 600 }}>
{files.length}
</span>
)}
</span>
)}
{/* S620: pulsanti header (FilePlus + toggle) */}
<div style={{ display: "flex", alignItems: "center", gap: 1, marginLeft: "auto" }}>
{showFileTree && (
<button
onClick={() => { setNewFilePath(""); setNewFileMode(true); }}
title="Nuovo file (S620)"
aria-label="Crea nuovo file"
style={{
background: "none", border: "none", cursor: "pointer",
color: "#4a4a78", padding: 3, borderRadius: 6, display: "flex",
transition: "color 0.15s",
}}
onMouseEnter={e => { e.currentTarget.style.color = "#34d399"; }}
onMouseLeave={e => { e.currentTarget.style.color = "#4a4a78"; }}
>
<FilePlus size={13} />
</button>
)}
<button
onClick={toggleFileTree}
title={showFileTree ? "Chiudi file tree (Ctrl+B)" : "Apri file tree (Ctrl+B)"}
style={{
background: "none", border: "none", cursor: "pointer",
color: "#4a4a78", padding: 3, borderRadius: 6, display: "flex",
transition: "color 0.15s",
}}
onMouseEnter={e => { e.currentTarget.style.color = "#818cf8"; }}
onMouseLeave={e => { e.currentTarget.style.color = "#4a4a78"; }}
>
{showFileTree
? <PanelLeftClose size={14} />
: <PanelLeftOpen size={14} />}
</button>
</div>
</div>
{/* S615: search input — solo visibile quando la sidebar è espansa */}
{showFileTree && (
<div style={{
padding: "5px 6px",
borderBottom: "1px solid rgba(99,102,241,0.08)",
flexShrink: 0,
}}>
<div style={{
display: "flex", alignItems: "center", gap: 4,
background: "rgba(255,255,255,0.04)",
border: `1px solid ${isSearching ? "rgba(129,140,248,0.40)" : "rgba(99,102,241,0.15)"}`,
borderRadius: 8, padding: "3px 6px",
transition: "border-color 0.15s",
}}>
<Search size={11} color={isSearching ? "#818cf8" : "#3a3a6a"} style={{ flexShrink: 0 }} />
<input
ref={searchInputRef}
value={rawQuery}
onChange={e => setRawQuery(e.target.value)}
onKeyDown={e => { if (e.key === "Escape") clearSearch(); }}
placeholder="Cerca file…"
aria-label="Cerca file nel VFS"
style={{
flex: 1, background: "none", border: "none", outline: "none",
color: "#c7d2fe", fontSize: "0.68rem",
caretColor: "#818cf8",
}}
/>
{rawQuery && (
<button
onClick={clearSearch}
aria-label="Cancella ricerca"
style={{
background: "none", border: "none", cursor: "pointer",
color: "#4a4a78", padding: 0, display: "flex",
transition: "color 0.15s",
}}
onMouseEnter={e => { e.currentTarget.style.color = "#ef4444"; }}
onMouseLeave={e => { e.currentTarget.style.color = "#4a4a78"; }}
>
<X size={10} />
</button>
)}
</div>
</div>
)}
{/* S620: input inline "nuovo file" — visibile solo in newFileMode */}
{showFileTree && newFileMode && (
<div style={{
padding: "5px 6px",
borderBottom: "1px solid rgba(52,211,153,0.20)",
flexShrink: 0,
background: "rgba(52,211,153,0.04)",
}}>
<div style={{
display: "flex", alignItems: "center", gap: 4,
border: "1px solid rgba(52,211,153,0.50)",
borderRadius: 8, padding: "3px 6px",
}}>
<FilePlus size={11} color="#34d399" style={{ flexShrink: 0 }} />
<input
ref={newFileInputRef}
value={newFilePath}
onChange={e => setNewFilePath(e.target.value)}
onKeyDown={e => {
if (e.key === "Enter") { e.preventDefault(); handleCreateFile(); }
if (e.key === "Escape") { setNewFileMode(false); setNewFilePath(""); }
}}
placeholder="src/nuovo.ts…"
aria-label="Path del nuovo file"
style={{
flex: 1, background: "none", border: "none", outline: "none",
color: "#6ee7b7", fontSize: "0.68rem",
caretColor: "#34d399",
}}
/>
<button
onClick={() => { setNewFileMode(false); setNewFilePath(""); }}
aria-label="Annulla nuovo file"
style={{
background: "none", border: "none", cursor: "pointer",
color: "#4a4a78", padding: 0, display: "flex",
transition: "color 0.15s",
}}
onMouseEnter={e => { e.currentTarget.style.color = "#ef4444"; }}
onMouseLeave={e => { e.currentTarget.style.color = "#4a4a78"; }}
>
<X size={10} />
</button>
</div>
</div>
)}
{/* Tree / Search results */}
{showFileTree && (
<div style={{ flex: 1, overflow: "auto", padding: "4px 0" }}>
{isSearching ? (
/* S615: risultati flat con highlight */
<SearchResults
files={files}
query={debouncedQuery.trim()}
activePath={openFile?.path ?? null}
onSelect={onRequestOpen}
/>
) : tree.length === 0 ? (
<p style={{ fontSize: "0.66rem", color: "#3a3a6a", padding: "8px 12px", lineHeight: 1.4 }}>
Nessun file nel VFS.{"\n"}L'agente creerà i file qui.
</p>
) : (
tree.map(node => (
<TreeRow
key={node.path}
node={node}
depth={0}
activePath={openFile?.path ?? null}
onSelect={onRequestOpen}
/>
))
)}
</div>
)}
</div>
);
});
// ─── Mobile drawer (bottom sheet) ────────────────────────────────────────────
export function MobileFileTreeDrawer({ onRequestOpen }: FileTreeSidebarProps) {
const [open, setOpen] = useState(false);
const [files, setFiles] = useState<VFSFile[]>([]);
const [tree, setTree] = useState<TreeNode[]>([]);
const [contextFile, setContextFile] = useState<VFSFile | null>(null); // S800: long-press context
// S615: search nel drawer mobile
const [rawQuery, setRawQuery] = useState("");
const debouncedQuery = useDebounce(rawQuery, 250);
const openFile = useWorkspaceStore(s => s.openFile);
const reload = useCallback(async () => {
const all = await vfsAsync.list();
setFiles(all);
setTree(buildTree(all));
}, []);
useEffect(() => {
reload();
return onVfsChanged(reload);
}, [reload]);
const isSearching = debouncedQuery.trim().length > 0;
return (
<>
{/* FAB button */}
<button
onClick={() => setOpen(v => !v)}
title="File tree"
style={{
position: "fixed", bottom: 72, right: 14,
zIndex: Z_INDEX.MODAL_BACKDROP,
width: 38, height: 38, borderRadius: "50%",
background: "rgba(99,102,241,0.85)",
border: "1px solid rgba(129,140,248,0.4)",
boxShadow: "0 4px 16px rgba(99,102,241,0.35)",
cursor: "pointer", color: "#fff",
display: "flex", alignItems: "center", justifyContent: "center",
transition: "transform 0.18s, background 0.18s",
}}
onMouseEnter={e => { e.currentTarget.style.transform = "scale(1.08)"; }}
onMouseLeave={e => { e.currentTarget.style.transform = "scale(1)"; }}
>
<Folder size={16} />
{files.length > 0 && (
<span style={{
position: "absolute", top: -4, right: -4,
background: "#3b82f6", color: "#fff",
fontSize: "0.55rem", fontWeight: 700,
borderRadius: "50%", width: 16, height: 16,
display: "flex", alignItems: "center", justifyContent: "center",
}}>
{files.length > 9 ? "9+" : files.length}
</span>
)}
</button>
{/* Backdrop */}
{open && (
<div
onClick={() => setOpen(false)}
style={{
position: "fixed", inset: 0, zIndex: Z_INDEX.MODAL,
background: "rgba(0,0,0,0.45)",
}}
/>
)}
{/* Bottom sheet */}
<div style={{
position: "fixed", left: 0, right: 0, bottom: 0,
zIndex: Z_INDEX.DRAWER_CONTENT,
height: "65vh",
background: "rgba(8,9,24,0.98)",
borderTop: "1px solid rgba(99,102,241,0.3)",
borderRadius: "18px 18px 0 0",
display: "flex", flexDirection: "column",
overflow: "hidden",
transform: open ? "translateY(0)" : "translateY(100%)",
transition: "transform 0.28s cubic-bezier(0.4,0,0.2,1)",
boxShadow: "0 -12px 40px rgba(0,0,0,0.5)",
paddingBottom: "max(8px, env(safe-area-inset-bottom, 0px))",
}}>
{/* Handle + header */}
<div style={{
display: "flex", alignItems: "center", justifyContent: "space-between",
padding: "10px 16px 8px",
borderBottom: "1px solid rgba(99,102,241,0.12)",
flexShrink: 0,
}}>
<div style={{ display: "flex", alignItems: "center", gap: 8 }}>
<div style={{
width: 36, height: 4, borderRadius: 3,
background: "rgba(255,255,255,0.15)",
position: "absolute", top: 6, left: "50%", transform: "translateX(-50%)",
}} />
<Folder size={14} color="#818cf8" />
<span style={{ fontSize: "0.78rem", fontWeight: 600, color: "#c7d2fe" }}>
File VFS
</span>
<span style={{ fontSize: "0.68rem", color: "#4a4a78" }}>
({files.length} file)
</span>
</div>
<button
onClick={() => setOpen(false)}
style={{
background: "none", border: "none", cursor: "pointer",
color: "#4a4a78", padding: 4, borderRadius: 4, display: "flex",
}}
>
<X size={16} />
</button>
</div>
{/* S615: search input mobile */}
<div style={{ padding: "8px 12px 6px", flexShrink: 0 }}>
<div style={{
display: "flex", alignItems: "center", gap: 6,
background: "rgba(255,255,255,0.05)",
border: `1px solid ${isSearching ? "rgba(129,140,248,0.4)" : "rgba(99,102,241,0.18)"}`,
borderRadius: 8, padding: "6px 10px",
transition: "border-color 0.15s",
}}>
<Search size={14} color={isSearching ? "#818cf8" : "#3a3a6a"} />
<input
value={rawQuery}
onChange={e => setRawQuery(e.target.value)}
onKeyDown={e => { if (e.key === "Escape") setRawQuery(""); }}
placeholder="Cerca file…"
autoComplete="off"
style={{
flex: 1, background: "none", border: "none", outline: "none",
color: "#c7d2fe", fontSize: "0.82rem",
caretColor: "#818cf8",
}}
/>
{rawQuery && (
<button
onClick={() => setRawQuery("")}
style={{ background: "none", border: "none", cursor: "pointer", color: "#4a4a78", padding: 0 }}
>
<X size={13} />
</button>
)}
</div>
</div>
{/* Tree / Search results */}
<div style={{ flex: 1, overflow: "auto", padding: "4px 0" }}>
{isSearching ? (
<SearchResults
files={files}
query={debouncedQuery.trim()}
activePath={openFile?.path ?? null}
onSelect={(f) => { onRequestOpen(f); setOpen(false); setRawQuery(""); }}
onLongPress={(f) => { setContextFile(f); }}
/>
) : tree.length === 0 ? (
<p style={{ fontSize: "0.72rem", color: "#3a3a6a", padding: "12px 16px", lineHeight: 1.5 }}>
Nessun file nel VFS.{"\n"}L'agente creerà i file qui.
</p>
) : (
tree.map(node => (
<TreeRow
key={node.path}
node={node}
depth={0}
activePath={openFile?.path ?? null}
onSelect={(f) => { onRequestOpen(f); setOpen(false); }}
onLongPress={(f) => { setContextFile(f); }}
/>
))
)}
</div>
</div>
{/* S800: context sheet long-press */}
<MobileFileContextSheet
file={contextFile}
onClose={() => setContextFile(null)}
onFileChanged={(oldPath, newPath) => {
// Se il file aperto era quello rinominato/eliminato, gestiscilo
if (openFile?.path === oldPath) {
onRequestOpen({ ...(openFile!), path: newPath ?? "", name: newPath?.split("/").pop() ?? "" } as VFSFile);
}
setContextFile(null);
}}
/>
</>
);
}
|