GlobalProvider.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use client";
  2. import React, { createContext, useState, useContext, ReactNode,useEffect } from "react";
  3. import { GetChatLastUsedForLocalStorage } from "@/utils/chat";
  4. const GlobalContext = createContext<any>(null);
  5. interface GlobalProviderProps {
  6. children: ReactNode;
  7. }
  8. export const GlobalProvider: React.FC<GlobalProviderProps> = ({ children }) => {
  9. const [menuIndex, setMenuIndex] = useState<number>(0);
  10. const [loginShow, setLoginShow] = useState<boolean>(false);
  11. const [listType, setListType] = useState<number>(0); //0会话历史 1助手列表
  12. const [agentId, setAgentId] = useState<number>(0); //助手ID
  13. const [conversationId, setConversationId] = useState<string>(""); //会话ID
  14. const changeMenuIndex = (index: number) => {
  15. setMenuIndex(index);
  16. };
  17. const changeLoginShow = (show: boolean) => {
  18. setLoginShow(show);
  19. };
  20. const changeListType = (listType: number) => {
  21. setListType(listType);
  22. };
  23. const changeAgentId = (agentId: number) => {
  24. setAgentId(agentId);
  25. };
  26. const changeConversationId = (conversationId: string) => {
  27. setConversationId(conversationId);
  28. };
  29. useEffect(() => {
  30. var lastUsed = GetChatLastUsedForLocalStorage();
  31. setListType(lastUsed.listType);
  32. setAgentId(lastUsed.agentId);
  33. setConversationId(lastUsed.conversationId);
  34. }, []);
  35. return (
  36. <GlobalContext.Provider
  37. value={{
  38. menuIndex,
  39. changeMenuIndex,
  40. loginShow,
  41. changeLoginShow,
  42. listType,
  43. changeListType,
  44. agentId,
  45. changeAgentId,
  46. conversationId,
  47. changeConversationId,
  48. }}
  49. >
  50. {children}
  51. </GlobalContext.Provider>
  52. );
  53. };
  54. export const useGlobalContext = () => {
  55. return useContext(GlobalContext);
  56. };