File size: 4,609 Bytes
23d418f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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>
}