File size: 5,465 Bytes
66f0620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';

export function FileEditor({ projectName }) {
  const [files, setFiles] = useState([]);
  const [selectedFile, setSelectedFile] = useState(null);
  const [fileContent, setFileContent] = useState('');
  const [expandedDirs, setExpandedDirs] = useState(new Set());
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    if (projectName) {
      loadProjectTree();
    }
  }, [projectName]);

  const loadProjectTree = async () => {
    try {
      const response = await fetch(`http://localhost:8002/api/files/tree?project=${projectName}`);
      const data = await response.json();
      setFiles(data.tree || []);
    } catch (err) {
      console.error('Error loading file tree:', err);
    }
  };

  const loadFile = async (filePath) => {
    setLoading(true);
    try {
      const response = await fetch(
        `http://localhost:8002/api/files/read?project=${projectName}&path=${encodeURIComponent(filePath)}`
      );
      const data = await response.json();
      setFileContent(data.content || '');
      setSelectedFile(filePath);
    } catch (err) {
      console.error('Error loading file:', err);
      setFileContent(`Error loading file: ${err.message}`);
    } finally {
      setLoading(false);
    }
  };

  const saveFile = async () => {
    if (!selectedFile) return;
    try {
      await fetch(`http://localhost:8002/api/files/write`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          project: projectName,
          path: selectedFile,
          content: fileContent
        })
      });
      alert('File saved!');
    } catch (err) {
      alert('Error saving file: ' + err.message);
    }
  };

  const toggleDir = (path) => {
    const newExpanded = new Set(expandedDirs);
    if (newExpanded.has(path)) {
      newExpanded.delete(path);
    } else {
      newExpanded.add(path);
    }
    setExpandedDirs(newExpanded);
  };

  const FileTreeNode = ({ node, depth = 0 }) => (
    <div style={{ marginLeft: `${depth * 16}px` }}>
      <div
        onClick={() => {
          if (node.type === 'dir') {
            toggleDir(node.path);
          } else {
            loadFile(node.path);
          }
        }}
        style={{
          padding: '6px 8px',
          cursor: 'pointer',
          background: selectedFile === node.path ? 'rgba(59, 130, 246, 0.2)' : 'transparent',
          borderLeft: selectedFile === node.path ? '2px solid #3b82f6' : 'none',
          fontSize: '12px',
          userSelect: 'none',
          display: 'flex',
          alignItems: 'center',
          gap: '6px'
        }}
      >
        {node.type === 'dir' ? (
          expandedDirs.has(node.path) ? '▼' : '▶'
        ) : (
          '📄'
        )}
        <span>{node.name}</span>
      </div>
      {node.type === 'dir' && expandedDirs.has(node.path) && node.children && (
        <div>
          {node.children.map((child, i) => (
            <FileTreeNode key={i} node={child} depth={depth + 1} />
          ))}
        </div>
      )}
    </div>
  );

  return (
    <div style={{
      display: 'flex',
      height: '100%',
      background: '#0f172a',
      color: '#e2e8f0'
    }}>
      {/* File Tree */}
      <div style={{
        width: '250px',
        background: '#1e293b',
        borderRight: '1px solid #334155',
        overflowY: 'auto',
        padding: '12px'
      }}>
        <h3 style={{ fontSize: '12px', margin: '0 0 12px 0', color: '#94a3b8' }}>
          {projectName}
        </h3>
        {files.map((node, i) => (
          <FileTreeNode key={i} node={node} />
        ))}
      </div>

      {/* Editor */}
      <div style={{
        flex: 1,
        display: 'flex',
        flexDirection: 'column',
        background: '#0f172a'
      }}>
        {selectedFile ? (
          <>
            <div style={{
              background: '#1e293b',
              borderBottom: '1px solid #334155',
              padding: '12px',
              display: 'flex',
              justifyContent: 'space-between',
              alignItems: 'center'
            }}>
              <span style={{ fontSize: '12px' }}>{selectedFile}</span>
              <button
                onClick={saveFile}
                style={{
                  padding: '6px 12px',
                  background: '#22c55e',
                  color: 'white',
                  border: 'none',
                  borderRadius: '4px',
                  cursor: 'pointer',
                  fontSize: '12px'
                }}
              >
                💾 Save
              </button>
            </div>
            <textarea
              value={fileContent}
              onChange={(e) => setFileContent(e.target.value)}
              style={{
                flex: 1,
                background: '#1e293b',
                color: '#e2e8f0',
                border: 'none',
                padding: '16px',
                fontFamily: 'monospace',
                fontSize: '13px',
                outline: 'none',
                resize: 'none'
              }}
            />
          </>
        ) : (
          <div style={{
            flex: 1,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            color: '#64748b'
          }}>
            Select a file to view/edit
          </div>
        )}
      </div>
    </div>
  );
}