use-endpoints.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { get, post } from './base'
  2. import type {
  3. EndpointsResponse,
  4. } from '@/app/components/plugins/types'
  5. import {
  6. useMutation,
  7. useQuery,
  8. useQueryClient,
  9. } from '@tanstack/react-query'
  10. const NAME_SPACE = 'endpoints'
  11. export const useEndpointList = (pluginID: string) => {
  12. return useQuery({
  13. queryKey: [NAME_SPACE, 'list', pluginID],
  14. queryFn: () => get<EndpointsResponse>('/workspaces/current/endpoints/list/plugin', {
  15. params: {
  16. plugin_id: pluginID,
  17. page: 1,
  18. page_size: 100,
  19. },
  20. }),
  21. })
  22. }
  23. export const useInvalidateEndpointList = () => {
  24. const queryClient = useQueryClient()
  25. return (pluginID: string) => {
  26. queryClient.invalidateQueries(
  27. {
  28. queryKey: [NAME_SPACE, 'list', pluginID],
  29. })
  30. }
  31. }
  32. export const useCreateEndpoint = ({
  33. onSuccess,
  34. onError,
  35. }: {
  36. onSuccess?: () => void
  37. onError?: (error: any) => void
  38. }) => {
  39. return useMutation({
  40. mutationKey: [NAME_SPACE, 'create'],
  41. mutationFn: (payload: { pluginUniqueID: string, state: Record<string, any> }) => {
  42. const { pluginUniqueID, state } = payload
  43. const newName = state.name
  44. delete state.name
  45. return post('/workspaces/current/endpoints/create', {
  46. body: {
  47. plugin_unique_identifier: pluginUniqueID,
  48. settings: state,
  49. name: newName,
  50. },
  51. })
  52. },
  53. onSuccess,
  54. onError,
  55. })
  56. }
  57. export const useUpdateEndpoint = ({
  58. onSuccess,
  59. onError,
  60. }: {
  61. onSuccess?: () => void
  62. onError?: (error: any) => void
  63. }) => {
  64. return useMutation({
  65. mutationKey: [NAME_SPACE, 'update'],
  66. mutationFn: (payload: { endpointID: string, state: Record<string, any> }) => {
  67. const { endpointID, state } = payload
  68. const newName = state.name
  69. delete state.name
  70. return post('/workspaces/current/endpoints/update', {
  71. body: {
  72. endpoint_id: endpointID,
  73. settings: state,
  74. name: newName,
  75. },
  76. })
  77. },
  78. onSuccess,
  79. onError,
  80. })
  81. }
  82. export const useDeleteEndpoint = ({
  83. onSuccess,
  84. onError,
  85. }: {
  86. onSuccess?: () => void
  87. onError?: (error: any) => void
  88. }) => {
  89. return useMutation({
  90. mutationKey: [NAME_SPACE, 'delete'],
  91. mutationFn: (endpointID: string) => {
  92. return post('/workspaces/current/endpoints/delete', {
  93. body: {
  94. endpoint_id: endpointID,
  95. },
  96. })
  97. },
  98. onSuccess,
  99. onError,
  100. })
  101. }
  102. export const useEnableEndpoint = ({
  103. onSuccess,
  104. onError,
  105. }: {
  106. onSuccess?: () => void
  107. onError?: (error: any) => void
  108. }) => {
  109. return useMutation({
  110. mutationKey: [NAME_SPACE, 'enable'],
  111. mutationFn: (endpointID: string) => {
  112. return post('/workspaces/current/endpoints/enable', {
  113. body: {
  114. endpoint_id: endpointID,
  115. },
  116. })
  117. },
  118. onSuccess,
  119. onError,
  120. })
  121. }
  122. export const useDisableEndpoint = ({
  123. onSuccess,
  124. onError,
  125. }: {
  126. onSuccess?: () => void
  127. onError?: (error: any) => void
  128. }) => {
  129. return useMutation({
  130. mutationKey: [NAME_SPACE, 'disable'],
  131. mutationFn: (endpointID: string) => {
  132. return post('/workspaces/current/endpoints/disable', {
  133. body: {
  134. endpoint_id: endpointID,
  135. },
  136. })
  137. },
  138. onSuccess,
  139. onError,
  140. })
  141. }