import type { EditorView } from 'codemirror'; import { updateSchema } from 'codemirror-json-schema'; import { useEffect, useRef } from 'react'; import { useActiveRequestId } from '../hooks/useActiveRequestId'; import { useAlert } from '../hooks/useAlert'; import { useGrpc } from '../hooks/useGrpc'; import { tryFormatJson } from '../lib/formatters'; import type { EditorProps } from './core/Editor'; import { Editor } from './core/Editor'; import { FormattedError } from './core/FormattedError'; import { InlineCode } from './core/InlineCode'; import { VStack } from './core/Stacks'; type Props = Pick< EditorProps, 'heightMode' | 'onChange' | 'defaultValue' | 'className' | 'forceUpdateKey' > & { url: string; service: string | null; method: string | null; }; export function GrpcEditor({ url, service, method, defaultValue, ...extraEditorProps }: Props) { const editorViewRef = useRef(null); const activeRequestId = useActiveRequestId(); const grpc = useGrpc(url, activeRequestId); const alert = useAlert(); useEffect(() => { if (editorViewRef.current == null || grpc.services == null) return; const s = grpc.services?.find((s) => s.name === service); if (service != null && s == null) { alert({ id: 'grpc-find-service-error', title: "Couldn't Find Service", body: ( <> Failed to find service {service} in schema ), }); return; } const schema = s?.methods.find((m) => m.name === method)?.schema; if (method != null && schema == null) { alert({ id: 'grpc-find-schema-error', title: "Couldn't Find Method", body: ( <> Failed to find method {method} for{' '} {service} in schema ), }); return; } if (schema == null) { return; } try { updateSchema(editorViewRef.current, JSON.parse(schema)); } catch (err) { alert({ id: 'grpc-parse-schema-error', title: 'Failed to Parse Schema', body: (

For service {service} and method{' '} {method}

{String(err)}
), }); console.log('Failed to parse method schema', method, schema); } }, [alert, grpc.services, method, service]); return (
); }