users-provider.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import React, { useState } from 'react'
  2. import useDialogState from '@/hooks/use-dialog-state'
  3. import { type User } from '../data/schema'
  4. type UsersDialogType = 'invite' | 'add' | 'edit' | 'delete'
  5. type UsersContextType = {
  6. open: UsersDialogType | null
  7. setOpen: (str: UsersDialogType | null) => void
  8. currentRow: User | null
  9. setCurrentRow: React.Dispatch<React.SetStateAction<User | null>>
  10. }
  11. const UsersContext = React.createContext<UsersContextType | null>(null)
  12. export function UsersProvider({ children }: { children: React.ReactNode }) {
  13. const [open, setOpen] = useDialogState<UsersDialogType>(null)
  14. const [currentRow, setCurrentRow] = useState<User | null>(null)
  15. return (
  16. <UsersContext value={{ open, setOpen, currentRow, setCurrentRow }}>
  17. {children}
  18. </UsersContext>
  19. )
  20. }
  21. // eslint-disable-next-line react-refresh/only-export-components
  22. export const useUsers = () => {
  23. const usersContext = React.useContext(UsersContext)
  24. if (!usersContext) {
  25. throw new Error('useUsers has to be used within <UsersContext>')
  26. }
  27. return usersContext
  28. }