'use client' import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { FileText } from 'lucide-react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import { useInsight } from '@/lib/hooks/use-insights' import { useModalManager } from '@/lib/hooks/use-modal-manager' import { useTranslation } from '@/lib/hooks/use-translation' interface SourceInsightDialogProps { open: boolean onOpenChange: (open: boolean) => void insight?: { id: string insight_type?: string content?: string created?: string source_id?: string } onDelete?: (insightId: string) => Promise } export function SourceInsightDialog({ open, onOpenChange, insight, onDelete }: SourceInsightDialogProps) { const { t } = useTranslation() const { openModal } = useModalManager() const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [isDeleting, setIsDeleting] = useState(false) // Ensure insight ID has 'source_insight:' prefix for API calls const insightIdWithPrefix = insight?.id ? (insight.id.includes(':') ? insight.id : `source_insight:${insight.id}`) : '' const { data: fetchedInsight, isLoading } = useInsight(insightIdWithPrefix, { enabled: open && !!insight?.id }) // Use fetched data if available, otherwise fall back to passed-in insight const displayInsight = fetchedInsight ?? insight // Get source_id from fetched data (preferred) or passed-in insight const sourceId = fetchedInsight?.source_id ?? insight?.source_id const handleViewSource = () => { if (sourceId) { openModal('source', sourceId) } } const handleDelete = async () => { if (!insight?.id || !onDelete) return setIsDeleting(true) try { await onDelete(insight.id) onOpenChange(false) } finally { setIsDeleting(false) setShowDeleteConfirm(false) } } // Reset delete confirmation when dialog closes useEffect(() => { if (!open) { setShowDeleteConfirm(false) } }, [open]) return ( {t('sources.sourceInsight')}
{displayInsight?.insight_type && ( {displayInsight.insight_type} )} {sourceId && ( )}
{showDeleteConfirm ? (

{t('sources.deleteInsightConfirm').split(/[??]/)[0]}?
{t('sources.deleteInsightConfirm').split(/[??]/)[1]?.trim() || t('common.deleteForever')}

) : (
{isLoading ? (
{t('common.loading')}
) : displayInsight ? (
(
{children}
), thead: ({ children }) => {children}, tbody: ({ children }) => {children}, tr: ({ children }) => {children}, th: ({ children }) => {children}, td: ({ children }) => {children}, }} > {displayInsight.content}
) : (

{t('sources.noInsightSelected')}

)}
)}
) }