All files / frontend/src/components SessionsList.tsx

90.44% Statements 142/157
77.14% Branches 27/35
71.42% Functions 5/7
90.44% Lines 142/157

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    1x 1x                   1x 1x 1x             1x 27x 27x 27x 27x 27x     27x 14x 27x     27x 14x 13x 2x 13x 13x 13x 13x 27x   27x 63x 63x 21x 63x   63x 21x 63x 21x 63x   63x 63x   27x 63x 63x 21x 63x   63x 21x 63x 21x 63x   63x 63x   27x 63x 63x 63x 63x   27x 63x 42x   63x   27x 27x 27x 27x 27x 27x 27x 27x 27x 27x   27x   27x 27x 27x 27x 27x 2x 2x 2x 2x 25x 4x 4x 4x 4x 4x   21x 21x 63x 63x 63x   63x   63x 63x 63x 63x 63x 63x 63x   63x 63x 63x 63x   63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x     63x         63x     63x 63x 63x 63x 63x 63x 63x   63x 63x   63x                     63x 21x 21x 21x 21x 21x   21x 21x   63x 63x 63x 21x 21x   27x 27x   27x   1x  
'use client';
 
import React, { useEffect, useState } from 'react';
import {
    Clock,
    CheckCircle,
    AlertCircle,
    RefreshCw,
    Download,
    Eye,
    Trash2,
    Zap,
} from 'lucide-react';
import { useSessionStore } from '../store/sessionStore';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
 
interface SessionsListProps {
    onSessionSelect?: (sessionId: number) => void;
    autoRefresh?: boolean;
}
 
export const SessionsList: React.FC<SessionsListProps> = ({
    onSessionSelect,
    autoRefresh = true,
}) => {
    const { sessions, isLoading, fetchSessions } = useSessionStore();
    const [, setAutoRefreshInterval] = useState<NodeJS.Timeout | null>(null);
 
    // Initial load
    useEffect(() => {
        fetchSessions();
    }, [fetchSessions]);
 
    // Auto-refresh
    useEffect(() => {
        if (autoRefresh) {
            const interval = setInterval(() => {
                fetchSessions();
            }, 5000); // Refresh every 5 seconds
            setAutoRefreshInterval(interval);
            return () => clearInterval(interval);
        }
    }, [autoRefresh, fetchSessions]);
 
    const getStatusColor = (status: string) => {
        switch (status) {
            case 'pending':
                return 'text-yellow-400 bg-yellow-900/20';
            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 '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 formatDate = (dateString: string | null) => {
        if (!dateString) return 'N/A';
        const date = new Date(dateString);
        return date.toLocaleString();
    };
 
    const formatDuration = (seconds: number) => {
        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 Queue</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>
            </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 recording sessions yet</p>
                        <p className="text-slate-500 text-sm">Create one using the form above</p>
                    </div>
                ) : (
                    <div className="space-y-3 max-h-96 overflow-y-auto">
                        {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"
                            >
                                <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">
                                            <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>
                                        </div>
 
                                        <div className="grid grid-cols-2 gap-2 text-xs text-slate-400 mb-2">
                                            <div>
                                                <span className="text-slate-500">Frequency:</span> {session.frequency_mhz.toFixed(3)} MHz
                                            </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">Receivers:</span> {session.websdrs_enabled}
                                            </div>
                                        </div>
 
                                        {/* Error message if failed */}
                                        {session.status === 'failed' && session.error_message && (
                                            <div className="text-xs text-red-400 bg-red-900/20 px-2 py-1 rounded mt-2">
                                                <strong>Error:</strong> {session.error_message}
                                            </div>
                                        )}
                                    </div>
 
                                    {/* Right side - Actions */}
                                    <div className="flex gap-2 shrink-0">
                                        <Button
                                            size="sm"
                                            variant="outline"
                                            onClick={() => onSessionSelect?.(session.id)}
                                            className="border-slate-700 text-slate-300 hover:bg-slate-700"
                                            title="View details"
                                        >
                                            <Eye className="w-4 h-4" />
                                        </Button>
 
                                        {session.status === 'completed' && session.minio_path && (
                                            <Button
                                                size="sm"
                                                variant="outline"
                                                className="border-slate-700 text-green-400 hover:bg-slate-700"
                                                title="Download results"
                                            >
                                                <Download className="w-4 h-4" />
                                            </Button>
                                        )}
 
                                        {session.status === 'pending' && (
                                            <Button
                                                size="sm"
                                                variant="outline"
                                                className="border-slate-700 text-red-400 hover:bg-slate-700"
                                                title="Cancel session"
                                            >
                                                <Trash2 className="w-4 h-4" />
                                            </Button>
                                        )}
                                    </div>
                                </div>
                            </div>
                        ))}
                    </div>
                )}
            </CardContent>
        </Card>
    );
};
 
export default SessionsList;