-
Notifications
You must be signed in to change notification settings - Fork 1
fix(console): ui audit fixes — forms, VNC, events, status badges #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2a4d169
20fe766
84f48eb
c4fce27
9f63d1e
7a6d674
81c0d5c
907a787
cc03251
0eca627
3323713
3eebd4f
de2f98b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useMemo } from "react" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useMemo, useEffect, useRef } from "react" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Form from "@rjsf/core" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import validator from "@rjsf/validator-ajv8" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getDefaultFormState } from "@rjsf/utils" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { RJSFSchema, UiSchema, TemplatesType } from "@rjsf/utils" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { keysOrderToUiSchema, sanitizeSchema } from "../lib/keys-order.ts" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { customTemplates, customWidgets } from "./rjsf-templates.tsx" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,6 +166,24 @@ export function SchemaForm({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [openAPISchema]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const onChangeRef = useRef(onChange) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChangeRef.current = onChange | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const initialFormDataRef = useRef(formData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const emittedSchemaRef = useRef<RJSFSchema | null>(null) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Emit defaults to parent once per schema so spec is never empty on first submit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Uses initialFormDataRef so edit-mode existing values are preserved as base. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // emittedSchemaRef prevents re-running on unrelated re-renders and avoids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // overwriting user data if the schema object changes identity unexpectedly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!schema || Object.keys(schema).length === 0) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (emittedSchemaRef.current === schema) return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| emittedSchemaRef.current = schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const defaults = getDefaultFormState(validator, schema, initialFormDataRef.current ?? {}, schema) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChangeRef.current(defaults) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+171
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the latest Line 171 captures 💡 Suggested fix- const initialFormDataRef = useRef(formData)
+ const latestFormDataRef = useRef(formData)
+ latestFormDataRef.current = formData
const emittedSchemaRef = useRef<RJSFSchema | null>(null)
@@
- // Uses initialFormDataRef so edit-mode existing values are preserved as base.
+ // Uses latest formData so async-loaded edit values are preserved as base.
@@
- const defaults = getDefaultFormState(validator, schema, initialFormDataRef.current ?? {}, schema)
+ const defaults = getDefaultFormState(validator, schema, latestFormDataRef.current ?? {}, schema)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [schema]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const uiSchema = useMemo<UiSchema>(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const baseUiSchema: UiSchema = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "ui:submitButtonOptions": { norender: true }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
initialFormDataRefis initialized only once during the component's mount and is never updated. IfformDatais provided asynchronously by the parent (for example, after an API fetch), this ref will remain empty. When theschemais loaded and theuseEffectruns,getDefaultFormStatewill use the stale empty object as a base, and the subsequentonChange(defaults)call will overwrite the parent's newly loaded data with just the schema defaults.To fix this, ensure the ref is updated on every render so that the effect always uses the most recent
formDatawhen calculating the default state.