2021-03-12 05:23:46 -05:00
|
|
|
import React, {
|
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
2021-04-27 03:52:58 -04:00
|
|
|
useState,
|
2021-03-12 05:23:46 -05:00
|
|
|
} from 'react'
|
|
|
|
import ShareProjectModalContent from './share-project-modal-content'
|
2024-01-26 04:23:48 -05:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
2021-12-14 05:52:29 -05:00
|
|
|
import { useSplitTestContext } from '../../../shared/context/split-test-context'
|
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2024-01-26 04:23:48 -05:00
|
|
|
import { Project } from '../../../../../types/project'
|
2021-03-12 05:23:46 -05:00
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
type ShareProjectContextValue = {
|
2024-01-26 04:23:48 -05:00
|
|
|
updateProject: (project: Project) => void
|
2023-10-27 04:43:50 -04:00
|
|
|
monitorRequest: <T extends Promise<unknown>>(request: () => T) => T
|
|
|
|
inFlight: boolean
|
|
|
|
setInFlight: React.Dispatch<
|
|
|
|
React.SetStateAction<ShareProjectContextValue['inFlight']>
|
|
|
|
>
|
|
|
|
error: string | undefined
|
|
|
|
setError: React.Dispatch<
|
|
|
|
React.SetStateAction<ShareProjectContextValue['error']>
|
|
|
|
>
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
const ShareProjectContext = createContext<ShareProjectContextValue | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
export function useShareProjectContext() {
|
|
|
|
const context = useContext(ShareProjectContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useShareProjectContext is only available inside ShareProjectProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
type ShareProjectModalProps = {
|
|
|
|
handleHide: () => void
|
|
|
|
show: boolean
|
|
|
|
animation?: boolean
|
|
|
|
}
|
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const ShareProjectModal = React.memo(function ShareProjectModal({
|
2021-03-12 05:23:46 -05:00
|
|
|
handleHide,
|
|
|
|
show,
|
|
|
|
animation = true,
|
2023-10-27 04:43:50 -04:00
|
|
|
}: ShareProjectModalProps) {
|
|
|
|
const [inFlight, setInFlight] =
|
|
|
|
useState<ShareProjectContextValue['inFlight']>(false)
|
|
|
|
const [error, setError] = useState<ShareProjectContextValue['error']>()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const project = useProjectContext()
|
2021-03-31 06:44:20 -04:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const { splitTestVariants } = useSplitTestContext()
|
2021-12-14 05:52:29 -05:00
|
|
|
|
|
|
|
// send tracking event when the modal is opened
|
|
|
|
useEffect(() => {
|
|
|
|
if (show) {
|
|
|
|
sendMB('share-modal-opened', {
|
|
|
|
splitTestVariant: splitTestVariants['null-test-share-modal'],
|
2023-07-31 11:27:12 -04:00
|
|
|
project_id: project._id,
|
2021-12-14 05:52:29 -05:00
|
|
|
})
|
|
|
|
}
|
2023-07-31 11:27:12 -04:00
|
|
|
}, [splitTestVariants, project._id, show])
|
2021-12-14 05:52:29 -05:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
// reset error when the modal is opened
|
|
|
|
useEffect(() => {
|
|
|
|
if (show) {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
|
|
|
}, [show])
|
|
|
|
|
|
|
|
// close the modal if not in flight
|
|
|
|
const cancel = useCallback(() => {
|
|
|
|
if (!inFlight) {
|
|
|
|
handleHide()
|
|
|
|
}
|
|
|
|
}, [handleHide, inFlight])
|
|
|
|
|
|
|
|
// update `error` and `inFlight` while sending a request
|
|
|
|
const monitorRequest = useCallback(request => {
|
|
|
|
setError(undefined)
|
|
|
|
setInFlight(true)
|
|
|
|
|
|
|
|
const promise = request()
|
|
|
|
|
2023-10-27 04:43:50 -04:00
|
|
|
promise.catch((error: { data?: Record<string, string> }) => {
|
2021-03-12 05:23:46 -05:00
|
|
|
setError(
|
|
|
|
error.data?.errorReason ||
|
|
|
|
error.data?.error ||
|
|
|
|
'generic_something_went_wrong'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
promise.finally(() => {
|
|
|
|
setInFlight(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
return promise
|
|
|
|
}, [])
|
|
|
|
|
2021-03-31 06:44:20 -04:00
|
|
|
// merge the new data with the old project data
|
2022-01-10 05:23:05 -05:00
|
|
|
const updateProject = useCallback(
|
|
|
|
data => Object.assign(project, data),
|
|
|
|
[project]
|
|
|
|
)
|
2021-03-31 06:44:20 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
if (!project) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ShareProjectContext.Provider
|
|
|
|
value={{
|
|
|
|
updateProject,
|
|
|
|
monitorRequest,
|
|
|
|
inFlight,
|
|
|
|
setInFlight,
|
|
|
|
error,
|
2021-04-27 03:52:58 -04:00
|
|
|
setError,
|
2021-03-12 05:23:46 -05:00
|
|
|
}}
|
|
|
|
>
|
2021-06-25 04:13:17 -04:00
|
|
|
<ShareProjectModalContent
|
|
|
|
animation={animation}
|
|
|
|
cancel={cancel}
|
|
|
|
error={error}
|
|
|
|
inFlight={inFlight}
|
|
|
|
show={show}
|
|
|
|
/>
|
2021-03-12 05:23:46 -05:00
|
|
|
</ShareProjectContext.Provider>
|
|
|
|
)
|
2021-06-21 06:02:38 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
export default ShareProjectModal
|