frontpage/lib/commands/ContextProvider.tsx

14 lines
869 B
TypeScript
Raw Permalink Normal View History

2022-06-12 14:22:15 +02:00
import { createContext, useContext } from "react";
import type { PropsWithChildren } from "react";
2022-06-21 18:44:20 +02:00
import { CommandInterface } from ".";
import type { ContentList } from "../content/types";
import type { CommandInterfaceCallbacks } from "./types";
2022-06-12 14:22:15 +02:00
const commandInterface = new CommandInterface();
const CommandContext = createContext(commandInterface);
const setCommandCallbacks = (callbacks: CommandInterfaceCallbacks) => commandInterface.callbacks = {...commandInterface.callbacks, ...callbacks};
const setContents = (content: ContentList) => commandInterface.content = content;
const useCommands = () => ({cmdContext: useContext(CommandContext), updateCallbacks: setCommandCallbacks, setContents});
const CommandsProvider = (props: PropsWithChildren<{}>) => <CommandContext.Provider value={commandInterface} {...props} />;
export { CommandsProvider, useCommands };