mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-17 00:06:34 +00:00
Migrate the management UI to Z components and managed notifications, register the library loading directive, and align fixed dependencies and submodules with the published UI version. Co-authored-by: Codex <noreply@openai.com>
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { ZNotification } from '@codex-proxy/ui'
|
|
|
|
import { shallowRef } from 'vue'
|
|
import { ApiError } from '@/api/request'
|
|
import { errorMessage, withMinimumDuration } from '@/utils/operation'
|
|
|
|
type MaybePromise<T> = T | Promise<T>
|
|
interface AsyncActionRunOptions {
|
|
// 仅用于本地校验、文件与浏览器操作;接口错误由请求层负责提示。
|
|
errorText?: string | false
|
|
minimumMs?: number
|
|
onError?: (error: unknown) => void
|
|
rethrow?: boolean
|
|
}
|
|
|
|
function resolveErrorText(error: unknown, errorText: string | false | undefined) {
|
|
if (error instanceof ApiError || errorText === false) {
|
|
return ''
|
|
}
|
|
|
|
return errorMessage(error, errorText || '操作失败')
|
|
}
|
|
|
|
export function useAsyncAction(defaults: AsyncActionRunOptions = {}) {
|
|
const loading = shallowRef(false)
|
|
|
|
async function run<T>(task: () => MaybePromise<T>, overrides: AsyncActionRunOptions = {}) {
|
|
if (loading.value) {
|
|
return undefined
|
|
}
|
|
|
|
loading.value = true
|
|
const options = { ...defaults, ...overrides }
|
|
try {
|
|
const execute = async () => task()
|
|
return options.minimumMs === undefined
|
|
? await execute()
|
|
: await withMinimumDuration(execute, options.minimumMs)
|
|
}
|
|
catch (error) {
|
|
options.onError?.(error)
|
|
|
|
const message = resolveErrorText(error, options.errorText)
|
|
if (message) {
|
|
ZNotification.error({ message })
|
|
}
|
|
|
|
if (options.rethrow) {
|
|
throw error
|
|
}
|
|
return undefined
|
|
}
|
|
finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
return {
|
|
loading,
|
|
run,
|
|
}
|
|
}
|