Limit the search scope to labels in the current subgraph

- Decouple datasource label selection from the search input field
- Improve label selection handling logic
This commit is contained in:
yangdx
2025-03-13 00:20:53 +08:00
parent fa1e7b13a1
commit 727b137506
3 changed files with 45 additions and 24 deletions

View File

@@ -9,7 +9,7 @@ import { useTranslation } from 'react-i18next'
const GraphLabels = () => {
const { t } = useTranslation()
const label = useSettingsStore.use.queryLabel()
const graphLabels = useGraphStore.use.graphLabels()
const allDatabaseLabels = useGraphStore.use.allDatabaseLabels()
const getSearchEngine = useCallback(() => {
// Create search engine
@@ -26,14 +26,14 @@ const GraphLabels = () => {
})
// Add documents
const documents = graphLabels.map((str, index) => ({ id: index, value: str }))
const documents = allDatabaseLabels.map((str, index) => ({ id: index, value: str }))
searchEngine.addAll(documents)
return {
labels: graphLabels,
labels: allDatabaseLabels,
searchEngine
}
}, [graphLabels])
}, [allDatabaseLabels])
const fetchData = useCallback(
async (query?: string): Promise<string[]> => {
@@ -47,27 +47,11 @@ const GraphLabels = () => {
return result.length <= labelListLimit
? result
: [...result.slice(0, labelListLimit), t('graphLabels.andOthers', { count: result.length - labelListLimit })]
: [...result.slice(0, labelListLimit), '...']
},
[getSearchEngine, t]
)
const setQueryLabel = useCallback((newLabel: string) => {
if (newLabel.startsWith('And ') && newLabel.endsWith(' others')) return
const currentLabel = useSettingsStore.getState().queryLabel
if (newLabel === '*' && currentLabel === '*') {
// When reselecting '*', just set it again to trigger a new fetch
useSettingsStore.getState().setQueryLabel('*')
} else if (newLabel === currentLabel && newLabel !== '*') {
// When selecting the same label (except '*'), switch to '*'
useSettingsStore.getState().setQueryLabel('*')
} else {
useSettingsStore.getState().setQueryLabel(newLabel)
}
}, [])
return (
<AsyncSelect<string>
className="ml-2"
@@ -81,8 +65,20 @@ const GraphLabels = () => {
notFound={<div className="py-6 text-center text-sm">No labels found</div>}
label={t('graphPanel.graphLabels.label')}
placeholder={t('graphPanel.graphLabels.placeholder')}
value={label !== null ? label : ''}
onChange={setQueryLabel}
value={label !== null ? label : '*'}
onChange={(newLabel) => {
const currentLabel = useSettingsStore.getState().queryLabel
if (newLabel === '...') {
newLabel = '*'
}
if (newLabel === currentLabel && newLabel !== '*') {
// 选择相同标签时切换到'*'
useSettingsStore.getState().setQueryLabel('*')
} else {
useSettingsStore.getState().setQueryLabel(newLabel)
}
}}
clearable={false} // Prevent clearing value on reselect
/>
)