|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import { useRouter } from 'next/router' |
| 3 | +import { connectWS } from '../../lib/ws' |
| 4 | +import Editor from '../../components/Editor' |
| 5 | + |
| 6 | +export default function CollectionPage() { |
| 7 | + const router = useRouter() |
| 8 | + const name = Array.isArray(router.query.name) ? router.query.name[0] : router.query.name || '' |
| 9 | + |
| 10 | + const [content, setContent] = useState('') |
| 11 | + const [status, setStatus] = useState<string | null>(null) |
| 12 | + const [wsLog, setWsLog] = useState<string[]>([]) |
| 13 | + const [ws, setWs] = useState<WebSocket | null>(null) |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + if (!name) return |
| 17 | + const backend = process.env.NEXT_PUBLIC_BACKEND_URL || process.env.BACKEND_URL || 'http://localhost:8080' |
| 18 | + fetch(`${backend}/api/collections/get?name=${encodeURIComponent(name)}`) |
| 19 | + .then((r) => r.json()) |
| 20 | + .then((data) => { |
| 21 | + setContent(JSON.stringify(data, null, 2)) |
| 22 | + }) |
| 23 | + .catch((e) => setStatus(String(e))) |
| 24 | + |
| 25 | + const socket = connectWS(name, (m) => setWsLog((s) => [...s, m])) |
| 26 | + setWs(socket) |
| 27 | + return () => { |
| 28 | + try { |
| 29 | + socket?.close() |
| 30 | + } catch {} |
| 31 | + } |
| 32 | + }, [name]) |
| 33 | + |
| 34 | + function save() { |
| 35 | + const backend = process.env.NEXT_PUBLIC_BACKEND_URL || process.env.BACKEND_URL || 'http://localhost:8080' |
| 36 | + fetch(`${backend}/api/collections/save`, { |
| 37 | + method: 'POST', |
| 38 | + headers: { 'Content-Type': 'application/json' }, |
| 39 | + body: JSON.stringify({ name, content }), |
| 40 | + }) |
| 41 | + .then((r) => r.json()) |
| 42 | + .then(() => setStatus('saved')) |
| 43 | + .catch((e) => setStatus(String(e))) |
| 44 | + } |
| 45 | + |
| 46 | + function run() { |
| 47 | + const backend = process.env.NEXT_PUBLIC_BACKEND_URL || process.env.BACKEND_URL || 'http://localhost:8080' |
| 48 | + setStatus('running') |
| 49 | + fetch(`${backend}/api/run`, { |
| 50 | + method: 'POST', |
| 51 | + headers: { 'Content-Type': 'application/json' }, |
| 52 | + body: JSON.stringify({ name }), |
| 53 | + }) |
| 54 | + .then((r) => r.json()) |
| 55 | + .then((data) => setStatus(JSON.stringify(data))) |
| 56 | + .catch((e) => setStatus(String(e))) |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <main style={{ padding: 24 }}> |
| 61 | + <h1>Collection: {name}</h1> |
| 62 | + <div style={{ marginBottom: 8 }}> |
| 63 | + <button onClick={save} style={{ marginRight: 8 }}>Save</button> |
| 64 | + <button onClick={run}>Run</button> |
| 65 | + </div> |
| 66 | + |
| 67 | + <Editor value={content} onChange={(v) => setContent(v ?? '')} height="60vh" /> |
| 68 | + |
| 69 | + <div style={{ marginTop: 12 }}> |
| 70 | + <strong>Status:</strong> {status} |
| 71 | + </div> |
| 72 | + |
| 73 | + <div style={{ marginTop: 12 }}> |
| 74 | + <strong>WS log:</strong> |
| 75 | + <div style={{ maxHeight: 200, overflow: 'auto', border: '1px solid #eee', padding: 8 }}> |
| 76 | + {wsLog.map((l, i) => ( |
| 77 | + <div key={i}>{l}</div> |
| 78 | + ))} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </main> |
| 82 | + ) |
| 83 | +} |
0 commit comments