Spaces:
Sleeping
Sleeping
File size: 12,561 Bytes
f871fed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
'use client'
import React, { useState, useEffect } from 'react'
import { SourceListResponse } from '@/lib/types/api'
import { Badge } from '@/components/ui/badge'
import { Card, CardContent } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
DropdownMenuSeparator
} from '@/components/ui/dropdown-menu'
import {
FileText,
ExternalLink,
Upload,
MoreVertical,
Trash2,
RefreshCw,
Clock,
CheckCircle,
AlertTriangle,
Loader2,
Unlink
} from 'lucide-react'
import { useSourceStatus } from '@/lib/hooks/use-sources'
import { cn } from '@/lib/utils'
import { ContextToggle } from '@/components/common/ContextToggle'
import { ContextMode } from '@/app/(dashboard)/notebooks/[id]/page'
interface SourceCardProps {
source: SourceListResponse
onDelete?: (sourceId: string) => void
onRetry?: (sourceId: string) => void
onRemoveFromNotebook?: (sourceId: string) => void
onClick?: (sourceId: string) => void
onRefresh?: () => void
className?: string
showRemoveFromNotebook?: boolean
contextMode?: ContextMode
onContextModeChange?: (mode: ContextMode) => void
}
const SOURCE_TYPE_ICONS = {
link: ExternalLink,
upload: Upload,
text: FileText,
} as const
const STATUS_CONFIG = {
new: {
icon: Clock,
color: 'text-blue-600',
bgColor: 'bg-blue-50',
borderColor: 'border-blue-200',
label: 'Processing',
description: 'Preparing to process'
},
queued: {
icon: Clock,
color: 'text-blue-600',
bgColor: 'bg-blue-50',
borderColor: 'border-blue-200',
label: 'Queued',
description: 'Waiting to be processed'
},
running: {
icon: Loader2,
color: 'text-blue-600',
bgColor: 'bg-blue-50',
borderColor: 'border-blue-200',
label: 'Processing',
description: 'Being processed'
},
completed: {
icon: CheckCircle,
color: 'text-green-600',
bgColor: 'bg-green-50',
borderColor: 'border-green-200',
label: 'Completed',
description: 'Successfully processed'
},
failed: {
icon: AlertTriangle,
color: 'text-red-600',
bgColor: 'bg-red-50',
borderColor: 'border-red-200',
label: 'Failed',
description: 'Processing failed'
}
} as const
type SourceStatus = keyof typeof STATUS_CONFIG
function isSourceStatus(status: unknown): status is SourceStatus {
return typeof status === 'string' && status in STATUS_CONFIG
}
function getSourceType(source: SourceListResponse): 'link' | 'upload' | 'text' {
// Determine type based on asset information
if (source.asset?.url) return 'link'
if (source.asset?.file_path) return 'upload'
return 'text'
}
export function SourceCard({
source,
onDelete,
onRetry,
onRemoveFromNotebook,
onClick,
onRefresh,
className,
showRemoveFromNotebook = false,
contextMode,
onContextModeChange
}: SourceCardProps) {
// Only fetch status for sources that might have async processing
const sourceWithStatus = source as SourceListResponse & { command_id?: string; status?: string }
// Track processing state to continue polling until we detect completion
const [wasProcessing, setWasProcessing] = useState(false)
const shouldFetchStatus = !!sourceWithStatus.command_id ||
sourceWithStatus.status === 'new' ||
sourceWithStatus.status === 'queued' ||
sourceWithStatus.status === 'running' ||
wasProcessing // Keep polling if we were processing to catch the completion
const { data: statusData, isLoading: statusLoading } = useSourceStatus(
source.id,
shouldFetchStatus
)
// Determine current status
// If source has a command_id but no status, treat as "new" (just created)
const rawStatus = statusData?.status || sourceWithStatus.status
const currentStatus: SourceStatus = isSourceStatus(rawStatus)
? rawStatus
: (sourceWithStatus.command_id ? 'new' : 'completed')
// Track processing state and detect completion
useEffect(() => {
const currentStatusFromData = statusData?.status || sourceWithStatus.status
// If we're currently processing, mark that we were processing
if (currentStatusFromData === 'new' || currentStatusFromData === 'running' || currentStatusFromData === 'queued') {
setWasProcessing(true)
}
// If we were processing and now completed/failed, trigger refresh and stop polling
if (wasProcessing &&
(currentStatusFromData === 'completed' || currentStatusFromData === 'failed')) {
setWasProcessing(false) // Stop polling
if (onRefresh) {
setTimeout(() => onRefresh(), 500) // Small delay to ensure API is updated
}
}
}, [statusData, sourceWithStatus.status, wasProcessing, onRefresh, source.id])
const statusConfig = STATUS_CONFIG[currentStatus] || STATUS_CONFIG.completed
const StatusIcon = statusConfig.icon
const sourceType = getSourceType(source)
const SourceTypeIcon = SOURCE_TYPE_ICONS[sourceType]
const title = source.title || 'Untitled Source'
const handleRetry = () => {
if (onRetry) {
onRetry(source.id)
}
}
const handleDelete = () => {
if (onDelete) {
onDelete(source.id)
}
}
const handleRemoveFromNotebook = () => {
if (onRemoveFromNotebook) {
onRemoveFromNotebook(source.id)
}
}
const handleCardClick = () => {
if (onClick) {
onClick(source.id)
}
}
const isProcessing: boolean = currentStatus === 'new' || currentStatus === 'running' || currentStatus === 'queued'
const isFailed: boolean = currentStatus === 'failed'
const isCompleted: boolean = currentStatus === 'completed'
return (
<Card
className={cn(
'transition-all duration-200 hover:shadow-md group relative cursor-pointer border border-border/60 dark:border-border/40',
className
)}
onClick={handleCardClick}
>
<CardContent className="px-3 py-1">
{/* Header with status indicator */}
<div className="flex items-start justify-between gap-3 mb-1">
<div className="flex-1 min-w-0">
{/* Status badge - only show if not completed */}
{!isCompleted && (
<div className="flex items-center gap-2 mb-2">
<div className={cn(
'flex items-center gap-1.5 px-2 py-1 rounded-md text-xs font-medium',
statusConfig.bgColor,
statusConfig.color
)}>
<StatusIcon className={cn(
'h-3 w-3',
isProcessing && 'animate-spin'
)} />
{statusLoading && shouldFetchStatus ? 'Checking...' : statusConfig.label}
</div>
{/* Source type indicator */}
<div className="flex items-center gap-1 text-gray-500">
<SourceTypeIcon className="h-3 w-3" />
<span className="text-xs capitalize">{sourceType}</span>
</div>
</div>
)}
{/* Title */}
<div className={cn('mb-1.5', !isCompleted && 'mb-1')}>
<h4
className="text-sm font-medium leading-tight line-clamp-2"
title={title}
>
{title}
</h4>
</div>
{/* Processing message for active statuses */}
{statusData?.message && (isProcessing || isFailed) && (
<p className="text-xs text-gray-600 mb-2 italic">
{statusData.message}
</p>
)}
{/* Metadata badges */}
<div className="flex items-center gap-2 flex-wrap">
{/* Source type badge */}
<Badge variant="secondary" className="text-xs flex items-center gap-1">
<SourceTypeIcon className="h-3 w-3" />
{sourceType}
</Badge>
{isCompleted && source.insights_count > 0 && (
<Badge variant="outline" className="text-xs">
{source.insights_count} insights
</Badge>
)}
{source.topics && source.topics.length > 0 && isCompleted && (
<>
{source.topics.slice(0, 2).map((topic, index) => (
<Badge key={index} variant="outline" className="text-xs">
{topic}
</Badge>
))}
{source.topics.length > 2 && (
<Badge variant="outline" className="text-xs">
+{source.topics.length - 2}
</Badge>
)}
</>
)}
</div>
</div>
{/* Context toggle and actions */}
<div className="flex items-center gap-1">
{/* Context toggle - only show if handler provided */}
{onContextModeChange && contextMode && (
<ContextToggle
mode={contextMode}
hasInsights={source.insights_count > 0}
onChange={onContextModeChange}
/>
)}
{/* Actions dropdown */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0 opacity-0 group-hover:opacity-100 transition-opacity"
onClick={(e) => e.stopPropagation()}
>
<MoreVertical className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48">
{showRemoveFromNotebook && (
<>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
handleRemoveFromNotebook()
}}
disabled={!onRemoveFromNotebook}
>
<Unlink className="h-4 w-4 mr-2" />
Remove from Notebook
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
)}
{isFailed && (
<>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
handleRetry()
}}
disabled={!onRetry}
>
<RefreshCw className="h-4 w-4 mr-2" />
Retry Processing
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
)}
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation()
handleDelete()
}}
disabled={!onDelete}
className="text-red-600 focus:text-red-600"
>
<Trash2 className="h-4 w-4 mr-2" />
Delete Source
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
{(isFailed as any) && (
<div className="flex gap-2 pt-2 border-t">
<Button
variant="outline"
size="sm"
onClick={handleRetry}
disabled={!onRetry}
className="h-7 text-xs"
>
<RefreshCw className="h-3 w-3 mr-1" />
Retry
</Button>
</div>
)}
{/* Processing progress indicator */}
{isProcessing && statusData?.processing_info?.progress && (
<div className="mt-3 pt-2 border-t">
<div className="flex justify-between items-center mb-1">
<span className="text-xs text-gray-600">Progress</span>
<span className="text-xs text-gray-600">
{Math.round(statusData.processing_info.progress as number)}%
</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-1.5">
<div
className="bg-blue-600 h-1.5 rounded-full transition-all duration-300"
style={{ width: `${statusData.processing_info.progress as number}%` }}
/>
</div>
</div>
)}
</CardContent>
</Card>
)
}
|