BannedIPsTable.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Table, Button } from 'antd';
  2. import format from 'date-fns/format';
  3. import { SortOrder } from 'antd/lib/table/interface';
  4. import React, { FC } from 'react';
  5. import { StopTwoTone } from '@ant-design/icons';
  6. import { User } from '../types/chat';
  7. import { BANNED_IP_REMOVE, fetchData } from '../utils/apis';
  8. function formatDisplayDate(date: string | Date) {
  9. return format(new Date(date), 'MMM d H:mma');
  10. }
  11. async function removeIPAddressBan(ipAddress: String) {
  12. try {
  13. await fetchData(BANNED_IP_REMOVE, {
  14. data: { value: ipAddress },
  15. method: 'POST',
  16. auth: true,
  17. });
  18. } catch (e) {
  19. // eslint-disable-next-line no-console
  20. console.error(e);
  21. }
  22. }
  23. export type UserTableProps = {
  24. data: User[];
  25. };
  26. export const BannedIPsTable: FC<UserTableProps> = ({ data }) => {
  27. const columns = [
  28. {
  29. title: 'IP Address',
  30. dataIndex: 'ipAddress',
  31. key: 'ipAddress',
  32. },
  33. {
  34. title: 'Reason',
  35. dataIndex: 'notes',
  36. key: 'notes',
  37. },
  38. {
  39. title: 'Created',
  40. dataIndex: 'createdAt',
  41. key: 'createdAt',
  42. render: (date: Date) => formatDisplayDate(date),
  43. sorter: (a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime(),
  44. sortDirections: ['descend', 'ascend'] as SortOrder[],
  45. },
  46. {
  47. title: '',
  48. key: 'block',
  49. className: 'actions-col',
  50. render: (_, ip) => (
  51. <Button
  52. title="Remove IP Address Ban"
  53. onClick={() => removeIPAddressBan(ip.ipAddress)}
  54. icon={<StopTwoTone twoToneColor="#ff4d4f" />}
  55. className="block-user-button"
  56. />
  57. ),
  58. },
  59. ];
  60. return (
  61. <Table
  62. pagination={{ hideOnSinglePage: true }}
  63. className="table-container"
  64. columns={columns}
  65. dataSource={data}
  66. size="large"
  67. rowKey="ipAddress"
  68. />
  69. );
  70. };