fix: open editor immediately when clicking suggested property slots
Previously, clicking a suggested property slot (Status, Date, URL) would add an empty property to frontmatter but fail to open the editor. This was due to a race condition where: 1. handleSuggestedAdd() called onAddProperty() which is async 2. The useEffect tried to detect when the property appeared in frontmatter 3. But frontmatter updates asynchronously after onAddProperty resolves 4. By the time frontmatter is updated, the useEffect had already run The fix is to directly set the editing key immediately when clicking a suggested slot, rather than relying on the useEffect to detect the property being added. This is more reliable because we know we're adding the property - we don't need to wait for it to appear in frontmatter.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useMemo, useCallback, useRef, useEffect } from 'react'
|
||||
import { useMemo, useCallback } from 'react'
|
||||
import type { VaultEntry } from '../types'
|
||||
import type { FrontmatterValue } from './Inspector'
|
||||
import type { ParsedFrontmatter } from '../utils/frontmatter'
|
||||
@@ -113,20 +113,12 @@ export function DynamicPropertiesPanel({
|
||||
? SUGGESTED_PROPERTIES.filter(p => !existingKeys.has(p.toLowerCase()))
|
||||
: []
|
||||
|
||||
const pendingEditKeyRef = useRef<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (pendingEditKeyRef.current && propertyEntries.some(([k]) => k === pendingEditKeyRef.current)) {
|
||||
setEditingKey(pendingEditKeyRef.current)
|
||||
pendingEditKeyRef.current = null
|
||||
}
|
||||
}, [propertyEntries, setEditingKey])
|
||||
|
||||
const handleSuggestedAdd = useCallback((key: string) => {
|
||||
if (!onAddProperty) return
|
||||
pendingEditKeyRef.current = key
|
||||
// Open the editor immediately - no need to wait for frontmatter to update
|
||||
setEditingKey(key)
|
||||
onAddProperty(key, '')
|
||||
}, [onAddProperty])
|
||||
}, [onAddProperty, setEditingKey])
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-3">
|
||||
|
||||
Reference in New Issue
Block a user