You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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