Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { ResponseType } from '@/interfaces/database/base';
  2. import { DSL, IFlow, IFlowTemplate } from '@/interfaces/database/flow';
  3. import i18n from '@/locales/config';
  4. import flowService from '@/services/flow-service';
  5. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  6. import { message } from 'antd';
  7. import { useParams } from 'umi';
  8. import { v4 as uuid } from 'uuid';
  9. export const EmptyDsl = {
  10. graph: {
  11. nodes: [
  12. {
  13. id: 'begin',
  14. type: 'beginNode',
  15. position: {
  16. x: 50,
  17. y: 200,
  18. },
  19. data: {
  20. label: 'Begin',
  21. name: 'begin',
  22. },
  23. sourcePosition: 'left',
  24. targetPosition: 'right',
  25. },
  26. ],
  27. edges: [],
  28. },
  29. components: {
  30. begin: {
  31. obj: {
  32. component_name: 'Begin',
  33. params: {},
  34. },
  35. downstream: ['Answer:China'], // other edge target is downstream, edge source is current node id
  36. upstream: [], // edge source is upstream, edge target is current node id
  37. },
  38. },
  39. messages: [],
  40. reference: [],
  41. history: [],
  42. path: [],
  43. answer: [],
  44. };
  45. export const useFetchFlowTemplates = (): ResponseType<IFlowTemplate[]> => {
  46. const { data } = useQuery({
  47. queryKey: ['fetchFlowTemplates'],
  48. initialData: [],
  49. queryFn: async () => {
  50. const { data } = await flowService.listTemplates();
  51. if (Array.isArray(data?.data)) {
  52. data.data.unshift({
  53. id: uuid(),
  54. title: 'Blank',
  55. description: 'Create from nothing',
  56. dsl: EmptyDsl,
  57. });
  58. }
  59. return data;
  60. },
  61. });
  62. return data;
  63. };
  64. export const useFetchFlowList = (): { data: IFlow[]; loading: boolean } => {
  65. const { data, isFetching: loading } = useQuery({
  66. queryKey: ['fetchFlowList'],
  67. initialData: [],
  68. queryFn: async () => {
  69. const { data } = await flowService.listCanvas();
  70. return data?.data ?? [];
  71. },
  72. });
  73. return { data, loading };
  74. };
  75. export const useFetchFlow = (): {
  76. data: IFlow;
  77. loading: boolean;
  78. refetch: () => void;
  79. } => {
  80. const { id } = useParams();
  81. const {
  82. data,
  83. isFetching: loading,
  84. refetch,
  85. } = useQuery({
  86. queryKey: ['flowDetail'],
  87. initialData: {} as IFlow,
  88. refetchOnReconnect: false,
  89. refetchOnMount: false,
  90. refetchOnWindowFocus: false,
  91. queryFn: async () => {
  92. const { data } = await flowService.getCanvas({}, id);
  93. return data?.data ?? {};
  94. },
  95. });
  96. return { data, loading, refetch };
  97. };
  98. export const useSetFlow = () => {
  99. const queryClient = useQueryClient();
  100. const {
  101. data,
  102. isPending: loading,
  103. mutateAsync,
  104. } = useMutation({
  105. mutationKey: ['setFlow'],
  106. mutationFn: async (params: {
  107. id?: string;
  108. title?: string;
  109. dsl?: DSL;
  110. avatar?: string;
  111. }) => {
  112. const { data = {} } = await flowService.setCanvas(params);
  113. if (data.retcode === 0) {
  114. message.success(
  115. i18n.t(`message.${params?.id ? 'modified' : 'created'}`),
  116. );
  117. queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
  118. }
  119. return data;
  120. },
  121. });
  122. return { data, loading, setFlow: mutateAsync };
  123. };
  124. export const useDeleteFlow = () => {
  125. const queryClient = useQueryClient();
  126. const {
  127. data,
  128. isPending: loading,
  129. mutateAsync,
  130. } = useMutation({
  131. mutationKey: ['deleteFlow'],
  132. mutationFn: async (canvasIds: string[]) => {
  133. const { data } = await flowService.removeCanvas({ canvasIds });
  134. if (data.retcode === 0) {
  135. queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
  136. }
  137. return data?.data ?? [];
  138. },
  139. });
  140. return { data, loading, deleteFlow: mutateAsync };
  141. };
  142. export const useRunFlow = () => {
  143. const {
  144. data,
  145. isPending: loading,
  146. mutateAsync,
  147. } = useMutation({
  148. mutationKey: ['runFlow'],
  149. mutationFn: async (params: { id: string; dsl: DSL }) => {
  150. const { data } = await flowService.runCanvas(params);
  151. if (data.retcode === 0) {
  152. message.success(i18n.t(`message.modified`));
  153. }
  154. return data?.data ?? {};
  155. },
  156. });
  157. return { data, loading, runFlow: mutateAsync };
  158. };
  159. export const useResetFlow = () => {
  160. const { id } = useParams();
  161. const {
  162. data,
  163. isPending: loading,
  164. mutateAsync,
  165. } = useMutation({
  166. mutationKey: ['resetFlow'],
  167. mutationFn: async () => {
  168. const { data } = await flowService.resetCanvas({ id });
  169. return data;
  170. },
  171. });
  172. return { data, loading, resetFlow: mutateAsync };
  173. };