chatShareM.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 嵌iframe、适配移动端
  2. import { useEffect, useMemo, useState, useRef, useContext } from "react";
  3. import { useLocation, useParams } from "react-router-dom";
  4. import { getFlowApi, readOnlineFlows } from "../../../controllers/API/flow";
  5. import { FlowType } from "../../../types/flow";
  6. import { generateUUID } from "../../../utils";
  7. import ChatPanneM from "./ChatPanneM";
  8. import { useTranslation } from "react-i18next";
  9. import { deleteChatApi, getChatsApi } from "../../../controllers/API";
  10. import { captureAndAlertRequestErrorHoc } from "../../../controllers/request";
  11. import { useDebounce, useTable } from "../../../util/hook";
  12. import { TabsContext } from "../../../contexts/tabsContext";
  13. import SkillTemps from "../../SkillPage/components/SkillTemps";
  14. import titIconL from "../../../assets/chatM/tit-icon-l.png";
  15. import titIconR from "../../../assets/chatM/tit-icon-r.png";
  16. export default function chatShare() {
  17. const { id: flowId } = useParams()
  18. const location = useLocation();
  19. const searchParams = new URLSearchParams(location.search);
  20. const libId = searchParams.get('lib')
  21. const tweak = searchParams.get('tweak')
  22. const { t } = useTranslation();
  23. const [open, setOpen] = useState(false)
  24. // 对话列表
  25. const { chatList, chatsRef, addChat, deleteChat } = useChatList()
  26. const queryString = useMemo(() => {
  27. const params = [];
  28. if (libId) params.push(`knowledge_id=${libId}`);
  29. if (tweak) params.push(`tweak=${tweak}`);
  30. return params.length > 0 ? `&${params.join('&')}` : '';
  31. }, [libId, tweak])
  32. const chatIdRef = useRef('')
  33. const handlerSelectFlow = async (node: FlowType) => {
  34. // 会话ID
  35. chatIdRef.current = generateUUID(32)
  36. setOpen(false)
  37. // add list
  38. addChat({
  39. "flow_name": node.name,
  40. "flow_description": node.description,
  41. "flow_id": node.id,
  42. "chat_id": chatIdRef.current,
  43. "create_time": "-",
  44. "update_time": "-"
  45. })
  46. const flow = await getFlowApi(node.id)
  47. setFlow(flow)
  48. setChatId(chatIdRef.current)
  49. // setFace(false)
  50. }
  51. //
  52. const { flow: initFlow } = useContext(TabsContext);
  53. const [flow, setFlow] = useState<FlowType>(null)
  54. const {
  55. data: onlineFlows,
  56. loading,
  57. search,
  58. } = useTable<FlowType>({}, (param) =>
  59. readOnlineFlows(param.page, param.keyword).then((res) => {
  60. return res;
  61. })
  62. );
  63. const [chatId, setChatId] = useState<string>('')
  64. console.log(flowId,libId,tweak)
  65. useEffect(() => {
  66. flowId && getFlowApi(flowId).then(node => {
  67. // 会话ID
  68. setFlow(node)
  69. setChatId(generateUUID(32))
  70. })
  71. }, [flowId])
  72. // select chat
  73. const handleSelectChat = useDebounce(async (chat) => {
  74. if (chat.chat_id === chatId) return
  75. const flow = initFlow?.id === chat.flow_id ? initFlow : await getFlowApi(chat.flow_id)
  76. // if (!flow) {
  77. // setInputState({ lock: true, errorCode: '1004' })
  78. // clearHistory()
  79. // return setFace(false)
  80. // }
  81. setFlow(flow)
  82. setChatId(chat.chat_id)
  83. }, 100, false)
  84. if (!flowId) return <div>请选择技能</div>
  85. return <div className="chatShareM">
  86. {/* <div className="chatShareM-tit">
  87. <img src={titIconL} alt=""/>
  88. <p>对话名称</p>
  89. <img src={titIconR} alt=""/>
  90. </div> */}
  91. {flow
  92. ? <div className="flex-1 chat-box h-screen relative">
  93. {flow && <ChatPanneM version='v2' queryString={queryString} chatId={chatId} flow={flow} />}
  94. </div>
  95. :<div className="flex-1 chat-box h-screen overflow-hidden relative">
  96. <p className="text-center mt-[100px] text-sm text-gray-600">{t('chat.selectChat')}</p>
  97. </div>}
  98. </div>
  99. // flow ? <ChatPanne version='v2' queryString={queryString} chatId={chatId} flow={flow} /> : null
  100. };
  101. /**
  102. * 本地对话列表
  103. */
  104. const useChatList = () => {
  105. const [chatList, setChatList] = useState([])
  106. const chatsRef = useRef(null)
  107. useEffect(() => {
  108. getChatsApi().then(setChatList)
  109. }, [])
  110. return {
  111. chatList,
  112. chatsRef,
  113. addChat: (chat) => {
  114. const newList = [chat, ...chatList]
  115. // localStorage.setItem(ITEM_KEY, JSON.stringify(newList))
  116. setChatList(newList)
  117. setTimeout(() => {
  118. chatsRef.current.scrollTop = 1
  119. }, 0);
  120. },
  121. deleteChat: (id: string) => {
  122. // api
  123. captureAndAlertRequestErrorHoc(deleteChatApi(id).then(res => {
  124. setChatList(oldList => oldList.filter(item => item.chat_id !== id))
  125. }))
  126. }
  127. }
  128. }