cal.pub0.org/packages/features/ee/support/lib/freshchat/FreshChatProvider.tsx

26 lines
807 B
TypeScript
Raw Normal View History

import type { ReactNode, Dispatch, SetStateAction } from "react";
import { createContext, useState, useContext } from "react";
import FreshChatScript from "./FreshChatScript";
type FreshChatContextType = { active: boolean; setActive: Dispatch<SetStateAction<boolean>> };
const FreshChatContext = createContext<FreshChatContextType>({ active: false, setActive: () => undefined });
interface FreshChatProviderProps {
children: ReactNode;
}
export const useFreshChat = () => useContext(FreshChatContext);
export default function FreshChatProvider(props: FreshChatProviderProps) {
const [active, setActive] = useState(false);
return (
<FreshChatContext.Provider value={{ active, setActive }}>
{props.children}
{active && <FreshChatScript />}
</FreshChatContext.Provider>
);
}