Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 4x 4x 4x 4x 52x 4x 205x 205x 2x 203x 4x 11x 11x 11x 11x 11x | import React, { createContext, useContext } from "react";
import { ChatbotRemoteProps } from "@/types/chatbot";
import { ChatbotService, createChatbotService } from "@/services/chatbotService";
const defaultGetAccessToken = async () => null;
const noop = () => {};
export interface ChatbotRuntimeActions {
onClose: () => void;
onUnauthorized?: () => void;
}
export interface ChatbotRuntimeMeta {
service: ChatbotService;
scope: string;
screenContext: string;
userName?: string;
userId?: string;
}
export interface ChatbotRuntimeContextValue {
actions: ChatbotRuntimeActions;
meta: ChatbotRuntimeMeta;
}
const ChatbotRuntimeContext = createContext<ChatbotRuntimeContextValue | null>(null);
export const ChatbotRuntimeProvider = ({
value,
children,
}: {
value: ChatbotRuntimeContextValue;
children: React.ReactNode;
}) => {
return <ChatbotRuntimeContext.Provider value={value}>{children}</ChatbotRuntimeContext.Provider>;
};
export const useChatbotRuntime = (): ChatbotRuntimeContextValue => {
const context = useContext(ChatbotRuntimeContext);
if (!context) {
throw new Error("useChatbotRuntime must be used within ChatbotRuntimeProvider");
}
return context;
};
export const createChatbotRuntimeValue = (
props: ChatbotRemoteProps,
): ChatbotRuntimeContextValue => {
const baseUrl = props.env?.chatbotBaseUrl ?? "http://localhost:8787";
const getAccessToken = props.auth?.getAccessToken ?? defaultGetAccessToken;
const onUnauthorized = props.auth?.onUnauthorized;
const service = createChatbotService({ baseUrl, getAccessToken });
return {
actions: {
onClose: props.onClose ?? noop,
onUnauthorized,
},
meta: {
service,
scope: props.scope ?? "on_behalf",
screenContext: props.screenContext ?? "on_behalf",
userName: props.user?.name,
userId: props.user?.id,
},
};
};
|