File size: 8,641 Bytes
f91a684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useCallback, useRef } from 'react';
import Editor, { type OnMount } from '@monaco-editor/react';
import { Plus, RotateCcw, X, Download, Lock } from 'lucide-react';
import { LANGUAGE_OPTIONS, DEFAULT_CODE_BY_LANGUAGE } from './useCompiler.js';
import { useSubscription } from '@/contexts/SubscriptionContext';

function getLang(value: string) {
  return LANGUAGE_OPTIONS.find((l: any) => l.value === value) ?? LANGUAGE_OPTIONS[0];
}

export interface EditorFile {
  id: string;
  name: string;
  language: string;
  code: string;
}

interface CodeEditorPanelProps {
  files: EditorFile[];
  activeFileId: string;
  onSelectFile: (id: string) => void;
  onCloseFile: (id: string) => void;
  onAddFile: () => void;
  onCodeChange: (code: string) => void;
  onLanguageChange: (lang: string) => void;
  onResetCode: () => void;
  onCursorChange: (line: number, col: number) => void;
  onCtrlEnter: () => void;
}

export default function CodeEditorPanel({
  files,
  activeFileId,
  onSelectFile,
  onCloseFile,
  onAddFile,
  onCodeChange,
  onLanguageChange,
  onResetCode,
  onCursorChange,
  onCtrlEnter,
}: CodeEditorPanelProps) {
  const { tier } = useSubscription();
  // Free tier: lock C++ (value: 'cpp')
  const LOCKED_LANGUAGES = tier === 'free' ? new Set(['cpp']) : new Set<string>();
  const activeFile = files.find((f) => f.id === activeFileId) ?? files[0];
  const activeLang = getLang(activeFile.language);
  const editorRef = useRef<any>(null);

  const handleDownload = useCallback(() => {
    const blob = new Blob([activeFile.code], { type: 'text/plain' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = activeFile.name;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  }, [activeFile]);

  const handleMount: OnMount = useCallback(
    (editor, monaco) => {
      editorRef.current = editor;

      // Define RYP dark theme
      monaco.editor.defineTheme('ryp-dark', {
        base: 'vs-dark',
        inherit: true,
        rules: [
          { token: '', foreground: 'e2e2f0', background: '000000' },
          { token: 'keyword', foreground: 'c084fc' },
          { token: 'string', foreground: '86efac' },
          { token: 'number', foreground: 'fbbf24' },
          { token: 'comment', foreground: '55556a', fontStyle: 'italic' },
          { token: 'type', foreground: '67e8f9' },
          { token: 'function', foreground: '60a5fa' },
          { token: 'variable', foreground: 'e2e2f0' },
          { token: 'operator', foreground: 'a78bfa' },
        ],
        colors: {
          'editor.background': '#000000',
          'editor.foreground': '#e2e2f0',
          'editorLineNumber.foreground': '#333344',
          'editorLineNumber.activeForeground': '#8b5cf6',
          'editorCursor.foreground': '#8b5cf6',
          'editor.selectionBackground': '#8b5cf633',
          'editor.inactiveSelectionBackground': '#8b5cf611',
          'editor.lineHighlightBackground': '#111111',
          'editorGutter.background': '#000000',
          'editorWidget.background': '#111111',
          'editorWidget.border': '#222233',
          'editorBracketMatch.background': '#8b5cf633',
          'editorBracketMatch.border': '#8b5cf6',
        },
      });

      monaco.editor.setTheme('ryp-dark');

      // Track cursor position
      editor.onDidChangeCursorPosition((e: any) => {
        onCursorChange(e.position.lineNumber, e.position.column);
      });

      // Ctrl+Enter = run
      editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {
        onCtrlEnter();
      });

      // Tab inserts 4 spaces
      editor.getModel()?.updateOptions({ tabSize: 4, insertSpaces: true });
    },
    [onCursorChange, onCtrlEnter],
  );

  return (
    <div className="flex flex-col h-full" style={{ background: '#000000' }}>
      {/* Tabs bar */}
      <div className="ryp-editor-tabs">
        <div className="flex items-center gap-1 flex-1 min-w-0 overflow-x-auto">
          {files.map((file) => (
            <button
              key={file.id}
              onClick={() => onSelectFile(file.id)}
              className={`ryp-editor-tab ${file.id === activeFileId ? 'ryp-editor-tab--active' : ''}`}
            >
              <span className="ryp-editor-tab__dot" />
              <span className="truncate max-w-[120px]">{file.name}</span>
              {files.length > 1 && (
                <span
                  role="button"
                  tabIndex={0}
                  onClick={(e) => {
                    e.stopPropagation();
                    onCloseFile(file.id);
                  }}
                  onKeyDown={(e) => {
                    if (e.key === 'Enter' || e.key === ' ') {
                      e.preventDefault();
                      e.stopPropagation();
                      onCloseFile(file.id);
                    }
                  }}
                  className="rounded p-0.5 hover:bg-white/10 transition-colors"
                  style={{ color: '#55556a', cursor: 'pointer' }}
                >
                  <X size={12} />
                </span>
              )}
            </button>
          ))}
          <button
            onClick={onAddFile}
            className="ryp-editor-tab"
            style={{ padding: '4px 8px' }}
            title="New file"
          >
            <Plus size={14} />
          </button>
        </div>

        <div className="flex items-center gap-2 ml-2 flex-shrink-0">
          <select
            value={activeFile.language}
            onChange={(e) => onLanguageChange(e.target.value)}
            className="h-8 rounded-md px-2 text-xs font-bold outline-none transition"
            style={{
              background: '#111111',
              border: '1px solid #222233',
              color: '#e2e2f0',
              fontFamily: "'Inter', sans-serif",
            }}
          >
          {LANGUAGE_OPTIONS.map((l: any) => {
            const isLangLocked = LOCKED_LANGUAGES.has(l.value);
            return (
              <option key={l.value} value={l.value} disabled={isLangLocked}
                style={isLangLocked ? { color: '#55556a' } : {}}>
                {l.label}{isLangLocked ? ' 🔒' : ''}
              </option>
            );
          })}
          </select>
          <button
            onClick={handleDownload}
            title="Download code"
            className="flex items-center justify-center w-8 h-8 rounded-md transition-colors"
            style={{ background: '#111111', border: '1px solid #222233', color: '#9090b0', cursor: 'pointer' }}
            onMouseEnter={(e) => (e.currentTarget.style.color = '#e2e2f0')}
            onMouseLeave={(e) => (e.currentTarget.style.color = '#9090b0')}
          >
            <Download size={14} />
          </button>
          <button
            onClick={onResetCode}
            title="Reset code"
            className="flex items-center justify-center w-8 h-8 rounded-md transition-colors"
            style={{ background: '#111111', border: '1px solid #222233', color: '#9090b0', cursor: 'pointer' }}
            onMouseEnter={(e) => (e.currentTarget.style.color = '#e2e2f0')}
            onMouseLeave={(e) => (e.currentTarget.style.color = '#9090b0')}
          >
            <RotateCcw size={14} />
          </button>
        </div>
      </div>

      {/* Monaco editor */}
      <div className="flex-1 min-h-0">
        <Editor
          height="100%"
          language={activeLang.monacoLanguage}
          value={activeFile.code}
          theme="ryp-dark"
          onMount={handleMount}
          onChange={(v) => onCodeChange(v ?? '')}
          options={{
            minimap: { enabled: false },
            fontSize: 14,
            fontLigatures: true,
            fontFamily: "'JetBrains Mono', Consolas, monospace",
            smoothScrolling: true,
            cursorBlinking: 'smooth',
            cursorSmoothCaretAnimation: 'on',
            scrollBeyondLastLine: false,
            automaticLayout: true,
            padding: { top: 16, bottom: 16 },
            tabSize: 4,
            insertSpaces: true,
            renderWhitespace: 'none',
            bracketPairColorization: { enabled: true },
            lineNumbers: 'on',
            glyphMargin: false,
            folding: true,
            lineDecorationsWidth: 12,
            overviewRulerBorder: false,
            scrollbar: {
              verticalScrollbarSize: 6,
              horizontalScrollbarSize: 6,
            },
          }}
        />
      </div>
    </div>
  );
}