import { useState } from 'react'
import { ChevronDown, ChevronUp, Info, Table2, Image as ImageIcon } from 'lucide-react'
import type { Source } from '../types'
interface WhyDrawerProps {
sources: Source[]
query?: string
focusChunkId?: number | null
}
function relevancePercent(distance: number): number {
return Math.max(0, Math.round((1 - distance / 2) * 100))
}
function highlightQuery(text: string, query?: string): React.ReactNode {
if (!query) return text
const words = query
.split(/\s+/)
.filter((w) => w.length > 2)
.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
if (words.length === 0) return text
const regex = new RegExp(`(${words.join('|')})`, 'gi')
const parts = text.split(regex)
return parts.map((part, i) =>
regex.test(part) ? (
{part}
) : (
part
)
)
}
function ChunkTypeIcon({ type }: { type: string }) {
switch (type) {
case 'table':
return
Used {sources.length} chunks from{' '}
{uniqueFiles} document{uniqueFiles !== 1 ? 's' : ''}
{tableCount > 0 && (
{highlightQuery(source.text_preview, query)}