|
1 | | -import React from 'react'; |
2 | | -import './CodeEditor.css'; |
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import Editor from '@monaco-editor/react'; |
3 | 3 | import { FaPlay } from 'react-icons/fa'; |
| 4 | +import './CodeEditor.css'; |
4 | 5 |
|
5 | | -const currentUser = { |
6 | | - name: '김코딩', |
7 | | - role: 'host', |
8 | | - code: `# 버블 정렬 구현 |
9 | | -def bubble_sort(arr): |
10 | | - n = len(arr) |
11 | | -
|
12 | | - for i in range(n): |
13 | | - for j in range(0, n - i - 1): |
14 | | - if arr[j] > arr[j + 1]: |
15 | | - # 두 요소 교환 |
16 | | - arr[j], arr[j + 1] = arr[j + 1], arr[j] |
17 | | -
|
18 | | - return arr |
19 | | -
|
20 | | -# 예제 배열 |
21 | | -array = [64, 34, 25, 12, 22, 11, 90] |
22 | | -print("정렬 전:", array) |
23 | | -print("정렬 후:", bubble_sort(array.copy()))` |
24 | | -}; |
| 6 | +const CodeEditor = ({ currentUser }) => { |
| 7 | + const [code, setCode] = useState(currentUser.code); |
25 | 8 |
|
26 | | -const getIcon = (role) => { |
27 | | - switch (role) { |
28 | | - case 'host': |
29 | | - return '👑'; |
30 | | - case 'editing': |
31 | | - return '✍️'; |
32 | | - default: |
33 | | - return ''; |
34 | | - } |
35 | | -}; |
| 9 | + // ✅ currentUser가 바뀔 때마다 코드 상태를 새로 설정 |
| 10 | + useEffect(() => { |
| 11 | + setCode(currentUser.code); |
| 12 | + }, [currentUser]); |
36 | 13 |
|
37 | | -const CodeEditor = () => { |
38 | 14 | const handleRun = () => { |
39 | 15 | alert("코드 실행 기능은 아직 미구현입니다."); |
40 | 16 | }; |
41 | 17 |
|
| 18 | + const getIcon = (role) => { |
| 19 | + switch (role) { |
| 20 | + case 'host': |
| 21 | + return '👑'; |
| 22 | + case 'editing': |
| 23 | + return '✍️'; |
| 24 | + default: |
| 25 | + return ''; |
| 26 | + } |
| 27 | + }; |
| 28 | + |
42 | 29 | return ( |
43 | 30 | <section className="code-editor"> |
44 | 31 | <div className="editor-header"> |
45 | 32 | <div className="current-user-badge"> |
46 | 33 | {getIcon(currentUser.role)} {currentUser.name} |
47 | 34 | </div> |
48 | 35 | <button className="run-button" onClick={handleRun}> |
49 | | - <FaPlay/> 실행 |
| 36 | + <FaPlay /> 실행 |
50 | 37 | </button> |
51 | 38 | </div> |
52 | | - <pre className="code-block"> |
53 | | - <code>{currentUser.code}</code> |
54 | | - </pre> |
| 39 | + |
| 40 | + <Editor |
| 41 | + height="calc(100vh - 160px)" // 헤더와 버튼 고려 |
| 42 | + defaultLanguage="python" |
| 43 | + theme="vs-dark" |
| 44 | + value={code} |
| 45 | + onChange={(newValue) => setCode(newValue)} |
| 46 | + options={{ |
| 47 | + fontSize: 14, |
| 48 | + minimap: { enabled: false }, |
| 49 | + padding: { top: 10 }, |
| 50 | + }} |
| 51 | + /> |
55 | 52 | </section> |
56 | 53 | ); |
57 | 54 | }; |
|
0 commit comments