fix(search): clean up node/property search matches
- add tests for notes, property key/value, hidden-property exclusion, matched-value display - fix flatMap indentation and stray semicolon - use stable match key instead of display_value (avoids dup-key collisions) - keep label searchable but not repeated in matched-values column
This commit is contained in:
@@ -45,43 +45,45 @@ export function SearchBar({ onOpenPending }: SearchBarProps) {
|
|||||||
const q = query.toLowerCase().trim()
|
const q = query.toLowerCase().trim()
|
||||||
|
|
||||||
const nodeResults = q
|
const nodeResults = q
|
||||||
? nodes.flatMap((n) => {
|
? nodes.flatMap((n) => {
|
||||||
if (n.data.type === 'groupRect') return []
|
if (n.data.type === 'groupRect') return []
|
||||||
|
|
||||||
const contains = (v?: string | null) => v?.toLowerCase().includes(q)
|
const contains = (v?: string | null) => v?.toLowerCase().includes(q)
|
||||||
|
|
||||||
const matches: Array<{ key: string; display_value: string }> = []
|
const matches: Array<{ key: string; display_value: string }> = []
|
||||||
|
|
||||||
for (const [key, value] of [
|
// Label matches make the node a result, but it is already shown in the
|
||||||
['label', n.data.label],
|
// header — don't repeat it in the matched-values column.
|
||||||
['notes', n.data.notes],
|
const labelMatch = contains(n.data.label)
|
||||||
['ip', n.data.ip],
|
|
||||||
['hostname', n.data.hostname],
|
for (const [key, value] of [
|
||||||
] as const) {
|
['notes', n.data.notes],
|
||||||
if (contains(value)) {
|
['ip', n.data.ip],
|
||||||
matches.push({ key, display_value: value ?? '' })
|
['hostname', n.data.hostname],
|
||||||
|
] as const) {
|
||||||
|
if (contains(value)) {
|
||||||
|
matches.push({ key, display_value: value ?? '' })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (const s of n.data.services ?? []) {
|
for (const s of n.data.services ?? []) {
|
||||||
if (contains(s.service_name)) {
|
if (contains(s.service_name)) {
|
||||||
matches.push({ key: 'services', display_value: s.service_name ?? '' })
|
matches.push({ key: 'services', display_value: s.service_name ?? '' })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (const p of (n.data.properties ?? []).filter((p) => p.visible)) {
|
for (const p of (n.data.properties ?? []).filter((p) => p.visible)) {
|
||||||
if (contains(p.key) || contains(p.value)) {
|
if (contains(p.key) || contains(p.value)) {
|
||||||
matches.push({
|
matches.push({
|
||||||
key: 'properties',
|
key: 'properties',
|
||||||
display_value: `${p.key ?? ''} = ${p.value ?? ''}`,
|
display_value: `${p.key ?? ''} = ${p.value ?? ''}`,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return matches.length > 0 ? [{ node: n, matches: matches }] : []
|
return labelMatch || matches.length > 0 ? [{ node: n, matches }] : []
|
||||||
})
|
})
|
||||||
: [];
|
: []
|
||||||
|
|
||||||
|
|
||||||
const pendingResults = q
|
const pendingResults = q
|
||||||
? pendingDevices.filter((d) =>
|
? pendingDevices.filter((d) =>
|
||||||
@@ -193,8 +195,8 @@ export function SearchBar({ onOpenPending }: SearchBarProps) {
|
|||||||
</span>
|
</span>
|
||||||
{n.matches.length > 0 && (
|
{n.matches.length > 0 && (
|
||||||
<span style={{ maxWidth: "50%", display: 'flex', flexDirection: 'column', gap: 2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
<span style={{ maxWidth: "50%", display: 'flex', flexDirection: 'column', gap: 2, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
|
||||||
{n.matches.map((m) => (
|
{n.matches.map((m, mIdx) => (
|
||||||
<span key={m.display_value} style={{ fontSize: 11, color: '#8b949e', fontFamily: 'JetBrains Mono, monospace', flexShrink: 0, display: 'inline-block' }}>
|
<span key={`${m.key}-${mIdx}`} style={{ fontSize: 11, color: '#8b949e', fontFamily: 'JetBrains Mono, monospace', flexShrink: 0, display: 'inline-block' }}>
|
||||||
{m.display_value}
|
{m.display_value}
|
||||||
</span>
|
</span>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -103,6 +103,50 @@ describe('SearchBar', () => {
|
|||||||
expect(screen.queryByText('DB Server')).toBeNull()
|
expect(screen.queryByText('DB Server')).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('filters by notes', () => {
|
||||||
|
setupStore([
|
||||||
|
makeNode('n1', { data: { label: 'Server A', type: 'server', status: 'online', services: [], ip: null, hostname: null, notes: 'backup target every night' } }),
|
||||||
|
makeNode('n2', { data: { label: 'Server B', type: 'server', status: 'online', services: [], ip: null, hostname: null, notes: 'primary web host' } }),
|
||||||
|
])
|
||||||
|
render(<SearchBar />)
|
||||||
|
openSearch()
|
||||||
|
fireEvent.change(screen.getByPlaceholderText(/search/i), { target: { value: 'backup' } })
|
||||||
|
expect(screen.getByText('Server A')).toBeDefined()
|
||||||
|
expect(screen.queryByText('Server B')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filters by visible property key or value', () => {
|
||||||
|
setupStore([
|
||||||
|
makeNode('n1', { data: { label: 'Server A', type: 'server', status: 'online', services: [], ip: null, hostname: null, properties: [{ key: 'rack', value: 'B12', icon: null, visible: true }] } }),
|
||||||
|
makeNode('n2', { data: { label: 'Server B', type: 'server', status: 'online', services: [], ip: null, hostname: null, properties: [{ key: 'rack', value: 'A01', icon: null, visible: true }] } }),
|
||||||
|
])
|
||||||
|
render(<SearchBar />)
|
||||||
|
openSearch()
|
||||||
|
fireEvent.change(screen.getByPlaceholderText(/search/i), { target: { value: 'b12' } })
|
||||||
|
expect(screen.getByText('Server A')).toBeDefined()
|
||||||
|
expect(screen.queryByText('Server B')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('ignores hidden properties', () => {
|
||||||
|
setupStore([
|
||||||
|
makeNode('n1', { data: { label: 'Server A', type: 'server', status: 'online', services: [], ip: null, hostname: null, properties: [{ key: 'secret', value: 'hidden-val', icon: null, visible: false }] } }),
|
||||||
|
])
|
||||||
|
render(<SearchBar />)
|
||||||
|
openSearch()
|
||||||
|
fireEvent.change(screen.getByPlaceholderText(/search/i), { target: { value: 'hidden-val' } })
|
||||||
|
expect(screen.queryByText('Server A')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('shows the matched value alongside the node label', () => {
|
||||||
|
setupStore([
|
||||||
|
makeNode('n1', { data: { label: 'Server A', type: 'server', status: 'online', services: [], ip: null, hostname: null, properties: [{ key: 'rack', value: 'B12', icon: null, visible: true }] } }),
|
||||||
|
])
|
||||||
|
render(<SearchBar />)
|
||||||
|
openSearch()
|
||||||
|
fireEvent.change(screen.getByPlaceholderText(/search/i), { target: { value: 'b12' } })
|
||||||
|
expect(screen.getByText('rack = B12')).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
it('excludes groupRect nodes from results', () => {
|
it('excludes groupRect nodes from results', () => {
|
||||||
setupStore([
|
setupStore([
|
||||||
makeNode('gr1', { data: { label: 'DMZ Zone', type: 'groupRect', status: 'unknown', services: [], ip: null, hostname: null } }),
|
makeNode('gr1', { data: { label: 'DMZ Zone', type: 'groupRect', status: 'unknown', services: [], ip: null, hostname: null } }),
|
||||||
|
|||||||
Reference in New Issue
Block a user