managed-users-select-user-checkbox.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { useTranslation } from 'react-i18next'
  2. import type { User } from '../../../../../../types/group-management/user'
  3. import { useGroupMembersContext } from '../../context/group-members-context'
  4. import { useCallback } from 'react'
  5. import getMeta from '@/utils/meta'
  6. type ManagedUsersSelectUserCheckboxProps = {
  7. user: User
  8. }
  9. export default function ManagedUsersSelectUserCheckbox({
  10. user,
  11. }: ManagedUsersSelectUserCheckboxProps) {
  12. const { t } = useTranslation()
  13. const groupSSOActive = getMeta('ol-groupSSOActive')
  14. const { users, selectedUsers, selectUser, unselectUser } =
  15. useGroupMembersContext()
  16. const handleSelectUser = useCallback(
  17. (event, user) => {
  18. if (event.target.checked) {
  19. selectUser(user)
  20. } else {
  21. unselectUser(user)
  22. }
  23. },
  24. [selectUser, unselectUser]
  25. )
  26. // Pending: user.enrollment will be `undefined`
  27. // Non managed: user.enrollment will be an empty object
  28. const nonManagedUsers = users.filter(user => !user.enrollment?.managedBy)
  29. // Hide the entire `td` (entire column) if no more users available to be click
  30. // because all users are currently managed
  31. if (nonManagedUsers.length === 0) {
  32. return null
  33. }
  34. const selected = selectedUsers.includes(user)
  35. return (
  36. <td
  37. className={
  38. groupSSOActive ? 'cell-checkbox-with-sso-col ' : 'cell-checkbox'
  39. }
  40. >
  41. {/* the next check will hide the `checkbox` but still show the `td` */}
  42. {user.enrollment?.managedBy ? null : (
  43. <>
  44. <label htmlFor={`select-user-${user.email}`} className="sr-only">
  45. {t('select_user')}
  46. </label>
  47. <input
  48. className="select-item"
  49. id={`select-user-${user.email}`}
  50. type="checkbox"
  51. checked={selected}
  52. onChange={e => handleSelectUser(e, user)}
  53. />
  54. </>
  55. )}
  56. </td>
  57. )
  58. }