share-project-modal.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, {
  2. createContext,
  3. useCallback,
  4. useContext,
  5. useEffect,
  6. useState,
  7. } from 'react'
  8. import PropTypes from 'prop-types'
  9. import ShareProjectModalContent from './share-project-modal-content'
  10. import {
  11. useProjectContext,
  12. projectShape,
  13. } from '../../../shared/context/project-context'
  14. import { useSplitTestContext } from '../../../shared/context/split-test-context'
  15. import { sendMB } from '../../../infrastructure/event-tracking'
  16. type ShareProjectContextValue = {
  17. updateProject: (data: unknown) => void
  18. monitorRequest: <T extends Promise<unknown>>(request: () => T) => T
  19. inFlight: boolean
  20. setInFlight: React.Dispatch<
  21. React.SetStateAction<ShareProjectContextValue['inFlight']>
  22. >
  23. error: string | undefined
  24. setError: React.Dispatch<
  25. React.SetStateAction<ShareProjectContextValue['error']>
  26. >
  27. }
  28. const ShareProjectContext = createContext<ShareProjectContextValue | undefined>(
  29. undefined
  30. )
  31. export function useShareProjectContext() {
  32. const context = useContext(ShareProjectContext)
  33. if (!context) {
  34. throw new Error(
  35. 'useShareProjectContext is only available inside ShareProjectProvider'
  36. )
  37. }
  38. return context
  39. }
  40. type ShareProjectModalProps = {
  41. handleHide: () => void
  42. show: boolean
  43. animation?: boolean
  44. }
  45. const ShareProjectModal = React.memo(function ShareProjectModal({
  46. handleHide,
  47. show,
  48. animation = true,
  49. }: ShareProjectModalProps) {
  50. const [inFlight, setInFlight] =
  51. useState<ShareProjectContextValue['inFlight']>(false)
  52. const [error, setError] = useState<ShareProjectContextValue['error']>()
  53. const project = useProjectContext(projectShape)
  54. const { splitTestVariants } = useSplitTestContext({
  55. splitTestVariants: PropTypes.object,
  56. })
  57. // send tracking event when the modal is opened
  58. useEffect(() => {
  59. if (show) {
  60. sendMB('share-modal-opened', {
  61. splitTestVariant: splitTestVariants['null-test-share-modal'],
  62. project_id: project._id,
  63. })
  64. }
  65. }, [splitTestVariants, project._id, show])
  66. // reset error when the modal is opened
  67. useEffect(() => {
  68. if (show) {
  69. setError(undefined)
  70. }
  71. }, [show])
  72. // close the modal if not in flight
  73. const cancel = useCallback(() => {
  74. if (!inFlight) {
  75. handleHide()
  76. }
  77. }, [handleHide, inFlight])
  78. // update `error` and `inFlight` while sending a request
  79. const monitorRequest = useCallback(request => {
  80. setError(undefined)
  81. setInFlight(true)
  82. const promise = request()
  83. promise.catch((error: { data?: Record<string, string> }) => {
  84. setError(
  85. error.data?.errorReason ||
  86. error.data?.error ||
  87. 'generic_something_went_wrong'
  88. )
  89. })
  90. promise.finally(() => {
  91. setInFlight(false)
  92. })
  93. return promise
  94. }, [])
  95. // merge the new data with the old project data
  96. const updateProject = useCallback(
  97. data => Object.assign(project, data),
  98. [project]
  99. )
  100. if (!project) {
  101. return null
  102. }
  103. return (
  104. <ShareProjectContext.Provider
  105. value={{
  106. updateProject,
  107. monitorRequest,
  108. inFlight,
  109. setInFlight,
  110. error,
  111. setError,
  112. }}
  113. >
  114. <ShareProjectModalContent
  115. animation={animation}
  116. cancel={cancel}
  117. error={error}
  118. inFlight={inFlight}
  119. show={show}
  120. />
  121. </ShareProjectContext.Provider>
  122. )
  123. })
  124. export default ShareProjectModal