fix(ocr): 改进OCR处理中的消息管理和错误处理

在useOcr钩子中统一管理OCR处理的消息提示,并完善错误处理逻辑
移除TranslatePage中重复的消息管理代码,简化OCR处理流程
This commit is contained in:
icarus 2025-08-22 20:21:29 +08:00
parent 9ac7d7f95e
commit a46697c5c3

View File

@ -1,8 +1,11 @@
import { loggerService } from '@logger'
import { useAppSelector } from '@renderer/store'
import { ImageFileMetadata, isImageFile, SupportedOcrFile } from '@renderer/types'
import { uuid } from '@renderer/utils'
import { formatErrorMessage } from '@renderer/utils/error'
import { useTranslation } from 'react-i18next'
export const useImageOcr = () => {}
const logger = loggerService.withContext('useOcr')
export const useOcr = () => {
const { t } = useTranslation()
@ -18,14 +21,24 @@ export const useOcr = () => {
}
/**
* OCR识别
* OCR识别.
* @param file OCR的文件
* @returns OCR识别结果的Promise
* @throws
* @throws OCR失败时抛出错
*/
const ocr = async (file: SupportedOcrFile) => {
if (isImageFile(file)) {
return ocrImage(file)
const key = uuid()
window.message.loading({ content: t('ocr.processing'), key, duration: 0 })
try {
if (isImageFile(file)) {
return ocrImage(file)
}
} catch (e) {
logger.error('Failed to ocr.', e as Error)
window.message.error(t('ocr.error.unknown') + ': ' + formatErrorMessage(e))
throw e
} finally {
window.message.destroy(key)
}
// @ts-expect-error all types should be covered
throw new Error(t('ocr.file.not_supported', { type: file.type }))