context.tsx 2.14 KB
"use client";

import type { RefObject } from "react";
import { createContext, useContext } from "use-context-selector";
import type { Callback, ChatConfig, ChatItemInTree, Feedback } from "../types";
import type { ThemeBuilder } from "../embedded-chatbot/theme/theme-context";
import type {
  AppData,
  AppMeta,
  ConversationItem,
} from "@/models/share";
import { noop } from "lodash-es";

export type ChatWithHistoryContextValue = {
  appInfoError?: any;
  appInfoLoading?: boolean;
  appMeta?: AppMeta;
  appData?: AppData;
  appParams?: ChatConfig;
  appChatListDataLoading?: boolean;
  currentConversationId: string;
  currentConversationItem?: ConversationItem;
  appPrevChatTree: ChatItemInTree[];
  newConversationInputs: Record<string, any>;
  newConversationInputsRef: RefObject<Record<string, any>>;
  handleNewConversation: (v?: Record<string, any>,skipClear?:boolean) => void;
  handleNewConversationCompleted?: (newConversationId: string) => void;
  chatShouldReloadKey?: string;
  isMobile?: boolean;
  isInstalledApp: boolean;
  appId?: string;
  handleFeedback: (messageId: string, feedback: Feedback) => void;
  currentChatInstanceRef: RefObject<{ handleStop: () => void }>;
  themeBuilder?: ThemeBuilder;
  clearChatList?: boolean;
  setClearChatList?: (state: boolean) => void;
  isResponding?: boolean;
  setIsResponding: (state: boolean) => void;
  currentConversationInputs?: Record<string, any> | null;
  isShowPracticeButton: boolean;
};

export const ChatWithHistoryContext =
  createContext<ChatWithHistoryContextValue>({
    currentConversationId: "",
    appPrevChatTree: [],
    newConversationInputs: {},
    newConversationInputsRef: { current: {} },
    handleNewConversation: noop,
    handleNewConversationCompleted: noop,
    chatShouldReloadKey: "",
    isMobile: false,
    isInstalledApp: false,
    handleFeedback: noop,
    currentChatInstanceRef: { current: { handleStop: noop } },
    clearChatList: false,
    setClearChatList: noop,
    isResponding: false,
    setIsResponding: noop,
    currentConversationInputs: {},
    isShowPracticeButton: false,
  });
export const useChatWithHistoryContext = () =>
  useContext(ChatWithHistoryContext);