mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-20 09:53:06 +00:00
Classificação automática com proveniência (job + CLI recordings:classify), facetas ano/tema/alcance/município/duração na URL canônica, busca por tema no corpus próprio, ordenação e a barra de filtros portada do design aprovado sobre os shells compartilhados. Deep-links existentes byte-idênticos; Câmara intocada.
113 lines
3.4 KiB
TypeScript
113 lines
3.4 KiB
TypeScript
/**
|
|
* Shared omnibox types + query matching for campaign list filter bars (B127).
|
|
* Domain adapters build chips/suggestions; the shared UI only renders them.
|
|
*/
|
|
import { normalizeSearchPhrase } from '@/lib/wordStartFilter'
|
|
|
|
export type CampaignListOmniboxChip = {
|
|
id: string
|
|
/** Full chip label, e.g. "Busca: Feira" or "Prioritária". */
|
|
label: string
|
|
}
|
|
|
|
export type CampaignListOmniboxSuggestion = {
|
|
id: string
|
|
group: string
|
|
label: string
|
|
/** Extra haystack tokens (dimension keywords) beyond `label`. */
|
|
keywords?: readonly string[]
|
|
}
|
|
|
|
/**
|
|
* The trigger text of a facet chip: the dimension alone when nothing is
|
|
* selected, "Ano: 2026" for one value and "Tema: 3" for several — one spelling
|
|
* for every acervo filter bar (C154/C219).
|
|
*/
|
|
export const selectedFilterTriggerLabel = (
|
|
base: string,
|
|
values: readonly string[],
|
|
labels?: Readonly<Record<string, string>>,
|
|
): string => {
|
|
if (values.length === 0) return base
|
|
if (values.length === 1) return `${base}: ${labels?.[values[0]!] ?? values[0]}`
|
|
return `${base}: ${values.length}`
|
|
}
|
|
|
|
const omniboxQueryMatches = (
|
|
haystack: string,
|
|
needle: string,
|
|
keywords: readonly string[] = [],
|
|
): boolean => {
|
|
const normalizedNeedle = normalizeSearchPhrase(needle)
|
|
if (!normalizedNeedle) return true
|
|
if (normalizeSearchPhrase(haystack).includes(normalizedNeedle)) return true
|
|
return keywords.some((keyword) => normalizeSearchPhrase(keyword).includes(normalizedNeedle))
|
|
}
|
|
|
|
export const omniboxGroupMatches = (group: string, needle: string): boolean =>
|
|
omniboxQueryMatches(group, needle)
|
|
|
|
const SUGGESTION_CAP_PER_GROUP = 8
|
|
|
|
export type OmniboxSuggestionSeed = CampaignListOmniboxSuggestion & {
|
|
/** When true, show even with an empty query (dimension shortcuts). */
|
|
emptyQueryVisible?: boolean
|
|
normalizedLabel: string
|
|
normalizedKeywords: readonly string[]
|
|
}
|
|
|
|
export const createOmniboxSuggestionSeed = (
|
|
suggestion: CampaignListOmniboxSuggestion,
|
|
extras?: { emptyQueryVisible?: boolean },
|
|
): OmniboxSuggestionSeed => ({
|
|
...suggestion,
|
|
...extras,
|
|
normalizedLabel: normalizeSearchPhrase(suggestion.label),
|
|
normalizedKeywords: (suggestion.keywords ?? []).map((keyword) => normalizeSearchPhrase(keyword)),
|
|
})
|
|
|
|
export const filterOmniboxSuggestionSeeds = (
|
|
seeds: readonly OmniboxSuggestionSeed[],
|
|
query: string,
|
|
): CampaignListOmniboxSuggestion[] => {
|
|
const suggestions: CampaignListOmniboxSuggestion[] = []
|
|
const trimmed = query.trim()
|
|
const normalizedNeedle = normalizeSearchPhrase(trimmed)
|
|
|
|
if (trimmed) {
|
|
suggestions.push({
|
|
id: `q:${trimmed}`,
|
|
group: 'Busca',
|
|
label: `Busca: ${trimmed}`,
|
|
keywords: ['busca', 'pesquisar', 'texto'],
|
|
})
|
|
}
|
|
|
|
const groupCounts = new Map<string, number>()
|
|
|
|
for (const seed of seeds) {
|
|
const count = groupCounts.get(seed.group) ?? 0
|
|
if (count >= SUGGESTION_CAP_PER_GROUP) continue
|
|
|
|
if (!normalizedNeedle) {
|
|
if (!seed.emptyQueryVisible) continue
|
|
} else {
|
|
const groupHit = omniboxGroupMatches(seed.group, trimmed)
|
|
const labelHit =
|
|
seed.normalizedLabel.includes(normalizedNeedle) ||
|
|
seed.normalizedKeywords.some((keyword) => keyword.includes(normalizedNeedle))
|
|
if (!groupHit && !labelHit) continue
|
|
}
|
|
|
|
const {
|
|
emptyQueryVisible: _visible,
|
|
normalizedLabel: _label,
|
|
normalizedKeywords: _kw,
|
|
...suggestion
|
|
} = seed
|
|
suggestions.push(suggestion)
|
|
groupCounts.set(seed.group, count + 1)
|
|
}
|
|
|
|
return suggestions
|
|
}
|