Remove public registration, support administrator-managed accounts, and persist personal preferences. Refs #2 #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
259 lines
9.0 KiB
TypeScript
259 lines
9.0 KiB
TypeScript
import { useState, useEffect, useCallback, useRef } from 'react'
|
|
import { Trash2, FileText, Clock, Loader2, XCircle, AlertCircle, Send } from 'lucide-react'
|
|
import { reportsApi, type Report, type ReportMessage } from '@/api/reports'
|
|
import { Card, CardContent } from '@/components/ui/card'
|
|
import { Markdown } from '@/components/ui/markdown'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Spinner } from '@/components/ui/spinner'
|
|
import { Badge } from '@/components/ui/badge'
|
|
|
|
function StatusBadge({ status }: { status: Report['status'] }) {
|
|
if (status === 'generating') return (
|
|
<Badge variant="secondary" className="gap-1 text-xs">
|
|
<Loader2 className="h-3 w-3 animate-spin" /> En cours
|
|
</Badge>
|
|
)
|
|
if (status === 'error') return (
|
|
<Badge variant="destructive" className="gap-1 text-xs">
|
|
<AlertCircle className="h-3 w-3" /> Erreur
|
|
</Badge>
|
|
)
|
|
return null
|
|
}
|
|
|
|
function MessageBubble({ msg }: { msg: ReportMessage }) {
|
|
const isUser = msg.role === 'user'
|
|
return (
|
|
<div className={`flex ${isUser ? 'justify-end' : 'justify-start'}`}>
|
|
<div className={`max-w-[85%] rounded-lg px-3 py-2 text-sm ${
|
|
isUser
|
|
? 'bg-primary text-primary-foreground'
|
|
: 'bg-muted'
|
|
}`}>
|
|
{msg.status === 'generating' ? (
|
|
<div className="flex items-center gap-2 text-muted-foreground">
|
|
<Loader2 className="h-3 w-3 animate-spin" />
|
|
<span className="text-xs">Génération…</span>
|
|
</div>
|
|
) : msg.status === 'error' ? (
|
|
<span className="text-destructive text-xs">{msg.content || 'Erreur lors de la génération.'}</span>
|
|
) : isUser ? (
|
|
<p>{msg.content}</p>
|
|
) : (
|
|
<Markdown content={msg.content} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ReportChat({ report }: { report: Report }) {
|
|
const [messages, setMessages] = useState<ReportMessage[]>([])
|
|
const [input, setInput] = useState('')
|
|
const [sending, setSending] = useState(false)
|
|
const bottomRef = useRef<HTMLDivElement>(null)
|
|
|
|
const loadMessages = useCallback(async () => {
|
|
const msgs = await reportsApi.listMessages(report.id)
|
|
setMessages(msgs ?? [])
|
|
}, [report.id])
|
|
|
|
useEffect(() => {
|
|
let active = true
|
|
void reportsApi.listMessages(report.id).then(msgs => {
|
|
if (active) setMessages(msgs ?? [])
|
|
})
|
|
return () => { active = false }
|
|
}, [report.id])
|
|
|
|
// Poll tant qu'un message assistant est en cours de génération
|
|
useEffect(() => {
|
|
const hasGenerating = messages.some(m => m.status === 'generating')
|
|
if (!hasGenerating) return
|
|
const interval = setInterval(loadMessages, 2000)
|
|
return () => clearInterval(interval)
|
|
}, [messages, loadMessages])
|
|
|
|
// Scroll vers le bas à chaque nouveau message
|
|
useEffect(() => {
|
|
bottomRef.current?.scrollIntoView({ behavior: 'smooth' })
|
|
}, [messages])
|
|
|
|
async function send() {
|
|
const content = input.trim()
|
|
if (!content || sending) return
|
|
setInput('')
|
|
setSending(true)
|
|
try {
|
|
// Optimistic: ajouter le message user immédiatement
|
|
const optimisticUser: ReportMessage = {
|
|
id: `tmp-${Date.now()}`,
|
|
report_id: report.id,
|
|
role: 'user',
|
|
content,
|
|
status: 'done',
|
|
created_at: new Date().toISOString(),
|
|
}
|
|
const optimisticAssistant: ReportMessage = {
|
|
id: `tmp-assistant-${Date.now()}`,
|
|
report_id: report.id,
|
|
role: 'assistant',
|
|
content: '',
|
|
status: 'generating',
|
|
created_at: new Date().toISOString(),
|
|
}
|
|
setMessages(prev => [...prev, optimisticUser, optimisticAssistant])
|
|
await reportsApi.createMessage(report.id, content)
|
|
await loadMessages()
|
|
} finally {
|
|
setSending(false)
|
|
}
|
|
}
|
|
|
|
const hasGenerating = messages.some(m => m.status === 'generating')
|
|
|
|
return (
|
|
<div className="border-t pt-3 space-y-3">
|
|
<p className="text-xs font-medium text-muted-foreground uppercase tracking-wide">Conversation</p>
|
|
|
|
{messages.length > 0 && (
|
|
<div className="space-y-2">
|
|
{messages.map(msg => (
|
|
<MessageBubble key={msg.id} msg={msg} />
|
|
))}
|
|
<div ref={bottomRef} />
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex gap-2">
|
|
<Input
|
|
value={input}
|
|
onChange={e => setInput(e.target.value)}
|
|
onKeyDown={e => e.key === 'Enter' && !e.shiftKey && send()}
|
|
placeholder="Posez une question sur cette réponse…"
|
|
disabled={hasGenerating || sending}
|
|
className="text-sm"
|
|
/>
|
|
<Button
|
|
size="sm"
|
|
onClick={send}
|
|
disabled={!input.trim() || hasGenerating || sending}
|
|
className="shrink-0"
|
|
>
|
|
{sending ? <Loader2 className="h-4 w-4 animate-spin" /> : <Send className="h-4 w-4" />}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function Reports() {
|
|
const [reports, setReports] = useState<Report[]>([])
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
const load = useCallback(async () => {
|
|
try { setReports((await reportsApi.list()) ?? []) }
|
|
finally { setLoading(false) }
|
|
}, [])
|
|
|
|
useEffect(() => { load() }, [load])
|
|
|
|
// Poll toutes les 3s tant qu'il y a des rapports en cours
|
|
useEffect(() => {
|
|
const hasGenerating = reports.some(r => r.status === 'generating')
|
|
if (!hasGenerating) return
|
|
const interval = setInterval(load, 3000)
|
|
return () => clearInterval(interval)
|
|
}, [reports, load])
|
|
|
|
async function remove(id: string) {
|
|
await reportsApi.delete(id)
|
|
setReports(prev => prev.filter(r => r.id !== id))
|
|
}
|
|
|
|
if (loading) return <div className="flex justify-center py-20"><Spinner /></div>
|
|
|
|
return (
|
|
<div className="p-4 md:p-6 space-y-6">
|
|
<div>
|
|
<h1 className="text-2xl font-bold">Rapports IA</h1>
|
|
<p className="text-muted-foreground text-sm">
|
|
Vos questions posées sur des extraits de résumés, avec les réponses de l'IA.
|
|
</p>
|
|
</div>
|
|
|
|
{reports.length === 0 ? (
|
|
<Card className="border-dashed">
|
|
<CardContent className="py-12 text-center text-muted-foreground">
|
|
<FileText className="h-8 w-8 mx-auto mb-3 opacity-50" />
|
|
<p>Aucun rapport enregistré</p>
|
|
<p className="text-xs mt-1">Sélectionnez du texte dans un résumé et posez une question à l'IA.</p>
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="space-y-4">
|
|
{reports.map(r => (
|
|
<Card key={r.id} className={r.status === 'generating' ? 'border-primary/30' : ''}>
|
|
<CardContent className="pt-4 pb-4 space-y-3">
|
|
{/* Meta */}
|
|
<div className="flex items-start justify-between gap-4">
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
<div className="flex items-center gap-1 text-xs text-muted-foreground">
|
|
<Clock className="h-3 w-3" />
|
|
{new Date(r.created_at).toLocaleString('fr-FR')}
|
|
</div>
|
|
<StatusBadge status={r.status} />
|
|
</div>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-6 w-6 p-0 text-muted-foreground hover:text-destructive shrink-0"
|
|
onClick={() => remove(r.id)}
|
|
title={r.status === 'generating' ? 'Annuler' : 'Supprimer'}
|
|
>
|
|
{r.status === 'generating'
|
|
? <XCircle className="h-3 w-3" />
|
|
: <Trash2 className="h-3 w-3" />}
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Extraits de contexte */}
|
|
{r.context_excerpt.split('\n\n---\n\n').map((excerpt, i) => (
|
|
<div key={i} className="rounded bg-muted/60 px-3 py-2 text-xs text-muted-foreground italic">
|
|
« {excerpt} »
|
|
</div>
|
|
))}
|
|
|
|
{/* Question */}
|
|
<p className="text-sm font-medium">{r.question}</p>
|
|
|
|
{/* Réponse initiale */}
|
|
{r.status === 'generating' && (
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin text-primary" />
|
|
L'IA génère la réponse…
|
|
</div>
|
|
)}
|
|
{r.status === 'done' && (
|
|
<div className="border-t pt-3">
|
|
<Markdown content={r.answer} />
|
|
</div>
|
|
)}
|
|
{r.status === 'error' && (
|
|
<div className="text-sm text-destructive border-t pt-3">
|
|
Erreur : {r.error_msg || 'Une erreur est survenue lors de la génération.'}
|
|
</div>
|
|
)}
|
|
|
|
{/* Chat de suivi (uniquement quand le rapport est terminé) */}
|
|
{r.status === 'done' && <ReportChat report={r} />}
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|