|
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { X, Copy, Check, Loader2 } from 'lucide-react'; |
| 3 | +import Editor from '@monaco-editor/react'; |
| 4 | +import type { ServerViewModel, ServerDefinition } from '../types/registry'; |
| 5 | + |
| 6 | +interface ServerDefinitionModalProps { |
| 7 | + server: ServerViewModel; |
| 8 | + onClose: () => void; |
| 9 | +} |
| 10 | + |
| 11 | +/** Extract only ServerDefinition fields, stripping runtime state */ |
| 12 | +function extractDefinition(server: ServerViewModel): ServerDefinition { |
| 13 | + const { |
| 14 | + is_installed: _a, |
| 15 | + enabled: _b, |
| 16 | + oauth_connected: _c, |
| 17 | + input_values: _d, |
| 18 | + connection_status: _e, |
| 19 | + missing_required_inputs: _f, |
| 20 | + last_error: _g, |
| 21 | + created_at: _h, |
| 22 | + installation_source: _i, |
| 23 | + env_overrides: _j, |
| 24 | + args_append: _k, |
| 25 | + extra_headers: _l, |
| 26 | + ...definition |
| 27 | + } = server; |
| 28 | + return definition; |
| 29 | +} |
| 30 | + |
| 31 | +export function ServerDefinitionModal({ server, onClose }: ServerDefinitionModalProps) { |
| 32 | + const [copied, setCopied] = useState(false); |
| 33 | + const [editorReady, setEditorReady] = useState(false); |
| 34 | + |
| 35 | + const definition = extractDefinition(server); |
| 36 | + const json = JSON.stringify(definition, null, 2); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + const timer = setTimeout(() => setEditorReady(true), 100); |
| 40 | + return () => clearTimeout(timer); |
| 41 | + }, []); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 45 | + if (e.key === 'Escape') { |
| 46 | + onClose(); |
| 47 | + } |
| 48 | + }; |
| 49 | + window.addEventListener('keydown', handleKeyDown); |
| 50 | + return () => window.removeEventListener('keydown', handleKeyDown); |
| 51 | + }, [onClose]); |
| 52 | + |
| 53 | + const handleCopy = useCallback(async () => { |
| 54 | + try { |
| 55 | + await navigator.clipboard.writeText(json); |
| 56 | + setCopied(true); |
| 57 | + setTimeout(() => setCopied(false), 2000); |
| 58 | + } catch { |
| 59 | + // Fallback for environments where clipboard API is unavailable |
| 60 | + } |
| 61 | + }, [json]); |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-50 p-4"> |
| 65 | + <div className="bg-[rgb(var(--surface))] w-full max-w-3xl h-[70vh] rounded-xl shadow-2xl flex flex-col border border-[rgb(var(--border))] animate-in fade-in scale-in duration-150"> |
| 66 | + {/* Header */} |
| 67 | + <div className="flex items-center justify-between p-4 border-b border-[rgb(var(--border))]"> |
| 68 | + <div className="min-w-0"> |
| 69 | + <h3 className="text-lg font-semibold truncate"> |
| 70 | + {server.name} |
| 71 | + </h3> |
| 72 | + <p className="text-sm text-[rgb(var(--muted))]"> |
| 73 | + Server Definition |
| 74 | + </p> |
| 75 | + </div> |
| 76 | + <div className="flex items-center gap-2"> |
| 77 | + <button |
| 78 | + onClick={handleCopy} |
| 79 | + className="flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-lg border border-[rgb(var(--border))] hover:bg-[rgb(var(--surface-hover))] transition-colors" |
| 80 | + title="Copy to clipboard" |
| 81 | + > |
| 82 | + {copied ? ( |
| 83 | + <> |
| 84 | + <Check className="h-4 w-4 text-[rgb(var(--success))]" /> |
| 85 | + Copied |
| 86 | + </> |
| 87 | + ) : ( |
| 88 | + <> |
| 89 | + <Copy className="h-4 w-4 text-[rgb(var(--muted))]" /> |
| 90 | + Copy |
| 91 | + </> |
| 92 | + )} |
| 93 | + </button> |
| 94 | + <button |
| 95 | + onClick={onClose} |
| 96 | + className="p-2 hover:bg-[rgb(var(--surface-hover))] rounded-lg transition-colors" |
| 97 | + > |
| 98 | + <X className="h-5 w-5 text-[rgb(var(--muted))]" /> |
| 99 | + </button> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + |
| 103 | + {/* Editor Area */} |
| 104 | + <div className="flex-1 relative min-h-0 bg-[#1e1e1e]"> |
| 105 | + {!editorReady ? ( |
| 106 | + <div className="absolute inset-0 flex items-center justify-center"> |
| 107 | + <Loader2 className="h-8 w-8 animate-spin text-[rgb(var(--muted))]" /> |
| 108 | + </div> |
| 109 | + ) : ( |
| 110 | + <Editor |
| 111 | + height="100%" |
| 112 | + defaultLanguage="json" |
| 113 | + value={json} |
| 114 | + theme="vs-dark" |
| 115 | + options={{ |
| 116 | + readOnly: true, |
| 117 | + minimap: { enabled: false }, |
| 118 | + fontSize: 14, |
| 119 | + fontFamily: "'Fira Code', 'Consolas', monospace", |
| 120 | + lineNumbers: 'on', |
| 121 | + scrollBeyondLastLine: false, |
| 122 | + automaticLayout: true, |
| 123 | + tabSize: 2, |
| 124 | + wordWrap: 'on', |
| 125 | + folding: true, |
| 126 | + bracketPairColorization: { enabled: true }, |
| 127 | + guides: { |
| 128 | + bracketPairs: true, |
| 129 | + indentation: true, |
| 130 | + }, |
| 131 | + padding: { top: 12, bottom: 12 }, |
| 132 | + domReadOnly: true, |
| 133 | + }} |
| 134 | + loading={ |
| 135 | + <div className="flex items-center justify-center h-full bg-[#1e1e1e]"> |
| 136 | + <Loader2 className="h-8 w-8 animate-spin text-[rgb(var(--muted))]" /> |
| 137 | + </div> |
| 138 | + } |
| 139 | + /> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +} |
0 commit comments