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.

flow-hooks.ts 4.4KB

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