|
| 1 | +import { useCallback, useState, useEffect } from 'react' |
| 2 | +import { createFileRoute } from '@tanstack/react-router' |
| 3 | + |
| 4 | +type Todo = { |
| 5 | + id: number |
| 6 | + title: string |
| 7 | +} |
| 8 | + |
| 9 | +export const Route = createFileRoute('/demo/mcp-todos')({ |
| 10 | + component: ORPCTodos, |
| 11 | +}) |
| 12 | + |
| 13 | +function ORPCTodos() { |
| 14 | + const [todos, setTodos] = useState<Todo[]>([]) |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const eventSource = new EventSource('/api/mcp-todos') |
| 18 | + eventSource.onmessage = (event) => { |
| 19 | + setTodos(JSON.parse(event.data)) |
| 20 | + } |
| 21 | + return () => eventSource.close() |
| 22 | + }, []) |
| 23 | + |
| 24 | + const [todo, setTodo] = useState('') |
| 25 | + |
| 26 | + const submitTodo = useCallback(async () => { |
| 27 | + await fetch('/api/mcp-todos', { |
| 28 | + method: 'POST', |
| 29 | + body: JSON.stringify({ title: todo }), |
| 30 | + }) |
| 31 | + setTodo('') |
| 32 | + }, [todo]) |
| 33 | + |
| 34 | + return ( |
| 35 | + <div |
| 36 | + className="flex items-center justify-center min-h-screen bg-gradient-to-br from-teal-200 to-emerald-900 p-4 text-white" |
| 37 | + style={{ |
| 38 | + backgroundImage: |
| 39 | + 'radial-gradient(70% 70% at 20% 20%, #07A798 0%, #045C4B 60%, #01251F 100%)', |
| 40 | + }} |
| 41 | + > |
| 42 | + <div className="w-full max-w-2xl p-8 rounded-xl backdrop-blur-md bg-black/50 shadow-xl border-8 border-black/10"> |
| 43 | + <h1 className="text-2xl mb-4">MCP Todos list</h1> |
| 44 | + <ul className="mb-4 space-y-2"> |
| 45 | + {todos?.map((t) => ( |
| 46 | + <li |
| 47 | + key={t.id} |
| 48 | + className="bg-white/10 border border-white/20 rounded-lg p-3 backdrop-blur-sm shadow-md" |
| 49 | + > |
| 50 | + <span className="text-lg text-white">{t.title}</span> |
| 51 | + </li> |
| 52 | + ))} |
| 53 | + </ul> |
| 54 | + <div className="flex flex-col gap-2"> |
| 55 | + <input |
| 56 | + type="text" |
| 57 | + value={todo} |
| 58 | + onChange={(e) => setTodo(e.target.value)} |
| 59 | + onKeyDown={(e) => { |
| 60 | + if (e.key === 'Enter') { |
| 61 | + submitTodo() |
| 62 | + } |
| 63 | + }} |
| 64 | + placeholder="Enter a new todo..." |
| 65 | + className="w-full px-4 py-3 rounded-lg border border-white/20 bg-white/10 backdrop-blur-sm text-white placeholder-white/60 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:border-transparent" |
| 66 | + /> |
| 67 | + <button |
| 68 | + disabled={todo.trim().length === 0} |
| 69 | + onClick={submitTodo} |
| 70 | + className="bg-blue-500 hover:bg-blue-600 disabled:bg-blue-500/50 disabled:cursor-not-allowed text-white font-bold py-3 px-4 rounded-lg transition-colors" |
| 71 | + > |
| 72 | + Add todo |
| 73 | + </button> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ) |
| 78 | +} |
0 commit comments