feat(ui): add ViewContext so components can know where they are being rendered (user-linear view, editor-linear view, or editor-nodes view)

This commit is contained in:
psychedelicious
2025-01-17 17:33:27 +11:00
parent 2d05579568
commit bce9a23b25

View File

@@ -0,0 +1,19 @@
import type { PropsWithChildren } from 'react';
import { createContext, memo, useContext } from 'react';
import { assert } from 'tsafe';
type ViewType = 'user-linear' | 'editor-linear' | 'editor-nodes';
const ViewContext = createContext<ViewType | null>(null);
export const ViewContextProvider = memo((props: PropsWithChildren<{ viewType: ViewType }>) => {
return <ViewContext.Provider value={props.viewType}>{props.children}</ViewContext.Provider>;
});
ViewContextProvider.displayName = 'ViewContextProvider';
export const useViewContext = () => {
const context = useContext(ViewContext);
assert(context !== null, 'useViewContext must be used within a ViewContextProvider');
return context;
};