2021-02-09 10:37:48 -05:00
|
|
|
import React, { createContext, useCallback, useContext, useEffect } from 'react'
|
2020-12-14 06:44:10 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-02-09 10:37:48 -05:00
|
|
|
import usePersistedState from '../../infrastructure/persisted-state-hook'
|
2020-12-14 06:44:10 -05:00
|
|
|
|
|
|
|
export const EditorContext = createContext()
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
EditorContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
cobranding: PropTypes.shape({
|
|
|
|
logoImgUrl: PropTypes.string.isRequired,
|
|
|
|
brandVariationName: PropTypes.string.isRequired,
|
|
|
|
brandVariationHomeUrl: PropTypes.string.isRequired
|
|
|
|
}),
|
|
|
|
loading: PropTypes.bool,
|
|
|
|
projectId: PropTypes.string.isRequired,
|
|
|
|
isProjectOwner: PropTypes.bool
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
export function EditorProvider({
|
|
|
|
children,
|
|
|
|
loading,
|
|
|
|
chatIsOpenAngular,
|
2021-02-10 05:57:25 -05:00
|
|
|
setChatIsOpenAngular,
|
|
|
|
openDoc,
|
|
|
|
onlineUsersArray
|
2021-02-09 10:37:48 -05:00
|
|
|
}) {
|
2021-01-27 05:30:55 -05:00
|
|
|
const cobranding = window.brandVariation
|
|
|
|
? {
|
|
|
|
logoImgUrl: window.brandVariation.logo_url,
|
|
|
|
brandVariationName: window.brandVariation.name,
|
|
|
|
brandVariationHomeUrl: window.brandVariation.home_url
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const ownerId =
|
|
|
|
window._ide.$scope.project && window._ide.$scope.project.owner
|
|
|
|
? window._ide.$scope.project.owner._id
|
|
|
|
: null
|
|
|
|
|
2021-02-09 10:37:48 -05:00
|
|
|
const [chatIsOpen, setChatIsOpen] = usePersistedState(
|
|
|
|
'editor.ui.chat.open',
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
|
|
|
const toggleChatOpen = useCallback(() => {
|
|
|
|
setChatIsOpen(!chatIsOpen)
|
|
|
|
setChatIsOpenAngular(!chatIsOpen)
|
|
|
|
}, [chatIsOpen, setChatIsOpenAngular, setChatIsOpen])
|
|
|
|
|
|
|
|
// updates React's `chatIsOpen` state when the chat is opened by Angular.
|
|
|
|
// In order to prevent race conditions with `toggleChatOpen` it's not a 1:1 binding:
|
|
|
|
// Angular forces the React state to `true`, but can only set it to `false` when
|
|
|
|
// the React state is explicitly `true`.
|
|
|
|
useEffect(() => {
|
|
|
|
if (chatIsOpenAngular) {
|
|
|
|
setChatIsOpen(true)
|
|
|
|
} else if (chatIsOpen) {
|
|
|
|
setChatIsOpen(false)
|
|
|
|
}
|
|
|
|
}, [chatIsOpenAngular, chatIsOpen, setChatIsOpen])
|
|
|
|
|
2021-01-14 10:16:54 -05:00
|
|
|
const editorContextValue = {
|
2021-01-27 05:30:55 -05:00
|
|
|
cobranding,
|
|
|
|
loading,
|
2021-01-14 10:16:54 -05:00
|
|
|
projectId: window.project_id,
|
2021-02-09 10:37:48 -05:00
|
|
|
isProjectOwner: ownerId === window.user.id,
|
2021-02-10 05:57:25 -05:00
|
|
|
openDoc,
|
|
|
|
onlineUsersArray,
|
2021-02-09 10:37:48 -05:00
|
|
|
ui: {
|
|
|
|
chatIsOpen,
|
|
|
|
toggleChatOpen
|
|
|
|
}
|
2021-01-14 10:16:54 -05:00
|
|
|
}
|
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
return (
|
2021-01-14 10:16:54 -05:00
|
|
|
<EditorContext.Provider value={editorContextValue}>
|
2020-12-14 06:44:10 -05:00
|
|
|
{children}
|
|
|
|
</EditorContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorProvider.propTypes = {
|
2021-01-27 05:30:55 -05:00
|
|
|
children: PropTypes.any,
|
2021-02-09 10:37:48 -05:00
|
|
|
loading: PropTypes.bool,
|
|
|
|
chatIsOpenAngular: PropTypes.bool,
|
2021-02-10 05:57:25 -05:00
|
|
|
setChatIsOpenAngular: PropTypes.func.isRequired,
|
|
|
|
openDoc: PropTypes.func.isRequired,
|
|
|
|
onlineUsersArray: PropTypes.array.isRequired
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 05:36:41 -05:00
|
|
|
export function useEditorContext(propTypes) {
|
|
|
|
const data = useContext(EditorContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'EditorContext.Provider')
|
|
|
|
return data
|
2020-12-14 06:44:10 -05:00
|
|
|
}
|