File size: 3,823 Bytes
c453128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from "react";
import type { FileNode, FilePreviewData } from "../types";
import { api } from "../api/client";

interface Props {
  nodes: FileNode[];
  onPreview: (data: FilePreviewData) => void;
}

const EXT_ICONS: Record<string, string> = {
  py: "🐍", ts: "πŸ“˜", tsx: "βš›οΈ", js: "πŸ“œ", jsx: "βš›οΈ",
  json: "πŸ“‹", yaml: "πŸ“‹", yml: "πŸ“‹", toml: "πŸ“‹",
  md: "πŸ“", mdx: "πŸ“", txt: "πŸ“„", pdf: "πŸ“•",
  png: "πŸ–ΌοΈ", jpg: "πŸ–ΌοΈ", jpeg: "πŸ–ΌοΈ", svg: "🎨",
  sh: "⚑", bash: "⚑", zsh: "⚑",
  rs: "πŸ¦€", go: "🐹", cpp: "βš™οΈ", c: "βš™οΈ",
  css: "🎨", scss: "🎨", html: "🌐",
  sql: "πŸ—ƒοΈ", db: "πŸ—ƒοΈ",
};

function extIcon(name: string): string {
  const ext = name.split(".").pop()?.toLowerCase() || "";
  return EXT_ICONS[ext] || "πŸ“„";
}

function FileItem({ node, depth, onPreview }: {
  node: FileNode;
  depth: number;
  onPreview: (d: FilePreviewData) => void;
}) {
  const [open, setOpen] = useState(depth < 1);
  const [loading, setLoading] = useState(false);

  async function handlePreview() {
    if (node.is_dir || !node.preview_type || node.preview_type === "none") return;
    // Images and PDFs: FilePreview renders them via URL β€” no JSON API call needed.
    if (node.preview_type === "image") {
      onPreview({ type: "image", path: node.path, name: node.name });
      return;
    }
    if (node.preview_type === "pdf") {
      onPreview({ type: "pdf", path: node.path, name: node.name });
      return;
    }
    setLoading(true);
    try {
      const data = await api.file.preview(node.path);
      onPreview(data);
    } catch (e) {
      console.error(e);
    } finally {
      setLoading(false);
    }
  }

  const indent = depth * 14;
  const canPreview = !node.is_dir && node.preview_type && node.preview_type !== "none";

  return (
    <div>
      <div
        style={{
          display: "flex",
          alignItems: "center",
          gap: 4,
          padding: "3px 8px 3px 0",
          paddingLeft: indent + 8,
          borderRadius: 4,
          cursor: node.is_dir ? "pointer" : canPreview ? "pointer" : "default",
          fontSize: 12,
          color: canPreview ? "var(--accent2)" : "var(--text-dim)",
          transition: "background 0.1s",
        }}
        onMouseEnter={(e) => {
          if (canPreview) (e.currentTarget as HTMLElement).style.background = "rgba(255,255,255,0.06)";
        }}
        onMouseLeave={(e) => {
          (e.currentTarget as HTMLElement).style.background = "transparent";
        }}
        onClick={node.is_dir ? () => setOpen(!open) : handlePreview}
      >
        <span style={{ fontSize: 10, opacity: 0.5, width: 10 }}>
          {node.is_dir ? (open ? "β–Ύ" : "β–Έ") : ""}
        </span>
        <span>{node.is_dir ? (open ? "πŸ“‚" : "πŸ“") : extIcon(node.name)}</span>
        <span style={{ marginLeft: 4, flex: 1 }}>{node.name}</span>
        {node.operation === "write" && !node.is_dir && (
          <span style={{ fontSize: 9, color: "var(--green)", opacity: 0.7 }}>✎</span>
        )}
        {loading && <span style={{ fontSize: 9 }}>…</span>}
      </div>
      {node.is_dir && open && node.children && (
        <div>
          {node.children.map((child) => (
            <FileItem key={child.path} node={child} depth={depth + 1} onPreview={onPreview} />
          ))}
        </div>
      )}
    </div>
  );
}

export function FileExplorer({ nodes, onPreview }: Props) {
  if (!nodes.length) {
    return (
      <div style={{ padding: "12px 8px", color: "var(--text-dim)", fontSize: 12 }}>
        No files touched in this session
      </div>
    );
  }

  return (
    <div style={{ padding: "4px 0" }}>
      {nodes.map((node) => (
        <FileItem key={node.path} node={node} depth={0} onPreview={onPreview} />
      ))}
    </div>
  );
}