# All files / src/components/admin BlacklistedDevicesTable.tsx

0% Statements 0/40
0% Branches 0/9
0% Functions 0/7
0% Lines 0/39

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

    import React, { useState, useEffect } from 'react';
    import { SecurityService } from '../../services/securityService';
    import { toast } from 'react-hot-toast';
    import { useTranslation } from 'react-i18next';
    import useLoadNamespace from '@/hooks/useLoadNamespace';

    interface BlacklistedDevice {
      user_id: number;
      username: string;
      device_id: string;
      blacklisted_at: string;
    }

    const BlacklistedDevicesTable: React.FC = () => {
      useLoadNamespace('admin/blacklistedDevices');
      const { t, i18n } = useTranslation(['admin/blacklistedDevices', 'admin', 'translation']);
      const [devices, setDevices] = useState<BlacklistedDevice[]>([]);
      const [loading, setLoading] = useState(true);
      const [removingDevice, setRemovingDevice] = useState<string | null>(null);

      useEffect(() => {
        loadBlacklistedDevices();
      }, []);

      const loadBlacklistedDevices = async () => {
        try {
          setLoading(true);
          const response = await SecurityService.getBlacklistedDevices();
          setDevices(response.devices);
          } catch (error) {
          console.error('Error loading blacklisted devices:', error);
          toast.error(t('notifications.blacklist.errorLoading'));
        } finally {
          setLoading(false);
        }
      };

      const handleRemoveFromBlacklist = async (device: BlacklistedDevice) => {
        if (!confirm(t('notifications.blacklist.confirmRemove', { device: device.device_id, user: device.username }))) {
          return;
        }

        try {
          setRemovingDevice(device.device_id);
          await SecurityService.removeDeviceFromBlacklist(device.user_id, device.device_id);
          toast.success(t('notifications.blacklist.removed'));
          loadBlacklistedDevices(); // Refresh the list
        } catch (error) {
          console.error('Error removing device from blacklist:', error);
          toast.error(t('notifications.blacklist.removeFailed'));
        } finally {
          setRemovingDevice(null);
        }
      };

      const formatDate = (dateString: string) => {
        const locale = i18n?.language || 'en-US';
        return new Date(dateString).toLocaleString(locale, {
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit',
          second: '2-digit'});
      };

      if (loading) {
        return (
          <div className="flex items-center justify-center h-64">
            <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
          </div>
        );
      }

      return (
        <div className="space-y-4">
          {/* Header */}
          <div className="flex items-center justify-between">
                      <h3 className="text-lg font-medium text-gray-900">
                        {t('title', { count: devices.length })}
                      </h3>
              <button
                onClick={loadBlacklistedDevices}
                className="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
              >
                <svg className="h-4 w-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                </svg>
                {t('common.refresh')}
              </button>
          </div>

          {/* Table */}
          <div className="bg-white shadow overflow-hidden sm:rounded-md">
            <div className="px-4 py-5 sm:p-6">
              {devices.length === 0 ? (
                <div className="text-center py-8">
                  <svg className="h-12 w-12 text-gray-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                  </svg>
                    <h3 className="text-lg font-medium text-gray-900 mb-2">
                      {t('empty.title')}
                    </h3>
                    <p className="text-gray-500">
                      {t('empty.description')}
                    </p>
                </div>
              ) : (
                <div className="overflow-x-auto">
                  <table className="min-w-full divide-y divide-gray-200">
                    <thead className="bg-gray-50">
                      <tr>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('table.username')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('table.deviceId')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('table.blacklistedAt')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('table.actions')}
                        </th>
                      </tr>
                    </thead>
                    <tbody className="bg-white divide-y divide-gray-200">
                      {devices.map((device) => (
                        <tr key={`${device.user_id}-${device.device_id}`} className="hover:bg-gray-50">
                          <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            {device.username}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500 font-mono">
                            {device.device_id}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {formatDate(device.blacklisted_at)}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            <button
                              onClick={() => handleRemoveFromBlacklist(device)}
                              disabled={removingDevice === device.device_id}
                              className="text-blue-600 hover:text-blue-900 disabled:opacity-50 disabled:cursor-not-allowed"
                            >
                                  {removingDevice === device.device_id ? (
                                <div className="flex items-center">
                                  <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600 mr-2"></div>
                                  {t('common.removing')}
                                </div>
                              ) : (
                                t('actions.remove')
                              )}
                            </button>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              )}
            </div>
          </div>
        </div>
      );
    };

    export default BlacklistedDevicesTable;