A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.
fix(frontend): fix first-mount data loss in adjust-during-render sync
dextmorgn58a3c39f0bf9
diff --git a/flowsint-app/src/components/analyses/analysis-editor.tsx b/flowsint-app/src/components/analyses/analysis-editor.tsxindex 87fdf964..90cf370e 100644--- a/flowsint-app/src/components/analyses/analysis-editor.tsx+++ b/flowsint-app/src/components/analyses/analysis-editor.tsx@@ -88,7 +88,9 @@ export const AnalysisEditor = ({const queryClient = useQueryClient()// State/refs for editorconst editorContentRef = useRef<any>('')- const [titleValue, setTitleValue] = useState('')+ // Lazy initializer: analysis can already be loaded on mount, and the+ // render-time sync below only fires on later id *changes*.+ const [titleValue, setTitleValue] = useState(() => analysis?.title || '')const [editor, setEditor] = useState<Editor | undefined>(undefined)const [isEditingTitle, setIsEditingTitle] = useState(false)const [saveStatus, setSaveStatus] = useState<'saved' | 'saving' | 'unsaved'>('saved')diff --git a/flowsint-app/src/components/sketches/details-panel/details-panel.tsx b/flowsint-app/src/components/sketches/details-panel/details-panel.tsxindex a0ce21ee..3ddac08a 100644--- a/flowsint-app/src/components/sketches/details-panel/details-panel.tsx+++ b/flowsint-app/src/components/sketches/details-panel/details-panel.tsx@@ -104,6 +104,46 @@ const formatDate = (value: unknown): string => {}}+const EMPTY_FORM_DATA: FormData = {+ nodeLabel: '',+ nodeColor: null,+ nodeIcon: null,+ nodeImage: null,+ nodeFlag: null,+ nodeShape: null,+ nodeSize: 0,+ nodeProperties: {},+ nodeMetadata: {},+ notes: ''+}++const buildFormData = (node: GraphNode | null | undefined): FormData => {+ if (!node) return EMPTY_FORM_DATA+ const {+ nodeLabel,+ nodeImage,+ nodeIcon,+ nodeFlag,+ nodeColor,+ nodeShape,+ nodeMetadata,+ nodeProperties,+ nodeSize+ } = node+ return {+ nodeLabel: nodeLabel || '',+ nodeProperties: nodeProperties || {},+ nodeMetadata: nodeMetadata || {},+ nodeColor,+ nodeIcon,+ nodeImage,+ nodeFlag,+ nodeShape,+ nodeSize,+ notes: (nodeMetadata?.notes as string) || ''+ }+}+// ── Sub-components ────────────────────────────────────────────────────────────function TitleInput({ value, onChange }: { value: string; onChange: (val: string) => void }) {@@ -274,19 +314,11 @@ const DetailsPanel = memo(() => {return field?.type}- const [formData, setFormData] = useState<FormData>({- nodeLabel: '',- nodeColor: null,- nodeIcon: null,- nodeImage: null,- nodeFlag: null,- nodeShape: null,- nodeSize: 0,- nodeProperties: {},- nodeMetadata: {},- notes: ''- })- const [nodeSize, setNodeSize] = useState<number>(0)+ // Lazy initializers derive from `node` directly so the panel is+ // populated on mount too, not just on later node switches (see the+ // render-time sync below, which only fires on a node *change*).+ const [formData, setFormData] = useState<FormData>(() => buildFormData(node))+ const [nodeSize, setNodeSize] = useState<number>(() => node?.nodeSize ?? 0)// Refs to always read latest values without stale closures. Handlers below// also write these synchronously so a value is available immediately@@ -313,31 +345,8 @@ const DetailsPanel = memo(() => {if (node !== prevNode) {setPrevNode(node)if (node) {- const {- nodeLabel,- nodeImage,- nodeIcon,- nodeFlag,- nodeColor,- nodeShape,- nodeMetadata,- nodeProperties,- nodeSize: ns- } = node- const fd: FormData = {- nodeLabel: nodeLabel || '',- nodeProperties: nodeProperties || {},- nodeMetadata: nodeMetadata || {},- nodeColor,- nodeIcon,- nodeImage,- nodeFlag,- nodeShape,- nodeSize: ns,- notes: (nodeMetadata?.notes as string) || ''- }- setFormData(fd)- setNodeSize(ns ?? 0)+ setFormData(buildFormData(node))+ setNodeSize(node.nodeSize ?? 0)}}diff --git a/flowsint-app/src/components/sketches/settings/settings.tsx b/flowsint-app/src/components/sketches/settings/settings.tsxindex bef9c1b2..9105ce45 100644--- a/flowsint-app/src/components/sketches/settings/settings.tsx+++ b/flowsint-app/src/components/sketches/settings/settings.tsx@@ -447,12 +447,15 @@ export default function GlobalSettings() {enabled: Boolean(id)})- // Form state- const [formData, setFormData] = useState({- title: '',- description: '',- status: 'active'- })+ // Form state. Lazy initializer derives from `sketch` directly (it can+ // already be populated on mount, e.g. from the query cache) — the sync+ // below only fires on later *changes*, so a hardcoded empty default+ // here would never get overwritten if the cache already had data.+ const [formData, setFormData] = useState(() => ({+ title: sketch?.title || '',+ description: sketch?.description || '',+ status: sketch?.status || 'active'+ }))// Update form data when sketch data changes — adjusted during render// rather than in an effect.diff --git a/flowsint-app/src/routes/_auth.dashboard.custom-types.$typeId.tsx b/flowsint-app/src/routes/_auth.dashboard.custom-types.$typeId.tsxindex 9d7271c7..3fed95b8 100644--- a/flowsint-app/src/routes/_auth.dashboard.custom-types.$typeId.tsx+++ b/flowsint-app/src/routes/_auth.dashboard.custom-types.$typeId.tsx@@ -37,35 +37,27 @@ function CustomTypeEditor() {const queryClient = useQueryClient()const isNew = id === 'new'- // Form state- const [name, setName] = useState('')- const [description, setDescription] = useState('')- const [status, setStatus] = useState<'draft' | 'published'>('draft')- const [category, setCategory] = useState<string>('custom_types_category')- const [icon, setIcon] = useState(DEFAULT_ICON)- const [color, setColor] = useState(DEFAULT_COLOR)- const [fields, setFields] = useState<SchemaField[]>([])- const [showPreview, setShowPreview] = useState(true)-- const parseSchemaToFields = (schema: any) => {+ const parseSchemaToFields = (schema: any): SchemaField[] => {const properties = schema.properties || {}const required = schema.required || []- const parsedFields: SchemaField[] = Object.entries(properties).map(- ([key, value]: [string, any]) => ({- id: Math.random().toString(36).substr(2, 9),- key,- title: value.title || key,- type: value.type || 'string',- format: value.format,- description: value.description,- required: required.includes(key)- })- )- setFields(parsedFields)+ return Object.entries(properties).map(([key, value]: [string, any]) => ({+ id: Math.random().toString(36).substr(2, 9),+ key,+ title: value.title || key,+ type: value.type || 'string',+ format: value.format,+ description: value.description,+ required: required.includes(key)+ }))}- // Hooks- const { actionItems, isLoading: actionLoading } = useActionItems()+ const emptyField = (): SchemaField => ({+ id: Math.random().toString(36).substr(2, 9),+ key: '',+ title: '',+ type: 'string',+ required: false+ })// Load existing type if editingconst { data: existingType, isLoading } = useQuery<CustomType>({@@ -74,20 +66,35 @@ function CustomTypeEditor() {enabled: !isNew})+ // Form state. Lazy initializers derive from `existingType` directly — it+ // can already be populated on mount (query cache), and the render-time+ // sync below only fires on later *changes*. For a brand new type+ // (existingType always undefined, query disabled) this also fixes+ // `fields` starting genuinely empty instead of relying on the sync+ // block's addField() call, which — same reasoning — would never fire.+ const [name, setName] = useState(() => existingType?.name.replaceAll(' ', '') ?? '')+ const [description, setDescription] = useState(() => existingType?.description ?? '')+ const [status, setStatus] = useState<'draft' | 'published'>(() =>+ existingType?.status === 'archived' ? 'draft' : (existingType?.status ?? 'draft')+ )+ const [category, setCategory] = useState<string>(+ () => existingType?.category ?? 'custom_types_category'+ )+ const [icon, setIcon] = useState(() => existingType?.icon ?? DEFAULT_ICON)+ const [color, setColor] = useState(() => existingType?.color ?? DEFAULT_COLOR)+ const [fields, setFields] = useState<SchemaField[]>(() =>+ existingType ? parseSchemaToFields(existingType.schema) : isNew ? [emptyField()] : []+ )+ const [showPreview, setShowPreview] = useState(true)++ // Hooks+ const { actionItems, isLoading: actionLoading } = useActionItems()+// Stable (functional state update, no closure over `fields`) so it's// safe to depend on below without re-running this effect after every// field it adds.const addField = useCallback(() => {- setFields((prev) => [- ...prev,- {- id: Math.random().toString(36).substr(2, 9),- key: '',- title: '',- type: 'string',- required: false- }- ])+ setFields((prev) => [...prev, emptyField()])}, [])// Adjusted during render rather than in an effect.@@ -101,7 +108,7 @@ function CustomTypeEditor() {setCategory(existingType.category || 'custom_types_category')setIcon(existingType.icon || DEFAULT_ICON)setColor(existingType.color || DEFAULT_COLOR)- parseSchemaToFields(existingType.schema)+ setFields(parseSchemaToFields(existingType.schema))} else if (isNew) {addField()}diff --git a/flowsint-app/src/routes/_auth.dashboard.profile.tsx b/flowsint-app/src/routes/_auth.dashboard.profile.tsxindex a65c6f41..edf5a7b0 100644--- a/flowsint-app/src/routes/_auth.dashboard.profile.tsx+++ b/flowsint-app/src/routes/_auth.dashboard.profile.tsx@@ -25,11 +25,13 @@ function ProfilePage() {queryFn: authService.getCurrentUser})- const [form, setForm] = useState({- first_name: '',- last_name: '',- avatar_url: ''- })+ // Lazy initializer: profile can already be loaded on mount (query+ // cache), and the render-time sync below only fires on later changes.+ const [form, setForm] = useState(() => ({+ first_name: profile?.first_name ?? '',+ last_name: profile?.last_name ?? '',+ avatar_url: profile?.avatar_url ?? ''+ }))// Adjusted during render rather than in an effect.const [prevProfile, setPrevProfile] = useState(profile)