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.

chunk-hooks.ts 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { ResponseGetType, ResponseType } from '@/interfaces/database/base';
  2. import { IChunk, IKnowledgeFile } from '@/interfaces/database/knowledge';
  3. import kbService from '@/services/knowledge-service';
  4. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  5. import { useDebounce } from 'ahooks';
  6. import { PaginationProps, message } from 'antd';
  7. import { useCallback, useState } from 'react';
  8. import { useTranslation } from 'react-i18next';
  9. import {
  10. useGetPaginationWithRouter,
  11. useHandleSearchChange,
  12. } from './logic-hooks';
  13. import {
  14. useGetKnowledgeSearchParams,
  15. useSetPaginationParams,
  16. } from './route-hook';
  17. export interface IChunkListResult {
  18. searchString?: string;
  19. handleInputChange?: React.ChangeEventHandler<HTMLInputElement>;
  20. pagination: PaginationProps;
  21. setPagination?: (pagination: { page: number; pageSize: number }) => void;
  22. available: number | undefined;
  23. handleSetAvailable: (available: number | undefined) => void;
  24. }
  25. export const useFetchNextChunkList = (): ResponseGetType<{
  26. data: IChunk[];
  27. total: number;
  28. documentInfo: IKnowledgeFile;
  29. }> &
  30. IChunkListResult => {
  31. const { pagination, setPagination } = useGetPaginationWithRouter();
  32. const { documentId } = useGetKnowledgeSearchParams();
  33. const { searchString, handleInputChange } = useHandleSearchChange();
  34. const [available, setAvailable] = useState<number | undefined>();
  35. const debouncedSearchString = useDebounce(searchString, { wait: 500 });
  36. const { data, isFetching: loading } = useQuery({
  37. queryKey: [
  38. 'fetchChunkList',
  39. documentId,
  40. pagination.current,
  41. pagination.pageSize,
  42. debouncedSearchString,
  43. available,
  44. ],
  45. placeholderData: (previousData) =>
  46. previousData ?? { data: [], total: 0, documentInfo: {} }, // https://github.com/TanStack/query/issues/8183
  47. gcTime: 0,
  48. queryFn: async () => {
  49. const { data } = await kbService.chunk_list({
  50. doc_id: documentId,
  51. page: pagination.current,
  52. size: pagination.pageSize,
  53. available_int: available,
  54. keywords: searchString,
  55. });
  56. if (data.code === 0) {
  57. const res = data.data;
  58. return {
  59. data: res.chunks,
  60. total: res.total,
  61. documentInfo: res.doc,
  62. };
  63. }
  64. return (
  65. data?.data ?? {
  66. data: [],
  67. total: 0,
  68. documentInfo: {},
  69. }
  70. );
  71. },
  72. });
  73. const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
  74. (e) => {
  75. setPagination({ page: 1 });
  76. handleInputChange(e);
  77. },
  78. [handleInputChange, setPagination],
  79. );
  80. const handleSetAvailable = useCallback(
  81. (a: number | undefined) => {
  82. setPagination({ page: 1 });
  83. setAvailable(a);
  84. },
  85. [setAvailable, setPagination],
  86. );
  87. return {
  88. data,
  89. loading,
  90. pagination,
  91. setPagination,
  92. searchString,
  93. handleInputChange: onInputChange,
  94. available,
  95. handleSetAvailable,
  96. };
  97. };
  98. export const useSelectChunkList = () => {
  99. const queryClient = useQueryClient();
  100. const data = queryClient.getQueriesData<{
  101. data: IChunk[];
  102. total: number;
  103. documentInfo: IKnowledgeFile;
  104. }>({ queryKey: ['fetchChunkList'] });
  105. return data?.at(-1)?.[1];
  106. };
  107. export const useDeleteChunk = () => {
  108. const queryClient = useQueryClient();
  109. const { setPaginationParams } = useSetPaginationParams();
  110. const {
  111. data,
  112. isPending: loading,
  113. mutateAsync,
  114. } = useMutation({
  115. mutationKey: ['deleteChunk'],
  116. mutationFn: async (params: { chunkIds: string[]; doc_id: string }) => {
  117. const { data } = await kbService.rm_chunk(params);
  118. if (data.code === 0) {
  119. setPaginationParams(1);
  120. queryClient.invalidateQueries({ queryKey: ['fetchChunkList'] });
  121. }
  122. return data?.code;
  123. },
  124. });
  125. return { data, loading, deleteChunk: mutateAsync };
  126. };
  127. export const useSwitchChunk = () => {
  128. const { t } = useTranslation();
  129. const queryClient = useQueryClient();
  130. const {
  131. data,
  132. isPending: loading,
  133. mutateAsync,
  134. } = useMutation({
  135. mutationKey: ['switchChunk'],
  136. mutationFn: async (params: {
  137. chunk_ids?: string[];
  138. available_int?: number;
  139. doc_id: string;
  140. }) => {
  141. const { data } = await kbService.switch_chunk(params);
  142. if (data.code === 0) {
  143. message.success(t('message.modified'));
  144. queryClient.invalidateQueries({ queryKey: ['fetchChunkList'] });
  145. }
  146. return data?.code;
  147. },
  148. });
  149. return { data, loading, switchChunk: mutateAsync };
  150. };
  151. export const useCreateChunk = () => {
  152. const { t } = useTranslation();
  153. const queryClient = useQueryClient();
  154. const {
  155. data,
  156. isPending: loading,
  157. mutateAsync,
  158. } = useMutation({
  159. mutationKey: ['createChunk'],
  160. mutationFn: async (payload: any) => {
  161. let service = kbService.create_chunk;
  162. if (payload.chunk_id) {
  163. service = kbService.set_chunk;
  164. }
  165. const { data } = await service(payload);
  166. if (data.code === 0) {
  167. message.success(t('message.created'));
  168. queryClient.invalidateQueries({ queryKey: ['fetchChunkList'] });
  169. }
  170. return data?.code;
  171. },
  172. });
  173. return { data, loading, createChunk: mutateAsync };
  174. };
  175. export const useFetchChunk = (chunkId?: string): ResponseType<any> => {
  176. const { data } = useQuery({
  177. queryKey: ['fetchChunk'],
  178. enabled: !!chunkId,
  179. initialData: {},
  180. gcTime: 0,
  181. queryFn: async () => {
  182. const data = await kbService.get_chunk({
  183. chunk_id: chunkId,
  184. });
  185. return data;
  186. },
  187. });
  188. return data;
  189. };
  190. export const useFetchKnowledgeGraph = (
  191. documentId: string,
  192. ): ResponseType<any> => {
  193. const { data } = useQuery({
  194. queryKey: ['fetchKnowledgeGraph', documentId],
  195. initialData: true,
  196. enabled: !!documentId,
  197. gcTime: 0,
  198. queryFn: async () => {
  199. const data = await kbService.knowledge_graph({
  200. doc_id: documentId,
  201. });
  202. return data;
  203. },
  204. });
  205. return data;
  206. };