Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { useAuthService } from './use-auth-service'
  9. import type {
  10. ConfigurationMethodEnum,
  11. Credential,
  12. CustomConfigurationModelFixedFields,
  13. CustomModel,
  14. ModelProvider,
  15. } from '../../declarations'
  16. import {
  17. useModelModalHandler,
  18. useRefreshModel,
  19. } from '@/app/components/header/account-setting/model-provider-page/hooks'
  20. export const useAuth = (
  21. provider: ModelProvider,
  22. configurationMethod: ConfigurationMethodEnum,
  23. currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
  24. isModelCredential?: boolean,
  25. onUpdate?: () => void,
  26. ) => {
  27. const { t } = useTranslation()
  28. const { notify } = useToastContext()
  29. const {
  30. getDeleteCredentialService,
  31. getActiveCredentialService,
  32. getEditCredentialService,
  33. getAddCredentialService,
  34. } = useAuthService(provider.provider)
  35. const handleOpenModelModal = useModelModalHandler()
  36. const { handleRefreshModel } = useRefreshModel()
  37. const pendingOperationCredentialId = useRef<string | null>(null)
  38. const pendingOperationModel = useRef<CustomModel | null>(null)
  39. const [deleteCredentialId, setDeleteCredentialId] = useState<string | null>(null)
  40. const openConfirmDelete = useCallback((credential?: Credential, model?: CustomModel) => {
  41. if (credential)
  42. pendingOperationCredentialId.current = credential.credential_id
  43. if (model)
  44. pendingOperationModel.current = model
  45. setDeleteCredentialId(pendingOperationCredentialId.current)
  46. }, [])
  47. const closeConfirmDelete = useCallback(() => {
  48. setDeleteCredentialId(null)
  49. pendingOperationCredentialId.current = null
  50. }, [])
  51. const [doingAction, setDoingAction] = useState(false)
  52. const doingActionRef = useRef(doingAction)
  53. const handleSetDoingAction = useCallback((doing: boolean) => {
  54. doingActionRef.current = doing
  55. setDoingAction(doing)
  56. }, [])
  57. const handleActiveCredential = useCallback(async (credential: Credential, model?: CustomModel) => {
  58. if (doingActionRef.current)
  59. return
  60. try {
  61. handleSetDoingAction(true)
  62. await getActiveCredentialService(!!model)({
  63. credential_id: credential.credential_id,
  64. model: model?.model,
  65. model_type: model?.model_type,
  66. })
  67. notify({
  68. type: 'success',
  69. message: t('common.api.actionSuccess'),
  70. })
  71. onUpdate?.()
  72. handleRefreshModel(provider, configurationMethod, undefined)
  73. }
  74. finally {
  75. handleSetDoingAction(false)
  76. }
  77. }, [getActiveCredentialService, onUpdate, notify, t, handleSetDoingAction])
  78. const handleConfirmDelete = useCallback(async () => {
  79. if (doingActionRef.current)
  80. return
  81. if (!pendingOperationCredentialId.current) {
  82. setDeleteCredentialId(null)
  83. return
  84. }
  85. try {
  86. handleSetDoingAction(true)
  87. await getDeleteCredentialService(!!isModelCredential)({
  88. credential_id: pendingOperationCredentialId.current,
  89. model: pendingOperationModel.current?.model,
  90. model_type: pendingOperationModel.current?.model_type,
  91. })
  92. notify({
  93. type: 'success',
  94. message: t('common.api.actionSuccess'),
  95. })
  96. onUpdate?.()
  97. handleRefreshModel(provider, configurationMethod, undefined)
  98. setDeleteCredentialId(null)
  99. pendingOperationCredentialId.current = null
  100. pendingOperationModel.current = null
  101. }
  102. finally {
  103. handleSetDoingAction(false)
  104. }
  105. }, [onUpdate, notify, t, handleSetDoingAction, getDeleteCredentialService, isModelCredential])
  106. const handleAddCredential = useCallback((model?: CustomModel) => {
  107. if (model)
  108. pendingOperationModel.current = model
  109. }, [])
  110. const handleSaveCredential = useCallback(async (payload: Record<string, any>) => {
  111. if (doingActionRef.current)
  112. return
  113. try {
  114. handleSetDoingAction(true)
  115. let res: { result?: string } = {}
  116. if (payload.credential_id)
  117. res = await getEditCredentialService(!!isModelCredential)(payload as any)
  118. else
  119. res = await getAddCredentialService(!!isModelCredential)(payload as any)
  120. if (res.result === 'success') {
  121. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  122. onUpdate?.()
  123. }
  124. }
  125. finally {
  126. handleSetDoingAction(false)
  127. }
  128. }, [onUpdate, notify, t, handleSetDoingAction, getEditCredentialService, getAddCredentialService])
  129. const handleOpenModal = useCallback((credential?: Credential, model?: CustomModel) => {
  130. handleOpenModelModal(
  131. provider,
  132. configurationMethod,
  133. currentCustomConfigurationModelFixedFields,
  134. isModelCredential,
  135. credential,
  136. model,
  137. onUpdate,
  138. )
  139. }, [handleOpenModelModal, provider, configurationMethod, currentCustomConfigurationModelFixedFields, isModelCredential, onUpdate])
  140. return {
  141. pendingOperationCredentialId,
  142. pendingOperationModel,
  143. openConfirmDelete,
  144. closeConfirmDelete,
  145. doingAction,
  146. handleActiveCredential,
  147. handleConfirmDelete,
  148. handleAddCredential,
  149. deleteCredentialId,
  150. handleSaveCredential,
  151. handleOpenModal,
  152. }
  153. }