overleaf/services/web/frontend/js/features/review-panel-new/context/review-panel-view-context.tsx
David 438192fc11 Open review panel current file view when clicking add comment tooltip (#20891)
* Remove need for custom set-review-panel-open event

* Open current file view when clicking add comment tooltip

---------

Co-authored-by: Domagoj Kriskovic <dom.kriskovic@overleaf.com>
GitOrigin-RevId: 2b21061193f4270030d73174014aabbd017fd38e
2024-10-14 11:08:56 +00:00

54 lines
1.1 KiB
TypeScript

import {
createContext,
Dispatch,
FC,
SetStateAction,
useContext,
useMemo,
useState,
} from 'react'
export type View = 'cur_file' | 'overview'
export const ReviewPanelViewContext = createContext<View>('cur_file')
type ViewActions = {
setView: Dispatch<SetStateAction<View>>
}
const ReviewPanelViewActionsContext = createContext<ViewActions | undefined>(
undefined
)
export const ReviewPanelViewProvider: FC = ({ children }) => {
const [view, setView] = useState<View>('cur_file')
const actions = useMemo(
() => ({
setView,
}),
[setView]
)
return (
<ReviewPanelViewActionsContext.Provider value={actions}>
<ReviewPanelViewContext.Provider value={view}>
{children}
</ReviewPanelViewContext.Provider>
</ReviewPanelViewActionsContext.Provider>
)
}
export const useReviewPanelViewContext = () => {
return useContext(ReviewPanelViewContext)
}
export const useReviewPanelViewActionsContext = () => {
const context = useContext(ReviewPanelViewActionsContext)
if (!context) {
throw new Error(
'useViewActionsContext is only available inside ViewProvider'
)
}
return context
}