overleaf/services/web/frontend/js/features/group-management/hooks/use-user-selection.ts
M Fahru 4019c69ea8 Merge pull request #14566 from overleaf/mf-fix-select-all-checkbox-managed-users
[web] Fix click all email checkbox behaviour on managed users

GitOrigin-RevId: 4c4e7171d4aed3d99bc08be4b029eb3badb0fac9
2023-08-31 08:04:26 +00:00

37 lines
1.1 KiB
TypeScript

import { useCallback, useState } from 'react'
import { User } from '../../../../../types/group-management/user'
export default function useUserSelection(initialUsers: User[]) {
const [users, setUsers] = useState<User[]>(initialUsers)
const [selectedUsers, setSelectedUsers] = useState<User[]>([])
const selectAllUsers = () => setSelectedUsers(users)
const unselectAllUsers = () => setSelectedUsers([])
const selectAllNonManagedUsers = useCallback(() => {
// Pending: user.enrollment will be `undefined`
// Not managed: user.enrollment will be an empty object
const nonManagedUsers = users.filter(user => !user.enrollment?.managedBy)
setSelectedUsers(nonManagedUsers)
}, [users])
const selectUser = useCallback((user: User) => {
setSelectedUsers(users => [...users, user])
}, [])
const unselectUser = useCallback((user: User) => {
setSelectedUsers(users => users.filter(u => u.email !== user.email))
}, [])
return {
users,
setUsers,
selectedUsers,
selectUser,
unselectUser,
selectAllUsers,
unselectAllUsers,
selectAllNonManagedUsers,
}
}