From 78126c3d0bc130320f36a92e07981ec73ce68b63 Mon Sep 17 00:00:00 2001 From: icarus Date: Mon, 20 Oct 2025 06:47:22 +0800 Subject: [PATCH] refactor(ocr): simplify useOcrProvider hook by using data api Replace complex provider and config management with useQuery and useMutation hooks Add loading states and error handling Remove unused imports and simplify return type --- src/renderer/src/hooks/ocr/useOcrProvider.ts | 115 ++++--------------- 1 file changed, 22 insertions(+), 93 deletions(-) diff --git a/src/renderer/src/hooks/ocr/useOcrProvider.ts b/src/renderer/src/hooks/ocr/useOcrProvider.ts index 8087235e13..87872a55f0 100644 --- a/src/renderer/src/hooks/ocr/useOcrProvider.ts +++ b/src/renderer/src/hooks/ocr/useOcrProvider.ts @@ -1,106 +1,35 @@ -import { usePreference } from '@data/hooks/usePreference' -import { loggerService } from '@logger' -import type { - BuiltinOcrProviderId, - OcrOvConfig, - OcrOvProvider, - OcrPpocrConfig, - OcrPpocrProvider, - OcrSystemConfig, - OcrSystemProvider, - OcrTesseractConfig, - OcrTesseractProvider -} from '@renderer/types' -import { BUILTIN_OCR_PROVIDERS_MAP } from '@shared/config/ocr' -import { merge } from 'lodash' -import { useCallback, useMemo } from 'react' +import { useMutation, useQuery } from '@data/hooks/useDataApi' +import type { OcrProviderConfig } from '@renderer/types' +import { getErrorMessage } from '@renderer/utils' +import type { ConcreteApiPaths } from '@shared/data/api' +import { useCallback } from 'react' +import { useTranslation } from 'react-i18next' -const logger = loggerService.withContext('useOcrProvider') +// const logger = loggerService.withContext('useOcrProvider') -const PROVIDER_REGISTRY = { - ovocr: null as unknown as OcrOvProvider, - paddleocr: null as unknown as OcrPpocrProvider, - system: null as unknown as OcrSystemProvider, - tesseract: null as unknown as OcrTesseractProvider -} +export const useOcrProvider = (id: string) => { + const { t } = useTranslation() -const CONFIG_REGISTRY = { - ovocr: null as unknown as OcrOvConfig, - paddleocr: null as unknown as OcrPpocrConfig, - system: null as unknown as OcrSystemConfig, - tesseract: null as unknown as OcrTesseractConfig -} as const - -type ProviderMap = typeof PROVIDER_REGISTRY - -type ConfigMap = typeof CONFIG_REGISTRY - -type TProvider = ProviderMap[T] - -type TConfig = ConfigMap[T] - -type UseOcrProviderReturn = { - provider: TProvider - config: TConfig - updateConfig: (update: Partial>) => void -} - -export const useOcrProvider = (id: T): UseOcrProviderReturn => { - const provider = useMemo(() => { - switch (id) { - case 'ovocr': - return BUILTIN_OCR_PROVIDERS_MAP.ovocr - case 'paddleocr': - return BUILTIN_OCR_PROVIDERS_MAP.paddleocr - case 'system': - return BUILTIN_OCR_PROVIDERS_MAP.system - case 'tesseract': - return BUILTIN_OCR_PROVIDERS_MAP.tesseract - } - }, [id]) - const [ovConfig, setOvConfig] = usePreference('ocr.provider.config.ovocr') - const [ppConfig, setPpConfig] = usePreference('ocr.provider.config.paddleocr') - const [sysConfig, setSysConfig] = usePreference('ocr.provider.config.system') - const [tesConfig, setTesConfig] = usePreference('ocr.provider.config.tesseract') - - const config = useMemo(() => { - switch (id) { - case 'ovocr': - return ovConfig - case 'paddleocr': - return ppConfig - case 'system': - return sysConfig - case 'tesseract': - return tesConfig - } - }, [id, ovConfig, ppConfig, sysConfig, tesConfig]) + const path: ConcreteApiPaths = `/ocr/providers/${id}` + const { data: provider, loading, error } = useQuery(path, undefined) + const { mutate, loading: mutating } = useMutation('PATCH', path) const updateConfig = useCallback( - (update: Partial>) => { - switch (id) { - case 'ovocr': - setOvConfig(merge({}, ovConfig, update)) - break - case 'paddleocr': - setPpConfig(merge({}, ppConfig, update)) - break - case 'system': - setSysConfig(merge({}, sysConfig, update)) - break - case 'tesseract': - setTesConfig(merge({}, tesConfig, update)) - break - default: - logger.warn(`Unsupported OCR provider id: ${id}`) + async (update: Partial) => { + try { + await mutate({ body: update }) + } catch (e) { + window.toast.error({ title: t('ocr.provider.config.patch.error.failed'), description: getErrorMessage(e) }) } }, - [id, ovConfig, ppConfig, setOvConfig, setPpConfig, setSysConfig, setTesConfig, sysConfig, tesConfig] + [mutate, t] ) return { provider, - config, + loading, + mutating, + error, updateConfig - } as UseOcrProviderReturn + } }