refactor: modularize frontend flows

This commit is contained in:
Lan
2026-06-03 02:01:57 +08:00
parent 9300607f96
commit a11e7900b4
85 changed files with 4654 additions and 4363 deletions
+122
View File
@@ -0,0 +1,122 @@
import { FileService, uploadChunkedFile } from '@/services'
import type { AlertType, ApiResponse, ExpireStyle, UploadProgress } from '@/types'
import { calculateFileHash, packFilesAsZip } from '@/utils/file-processing'
import { usePresignedUpload } from './usePresignedUpload'
type Translate = (
key: string,
params?: Record<string, string | number | undefined>
) => string
type UseSendSubmitOptions = {
getMaxFileSize: () => number
notify: (message: string, type: AlertType) => void
translate: Translate
onProgress: (progress: number) => void
onHashCalculated: (hash: string) => void
}
type SubmitFileOptions = {
selectedFile: File | null
selectedFiles: File[]
expireValue: number
expireStyle: string
enableChunk: boolean
validateFileSize: (file: File) => boolean
}
type SubmitTextOptions = {
text: string
expireValue: number
expireStyle: string
}
export function useSendSubmit(options: UseSendSubmitOptions) {
const { uploadFile: presignUploadFile, reset: resetPresignUpload } = usePresignedUpload({
getMaxFileSize: options.getMaxFileSize,
notify: options.notify
})
const handleChunkUpload = async (
file: File,
expireValue: number,
expireStyle: string
): Promise<ApiResponse> => {
return uploadChunkedFile(file, {
expireValue,
expireStyle,
onHashCalculated: options.onHashCalculated,
onProgress: (progress: UploadProgress) => {
options.onProgress(progress.percentage)
},
messages: {
initFailed: options.translate('send.messages.initChunkUploadFailed'),
chunkFailed: (index) => options.translate('send.messages.chunkUploadFailed', { index }),
completeFailed: options.translate('send.messages.completeUploadFailed')
}
})
}
const handlePresignedUpload = async (
file: File,
expireValue: number,
expireStyle: string
): Promise<ApiResponse<{ code?: string; name?: string }>> => {
const code = await presignUploadFile(file, {
expireValue,
expireStyle: expireStyle as ExpireStyle,
onProgress: (progress) => {
options.onProgress(progress.percentage)
}
})
if (!code) {
throw new Error(options.translate('send.messages.uploadFailed'))
}
return {
code: 200,
detail: {
code,
name: file.name
}
}
}
const submitFile = async ({
selectedFile,
selectedFiles,
expireValue,
expireStyle,
enableChunk,
validateFileSize
}: SubmitFileOptions): Promise<ApiResponse | null> => {
let fileToUpload = selectedFile
if (selectedFiles.length > 0) {
options.notify('正在打包文件...', 'success')
fileToUpload = await packFilesAsZip(selectedFiles)
if (!validateFileSize(fileToUpload)) {
return null
}
options.onHashCalculated(await calculateFileHash(fileToUpload))
}
if (!fileToUpload) {
throw new Error(options.translate('send.messages.selectFile'))
}
return enableChunk
? handleChunkUpload(fileToUpload, expireValue, expireStyle)
: handlePresignedUpload(fileToUpload, expireValue, expireStyle)
}
const submitText = ({ text, expireValue, expireStyle }: SubmitTextOptions) =>
FileService.uploadText(text, expireValue, expireStyle)
return {
resetPresignUpload,
submitFile,
submitText
}
}