|
| 1 | +import { python } from '@codemirror/lang-python'; |
| 2 | +import { oneDark } from '@codemirror/theme-one-dark'; |
| 3 | +import CodeMirror from '@uiw/react-codemirror'; |
| 4 | +import { v4 as uuidv4 } from 'uuid'; |
| 5 | + |
| 6 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 7 | +import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd'; |
| 8 | + |
| 9 | +import { Alert } from '@/components/ui/alert'; |
| 10 | +import { Button } from '@/components/ui/button'; |
| 11 | + |
| 12 | +import { comm } from '@/utils/websocket'; |
| 13 | + |
| 14 | +import HTMLRenderer from '../common/HTMLRenderer'; |
| 15 | + |
| 16 | +const NotebookView = () => { |
| 17 | + // Each cell holds its id, code, output, status, error, and whether it’s been executed. |
| 18 | + const [cells, setCells] = useState([ |
| 19 | + { id: uuidv4(), code: '', output: '', status: 'idle', error: null, executed: false }, |
| 20 | + ]); |
| 21 | + |
| 22 | + // Subscribe to WebSocket messages to update cell outputs and restore state. |
| 23 | + useEffect(() => { |
| 24 | + const unsubscribe = comm.subscribe((message) => { |
| 25 | + if (message.type === 'notebook_state' && message.cells) { |
| 26 | + // On refresh, clear the executed flag on all cells. |
| 27 | + const resetCells = message.cells.map((cell) => ({ ...cell, executed: false })); |
| 28 | + setCells(resetCells); |
| 29 | + } |
| 30 | + if (message.type === 'cell_result' && message.cell_id) { |
| 31 | + setCells((prevCells) => { |
| 32 | + const updatedCells = prevCells.map((cell) => |
| 33 | + cell.id === message.cell_id |
| 34 | + ? { |
| 35 | + ...cell, |
| 36 | + output: message.output, |
| 37 | + status: message.error ? 'error' : 'idle', |
| 38 | + error: message.error || null, |
| 39 | + executed: true, |
| 40 | + } |
| 41 | + : cell |
| 42 | + ); |
| 43 | + // Optionally persist this updated state to the server if needed. |
| 44 | + comm.send({ type: 'update_notebook', cells: updatedCells }); |
| 45 | + return updatedCells; |
| 46 | + }); |
| 47 | + } |
| 48 | + }); |
| 49 | + return () => unsubscribe(); |
| 50 | + }, []); |
| 51 | + |
| 52 | + // Run a cell: send code via WebSocket and mark cell as running. |
| 53 | + const runCell = useCallback( |
| 54 | + (cellId) => { |
| 55 | + setCells((prevCells) => |
| 56 | + prevCells.map((cell) => |
| 57 | + cell.id === cellId |
| 58 | + ? { ...cell, status: 'running', output: '', error: null, executed: false } |
| 59 | + : cell |
| 60 | + ) |
| 61 | + ); |
| 62 | + const cell = cells.find((c) => c.id === cellId); |
| 63 | + if (cell) { |
| 64 | + comm.send({ type: 'run_cell', cell_id: cell.id, code: cell.code }); |
| 65 | + } |
| 66 | + }, |
| 67 | + [cells] |
| 68 | + ); |
| 69 | + |
| 70 | + // Update cell code and notify the server. |
| 71 | + const updateCellCode = (cellId, newCode) => { |
| 72 | + setCells((prevCells) => { |
| 73 | + const updatedCells = prevCells.map((cell) => |
| 74 | + cell.id === cellId ? { ...cell, code: newCode, executed: false } : cell |
| 75 | + ); |
| 76 | + comm.send({ type: 'update_notebook', cells: updatedCells }); |
| 77 | + return updatedCells; |
| 78 | + }); |
| 79 | + }; |
| 80 | + |
| 81 | + // Append a new cell. |
| 82 | + const addCell = () => { |
| 83 | + const newCell = { |
| 84 | + id: uuidv4(), |
| 85 | + code: '', |
| 86 | + output: '', |
| 87 | + status: 'idle', |
| 88 | + error: null, |
| 89 | + executed: false, |
| 90 | + }; |
| 91 | + setCells((prevCells) => { |
| 92 | + const updatedCells = [...prevCells, newCell]; |
| 93 | + comm.send({ type: 'update_notebook', cells: updatedCells }); |
| 94 | + return updatedCells; |
| 95 | + }); |
| 96 | + }; |
| 97 | + |
| 98 | + // Remove a cell. |
| 99 | + const deleteCell = (cellId) => { |
| 100 | + setCells((prevCells) => { |
| 101 | + const updatedCells = prevCells.filter((cell) => cell.id !== cellId); |
| 102 | + comm.send({ type: 'update_notebook', cells: updatedCells }); |
| 103 | + return updatedCells; |
| 104 | + }); |
| 105 | + }; |
| 106 | + |
| 107 | + // Handle drag-and-drop reordering. |
| 108 | + const onDragEnd = (result) => { |
| 109 | + if (!result.destination) return; |
| 110 | + const newCells = Array.from(cells); |
| 111 | + const [removed] = newCells.splice(result.source.index, 1); |
| 112 | + newCells.splice(result.destination.index, 0, removed); |
| 113 | + setCells(newCells); |
| 114 | + comm.send({ type: 'update_notebook', cells: newCells }); |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <div className="notebook-container max-w-4xl mx-auto p-6"> |
| 119 | + <h1 className="text-3xl font-bold mb-6 text-center">Interactive Notebook</h1> |
| 120 | + <DragDropContext onDragEnd={onDragEnd}> |
| 121 | + <Droppable droppableId="notebook-cells"> |
| 122 | + {(provided) => ( |
| 123 | + <div {...provided.droppableProps} ref={provided.innerRef}> |
| 124 | + {cells.map((cell, index) => ( |
| 125 | + <Draggable key={cell.id} draggableId={cell.id} index={index}> |
| 126 | + {(provided) => ( |
| 127 | + <div |
| 128 | + ref={provided.innerRef} |
| 129 | + {...provided.draggableProps} |
| 130 | + {...provided.dragHandleProps} |
| 131 | + className="notebook-cell bg-white rounded shadow p-4 mb-6 border" |
| 132 | + > |
| 133 | + <div className="flex items-center justify-between mb-2"> |
| 134 | + <div className="flex items-center"> |
| 135 | + <span className="text-sm font-medium text-gray-600"> |
| 136 | + Cell {index + 1} |
| 137 | + </span> |
| 138 | + {cell.executed && ( |
| 139 | + <span title="Cell executed" className="ml-2 text-green-500"> |
| 140 | + ✔ |
| 141 | + </span> |
| 142 | + )} |
| 143 | + </div> |
| 144 | + <div className="flex space-x-2"> |
| 145 | + <Button |
| 146 | + onClick={() => runCell(cell.id)} |
| 147 | + disabled={cell.status === 'running'} |
| 148 | + className="px-3 py-1 text-sm" |
| 149 | + > |
| 150 | + {cell.status === 'running' ? 'Running...' : 'Run'} |
| 151 | + </Button> |
| 152 | + <Button |
| 153 | + variant="destructive" |
| 154 | + onClick={() => deleteCell(cell.id)} |
| 155 | + className="px-3 py-1 text-sm" |
| 156 | + > |
| 157 | + Delete |
| 158 | + </Button> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + <div className="mb-4"> |
| 162 | + {/* CodeMirror-based Python editor */} |
| 163 | + <CodeMirror |
| 164 | + value={cell.code} |
| 165 | + height="auto" |
| 166 | + theme={oneDark} |
| 167 | + extensions={[python()]} |
| 168 | + onChange={(value) => updateCellCode(cell.id, value)} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + {(cell.status === 'running' || cell.status === 'error') && ( |
| 172 | + <div className="mt-2 text-sm text-gray-500"> |
| 173 | + {cell.status === 'error' ? 'Error occurred' : 'Executing cell...'} |
| 174 | + </div> |
| 175 | + )} |
| 176 | + {cell.output && ( |
| 177 | + <div className="mt-4"> |
| 178 | + <HTMLRenderer |
| 179 | + html={cell.output} |
| 180 | + className="p-3 border rounded bg-gray-50 text-sm" |
| 181 | + /> |
| 182 | + </div> |
| 183 | + )} |
| 184 | + {cell.error && ( |
| 185 | + <Alert variant="destructive" className="mt-4"> |
| 186 | + <span>{cell.error}</span> |
| 187 | + </Alert> |
| 188 | + )} |
| 189 | + </div> |
| 190 | + )} |
| 191 | + </Draggable> |
| 192 | + ))} |
| 193 | + {provided.placeholder} |
| 194 | + </div> |
| 195 | + )} |
| 196 | + </Droppable> |
| 197 | + </DragDropContext> |
| 198 | + <div className="flex justify-center"> |
| 199 | + <Button onClick={addCell} className="px-6 py-2 text-lg"> |
| 200 | + + Add Cell |
| 201 | + </Button> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + ); |
| 205 | +}; |
| 206 | + |
| 207 | +export default NotebookView; |
0 commit comments