Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { SharedFrom } from '@/constants/chat';
  2. import {
  3. useCreateNextToken,
  4. useFetchTokenList,
  5. useRemoveNextToken,
  6. } from '@/hooks/chat-hooks';
  7. import {
  8. useSetModalState,
  9. useShowDeleteConfirm,
  10. useTranslate,
  11. } from '@/hooks/common-hooks';
  12. import { IStats } from '@/interfaces/database/chat';
  13. import { useQueryClient } from '@tanstack/react-query';
  14. import { message } from 'antd';
  15. import { useCallback } from 'react';
  16. export const useOperateApiKey = (dialogId: string, idKey: string) => {
  17. const { removeToken } = useRemoveNextToken();
  18. const { createToken, loading: creatingLoading } = useCreateNextToken();
  19. const { data: tokenList, loading: listLoading } = useFetchTokenList({
  20. [idKey]: dialogId,
  21. });
  22. const showDeleteConfirm = useShowDeleteConfirm();
  23. const onRemoveToken = (token: string, tenantId: string) => {
  24. showDeleteConfirm({
  25. onOk: () => removeToken({ dialogId, tokens: [token], tenantId }),
  26. });
  27. };
  28. const onCreateToken = useCallback(() => {
  29. createToken({ [idKey]: dialogId });
  30. }, [createToken, idKey, dialogId]);
  31. return {
  32. removeToken: onRemoveToken,
  33. createToken: onCreateToken,
  34. tokenList,
  35. creatingLoading,
  36. listLoading,
  37. };
  38. };
  39. type ChartStatsType = {
  40. [k in keyof IStats]: Array<{ xAxis: string; yAxis: number }>;
  41. };
  42. export const useSelectChartStatsList = (): ChartStatsType => {
  43. const queryClient = useQueryClient();
  44. const data = queryClient.getQueriesData({ queryKey: ['fetchStats'] });
  45. const stats: IStats = data[0][1] as IStats;
  46. return Object.keys(stats).reduce((pre, cur) => {
  47. const item = stats[cur as keyof IStats];
  48. if (item.length > 0) {
  49. pre[cur as keyof IStats] = item.map((x) => ({
  50. xAxis: x[0] as string,
  51. yAxis: x[1] as number,
  52. }));
  53. }
  54. return pre;
  55. }, {} as ChartStatsType);
  56. };
  57. export const useShowTokenEmptyError = () => {
  58. const { t } = useTranslate('chat');
  59. const showTokenEmptyError = useCallback(() => {
  60. message.error(t('tokenError'));
  61. }, [t]);
  62. return { showTokenEmptyError };
  63. };
  64. const getUrlWithToken = (token: string, from: string = 'chat') => {
  65. const { protocol, host } = window.location;
  66. return `${protocol}//${host}/chat/share?shared_id=${token}&from=${from}`;
  67. };
  68. const useFetchTokenListBeforeOtherStep = (dialogId: string, idKey: string) => {
  69. const { showTokenEmptyError } = useShowTokenEmptyError();
  70. const { data: tokenList, refetch } = useFetchTokenList({ [idKey]: dialogId });
  71. const token =
  72. Array.isArray(tokenList) && tokenList.length > 0 ? tokenList[0].token : '';
  73. const handleOperate = useCallback(async () => {
  74. const ret = await refetch();
  75. const list = ret.data;
  76. if (Array.isArray(list) && list.length > 0) {
  77. return list[0]?.token;
  78. } else {
  79. showTokenEmptyError();
  80. return false;
  81. }
  82. }, [showTokenEmptyError, refetch]);
  83. return {
  84. token,
  85. handleOperate,
  86. };
  87. };
  88. export const useShowEmbedModal = (dialogId: string, idKey: string) => {
  89. const {
  90. visible: embedVisible,
  91. hideModal: hideEmbedModal,
  92. showModal: showEmbedModal,
  93. } = useSetModalState();
  94. const { handleOperate, token } = useFetchTokenListBeforeOtherStep(
  95. dialogId,
  96. idKey,
  97. );
  98. const handleShowEmbedModal = useCallback(async () => {
  99. const succeed = await handleOperate();
  100. if (succeed) {
  101. showEmbedModal();
  102. }
  103. }, [handleOperate, showEmbedModal]);
  104. return {
  105. showEmbedModal: handleShowEmbedModal,
  106. hideEmbedModal,
  107. embedVisible,
  108. embedToken: token,
  109. };
  110. };
  111. export const usePreviewChat = (dialogId: string, idKey: string) => {
  112. const { handleOperate } = useFetchTokenListBeforeOtherStep(dialogId, idKey);
  113. const open = useCallback(
  114. (t: string) => {
  115. window.open(
  116. getUrlWithToken(
  117. t,
  118. idKey === 'canvasId' ? SharedFrom.Agent : SharedFrom.Chat,
  119. ),
  120. '_blank',
  121. );
  122. },
  123. [idKey],
  124. );
  125. const handlePreview = useCallback(async () => {
  126. const token = await handleOperate();
  127. if (token) {
  128. open(token);
  129. }
  130. }, [handleOperate, open]);
  131. return {
  132. handlePreview,
  133. };
  134. };