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.

use-auth.ts 6.2KB

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