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

View File

@@ -96,12 +96,24 @@ export function AsyncSelect<T>({
const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebounce(searchTerm, preload ? 0 : 150)
const [originalOptions, setOriginalOptions] = useState<T[]>([])
const [initialValueDisplay, setInitialValueDisplay] = useState<React.ReactNode | null>(null)
useEffect(() => {
setMounted(true)
setSelectedValue(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
useEffect(() => {
if (value && options.length > 0) {
@@ -194,7 +206,7 @@ export function AsyncSelect<T>({
tooltip={triggerTooltip}
side="bottom"
>
{selectedOption ? getDisplayValue(selectedOption) : placeholder}
{selectedOption ? getDisplayValue(selectedOption) : (initialValueDisplay || placeholder)}
<ChevronsUpDown className="opacity-50" size={10} />
</Button>
</PopoverTrigger>

View File

@@ -450,13 +450,6 @@ const useLightrangeGraph = () => {
state.setRawGraph(data);
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
state.setLastSuccessfulQueryLabel(currentQueryLabel);