All files / frontend/src/components SessionListEnhanced.tsx

2.64% Statements 8/302
100% Branches 0/0
0% Functions 0/1
2.64% Lines 8/302

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              1x 1x                       1x 1x 1x 1x           1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           1x  
/**
 * Enhanced Session List with Approval Management
 * Displays sessions with approval status badges, filters, and pagination
 */
 
'use client';
 
import React, { useEffect, useState } from 'react';
import {
    Clock,
    CheckCircle,
    AlertCircle,
    RefreshCw,
    Eye,
    Zap,
    XCircle,
    Filter,
    ChevronLeft,
    ChevronRight,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { useSessions } from '@/hooks/useSessions';
import { SessionDetailModal } from './SessionDetailModal';
 
interface SessionListEnhancedProps {
    autoRefresh?: boolean;
}
 
export const SessionListEnhanced: React.FC<SessionListEnhancedProps> = ({
    autoRefresh = true,
}) => {
    const {
        sessions,
        isLoading,
        statusFilter,
        approvalFilter,
        currentPage,
        totalPages,
        totalSessions,
        fetchSessions,
        setStatusFilter,
        setApprovalFilter,
        nextPage,
        previousPage,
    } = useSessions();
 
    const [selectedSessionId, setSelectedSessionId] = useState<number | null>(null);
    const [showDetailModal, setShowDetailModal] = useState(false);
 
    // Initial load
    useEffect(() => {
        fetchSessions();
    }, [fetchSessions]);
 
    // Auto-refresh
    useEffect(() => {
        if (autoRefresh) {
            const interval = setInterval(() => {
                fetchSessions();
            }, 10000); // Refresh every 10 seconds
            return () => clearInterval(interval);
        }
    }, [autoRefresh, fetchSessions]);
 
    const handleSessionClick = (sessionId: number) => {
        setSelectedSessionId(sessionId);
        setShowDetailModal(true);
    };
 
    const getStatusColor = (status: string) => {
        switch (status) {
            case 'pending':
                return 'text-yellow-400 bg-yellow-900/20';
            case 'in_progress':
            case 'processing':
                return 'text-blue-400 bg-blue-900/20';
            case 'completed':
                return 'text-green-400 bg-green-900/20';
            case 'failed':
                return 'text-red-400 bg-red-900/20';
            default:
                return 'text-slate-400 bg-slate-900/20';
        }
    };
 
    const getStatusIcon = (status: string) => {
        switch (status) {
            case 'pending':
                return <Clock className="w-4 h-4" />;
            case 'in_progress':
            case 'processing':
                return <RefreshCw className="w-4 h-4 animate-spin" />;
            case 'completed':
                return <CheckCircle className="w-4 h-4" />;
            case 'failed':
                return <AlertCircle className="w-4 h-4" />;
            default:
                return <Zap className="w-4 h-4" />;
        }
    };
 
    const getApprovalBadge = (approvalStatus?: string) => {
        switch (approvalStatus) {
            case 'approved':
                return (
                    <span className="inline-flex items-center gap-1 px-2 py-1 rounded text-xs font-semibold bg-green-900/20 text-green-400">
                        <CheckCircle className="w-3 h-3" />
                        APPROVED
                    </span>
                );
            case 'rejected':
                return (
                    <span className="inline-flex items-center gap-1 px-2 py-1 rounded text-xs font-semibold bg-red-900/20 text-red-400">
                        <XCircle className="w-3 h-3" />
                        REJECTED
                    </span>
                );
            default:
                return (
                    <span className="inline-flex items-center gap-1 px-2 py-1 rounded text-xs font-semibold bg-yellow-900/20 text-yellow-400">
                        <Clock className="w-3 h-3" />
                        PENDING
                    </span>
                );
        }
    };
 
    const formatDate = (dateString: string) => {
        const date = new Date(dateString);
        return date.toLocaleString();
    };
 
    const formatDuration = (seconds?: number | null) => {
        if (!seconds) return 'N/A';
        if (seconds < 60) return `${seconds}s`;
        if (seconds < 3600) return `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
        return `${Math.floor(seconds / 3600)}h ${Math.floor((seconds % 3600) / 60)}m`;
    };
 
    return (
        <>
            <Card className="bg-slate-900 border-slate-800 w-full">
                <CardHeader>
                    <div className="flex items-center justify-between">
                        <CardTitle className="text-white">Recording Sessions</CardTitle>
                        <Button
                            size="sm"
                            variant="outline"
                            onClick={() => fetchSessions()}
                            className="border-slate-700 text-slate-300 hover:bg-slate-800"
                        >
                            <RefreshCw className="w-4 h-4 mr-2" />
                            Refresh
                        </Button>
                    </div>
 
                    {/* Filters */}
                    <div className="flex flex-wrap gap-2 mt-4">
                        <div className="flex items-center gap-2">
                            <Filter className="w-4 h-4 text-slate-400" />
                            <span className="text-sm text-slate-400">Status:</span>
                            <Button
                                size="sm"
                                variant={statusFilter === null ? 'default' : 'outline'}
                                onClick={() => setStatusFilter(null)}
                                className="h-7 text-xs"
                            >
                                All
                            </Button>
                            <Button
                                size="sm"
                                variant={statusFilter === 'completed' ? 'default' : 'outline'}
                                onClick={() => setStatusFilter('completed')}
                                className="h-7 text-xs"
                            >
                                Completed
                            </Button>
                            <Button
                                size="sm"
                                variant={statusFilter === 'pending' ? 'default' : 'outline'}
                                onClick={() => setStatusFilter('pending')}
                                className="h-7 text-xs"
                            >
                                Pending
                            </Button>
                            <Button
                                size="sm"
                                variant={statusFilter === 'failed' ? 'default' : 'outline'}
                                onClick={() => setStatusFilter('failed')}
                                className="h-7 text-xs"
                            >
                                Failed
                            </Button>
                        </div>
 
                        <div className="flex items-center gap-2 ml-4">
                            <span className="text-sm text-slate-400">Approval:</span>
                            <Button
                                size="sm"
                                variant={approvalFilter === null ? 'default' : 'outline'}
                                onClick={() => setApprovalFilter(null)}
                                className="h-7 text-xs"
                            >
                                All
                            </Button>
                            <Button
                                size="sm"
                                variant={approvalFilter === 'pending' ? 'default' : 'outline'}
                                onClick={() => setApprovalFilter('pending')}
                                className="h-7 text-xs"
                            >
                                Pending
                            </Button>
                            <Button
                                size="sm"
                                variant={approvalFilter === 'approved' ? 'default' : 'outline'}
                                onClick={() => setApprovalFilter('approved')}
                                className="h-7 text-xs"
                            >
                                Approved
                            </Button>
                            <Button
                                size="sm"
                                variant={approvalFilter === 'rejected' ? 'default' : 'outline'}
                                onClick={() => setApprovalFilter('rejected')}
                                className="h-7 text-xs"
                            >
                                Rejected
                            </Button>
                        </div>
                    </div>
                </CardHeader>
 
                <CardContent>
                    {isLoading && sessions.length === 0 ? (
                        <div className="flex items-center justify-center py-12">
                            <RefreshCw className="w-8 h-8 animate-spin text-purple-500 mr-3" />
                            <p className="text-slate-400">Loading sessions...</p>
                        </div>
                    ) : sessions.length === 0 ? (
                        <div className="text-center py-12">
                            <Zap className="w-12 h-12 text-slate-600 mx-auto mb-4" />
                            <p className="text-slate-400">No sessions found</p>
                            <p className="text-slate-500 text-sm">
                                {statusFilter || approvalFilter
                                    ? 'Try adjusting your filters'
                                    : 'Create one using the form above'}
                            </p>
                        </div>
                    ) : (
                        <>
                            {/* Session List */}
                            <div className="space-y-3">
                                {sessions.map((session) => (
                                    <div
                                        key={session.id}
                                        className="bg-slate-800/50 border border-slate-700 rounded-lg p-4 hover:border-slate-600 transition cursor-pointer"
                                        onClick={() => handleSessionClick(session.id)}
                                    >
                                        <div className="flex items-start justify-between gap-4">
                                            {/* Left side - Info */}
                                            <div className="flex-1 min-w-0">
                                                <div className="flex items-center gap-2 mb-2 flex-wrap">
                                                    <h3 className="text-white font-semibold truncate">
                                                        {session.session_name}
                                                    </h3>
                                                    <span
                                                        className={`inline-flex items-center gap-1 px-2 py-1 rounded text-xs font-semibold ${getStatusColor(
                                                            session.status
                                                        )}`}
                                                    >
                                                        {getStatusIcon(session.status)}
                                                        {session.status.toUpperCase()}
                                                    </span>
                                                    {getApprovalBadge(session.approval_status)}
                                                </div>
 
                                                <div className="grid grid-cols-2 gap-2 text-xs text-slate-400 mb-2">
                                                    <div>
                                                        <span className="text-slate-500">Source:</span>{' '}
                                                        {session.source_name || 'N/A'}
                                                    </div>
                                                    <div>
                                                        <span className="text-slate-500">Frequency:</span>{' '}
                                                        {session.source_frequency
                                                            ? `${(session.source_frequency / 1e6).toFixed(3)} MHz`
                                                            : 'N/A'}
                                                    </div>
                                                    <div>
                                                        <span className="text-slate-500">Duration:</span>{' '}
                                                        {formatDuration(session.duration_seconds)}
                                                    </div>
                                                    <div>
                                                        <span className="text-slate-500">Created:</span>{' '}
                                                        {formatDate(session.created_at)}
                                                    </div>
                                                    <div>
                                                        <span className="text-slate-500">Measurements:</span>{' '}
                                                        {session.measurements_count || 0}
                                                    </div>
                                                </div>
 
                                                {/* Notes preview */}
                                                {session.notes && (
                                                    <div className="text-xs text-slate-500 italic truncate">
                                                        {session.notes}
                                                    </div>
                                                )}
                                            </div>
 
                                            {/* Right side - Actions */}
                                            <div className="flex gap-2 shrink-0">
                                                <Button
                                                    size="sm"
                                                    variant="outline"
                                                    onClick={(e) => {
                                                        e.stopPropagation();
                                                        handleSessionClick(session.id);
                                                    }}
                                                    className="border-slate-700 text-slate-300 hover:bg-slate-700"
                                                    title="View details"
                                                >
                                                    <Eye className="w-4 h-4" />
                                                </Button>
                                            </div>
                                        </div>
                                    </div>
                                ))}
                            </div>
 
                            {/* Pagination */}
                            {totalPages > 1 && (
                                <div className="flex items-center justify-between mt-6 pt-4 border-t border-slate-700">
                                    <div className="text-sm text-slate-400">
                                        Page {currentPage} of {totalPages} ({totalSessions} total)
                                    </div>
                                    <div className="flex gap-2">
                                        <Button
                                            size="sm"
                                            variant="outline"
                                            onClick={previousPage}
                                            disabled={currentPage === 1}
                                            className="border-slate-700"
                                        >
                                            <ChevronLeft className="w-4 h-4" />
                                            Previous
                                        </Button>
                                        <Button
                                            size="sm"
                                            variant="outline"
                                            onClick={nextPage}
                                            disabled={currentPage === totalPages}
                                            className="border-slate-700"
                                        >
                                            Next
                                            <ChevronRight className="w-4 h-4" />
                                        </Button>
                                    </div>
                                </div>
                            )}
                        </>
                    )}
                </CardContent>
            </Card>
 
            {/* Session Detail Modal */}
            <SessionDetailModal
                isOpen={showDetailModal}
                onClose={() => {
                    setShowDetailModal(false);
                    setSelectedSessionId(null);
                }}
                sessionId={selectedSessionId}
            />
        </>
    );
};
 
export default SessionListEnhanced;