Fix reslectiton problem by efactor graph search input box handling logic

This commit is contained in:
yangdx
2025-03-24 21:33:42 +08:00
parent f0054545c0
commit 56245b2fcd

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from 'react' import React, { useState, useEffect, useCallback, useRef } from 'react'
import { Loader2 } from 'lucide-react' import { Loader2 } from 'lucide-react'
import { useDebounce } from '@/hooks/useDebounce' import { useDebounce } from '@/hooks/useDebounce'
@@ -81,100 +81,97 @@ export function AsyncSearch<T>({
const [options, setOptions] = useState<T[]>([]) const [options, setOptions] = useState<T[]>([])
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const [selectedValue, setSelectedValue] = useState(value)
const [focusedValue, setFocusedValue] = useState<string | null>(null)
const [searchTerm, setSearchTerm] = useState('') const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebounce(searchTerm, preload ? 0 : 150) const debouncedSearchTerm = useDebounce(searchTerm, preload ? 0 : 150)
const [originalOptions, setOriginalOptions] = useState<T[]>([]) const containerRef = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
setMounted(true) setMounted(true)
setSelectedValue(value) }, [])
}, [value])
// Effect for initial fetch // Handle clicks outside of the component
useEffect(() => { useEffect(() => {
const initializeOptions = async () => { const handleClickOutside = (event: MouseEvent) => {
if (
containerRef.current &&
!containerRef.current.contains(event.target as Node) &&
open
) {
setOpen(false)
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => {
document.removeEventListener('mousedown', handleClickOutside)
}
}, [open])
const fetchOptions = useCallback(async (query: string) => {
try { try {
setLoading(true) setLoading(true)
setError(null) setError(null)
// If we have a value, use it for the initial search const data = await fetcher(query)
const data = value !== null ? await fetcher(value) : []
setOriginalOptions(data)
setOptions(data) setOptions(data)
} catch (err) { } catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch options') setError(err instanceof Error ? err.message : 'Failed to fetch options')
} finally { } finally {
setLoading(false) setLoading(false)
} }
} }, [fetcher])
if (!mounted) {
initializeOptions()
}
}, [mounted, fetcher, value])
// Load options when search term changes
useEffect(() => { useEffect(() => {
const fetchOptions = async () => { if (!mounted) return
try {
setLoading(true)
setError(null)
const data = await fetcher(debouncedSearchTerm)
setOriginalOptions(data)
setOptions(data)
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch options')
} finally {
setLoading(false)
}
}
if (!mounted) { if (preload) {
fetchOptions()
} else if (!preload) {
fetchOptions()
} else if (preload) {
if (debouncedSearchTerm) { if (debouncedSearchTerm) {
setOptions( setOptions((prev) =>
originalOptions.filter((option) => prev.filter((option) =>
filterFn ? filterFn(option, debouncedSearchTerm) : true filterFn ? filterFn(option, debouncedSearchTerm) : true
) )
) )
}
} else { } else {
setOptions(originalOptions) fetchOptions(debouncedSearchTerm)
} }
} }, [mounted, debouncedSearchTerm, preload, filterFn, fetchOptions])
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher, debouncedSearchTerm, mounted, preload, filterFn])
const handleSelect = useCallback( // Load initial value
(currentValue: string) => { useEffect(() => {
if (currentValue !== selectedValue) { if (!mounted || !value) return
setSelectedValue(currentValue) fetchOptions(value)
}, [mounted, value, fetchOptions])
const handleSelect = useCallback((currentValue: string) => {
onChange(currentValue) onChange(currentValue)
} requestAnimationFrame(() => {
// Blur the input to ensure focus event triggers on next click
const input = document.activeElement as HTMLElement
input?.blur()
// Close the dropdown
setOpen(false) setOpen(false)
}, })
[selectedValue, setSelectedValue, setOpen, onChange] }, [onChange])
)
const handleFocus = useCallback( const handleFocus = useCallback(() => {
(currentValue: string) => { setOpen(true)
if (currentValue !== focusedValue) { // Use current search term to fetch options
setFocusedValue(currentValue) fetchOptions(searchTerm)
onFocus(currentValue) }, [searchTerm, fetchOptions])
const handleMouseDown = useCallback((e: React.MouseEvent) => {
const target = e.target as HTMLElement
if (target.closest('.cmd-item')) {
e.preventDefault()
} }
}, }, [])
[focusedValue, setFocusedValue, onFocus]
)
return ( return (
<div <div
ref={containerRef}
className={cn(disabled && 'cursor-not-allowed opacity-50', className)} className={cn(disabled && 'cursor-not-allowed opacity-50', className)}
onFocus={() => { onMouseDown={handleMouseDown}
setOpen(true)
}}
onBlur={() => setOpen(false)}
> >
<Command shouldFilter={false} className="bg-transparent"> <Command shouldFilter={false} className="bg-transparent">
<div> <div>
@@ -182,12 +179,13 @@ export function AsyncSearch<T>({
placeholder={placeholder} placeholder={placeholder}
value={searchTerm} value={searchTerm}
className="max-h-8" className="max-h-8"
onFocus={handleFocus}
onValueChange={(value) => { onValueChange={(value) => {
setSearchTerm(value) setSearchTerm(value)
if (value && !open) setOpen(true) if (!open) setOpen(true)
}} }}
/> />
{loading && options.length > 0 && ( {loading && (
<div className="absolute top-1/2 right-2 flex -translate-y-1/2 transform items-center"> <div className="absolute top-1/2 right-2 flex -translate-y-1/2 transform items-center">
<Loader2 className="h-4 w-4 animate-spin" /> <Loader2 className="h-4 w-4 animate-spin" />
</div> </div>
@@ -209,8 +207,8 @@ export function AsyncSearch<T>({
key={getOptionValue(option) + `${idx}`} key={getOptionValue(option) + `${idx}`}
value={getOptionValue(option)} value={getOptionValue(option)}
onSelect={handleSelect} onSelect={handleSelect}
onMouseEnter={() => handleFocus(getOptionValue(option))} onMouseMove={() => onFocus(getOptionValue(option))}
className="truncate" className="truncate cmd-item"
> >
{renderOption(option)} {renderOption(option)}
</CommandItem> </CommandItem>