Add initial value display for AsyncSelect

This commit is contained in:
yangdx
2025-04-06 15:53:04 +08:00
parent 0942bf102c
commit b003d613ee
3 changed files with 19 additions and 14 deletions

View File

@@ -57,20 +57,20 @@ const GraphLabels = () => {
[getSearchEngine] [getSearchEngine]
) )
// Show queryLabel validation status // Validate label
useEffect(() => { useEffect(() => {
if (labelsFetchAttempted) { if (labelsFetchAttempted) {
if (allDatabaseLabels.length > 1) { if (allDatabaseLabels.length > 1) {
if (label && label !== '*' && !allDatabaseLabels.includes(label)) { if (label && label !== '*' && !allDatabaseLabels.includes(label)) {
console.log(`Label "${label}" not in available labels`); console.log(`Label "${label}" not in available labels, setting to "*"`);
// useSettingsStore.getState().setQueryLabel('*'); useSettingsStore.getState().setQueryLabel('*');
} else { } else {
console.log(`Label "${label}" is valid`); console.log(`Label "${label}" is valid`);
} }
} else if (allDatabaseLabels.length <= 1 && label && label !== '*') { } else if (label && allDatabaseLabels.length <= 1 && label && label !== '*') {
console.log('Available labels list is empty'); console.log('Available labels list is empty, setting label to empty');
// useSettingsStore.getState().setQueryLabel(''); useSettingsStore.getState().setQueryLabel('');
} }
useGraphStore.getState().setLabelsFetchAttempted(false) useGraphStore.getState().setLabelsFetchAttempted(false)
} }

View File

@@ -96,12 +96,24 @@ export function AsyncSelect<T>({
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 [originalOptions, setOriginalOptions] = useState<T[]>([])
const [initialValueDisplay, setInitialValueDisplay] = useState<React.ReactNode | null>(null)
useEffect(() => { useEffect(() => {
setMounted(true) setMounted(true)
setSelectedValue(value) setSelectedValue(value)
}, [value]) }, [value])
// Add an effect to handle initial value display
useEffect(() => {
if (value && (!options.length || !selectedOption)) {
// Create a temporary display until options are loaded
setInitialValueDisplay(<div>{value}</div>)
} else if (selectedOption) {
// Once we find the actual selectedOption, clear the temporary display
setInitialValueDisplay(null)
}
}, [value, options.length, selectedOption])
// Initialize selectedOption when options are loaded and value exists // Initialize selectedOption when options are loaded and value exists
useEffect(() => { useEffect(() => {
if (value && options.length > 0) { if (value && options.length > 0) {
@@ -194,7 +206,7 @@ export function AsyncSelect<T>({
tooltip={triggerTooltip} tooltip={triggerTooltip}
side="bottom" side="bottom"
> >
{selectedOption ? getDisplayValue(selectedOption) : placeholder} {selectedOption ? getDisplayValue(selectedOption) : (initialValueDisplay || placeholder)}
<ChevronsUpDown className="opacity-50" size={10} /> <ChevronsUpDown className="opacity-50" size={10} />
</Button> </Button>
</PopoverTrigger> </PopoverTrigger>

View File

@@ -450,13 +450,6 @@ const useLightrangeGraph = () => {
state.setRawGraph(data); state.setRawGraph(data);
state.setGraphIsEmpty(false); state.setGraphIsEmpty(false);
// ensusre GraphLabels show the current label on first load
const lastSuccessfulQueryLabel = useGraphStore.getState().lastSuccessfulQueryLabel;
if (!lastSuccessfulQueryLabel){
useSettingsStore.getState().setQueryLabel('');
useSettingsStore.getState().setQueryLabel(queryLabel);
console.log('Set queryLabel after query on page first load');
}
// Update last successful query label // Update last successful query label
state.setLastSuccessfulQueryLabel(currentQueryLabel); state.setLastSuccessfulQueryLabel(currentQueryLabel);