Spaces:
Sleeping
Sleeping
Download web/src/components/chat/Sources.tsx from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 4.61 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/web/src/components/chat/Sources.tsx
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/web/src/components/chat/Sources.tsx
-
curl -L -o Sources.tsx https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/web/src/components/chat/Sources.tsx
4.61 kB
| import { CircleAlert, CircleCheck, ExternalLink } from 'lucide-react' | |
| import { useState } from 'react' | |
| import { VERDICTS } from '@/lib/content' | |
| import { inForce } from '@/lib/format' | |
| import { pieces } from '@/lib/highlight' | |
| import type { CitationView } from '@/lib/types' | |
| import { cn } from '@/lib/utils' | |
| const TONE = { | |
| good: 'text-emerald-600', | |
| warn: 'text-amber-600', | |
| bad: 'text-red-600', | |
| } | |
| /** The answer's numbered sources as a row of chips; clicking one opens it below the | |
| * answer: where it is (version, pages, link), the checker's verdict, the quote, and the | |
| * section text with the quote highlighted. The colour comes from the VERDICT, never | |
| * from how the quote was located (9a audit). */ | |
| export function Sources({ citations }: { citations: CitationView[] }) { | |
| const [open, setOpen] = useState<number | null>(null) | |
| const shown = citations.find((c) => c.n === open) | |
| return ( | |
| <div className="space-y-2"> | |
| <div className="flex flex-wrap items-center gap-1.5 text-xs"> | |
| <span className="mr-1 font-medium text-muted-foreground">Sources</span> | |
| {citations.map((c) => { | |
| const v = VERDICTS[c.verdict] | |
| const Icon = v.tone === 'good' ? CircleCheck : CircleAlert | |
| return ( | |
| <button | |
| key={c.n} | |
| type="button" | |
| onClick={() => setOpen(open === c.n ? null : c.n)} | |
| aria-expanded={open === c.n} | |
| title={`${v.label}: ${v.why}`} | |
| className={cn( | |
| 'inline-flex max-w-full items-center gap-1 rounded-full border px-2 py-1 hover:bg-accent', | |
| open === c.n && 'border-primary bg-accent text-accent-foreground', | |
| )} | |
| > | |
| <span className="font-semibold tabular-nums">{c.n}</span> | |
| <span className="truncate">{label(c)}</span> | |
| <Icon className={cn('size-3.5 shrink-0', TONE[v.tone])} aria-label={v.label} /> | |
| </button> | |
| ) | |
| })} | |
| </div> | |
| {shown && <SourceDetail c={shown} />} | |
| </div> | |
| ) | |
| } | |
| /** A short name for a source: the form and its section number. */ | |
| function label(c: CitationView): string { | |
| const s = c.section | |
| const section = c.source_id.includes('#') ? ` §${c.source_id.split('#')[1]}` : '' | |
| return `${s?.form_id ?? c.source_id.split('#')[0]}${section}` | |
| } | |
| function SourceDetail({ c }: { c: CitationView }) { | |
| const v = VERDICTS[c.verdict] | |
| const s = c.section | |
| const x = c.excerpt | |
| const highlighted = x?.parts.some((p) => p.spans.length > 0) | |
| return ( | |
| <div className="rounded-xl border bg-muted p-3 text-sm"> | |
| <div className="flex flex-wrap items-baseline gap-x-2"> | |
| <span className="font-medium">{s?.title ?? c.source_id}</span> | |
| <span className="text-xs text-muted-foreground"> | |
| {c.source_id} | |
| {s ? ` · ${inForce(s.effective_from, s.effective_to)}` : ''} | |
| {s?.pages ? ` · ${s.pages}` : ''} | |
| </span> | |
| {s?.url && ( | |
| <a href={s.url} target="_blank" rel="noreferrer" className="inline-flex items-center gap-0.5 text-xs text-primary underline"> | |
| source <ExternalLink className="size-3" /> | |
| </a> | |
| )} | |
| </div> | |
| <p className={cn('mt-1 text-xs font-medium', TONE[v.tone])}> | |
| {v.label}: {v.why} | |
| {c.verdict !== 'exact' && c.problems.length > 0 ? ` (${c.problems.join('; ')})` : ''} | |
| </p> | |
| {x && ( | |
| <div className="mt-2 max-h-80 overflow-auto rounded-lg bg-background p-3 text-xs leading-relaxed whitespace-pre-wrap"> | |
| {s?.heading_path && <p className="mb-2 font-medium">{s.heading_path}</p>} | |
| {!highlighted && ( | |
| <p className="mb-2 text-amber-700">The quote could not be placed in this text; the start of the section is shown.</p> | |
| )} | |
| {x.cut_start && <Gap />} | |
| {x.parts.map((part, i) => ( | |
| <span key={i}> | |
| {i > 0 && <Gap />} | |
| {pieces(part.text, part.spans).map((p, j) => | |
| p.mark ? ( | |
| <mark key={j} className="rounded-sm bg-blue-100 px-0.5 text-foreground ring-1 ring-blue-200"> | |
| {p.text} | |
| </mark> | |
| ) : ( | |
| <span key={j}>{p.text}</span> | |
| ), | |
| )} | |
| </span> | |
| ))} | |
| {x.cut_end && <Gap />} | |
| </div> | |
| )} | |
| {!x && <blockquote className="mt-2 border-l-2 pl-3 text-xs italic">“{c.quote}”</blockquote>} | |
| </div> | |
| ) | |
| } | |
| function Gap() { | |
| return <span className="my-1 block text-center text-muted-foreground">[…]</span> | |
| } | |