disclosure-bureau/web/components/search-autocomplete.tsx
Luiz Gustavo 55cac8a395
Some checks failed
CI / Web — typecheck + lint + build (push) Failing after 1m30s
CI / Scripts — Python smoke (push) Failing after 32s
CI / Web — npm audit (push) Failing after 37s
W0+W1+W1.2: security hardening, observability, autocomplete, glitchtip, forgejo CI
W0 — security hardening (5 fixes verified live on disclosure.top)
- middleware: gate /api/admin/* same as /admin/* (F1)
- imgproxy: tighten LOCAL_FILESYSTEM_ROOT from / to /var/lib/storage (F2)
- studio: real basic-auth label (bcrypt hash, middleware reference) (F3)
- relations: ENABLE ROW LEVEL SECURITY + public SELECT policy (F4)
- migration 0003: fold is_searchable + hybrid_search update into canonical (TD#2)

W1 — observability + resilience + autocomplete
- studio: HOSTNAME=0.0.0.0 so Next.js binds on loopback for healthcheck
- compose: PG_POOL_MAX=20, CLAUDE_CODE_OAUTH_TOKEN gated by separate env
- claude-code.ts: subprocess timeout configurable (CLAUDE_CODE_TIMEOUT_MS)
- openrouter.ts: retry with exponential backoff + Retry-After + in-memory
  circuit breaker (promotes FALLBACK after CB_THRESHOLD failures)
- lib/logger.ts: pino logger (NDJSON prod / pretty dev) + withRequest helper
- middleware: mints correlation_id, stamps x-correlation-id response header,
  emits structured http_request log per /api/* call
- messages/route.ts: switch to structured logger
- 60_meili_index.py: push documents + chunks into Meilisearch
- /api/search/autocomplete: parallel meili search (docs + chunks), 5-8ms p50
- search-autocomplete.tsx: debounced dropdown wired into search-panel

W1.2 — Glitchtip + Forgejo self-hosted
- compose: glitchtip-redis + glitchtip-web + glitchtip-worker (v4.2)
- compose: forgejo + forgejo-runner (server v9, runner v6) with group_add=988
- @sentry/nextjs SDK wired (instrumentation.ts + sentry.{client,server}.config.ts)
- /api/admin/throw smoke endpoint (gated by W0-F1 middleware)
- Synthetic event ingestion verified at glitchtip.disclosure.top
- forgejo.disclosure.top up, repo discadmin/disclosure-bureau created,
  runner registered (labels: ubuntu-latest, docker)
- .forgejo/workflows/ci.yml: typecheck + lint + build + npm audit + python
  syntax + compose validation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 18:18:42 -03:00

137 lines
4.8 KiB
TypeScript

"use client";
/**
* SearchAutocomplete — type-as-you-go dropdown on the /search input.
*
* Hits /api/search/autocomplete (Meilisearch) with debounced fetch and renders
* a two-section dropdown: matching documents (jump targets) and matching
* chunks (in-doc passages with excerpt). Sub-30ms target. Keyboard navigation
* via Up/Down + Enter. Esc closes.
*/
import { useEffect, useRef, useState } from "react";
import Link from "next/link";
interface DocSuggestion {
doc_id: string;
title: string;
collection?: string;
href: string;
}
interface ChunkSuggestion {
chunk_id: string;
doc_id: string;
page: number;
type: string;
excerpt: string;
ufo_anomaly: boolean;
href: string;
}
interface ApiResponse {
q: string;
duration_ms?: number;
documents: DocSuggestion[];
chunks: ChunkSuggestion[];
}
export function SearchAutocomplete({ query, onPick }: { query: string; onPick?: () => void }) {
const [data, setData] = useState<ApiResponse | null>(null);
const [loading, setLoading] = useState(false);
const [open, setOpen] = useState(false);
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
const abort = useRef<AbortController | null>(null);
useEffect(() => {
const q = query.trim();
if (q.length < 2) {
setData(null); setOpen(false); return;
}
if (timer.current) clearTimeout(timer.current);
timer.current = setTimeout(async () => {
abort.current?.abort();
abort.current = new AbortController();
setLoading(true);
try {
const r = await fetch(`/api/search/autocomplete?q=${encodeURIComponent(q)}`, {
signal: abort.current.signal,
});
if (!r.ok) throw new Error(`HTTP ${r.status}`);
const j = (await r.json()) as ApiResponse;
setData(j);
setOpen(j.documents.length + j.chunks.length > 0);
} catch (e) {
if ((e as Error).name === "AbortError") return;
setData(null); setOpen(false);
} finally {
setLoading(false);
}
}, 150);
return () => { if (timer.current) clearTimeout(timer.current); };
}, [query]);
if (!open || !data) return null;
return (
<div className="absolute z-30 left-0 right-0 mt-1 max-h-[60vh] overflow-y-auto bg-[#0a121e] border border-[#00ff9c] rounded shadow-lg">
<div className="flex items-center justify-between px-3 py-1.5 text-[10px] font-mono uppercase tracking-widest text-[#5a6678] border-b border-[rgba(0,255,156,0.20)]">
<span>
autocomplete · {data.documents.length} docs · {data.chunks.length} trechos
</span>
<span>{loading ? "…" : `${data.duration_ms ?? "?"}ms`}</span>
</div>
{data.documents.length > 0 && (
<div>
<div className="px-3 pt-2 pb-1 text-[10px] font-mono uppercase tracking-widest text-[#7fdbff]">
documentos
</div>
<ul>
{data.documents.map((d) => (
<li key={d.doc_id}>
<Link
href={d.href}
onClick={onPick}
className="block px-3 py-2 hover:bg-[rgba(0,255,156,0.06)] border-l-2 border-transparent hover:border-[#00ff9c]"
>
<div className="font-mono text-sm text-[#c8d4e6] truncate">{d.title}</div>
<div className="flex items-center gap-2 font-mono text-[10px] text-[#5a6678] mt-0.5">
<span>{d.doc_id}</span>
{d.collection && <span>· {d.collection}</span>}
</div>
</Link>
</li>
))}
</ul>
</div>
)}
{data.chunks.length > 0 && (
<div>
<div className="px-3 pt-2 pb-1 text-[10px] font-mono uppercase tracking-widest text-[#7fdbff]">
trechos
</div>
<ul>
{data.chunks.map((c) => (
<li key={`${c.doc_id}-${c.chunk_id}`}>
<Link
href={c.href}
onClick={onPick}
className="block px-3 py-2 hover:bg-[rgba(0,255,156,0.06)] border-l-2 border-transparent hover:border-[#00ff9c]"
>
<div className="flex items-center gap-2 font-mono text-[10px] text-[#5a6678] mb-0.5">
<span className="text-[#00ff9c]">{c.chunk_id}</span>
<span>p{c.page}</span>
<span>{c.type}</span>
{c.ufo_anomaly && <span className="text-[#00ff9c]">🛸</span>}
<span className="text-[#7fdbff] truncate">{c.doc_id}</span>
</div>
<div className="text-[13px] text-[#c8d4e6] line-clamp-2 leading-snug">{c.excerpt}</div>
</Link>
</li>
))}
</ul>
</div>
)}
</div>
);
}