/** * TeamFileRepo โ€” a per-team shared "File Repo" below the manager in each team column. * * Drop files or whole folders from the local machine onto it; the bytes are COPIED * into the team's server-side cache (~/.hermes/gui_team_repos//) and then * synced into every desk's workspace under team_files/, so every agent on the team * can read them. The user's original files on disk are never referenced directly. */ import { useCallback, useEffect, useRef, useState } from "react"; import type { FileNode, FilePreviewData } from "../types"; import { api } from "../api/client"; import type { SceneFloorChrome } from "../sceneFloorChrome"; interface Props { teamId: string; accentColor: string; chrome: SceneFloorChrome; onPreview: (data: FilePreviewData) => void; tileWidth?: number; tileMinHeight?: number; } interface GatheredFile { relPath: string; file: File; } /** Read a base64 data-URL for a File. */ function readDataUrl(file: File): Promise { return new Promise((res, rej) => { const fr = new FileReader(); fr.onload = (e) => res(e.target!.result as string); fr.onerror = () => rej(fr.error); fr.readAsDataURL(file); }); } /** Drain a directory reader (it returns entries in batches). */ function readAllEntries(reader: { readEntries: (cb: (e: FileSystemEntry[]) => void, err: (e: unknown) => void) => void }): Promise { return new Promise((resolve) => { const all: FileSystemEntry[] = []; const step = () => reader.readEntries((batch) => { if (!batch.length) { resolve(all); return; } all.push(...batch); step(); }, () => resolve(all)); step(); }); } /** Recursively collect files (with team-repo-relative paths) from a dropped entry. */ async function gatherEntry(entry: FileSystemEntry, prefix: string, out: GatheredFile[]): Promise { // eslint-disable-next-line @typescript-eslint/no-explicit-any const e = entry as any; if (entry.isFile) { const file: File = await new Promise((res, rej) => e.file(res, rej)); out.push({ relPath: prefix + entry.name, file }); } else if (entry.isDirectory) { const children = await readAllEntries(e.createReader()); for (const child of children) { await gatherEntry(child, `${prefix}${entry.name}/`, out); } } } function FileTree({ nodes, root, depth, onPreview, onDelete }: { nodes: FileNode[]; root: string; depth: number; onPreview: (n: FileNode) => void; onDelete: (n: FileNode) => void; }) { return ( <> {nodes.map((n) => { const rel = root && n.path.startsWith(root) ? n.path.slice(root.length).replace(/^\//, "") : n.name; return (
(e.currentTarget.style.background = "rgba(255,255,255,0.05)")} onMouseLeave={(e) => (e.currentTarget.style.background = "transparent")} > {n.is_dir ? "๐Ÿ“" : "๐Ÿ“„"} { if (!n.is_dir && n.preview_type) onPreview(n); }} title={n.is_dir ? rel : (n.preview_type ? "Preview" : rel)} style={{ flex: 1, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }} > {n.name}
{n.is_dir && n.children && n.children.length > 0 && ( )}
); })} ); } export function TeamFileRepo({ teamId, accentColor, chrome, onPreview, tileWidth = 108, tileMinHeight = 78, }: Props) { const [open, setOpen] = useState(false); const [files, setFiles] = useState([]); const [root, setRoot] = useState(""); const [dragOver, setDragOver] = useState(false); const [busy, setBusy] = useState(null); const [refreshing, setRefreshing] = useState(false); const panelRef = useRef(null); const refresh = useCallback(async () => { setRefreshing(true); try { const r = await api.teams.files(teamId); setFiles(r.files); setRoot(r.root); } catch { /* repo may not exist yet */ } finally { setRefreshing(false); } }, [teamId]); useEffect(() => { refresh(); }, [refresh]); // Re-list when the panel opens so agent writes into team_files/ show up. useEffect(() => { if (open) refresh(); }, [open]); // eslint-disable-line react-hooks/exhaustive-deps // Count only top-level entries for the badge. const count = files.length; async function handleDrop(e: React.DragEvent) { e.preventDefault(); e.stopPropagation(); setDragOver(false); const items = Array.from(e.dataTransfer.items || []); const gathered: GatheredFile[] = []; const entries = items .map((it) => (it.webkitGetAsEntry ? it.webkitGetAsEntry() : null)) .filter(Boolean) as FileSystemEntry[]; if (entries.length) { for (const entry of entries) await gatherEntry(entry, "", gathered); } else { // Fallback: flat file list (no directory support in this browser). for (const f of Array.from(e.dataTransfer.files)) gathered.push({ relPath: f.name, file: f }); } if (!gathered.length) return; setOpen(true); let done = 0; for (const g of gathered) { setBusy(`Copying ${++done}/${gathered.length}: ${g.relPath}`); try { const dataUrl = await readDataUrl(g.file); await api.teams.upload(teamId, g.relPath, dataUrl); } catch (err) { console.warn("team file upload failed:", g.relPath, err); } } setBusy(null); refresh(); } async function handleDelete(n: FileNode) { const rel = root && n.path.startsWith(root) ? n.path.slice(root.length).replace(/^\//, "") : n.name; if (!window.confirm(`Remove "${rel}" from the team file repo?`)) return; try { await api.teams.delete(teamId, rel); refresh(); } catch (err) { console.warn("team file delete failed:", err); } } async function handlePreview(n: FileNode) { try { const data = await api.file.preview(n.path); onPreview(data); } catch { /* unsupported type */ } } return (
{ e.preventDefault(); e.stopPropagation(); setDragOver(true); }} onDragLeave={(e) => { if (e.currentTarget === e.target) setDragOver(false); }} onDrop={handleDrop} style={{ position: "relative", width: tileWidth, flexShrink: 0, userSelect: "none", }} > {/* Filing-cabinet button */}
setOpen((o) => !o)} title="Team File Repo โ€” drop files or folders here to share with every agent on the team" style={{ display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: 3, minHeight: tileMinHeight, padding: "8px 6px", borderRadius: 8, cursor: "pointer", background: dragOver ? "rgba(100,160,255,0.16)" : chrome.controlBg, border: `1px ${dragOver ? "dashed" : "solid"} ${dragOver ? chrome.labelAccent : chrome.controlBorder}`, transition: "background 0.15s, border-color 0.15s", }} >
File Repo{count > 0 ? ` ยท ${count}` : ""}
{/* Expanded panel */} {open && (
e.stopPropagation()} style={{ position: "absolute", top: 0, left: tileWidth + 8, width: 268, maxHeight: 320, background: "var(--bg2)", border: "1px solid var(--card-border)", borderRadius: 8, boxShadow: "0 8px 28px rgba(0,0,0,0.5)", zIndex: 40, display: "flex", flexDirection: "column", overflow: "hidden", }} >
๐Ÿ“ Team File Repo
{files.length === 0 ? (
Drop files or folders here.
Shared with every agent on this team (copied into each desk's team_files/).
) : ( )}
{busy && (
{busy}
)}
)}
); }