evaluations-provider.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { type Evaluation } from '@/types/evaluations.types'
  2. import React, { createContext, useContext, useState } from 'react'
  3. type EvaluationsDialogType = 'retry' | 'view' | 'approve' | 'edit'
  4. interface EvaluationsContextType {
  5. open: EvaluationsDialogType | null
  6. setOpen: (str: EvaluationsDialogType | null) => void
  7. currentRow: Evaluation | null
  8. setCurrentRow: React.Dispatch<React.SetStateAction<Evaluation | null>>
  9. }
  10. const EvaluationsContext = createContext<EvaluationsContextType | undefined>(
  11. undefined
  12. )
  13. interface Props {
  14. children: React.ReactNode
  15. }
  16. export function EvaluationsProvider({ children }: Props) {
  17. const [open, setOpen] = useState<EvaluationsDialogType | null>(null)
  18. const [currentRow, setCurrentRow] = useState<Evaluation | null>(null)
  19. return (
  20. <EvaluationsContext.Provider
  21. value={{
  22. open,
  23. setOpen,
  24. currentRow,
  25. setCurrentRow,
  26. }}
  27. >
  28. {children}
  29. </EvaluationsContext.Provider>
  30. )
  31. }
  32. // eslint-disable-next-line react-refresh/only-export-components
  33. export function useEvaluations() {
  34. const context = useContext(EvaluationsContext)
  35. if (!context) {
  36. throw new Error('useEvaluations must be used within EvaluationsProvider')
  37. }
  38. return context
  39. }