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 PropTypes from 'prop-types'
|
|
|
|
import ShareProjectModalContent from './share-project-modal-content'
|
2022-01-10 10:47:10 -05:00
|
|
|
import {
|
|
|
|
useProjectContext,
|
|
|
|
projectShape,
|
|
|
|
} 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'
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
const ShareProjectContext = createContext()
|
|
|
|
|
|
|
|
ShareProjectContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
isAdmin: PropTypes.bool.isRequired,
|
|
|
|
updateProject: PropTypes.func.isRequired,
|
|
|
|
monitorRequest: PropTypes.func.isRequired,
|
|
|
|
inFlight: PropTypes.bool,
|
|
|
|
setInFlight: PropTypes.func,
|
|
|
|
error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
|
2021-04-27 03:52:58 -04:00
|
|
|
setError: PropTypes.func,
|
|
|
|
}),
|
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
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
isAdmin,
|
|
|
|
}) {
|
|
|
|
const [inFlight, setInFlight] = useState(false)
|
|
|
|
const [error, setError] = useState()
|
|
|
|
|
2021-06-25 04:13:17 -04:00
|
|
|
const project = useProjectContext(projectShape)
|
2021-03-31 06:44:20 -04:00
|
|
|
|
2021-12-14 05:52:29 -05:00
|
|
|
const { splitTestVariants } = useSplitTestContext({
|
|
|
|
splitTestVariants: PropTypes.object,
|
|
|
|
})
|
|
|
|
|
|
|
|
// send tracking event when the modal is opened
|
|
|
|
useEffect(() => {
|
|
|
|
if (show) {
|
|
|
|
sendMB('share-modal-opened', {
|
|
|
|
splitTestVariant: splitTestVariants['null-test-share-modal'],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [splitTestVariants, show])
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
promise.catch(error => {
|
|
|
|
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={{
|
|
|
|
isAdmin,
|
|
|
|
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
|
|
|
})
|
2021-03-12 05:23:46 -05:00
|
|
|
ShareProjectModal.propTypes = {
|
|
|
|
animation: PropTypes.bool,
|
|
|
|
handleHide: PropTypes.func.isRequired,
|
|
|
|
isAdmin: PropTypes.bool.isRequired,
|
|
|
|
show: PropTypes.bool.isRequired,
|
|
|
|
}
|
2021-06-21 06:02:38 -04:00
|
|
|
|
|
|
|
export default ShareProjectModal
|