insomnia/packages/insomnia-app/app/ui/context/nunjucks/nunjucks-enabled-context.tsx
2021-12-04 09:08:28 +13:00

28 lines
717 B
TypeScript

import React, { createContext, FC, useContext } from 'react';
interface Props {
disable?: boolean;
}
interface NunjucksEnabledState {
enabled: boolean;
}
const NunjucksEnabledContext = createContext<NunjucksEnabledState | undefined>(undefined);
export const NunjucksEnabledProvider: FC<Props> = ({ disable, children }) => (
<NunjucksEnabledContext.Provider value={{ enabled: !disable }}>
{children}
</NunjucksEnabledContext.Provider>
);
export const useNunjucksEnabled = () => {
const context = useContext(NunjucksEnabledContext);
if (context === undefined) {
throw new Error('useNunjucksEnabled must be used within a NunjucksEnabledProvider or NunjucksProvider');
}
return context;
};