您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-plugin-auth-action.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. useCallback,
  3. useRef,
  4. useState,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import { useToastContext } from '@/app/components/base/toast'
  8. import type { PluginPayload } from '@/app/components/plugins/plugin-auth/types'
  9. import {
  10. useDeletePluginCredentialHook,
  11. useSetPluginDefaultCredentialHook,
  12. useUpdatePluginCredentialHook,
  13. } from '../hooks/use-credential'
  14. export const usePluginAuthAction = (
  15. pluginPayload: PluginPayload,
  16. onUpdate?: () => void,
  17. ) => {
  18. const { t } = useTranslation()
  19. const { notify } = useToastContext()
  20. const pendingOperationCredentialId = useRef<string | null>(null)
  21. const [deleteCredentialId, setDeleteCredentialId] = useState<string | null>(null)
  22. const { mutateAsync: deletePluginCredential } = useDeletePluginCredentialHook(pluginPayload)
  23. const openConfirm = useCallback((credentialId?: string) => {
  24. if (credentialId)
  25. pendingOperationCredentialId.current = credentialId
  26. setDeleteCredentialId(pendingOperationCredentialId.current)
  27. }, [])
  28. const closeConfirm = useCallback(() => {
  29. setDeleteCredentialId(null)
  30. pendingOperationCredentialId.current = null
  31. }, [])
  32. const [doingAction, setDoingAction] = useState(false)
  33. const doingActionRef = useRef(doingAction)
  34. const handleSetDoingAction = useCallback((doing: boolean) => {
  35. doingActionRef.current = doing
  36. setDoingAction(doing)
  37. }, [])
  38. const [editValues, setEditValues] = useState<Record<string, any> | null>(null)
  39. const handleConfirm = useCallback(async () => {
  40. if (doingActionRef.current)
  41. return
  42. if (!pendingOperationCredentialId.current) {
  43. setDeleteCredentialId(null)
  44. return
  45. }
  46. try {
  47. handleSetDoingAction(true)
  48. await deletePluginCredential({ credential_id: pendingOperationCredentialId.current })
  49. notify({
  50. type: 'success',
  51. message: t('common.api.actionSuccess'),
  52. })
  53. onUpdate?.()
  54. setDeleteCredentialId(null)
  55. pendingOperationCredentialId.current = null
  56. setEditValues(null)
  57. }
  58. finally {
  59. handleSetDoingAction(false)
  60. }
  61. }, [deletePluginCredential, onUpdate, notify, t, handleSetDoingAction])
  62. const handleEdit = useCallback((id: string, values: Record<string, any>) => {
  63. pendingOperationCredentialId.current = id
  64. setEditValues(values)
  65. }, [])
  66. const handleRemove = useCallback(() => {
  67. setDeleteCredentialId(pendingOperationCredentialId.current)
  68. }, [])
  69. const { mutateAsync: setPluginDefaultCredential } = useSetPluginDefaultCredentialHook(pluginPayload)
  70. const handleSetDefault = useCallback(async (id: string) => {
  71. if (doingActionRef.current)
  72. return
  73. try {
  74. handleSetDoingAction(true)
  75. await setPluginDefaultCredential(id)
  76. notify({
  77. type: 'success',
  78. message: t('common.api.actionSuccess'),
  79. })
  80. onUpdate?.()
  81. }
  82. finally {
  83. handleSetDoingAction(false)
  84. }
  85. }, [setPluginDefaultCredential, onUpdate, notify, t, handleSetDoingAction])
  86. const { mutateAsync: updatePluginCredential } = useUpdatePluginCredentialHook(pluginPayload)
  87. const handleRename = useCallback(async (payload: {
  88. credential_id: string
  89. name: string
  90. }) => {
  91. if (doingActionRef.current)
  92. return
  93. try {
  94. handleSetDoingAction(true)
  95. await updatePluginCredential(payload)
  96. notify({
  97. type: 'success',
  98. message: t('common.api.actionSuccess'),
  99. })
  100. onUpdate?.()
  101. }
  102. finally {
  103. handleSetDoingAction(false)
  104. }
  105. }, [updatePluginCredential, notify, t, handleSetDoingAction, onUpdate])
  106. return {
  107. doingAction,
  108. handleSetDoingAction,
  109. openConfirm,
  110. closeConfirm,
  111. deleteCredentialId,
  112. setDeleteCredentialId,
  113. handleConfirm,
  114. editValues,
  115. setEditValues,
  116. handleEdit,
  117. handleRemove,
  118. handleSetDefault,
  119. handleRename,
  120. pendingOperationCredentialId,
  121. }
  122. }