File size: 8,329 Bytes
ffe328b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import { Project } from '../types';
import { downloadSingleFile } from '../utils/exportZip';
import {
  FileCode,
  FileText,
  Plus,
  Trash2,
  Download,
  Copy,
  Check,
  FolderTree,
} from 'lucide-react';

interface CodeWorkspaceProps {
  project: Project;
  onUpdateFileContent: (fileId: string, newContent: string) => void;
  onAddFile: (name: string, content?: string) => void;
  onDeleteFile: (fileId: string) => void;
}

export const CodeWorkspace: React.FC<CodeWorkspaceProps> = ({
  project,
  onUpdateFileContent,
  onAddFile,
  onDeleteFile,
}) => {
  const [activeFileId, setActiveFileId] = useState<string>(
    project.activeFileId || project.files[0]?.id || ''
  );
  const [copied, setCopied] = useState(false);
  const [newFileName, setNewFileName] = useState('');
  const [isAddingFile, setIsAddingFile] = useState(false);

  const activeFile = project.files.find((f) => f.id === activeFileId) || project.files[0];

  const handleCopy = () => {
    if (!activeFile) return;
    navigator.clipboard.writeText(activeFile.content);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  const handleCreateFile = (e: React.FormEvent) => {
    e.preventDefault();
    if (!newFileName.trim()) return;
    onAddFile(newFileName.trim(), '// New file created in RiShre\n');
    setNewFileName('');
    setIsAddingFile(false);
  };

  const lineCount = activeFile?.content ? activeFile.content.split('\n').length : 0;
  const lineNumbers = Array.from({ length: Math.max(lineCount, 1) }, (_, i) => i + 1);

  return (
    <div className="flex h-full bg-[#070312] overflow-hidden select-none">
      {/* File Explorer Sidebar */}
      <div className="w-56 bg-[#0a0517] border-r border-purple-500/20 flex flex-col shrink-0">
        <div className="h-10 px-3 border-b border-purple-500/20 flex items-center justify-between text-xs text-purple-200 font-bold">
          <div className="flex items-center gap-1.5 font-mono">
            <FolderTree className="w-3.5 h-3.5 text-fuchsia-400" />
            <span>PROJECT FILES</span>
          </div>
          <button
            onClick={() => setIsAddingFile(true)}
            title="Create New File"
            className="p-1 rounded-lg hover:bg-[#180a33] text-purple-300 hover:text-white transition-colors cursor-pointer"
          >
            <Plus className="w-3.5 h-3.5" />
          </button>
        </div>

        {/* Add File Inline Input */}
        {isAddingFile && (
          <form onSubmit={handleCreateFile} className="p-2 border-b border-purple-500/20 bg-[#120826]/70">
            <input
              type="text"
              autoFocus
              placeholder="e.g. script.js"
              value={newFileName}
              onChange={(e) => setNewFileName(e.target.value)}
              className="w-full bg-[#080314] border border-purple-500/50 rounded-lg px-2.5 py-1 text-xs text-white focus:outline-none"
            />
            <div className="flex justify-end gap-1 mt-1.5">
              <button
                type="button"
                onClick={() => setIsAddingFile(false)}
                className="px-2 py-0.5 rounded text-[10px] text-purple-300/70 hover:text-white"
              >
                Cancel
              </button>
              <button
                type="submit"
                className="px-2.5 py-0.5 rounded text-[10px] bg-gradient-to-r from-purple-600 to-fuchsia-600 text-white font-bold"
              >
                Create
              </button>
            </div>
          </form>
        )}

        {/* File Tree List */}
        <div className="flex-1 overflow-y-auto p-2 space-y-0.5">
          {project.files.map((file) => {
            const isActive = file.id === activeFile?.id;
            return (
              <div
                key={file.id}
                onClick={() => setActiveFileId(file.id)}
                className={`group flex items-center justify-between px-2.5 py-1.5 rounded-xl text-xs font-mono transition-colors cursor-pointer ${
                  isActive
                    ? 'bg-purple-600/20 text-purple-100 font-bold border border-purple-500/40 shadow-[0_0_12px_rgba(168,85,247,0.2)]'
                    : 'text-purple-300/70 hover:text-purple-100 hover:bg-[#130829]'
                }`}
              >
                <div className="flex items-center gap-2 truncate">
                  <FileCode className={`w-3.5 h-3.5 shrink-0 ${isActive ? 'text-fuchsia-400' : 'text-purple-400/60'}`} />
                  <span className="truncate">{file.name}</span>
                </div>

                {project.files.length > 1 && (
                  <button
                    onClick={(e) => {
                      e.stopPropagation();
                      if (confirm(`Delete "${file.name}"?`)) {
                        onDeleteFile(file.id);
                      }
                    }}
                    title="Delete File"
                    className="opacity-0 group-hover:opacity-100 p-1 hover:text-rose-400 transition-opacity"
                  >
                    <Trash2 className="w-3 h-3" />
                  </button>
                )}
              </div>
            );
          })}
        </div>
      </div>

      {/* Code Editor Area */}
      <div className="flex-1 flex flex-col bg-[#05020c] overflow-hidden">
        {activeFile ? (
          <>
            {/* Tab & Editor Actions Header */}
            <div className="h-10 bg-[#0a0517] border-b border-purple-500/20 px-4 flex items-center justify-between shrink-0">
              <div className="flex items-center gap-2.5">
                <FileText className="w-3.5 h-3.5 text-fuchsia-400" />
                <span className="text-xs font-mono font-bold text-white">{activeFile.name}</span>
                <span className="text-[10px] font-mono px-2 py-0.5 rounded-full bg-purple-950/80 text-purple-300 border border-purple-500/30 uppercase">
                  {activeFile.language}
                </span>
                <span className="text-[11px] font-mono text-purple-400/60">{lineCount} lines</span>
              </div>

              <div className="flex items-center gap-1.5">
                <button
                  onClick={handleCopy}
                  className="flex items-center gap-1 px-2.5 py-1 rounded-lg bg-[#14082b] hover:bg-[#1e0c3d] border border-purple-500/30 text-xs text-purple-200 transition-colors cursor-pointer"
                >
                  {copied ? <Check className="w-3 h-3 text-emerald-400" /> : <Copy className="w-3 h-3 text-purple-300" />}
                  <span>{copied ? 'Copied' : 'Copy'}</span>
                </button>
                <button
                  onClick={() => downloadSingleFile(activeFile.name, activeFile.content)}
                  className="flex items-center gap-1 px-2.5 py-1 rounded-lg bg-[#14082b] hover:bg-[#1e0c3d] border border-purple-500/30 text-xs text-purple-200 transition-colors cursor-pointer"
                >
                  <Download className="w-3 h-3 text-purple-300" />
                  <span>Download</span>
                </button>
              </div>
            </div>

            {/* Editor Body with Gutter */}
            <div className="flex-1 flex overflow-hidden font-mono text-xs leading-relaxed">
              <div className="w-12 py-3 bg-[#080314] text-purple-400/40 text-right pr-3 select-none border-r border-purple-500/15 overflow-hidden font-mono shrink-0">
                {lineNumbers.map((num) => (
                  <div key={num} className="h-5">
                    {num}
                  </div>
                ))}
              </div>

              <textarea
                value={activeFile.content}
                onChange={(e) => onUpdateFileContent(activeFile.id, e.target.value)}
                spellCheck={false}
                className="flex-1 p-3 bg-transparent text-purple-100 resize-none focus:outline-none overflow-auto font-mono whitespace-pre leading-5"
                style={{ tabSize: 2 }}
              />
            </div>
          </>
        ) : (
          <div className="flex-1 flex items-center justify-center text-purple-400/40 font-mono text-xs">
            No active file selected.
          </div>
        )}
      </div>
    </div>
  );
};