overleaf/services/web/frontend/js/features/group-management/hooks/use-user-selection.ts
Alexandre Bourdin a4a5a08c31 Merge pull request #14256 from overleaf/ab-unmanaged-remove-from-group-dropdown-action
[web] Add Remove from group action in dropdown for unmanaged/pending users

GitOrigin-RevId: daa66598e42befa2f8430bdf118e907a8758d60e
2023-08-23 08:05:10 +00:00

28 lines
795 B
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 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,
}
}