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

0% Statements 0/53
0% Branches 0/19
0% Functions 0/14
0% Lines 0/49

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

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

    interface SecurityBansTableProps {
      onRefresh?: () => void;
    }

    const SecurityBansTable: React.FC<SecurityBansTableProps> = ({ onRefresh }) => {
      useLoadNamespace('admin/securityBans');
      const { t } = useTranslation(['admin/securityBans','admin','translation']);
      const [bans, setBans] = useState<SecurityBan[]>([]);
      const [loading, setLoading] = useState(true);
      const [filters, setFilters] = useState<SecurityFilters>({
        page: 1,
        limit: 20});
      const [total, setTotal] = useState(0);
      const [totalPages, setTotalPages] = useState(0);

      const loadBans = useCallback(async () => {
        try {
          setLoading(true);
          const response = await SecurityService.getSecurityBans(filters);
          setBans(response.bans);
          setTotal(response.total);
          setTotalPages(response.total_pages);
        } catch (error) {
          console.error('Error loading bans:', error);
          toast.error(t('notifications.security.errorLoadingBans'));
        } finally {
          setLoading(false);
        }
      }, [filters]);

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

      const handleUnban = async (username: string) => {
        if (!confirm(t('unban.confirm', { user: username }))) {
          return;
        }

        try {
          const reason = prompt(t('unban.promptReason'));
          if (!reason) return;

          await SecurityService.unbanUser(username, reason);
          toast.success(t('notifications.ban.userUnbanned', { user: username }));
          loadBans(); // Refresh the list
          onRefresh?.(); // Refresh parent stats
        } catch (error) {
          console.error('Error unbanning user:', error);
          toast.error(t('notifications.security.errorUnbanningUser'));
        }
      };

      const handleFilterChange = (key: keyof SecurityFilters, value: string | number | undefined) => {
        setFilters(prev => ({
          ...prev,
          [key]: value,
          page: 1, // Reset to first page when filtering
        }));
      };

      const handlePageChange = (page: number) => {
        setFilters(prev => ({ ...prev, page }));
      };

      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">
          {/* Filters */}
          <div className="bg-gray-50 p-4 rounded-lg">
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              <div>
                <label className="block text-sm font-medium text-gray-700 mb-1">
                  {t('common.username')}
                </label>
                <input
                  type="text"
                  value={filters.username || ''}
                  onChange={(e) => handleFilterChange('username', e.target.value || undefined)}
                  placeholder={t('admin.securityBans.searchByUsernamePlaceholder')}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                />
              </div>
              <div>
                <label className="block text-sm font-medium text-gray-700 mb-1">
                  {t('itemsPerPage')}
                </label>
                <select
                  value={filters.limit || 20}
                  onChange={(e) => handleFilterChange('limit', parseInt(e.target.value))}
                  className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                >
                  <option value={10}>10</option>
                  <option value={20}>20</option>
                  <option value={50}>50</option>
                  <option value={100}>100</option>
                </select>
              </div>
            </div>
          </div>

          {/* Table */}
          <div className="bg-white shadow overflow-hidden sm:rounded-md">
            <div className="px-4 py-5 sm:p-6">
                <div className="flex items-center justify-between mb-4">
                  <h3 className="text-lg font-medium text-gray-900">
                    {t('title', { total })}
                  </h3>
                </div>

                {bans.length === 0 ? (
                <div className="text-center py-8">
                  <p className="text-gray-500">{t('noneFound')}</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('common.username')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('admin.securityBans.level')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('admin.securityBans.type')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('admin.securityBans.reason')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('admin.securityBans.timeRemaining')}
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                          {t('common.actions')}
                        </th>
                      </tr>
                    </thead>
                    <tbody className="bg-white divide-y divide-gray-200">
                      {bans.map((ban) => (
                        <tr key={ban.id} className="hover:bg-gray-50">
                          <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            {ban.username}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap">
                            <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
                              SecurityService.getBanSeverityColor(ban.ban_level)
                            }`}>
                              {SecurityService.getBanLevelLabel(ban.ban_level)}
                            </span>
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {ban.ban_type}
                          </td>
                          <td className="px-6 py-4 text-sm text-gray-500 max-w-xs truncate">
                            {ban.ban_reason}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {SecurityService.calculateTimeRemaining(ban.expires_at)}
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap text-sm font-medium">
                            {ban.is_active && (
                              <button
                                onClick={() => handleUnban(ban.username)}
                                className="text-red-600 hover:text-red-900"
                              >
                                {t('unban.action')}
                              </button>
                            )}
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              )}

              {/* Pagination */}
              {totalPages > 1 && (
                <div className="flex items-center justify-between mt-6">
                  <div className="text-sm text-gray-700">
                    {t('common.paginationShowing', { page: filters.page, totalPages })}
                  </div>
                  <div className="flex space-x-2">
                    <button
                      onClick={() => handlePageChange((filters.page || 1) - 1)}
                      disabled={filters.page === 1}
                      className="px-3 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed"
                    >
                      {t('common.previous')}
                    </button>
                    <button
                      onClick={() => handlePageChange((filters.page || 1) + 1)}
                      disabled={filters.page === totalPages}
                      className="px-3 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed"
                    >
                      {t('common.next')}
                    </button>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      );
    };

    export default SecurityBansTable;