import { useState } from 'react' import { useNavigate, Link } from 'react-router-dom' import { toast } from '@/lib/toast' import { CheckCircle2, XCircle, Pencil, EyeOff, ChevronDown, ChevronRight, ExternalLink, Clock, Hash, Sparkles, Loader2, } from 'lucide-react' import type { FlowProposalDetail as ProposalDetailType } from '@/types/flow-proposal' interface ProposalDetailProps { proposal: ProposalDetailType onReview: (action: 'approve' | 'reject' | 'modify' | 'dismiss', notes?: string) => Promise } export function ProposalDetail({ proposal, onReview }: ProposalDetailProps) { const navigate = useNavigate() const [reviewNotes, setReviewNotes] = useState('') const [showFlowData, setShowFlowData] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) const [showRejectConfirm, setShowRejectConfirm] = useState(false) const canReview = proposal.status === 'pending' || proposal.status === 'dismissed' const handleAction = async (action: 'approve' | 'reject' | 'modify' | 'dismiss') => { setIsSubmitting(true) try { await onReview(action, reviewNotes || undefined) } finally { setIsSubmitting(false) setShowRejectConfirm(false) } } const handleEditAndPublish = async () => { // Mark proposal as dismissed so it doesn't stay in the pending queue. // The tree editor will create the flow independently. // TODO: Wire tree editor to call review API with action='modify' on save // when it detects proposalId in location.state, linking the published tree // back to the proposal. setIsSubmitting(true) try { await onReview('dismiss', 'Opened in Flow Editor for manual editing') } catch { // Warn but continue — the editor will still open toast.warning('Could not update proposal status — it may still appear in the queue') } finally { setIsSubmitting(false) } const flowData = proposal.proposed_flow_data navigate('/trees/new', { state: { preloadedStructure: flowData.tree_structure || flowData, proposalId: proposal.id, proposalTitle: proposal.title, }, }) } return (
{/* Header */}

{proposal.title}

{proposal.description && (

{proposal.description}

)}
{proposal.problem_domain && ( {proposal.problem_domain} )} {Math.round(proposal.confidence_score * 100)}% confidence {proposal.supporting_session_count} supporting session{proposal.supporting_session_count !== 1 ? 's' : ''} {new Date(proposal.created_at).toLocaleString()}
{/* Content */}
{/* Source — exactly one of a FlowPilot session XOR an L1 walk is set (DB CHECK). Never link to /pilot for an L1-sourced proposal: source_session_id is NULL there, so the old unconditional link rendered a broken /pilot/null. */} {proposal.source_session_id ? (

Source Session

View session that generated this proposal
) : proposal.l1_session_id ? (

Source — L1 AI walkthrough

Captured from an L1 technician's AI-guided walk and validated by a successful resolution. The proposed flow is the path that resolved the ticket.

L1 session {proposal.l1_session_id.slice(0, 8)}

) : null} {/* Proposed diff (for enhancements) */} {proposal.proposed_diff && (() => { const diff = proposal.proposed_diff as { diff_description?: string; new_nodes?: Array<{ title?: string; question?: string; description?: string }> } return (

Proposed Changes

{diff.diff_description && (

{diff.diff_description}

)} {diff.new_nodes && diff.new_nodes.length > 0 && (

New nodes:

{diff.new_nodes.map((node, i) => (
+ {node.title || node.question || node.description || 'New node'}
))}
)}
) })()} {/* Flow data preview */}
{showFlowData && (
              {JSON.stringify(proposal.proposed_flow_data, null, 2)}
            
)}
{/* Supporting sessions */} {proposal.supporting_session_ids.length > 1 && (

Supporting Sessions ({proposal.supporting_session_ids.length})

{proposal.supporting_session_ids.map((sid) => ( {sid} ))}
)} {/* Review info (for already-reviewed proposals) */} {proposal.reviewed_at && (

Review

{proposal.status} on{' '} {new Date(proposal.reviewed_at).toLocaleString()}

{proposal.reviewer_notes && (

{proposal.reviewer_notes}

)}
)}
{/* Review actions bar */} {canReview && (
{/* Notes input */} setReviewNotes(e.target.value)} placeholder="Reviewer notes (optional)" className="w-full rounded-lg border border-border bg-card px-3 py-2 text-sm text-foreground placeholder:text-muted-foreground focus:border-[rgba(96,165,250,0.3)] focus:outline-none" /> {/* Action buttons */}
{proposal.proposal_type === 'new_flow' ? ( ) : ( Enhancement proposals require Edit & Publish )}
)}
) }