Feat: Support re-segmentation (#114)
Co-authored-by: John Wang <takatost@gmail.com> Co-authored-by: Jyong <718720800@qq.com> Co-authored-by: 金伟强 <iamjoel007@gmail.com>
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
import { useState, useCallback, SetStateAction, Dispatch } from 'react'
|
||||
import type { Dispatch, SetStateAction } from 'react'
|
||||
import { useCallback, useState } from 'react'
|
||||
import debounce from 'lodash-es/debounce'
|
||||
import { DebouncedFunc } from 'lodash-es'
|
||||
import type { DebouncedFunc } from 'lodash-es'
|
||||
import { validateProviderKey } from '@/service/common'
|
||||
|
||||
export enum ValidatedStatus {
|
||||
Success = 'success',
|
||||
Error = 'error',
|
||||
Exceed = 'exceed'
|
||||
Exceed = 'exceed',
|
||||
}
|
||||
export type ValidatedStatusState = {
|
||||
status?: ValidatedStatus,
|
||||
status?: ValidatedStatus
|
||||
message?: string
|
||||
}
|
||||
// export type ValidatedStatusState = ValidatedStatus | undefined | ValidatedError
|
||||
export type SetValidatedStatus = Dispatch<SetStateAction<ValidatedStatusState>>
|
||||
export type ValidateFn = DebouncedFunc<(token: any, config: ValidateFnConfig) => void>
|
||||
type ValidateTokenReturn = [
|
||||
boolean,
|
||||
ValidatedStatusState,
|
||||
boolean,
|
||||
ValidatedStatusState,
|
||||
SetValidatedStatus,
|
||||
ValidateFn
|
||||
ValidateFn,
|
||||
]
|
||||
export type ValidateFnConfig = {
|
||||
beforeValidating: (token: any) => boolean
|
||||
@@ -29,19 +30,21 @@ const useValidateToken = (providerName: string): ValidateTokenReturn => {
|
||||
const [validating, setValidating] = useState(false)
|
||||
const [validatedStatus, setValidatedStatus] = useState<ValidatedStatusState>({})
|
||||
const validate = useCallback(debounce(async (token: string, config: ValidateFnConfig) => {
|
||||
if (!config.beforeValidating(token)) {
|
||||
if (!config.beforeValidating(token))
|
||||
return false
|
||||
}
|
||||
|
||||
setValidating(true)
|
||||
try {
|
||||
const res = await validateProviderKey({ url: `/workspaces/current/providers/${providerName}/token-validate`, body: { token } })
|
||||
setValidatedStatus(
|
||||
res.result === 'success'
|
||||
? { status: ValidatedStatus.Success }
|
||||
res.result === 'success'
|
||||
? { status: ValidatedStatus.Success }
|
||||
: { status: ValidatedStatus.Error, message: res.error })
|
||||
} catch (e: any) {
|
||||
}
|
||||
catch (e: any) {
|
||||
setValidatedStatus({ status: ValidatedStatus.Error, message: e.message })
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
setValidating(false)
|
||||
}
|
||||
}, 500), [])
|
||||
@@ -50,8 +53,8 @@ const useValidateToken = (providerName: string): ValidateTokenReturn => {
|
||||
validating,
|
||||
validatedStatus,
|
||||
setValidatedStatus,
|
||||
validate
|
||||
validate,
|
||||
]
|
||||
}
|
||||
|
||||
export default useValidateToken
|
||||
export default useValidateToken
|
||||
|
Reference in New Issue
Block a user