'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' 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 { 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 (
Source Insight
{displayInsight?.insight_type && ( {displayInsight.insight_type} )} {sourceId && ( )}
{showDeleteConfirm ? (

Are you sure you want to delete this insight?
This action cannot be undone.

) : (
{isLoading ? (
Loading insight…
) : displayInsight ? (

{children}

, h2: ({ children }) =>

{children}

, h3: ({ children }) =>

{children}

, p: ({ children }) =>

{children}

, ul: ({ children }) =>
    {children}
, ol: ({ children }) =>
    {children}
, li: ({ children }) =>
  • {children}
  • , blockquote: ({ children }) => (
    {children}
    ), code: ({ className, children, ...props }) => { const match = /language-(\w+)/.exec(className || '') const isInline = !match return isInline ? ( {children} ) : ( {children} ) }, pre: ({ children }) => (
                            {children}
                          
    ), table: ({ children }) => (
    {children}
    ), thead: ({ children }) => {children}, tbody: ({ children }) => {children}, tr: ({ children }) => {children}, th: ({ children }) => ( {children} ), td: ({ children }) => ( {children} ), hr: () =>
    , a: ({ children, href }) => ( {children} ), }} > {displayInsight.content}
    ) : (

    No insight selected.

    )}
    )}
    ) }