List.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React from 'react';
  2. import { ColumnDef } from '@tanstack/react-table';
  3. import { useTheme } from 'styled-components';
  4. import PageHeading from 'components/common/PageHeading/PageHeading';
  5. import Table from 'components/common/NewTable';
  6. import DeleteIcon from 'components/common/Icons/DeleteIcon';
  7. import { useConfirm } from 'lib/hooks/useConfirm';
  8. import useAppParams from 'lib/hooks/useAppParams';
  9. import { useAcls, useDeleteAcl } from 'lib/hooks/api/acl';
  10. import { ClusterName } from 'redux/interfaces';
  11. import {
  12. KafkaAcl,
  13. KafkaAclNamePatternType,
  14. KafkaAclPermissionEnum,
  15. } from 'generated-sources';
  16. import * as S from './List.styled';
  17. const ACList: React.FC = () => {
  18. const { clusterName } = useAppParams<{ clusterName: ClusterName }>();
  19. const theme = useTheme();
  20. const { data: aclList } = useAcls(clusterName);
  21. const { deleteResource } = useDeleteAcl(clusterName);
  22. const modal = useConfirm(true);
  23. const [rowId, setRowId] = React.useState('');
  24. const onDeleteClick = (acl: KafkaAcl | null) => {
  25. if (acl) {
  26. modal('Are you sure want to delete this ACL record?', () =>
  27. deleteResource(acl)
  28. );
  29. }
  30. };
  31. const columns = React.useMemo<ColumnDef<KafkaAcl>[]>(
  32. () => [
  33. {
  34. header: 'Principal',
  35. accessorKey: 'principal',
  36. size: 257,
  37. },
  38. {
  39. header: 'Resource',
  40. accessorKey: 'resourceType',
  41. // eslint-disable-next-line react/no-unstable-nested-components
  42. cell: ({ getValue }) => (
  43. <S.EnumCell>{getValue<string>().toLowerCase()}</S.EnumCell>
  44. ),
  45. size: 145,
  46. },
  47. {
  48. header: 'Pattern',
  49. accessorKey: 'resourceName',
  50. // eslint-disable-next-line react/no-unstable-nested-components
  51. cell: ({ getValue, row }) => {
  52. let chipType;
  53. if (
  54. row.original.namePatternType === KafkaAclNamePatternType.PREFIXED
  55. ) {
  56. chipType = 'default';
  57. }
  58. if (
  59. row.original.namePatternType === KafkaAclNamePatternType.LITERAL
  60. ) {
  61. chipType = 'secondary';
  62. }
  63. return (
  64. <S.PatternCell>
  65. {getValue<string>()}
  66. {chipType ? (
  67. <S.Chip chipType={chipType}>
  68. {row.original.namePatternType.toLowerCase()}
  69. </S.Chip>
  70. ) : null}
  71. </S.PatternCell>
  72. );
  73. },
  74. size: 257,
  75. },
  76. {
  77. header: 'Host',
  78. accessorKey: 'host',
  79. size: 257,
  80. },
  81. {
  82. header: 'Operation',
  83. accessorKey: 'operation',
  84. // eslint-disable-next-line react/no-unstable-nested-components
  85. cell: ({ getValue }) => (
  86. <S.EnumCell>{getValue<string>().toLowerCase()}</S.EnumCell>
  87. ),
  88. size: 121,
  89. },
  90. {
  91. header: 'Permission',
  92. accessorKey: 'permission',
  93. // eslint-disable-next-line react/no-unstable-nested-components
  94. cell: ({ getValue }) => (
  95. <S.Chip
  96. chipType={
  97. getValue<string>() === KafkaAclPermissionEnum.ALLOW
  98. ? 'success'
  99. : 'danger'
  100. }
  101. >
  102. {getValue<string>().toLowerCase()}
  103. </S.Chip>
  104. ),
  105. size: 111,
  106. },
  107. {
  108. id: 'delete',
  109. // eslint-disable-next-line react/no-unstable-nested-components
  110. cell: ({ row }) => {
  111. return (
  112. <S.DeleteCell onClick={() => onDeleteClick(row.original)}>
  113. <DeleteIcon
  114. fill={
  115. rowId === row.id ? theme.acl.table.deleteIcon : 'transparent'
  116. }
  117. />
  118. </S.DeleteCell>
  119. );
  120. },
  121. size: 76,
  122. },
  123. ],
  124. [rowId]
  125. );
  126. const onRowHover = (value: unknown) => {
  127. if (value && typeof value === 'object' && 'id' in value) {
  128. setRowId(value.id as string);
  129. }
  130. };
  131. return (
  132. <>
  133. <PageHeading text="Access Control List" />
  134. <Table
  135. columns={columns}
  136. data={aclList ?? []}
  137. emptyMessage="No ACL items found"
  138. onRowHover={onRowHover}
  139. onMouseLeave={() => setRowId('')}
  140. />
  141. </>
  142. );
  143. };
  144. export default ACList;