From 2b43407ddf0a4bd38f31b25929a390652c62099b Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 10 Jan 2026 14:57:15 -0800 Subject: [PATCH] Fix gRPC autocomplete schema not being applied Two issues fixed: 1. Initialize stateExtensions with empty object {} instead of undefined. When called with no argument, the schema state was undefined, causing jsonCompletion() to return [] instead of a proper result object, which CodeMirror's autocomplete didn't handle correctly. 2. Change editorView from useRef to useState so the effect that calls updateSchema() properly re-runs when the editor view is set. With useRef, the effect could run before the editor was mounted or with a stale reference when the editor was recreated. --- src-web/components/GrpcEditor.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src-web/components/GrpcEditor.tsx b/src-web/components/GrpcEditor.tsx index 2ebd01a1..4a98582a 100644 --- a/src-web/components/GrpcEditor.tsx +++ b/src-web/components/GrpcEditor.tsx @@ -10,7 +10,7 @@ import { stateExtensions, updateSchema, } from 'codemirror-json-schema'; -import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import type { ReflectResponseService } from '../hooks/useGrpc'; import { showAlert } from '../lib/alert'; import { showDialog } from '../lib/dialog'; @@ -39,15 +39,15 @@ export function GrpcEditor({ protoFiles, ...extraEditorProps }: Props) { - const editorViewRef = useRef(null); + const [editorView, setEditorView] = useState(null); const handleInitEditorViewRef = useCallback((h: EditorView | null) => { - editorViewRef.current = h; + setEditorView(h); }, []); // Find the schema for the selected service and method and update the editor useEffect(() => { if ( - editorViewRef.current == null || + editorView == null || services === null || request.service === null || request.method === null @@ -91,7 +91,7 @@ export function GrpcEditor({ } try { - updateSchema(editorViewRef.current, JSON.parse(schema)); + updateSchema(editorView, JSON.parse(schema)); } catch (err) { showAlert({ id: 'grpc-parse-schema-error', @@ -107,7 +107,7 @@ export function GrpcEditor({ ), }); } - }, [services, request.method, request.service]); + }, [editorView, services, request.method, request.service]); const extraExtensions = useMemo( () => [ @@ -118,7 +118,7 @@ export function GrpcEditor({ jsonLanguage.data.of({ autocomplete: jsonCompletion(), }), - stateExtensions(/** Init with empty schema **/), + stateExtensions({}), ], [], );