From 5078baca1b91637d96dfb0fe4df642c45c9637eb Mon Sep 17 00:00:00 2001 From: IndieKKY Date: Sat, 20 Jan 2024 12:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81gemini?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/biz/Body.tsx | 9 ++-- src/biz/SegmentCard.tsx | 5 ++- src/biz/Settings.tsx | 29 +++++++----- src/chrome/openaiService.ts | 13 ++++++ src/chrome/taskService.ts | 5 ++- src/hooks/useTranslate.ts | 90 +++++++++++++++++++++++++------------ src/typings.d.ts | 2 +- 7 files changed, 104 insertions(+), 49 deletions(-) diff --git a/src/biz/Body.tsx b/src/biz/Body.tsx index 04006c4..cf8d3a9 100644 --- a/src/biz/Body.tsx +++ b/src/biz/Body.tsx @@ -45,7 +45,6 @@ const Body = () => { const foldAll = useAppSelector(state => state.env.foldAll) const envData = useAppSelector(state => state.env.envData) const compact = useAppSelector(state => state.env.tempData.compact) - const apiKey = useAppSelector(state => state.env.envData.apiKey) const floatKeyPointsSegIdx = useAppSelector(state => state.env.floatKeyPointsSegIdx) const translateEnable = useAppSelector(state => state.env.envData.translateEnable) const summarizeEnable = useAppSelector(state => state.env.envData.summarizeEnable) @@ -76,6 +75,7 @@ const Body = () => { }, [dispatch]) const onSummarizeAll = useCallback(() => { + const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey if (!apiKey) { dispatch(setPage(PAGE_SETTINGS)) toast.error('需要先设置ApiKey!') @@ -98,7 +98,7 @@ const Body = () => { } toast.success(`已添加${segments_.length}个总结任务!`) } - }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segments]) + }, [addSummarizeTask, curSummaryType, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, segments]) const onFoldAll = useCallback(() => { dispatch(setFoldAll(!foldAll)) @@ -111,13 +111,14 @@ const Body = () => { }, [dispatch, foldAll, segments]) const toggleAutoTranslateCallback = useCallback(() => { - if (envData.apiKey) { + const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey + if (apiKey) { dispatch(setAutoTranslate(!autoTranslate)) } else { dispatch(setPage(PAGE_SETTINGS)) toast.error('需要先设置ApiKey!') } - }, [autoTranslate, dispatch, envData.apiKey]) + }, [autoTranslate, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey]) const onEnableAutoScroll = useCallback(() => { dispatch(setAutoScroll(true)) diff --git a/src/biz/SegmentCard.tsx b/src/biz/SegmentCard.tsx index 8eed898..66fa963 100644 --- a/src/biz/SegmentCard.tsx +++ b/src/biz/SegmentCard.tsx @@ -66,19 +66,20 @@ const Summarize = (props: { const {segment, segmentIdx, summary, float} = props const dispatch = useAppDispatch() - const apiKey = useAppSelector(state => state.env.envData.apiKey) + const envData = useAppSelector(state => state.env.envData) const fontSize = useAppSelector(state => state.env.envData.fontSize) const curSummaryType = useAppSelector(state => state.env.tempData.curSummaryType) const {addSummarizeTask} = useTranslate() const onGenerate = useCallback(() => { + const apiKey = envData.aiType === 'gemini'?envData.geminiApiKey:envData.apiKey if (apiKey) { addSummarizeTask(curSummaryType, segment).catch(console.error) } else { dispatch(setPage(PAGE_SETTINGS)) toast.error('需要先设置ApiKey!') } - }, [addSummarizeTask, apiKey, curSummaryType, dispatch, segment]) + }, [addSummarizeTask, curSummaryType, dispatch, envData.aiType, envData.apiKey, envData.geminiApiKey, segment]) const onCopy = useCallback(() => { if (summary != null) { diff --git a/src/biz/Settings.tsx b/src/biz/Settings.tsx index e3c481b..6047420 100644 --- a/src/biz/Settings.tsx +++ b/src/biz/Settings.tsx @@ -262,18 +262,21 @@ const Settings = () => { {aiTypeValue === 'gemini' &&
- +
- {moreFold?'点击查看说明':'点击折叠说明'} + {moreFold ? '点击查看说明' : '点击折叠说明'}
{!moreFold &&
}
- {promptsFold?'点击查看提示词':'点击折叠提示词'} + {promptsFold ? '点击查看提示词' : '点击折叠提示词'}
{!promptsFold &&
{PROMPT_TYPES.map((item, idx) => @@ -282,16 +285,18 @@ const Settings = () => { setPromptsValue({ ...promptsValue, // @ts-expect-error - [item.type]: PROMPT_DEFAULTS[item.type]??'' + [item.type]: PROMPT_DEFAULTS[item.type] ?? '' }) - }}>点击填充默认
+ }}>点击填充默认 + } htmlFor={`prompt-${item.type}`}> -