CoverageCheck / web /src /pages /AskPage.tsx
TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
3.88 kB
import { RotateCcw, ShieldCheck } from 'lucide-react'
import { useEffect, useRef, useState } from 'react'
import { Composer } from '@/components/chat/Composer'
import { Message } from '@/components/chat/Message'
import { useChatContext } from '@/hooks/chatContext'
import { NOT_ADVICE } from '@/lib/content'
import type { SystemsFile } from '@/lib/types'
type Info = { questions_left: number; model: string }
async function getJson<T>(url: string): Promise<T | null> {
try {
const response = await fetch(url)
return response.ok ? ((await response.json()) as T) : null
} catch {
return null
}
}
/** A chat: the conversation scrolls above the question box; the box sets the policy date
* and the system. Each question is answered on its own. The conversation itself lives
* in ChatProvider, so it is still here after a visit to another page. */
export function AskPage() {
const [numbers, setNumbers] = useState<SystemsFile | null>(null)
const [info, setInfo] = useState<Info | null>(null)
const { turns, busy, send, stop, clear, system, setSystem, day, setDay } = useChatContext()
const end = useRef<HTMLDivElement>(null)
const last = turns[turns.length - 1]
useEffect(() => {
getJson<SystemsFile>('/data/systems.json').then(setNumbers)
const refresh = () => getJson<Info>('/api/info').then(setInfo)
refresh()
window.addEventListener('focus', refresh) // a backend that was down may be back
return () => window.removeEventListener('focus', refresh)
}, [])
// keep the newest message in view as it grows (a new question, a step, the answer)
useEffect(() => {
end.current?.scrollIntoView({ behavior: 'smooth', block: 'end' })
}, [turns.length, last?.run.steps, last?.run.phase])
async function ask(question: string) {
await send(question, system, day)
getJson<Info>('/api/info').then(setInfo)
}
return (
<div className="flex h-full flex-col">
<div className="min-h-0 flex-1 overflow-y-auto">
<div className="mx-auto max-w-3xl space-y-10 px-4 py-8">
{turns.length === 0 ? <Welcome /> : turns.map((t) => <Message key={t.id} turn={t} />)}
<div ref={end} />
</div>
</div>
<div className="mx-auto w-full max-w-3xl px-4 pb-3">
{turns.length > 0 && !busy && (
<div className="mb-2 flex justify-end">
<button
type="button"
onClick={clear}
className="inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
>
<RotateCcw className="size-3" /> New conversation
</button>
</div>
)}
<Composer
system={system}
onSystem={setSystem}
day={day}
onDay={setDay}
numbers={numbers}
busy={busy}
live={info !== null}
onSend={ask}
onStop={stop}
/>
<p className="mt-2 text-center text-[11px] text-muted-foreground">
{info
? `${NOT_ADVICE} · ${info.questions_left} questions left today`
: 'The answering service is not reachable right now. Please try again later.'}
</p>
</div>
</div>
)
}
function Welcome() {
return (
<div className="flex min-h-[50vh] flex-col items-center justify-center text-center">
<ShieldCheck className="mb-4 size-10 text-primary" />
<h1 className="text-2xl font-semibold tracking-tight">Ask about Ontario auto insurance</h1>
<p className="mt-2 max-w-lg text-sm text-muted-foreground">
Answers come only from the public FSRA policy forms and Ontario laws, and every quote is checked against its
source. Set the policy date and choose which of the three systems answers in the box below; ask
follow-up questions as you would in any chat.
</p>
</div>
)
}