Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

user-setting-hooks.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { LanguageTranslationMap } from '@/constants/common';
  2. import { ResponseGetType } from '@/interfaces/database/base';
  3. import { ITenantInfo } from '@/interfaces/database/knowledge';
  4. import { ISystemStatus, IUserInfo } from '@/interfaces/database/user-setting';
  5. import userService from '@/services/user-service';
  6. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  7. import { message } from 'antd';
  8. import { useCallback, useMemo, useState } from 'react';
  9. import { useTranslation } from 'react-i18next';
  10. export const useFetchUserInfo = (): ResponseGetType<IUserInfo> => {
  11. const { i18n } = useTranslation();
  12. const { data, isFetching: loading } = useQuery({
  13. queryKey: ['userInfo'],
  14. initialData: {},
  15. gcTime: 0,
  16. queryFn: async () => {
  17. const { data } = await userService.user_info();
  18. if (data.retcode === 0) {
  19. i18n.changeLanguage(
  20. LanguageTranslationMap[
  21. data.data.language as keyof typeof LanguageTranslationMap
  22. ],
  23. );
  24. }
  25. return data?.data ?? {};
  26. },
  27. });
  28. return { data, loading };
  29. };
  30. export const useFetchTenantInfo = (): ResponseGetType<ITenantInfo> => {
  31. const { data, isFetching: loading } = useQuery({
  32. queryKey: ['tenantInfo'],
  33. initialData: {},
  34. gcTime: 0,
  35. queryFn: async () => {
  36. const { data: res } = await userService.get_tenant_info();
  37. if (res.retcode === 0) {
  38. // llm_id is chat_id
  39. // asr_id is speech2txt
  40. const { data } = res;
  41. data.chat_id = data.llm_id;
  42. data.speech2text_id = data.asr_id;
  43. return data;
  44. }
  45. return res;
  46. },
  47. });
  48. return { data, loading };
  49. };
  50. export const useSelectParserList = (): Array<{
  51. value: string;
  52. label: string;
  53. }> => {
  54. const { data: tenantInfo } = useFetchTenantInfo();
  55. const parserList = useMemo(() => {
  56. const parserArray: Array<string> = tenantInfo?.parser_ids?.split(',') ?? [];
  57. return parserArray.map((x) => {
  58. const arr = x.split(':');
  59. return { value: arr[0], label: arr[1] };
  60. });
  61. }, [tenantInfo]);
  62. return parserList;
  63. };
  64. export const useSaveSetting = () => {
  65. const queryClient = useQueryClient();
  66. const { t } = useTranslation();
  67. const {
  68. data,
  69. isPending: loading,
  70. mutateAsync,
  71. } = useMutation({
  72. mutationKey: ['saveSetting'],
  73. mutationFn: async (
  74. userInfo: { new_password: string } | Partial<IUserInfo>,
  75. ) => {
  76. const { data } = await userService.setting(userInfo);
  77. if (data.retcode === 0) {
  78. message.success(t('message.modified'));
  79. queryClient.invalidateQueries({ queryKey: ['userInfo'] });
  80. }
  81. return data?.retcode;
  82. },
  83. });
  84. return { data, loading, saveSetting: mutateAsync };
  85. };
  86. export const useFetchSystemVersion = () => {
  87. const [version, setVersion] = useState('');
  88. const [loading, setLoading] = useState(false);
  89. const fetchSystemVersion = useCallback(async () => {
  90. try {
  91. setLoading(true);
  92. const { data } = await userService.getSystemVersion();
  93. if (data.retcode === 0) {
  94. setVersion(data.data);
  95. setLoading(false);
  96. }
  97. } catch (error) {
  98. setLoading(false);
  99. }
  100. }, []);
  101. return { fetchSystemVersion, version, loading };
  102. };
  103. export const useFetchSystemStatus = () => {
  104. const [systemStatus, setSystemStatus] = useState<ISystemStatus>(
  105. {} as ISystemStatus,
  106. );
  107. const [loading, setLoading] = useState(false);
  108. const fetchSystemStatus = useCallback(async () => {
  109. setLoading(true);
  110. const { data } = await userService.getSystemStatus();
  111. if (data.retcode === 0) {
  112. setSystemStatus(data.data);
  113. setLoading(false);
  114. }
  115. }, []);
  116. return {
  117. systemStatus,
  118. fetchSystemStatus,
  119. loading,
  120. };
  121. };