Set queryLabel after query on page first load

This commit is contained in:
yangdx
2025-04-06 14:56:55 +08:00
parent 0defac0e29
commit df0594852a
2 changed files with 31 additions and 15 deletions

View File

@@ -12,7 +12,8 @@ const GraphLabels = () => {
const { t } = useTranslation()
const label = useSettingsStore.use.queryLabel()
const allDatabaseLabels = useGraphStore.use.allDatabaseLabels()
const labelsFetchAttempted = useGraphStore.use.labelsFetchAttempted()
// Remove initial label fetch effect as it's now handled by fetchGraph based on lastSuccessfulQueryLabel
const getSearchEngine = useCallback(() => {
@@ -56,22 +57,25 @@ const GraphLabels = () => {
[getSearchEngine]
)
// Validate if current queryLabel exists in allDatabaseLabels
// Show queryLabel validation status
useEffect(() => {
// Only update label when all conditions are met:
// 1. allDatabaseLabels is loaded (length > 1, as it has at least '*' by default)
// 2. Current label is not the default '*'
// 3. Current label doesn't exist in allDatabaseLabels
if (
allDatabaseLabels.length > 1 &&
label &&
label !== '*' &&
!allDatabaseLabels.includes(label)
) {
console.log(`Label "${label}" not found in available labels, resetting to default`);
useSettingsStore.getState().setQueryLabel('*');
if (labelsFetchAttempted) {
if (allDatabaseLabels.length > 1) {
if (label && label !== '*' && !allDatabaseLabels.includes(label)) {
console.log(`Label "${label}" not in available labels`);
// 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('');
}
useGraphStore.getState().setLabelsFetchAttempted(false)
}
}, [allDatabaseLabels, label]);
}, [allDatabaseLabels, label, labelsFetchAttempted]);
const handleRefresh = useCallback(() => {
// Reset fetch status flags

View File

@@ -119,6 +119,10 @@ const fetchGraph = async (label: string, maxDepth: number, maxNodes: number) =>
// Continue with graph fetch even if labels fetch fails
}
}
// Trigger GraphLabels component to check if the label is valid
// console.log('Setting labelsFetchAttempted to true');
useGraphStore.getState().setLabelsFetchAttempted(true)
// If label is empty, use default label '*'
const queryLabel = label || '*';
@@ -339,6 +343,7 @@ const useLightrangeGraph = () => {
}
// Only fetch data when graphDataFetchAttempted is false (avoids re-fetching on vite dev mode)
// GraphDataFetchAttempted must set to false when queryLabel is changed
if (!isFetching && !useGraphStore.getState().graphDataFetchAttempted) {
// Set flags
fetchInProgressRef.current = true
@@ -445,6 +450,13 @@ 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);