import { useMemo } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { ArrowDown, AlertCircle } from 'lucide-react'; import { type SubmissionItemWithDeps } from '@/lib/submissionItemsService'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { useIsMobile } from '@/hooks/use-mobile'; import { DependencyTreeView } from './DependencyTreeView'; interface DependencyVisualizerProps { items: SubmissionItemWithDeps[]; selectedIds: Set; } export function DependencyVisualizer({ items, selectedIds }: DependencyVisualizerProps) { const isMobile = useIsMobile(); const dependencyLevels = useMemo(() => { const levels: SubmissionItemWithDeps[][] = []; const visited = new Set(); const getRootItems = () => items.filter(item => !item.depends_on); const addLevel = (currentItems: SubmissionItemWithDeps[]) => { if (currentItems.length === 0) return; const nextLevel: SubmissionItemWithDeps[] = []; currentItems.forEach(item => { if (!visited.has(item.id)) { visited.add(item.id); if (item.dependents) { nextLevel.push(...item.dependents); } } }); levels.push(currentItems); addLevel(nextLevel); }; addLevel(getRootItems()); return levels; }, [items]); const hasCircularDependency = items.length > 0 && dependencyLevels.flat().length !== items.length; return (
{/* Compact dependency tree view */} {hasCircularDependency && ( Circular dependency detected! This submission needs admin review. )} {dependencyLevels.length === 0 && ( No dependencies found in this submission )} {dependencyLevels.map((level, levelIdx) => (

Level {levelIdx + 1}

{level.map((item) => (
{item.item_type.replace('_', ' ').toUpperCase()} {item.status}

{typeof item.item_data === 'object' && item.item_data !== null && !Array.isArray(item.item_data) && 'name' in item.item_data ? String((item.item_data as Record).name) : 'Unnamed'}

{item.dependents && item.dependents.length > 0 && (

Has {item.dependents.length} dependent(s)

)}
))}
{levelIdx < dependencyLevels.length - 1 && (
)}
))}
); }