fix(translate): use state instead of ref as hook parameter

fix: Error: Cannot access refs during render
This commit is contained in:
icarus 2025-10-14 17:34:15 +08:00
parent d482e661fb
commit e0c334b5ed

View File

@ -41,14 +41,20 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
const [isContented, setIsContented] = useState(false) const [isContented, setIsContented] = useState(false)
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [contentToCopy, setContentToCopy] = useState('') const [contentToCopy, setContentToCopy] = useState('')
const [topic, setTopic] = useState<Topic | null>(null)
const { getLanguageByLangcode } = useTranslate() const { getLanguageByLangcode } = useTranslate()
// Use useRef for values that shouldn't trigger re-renders // Use useRef for values that shouldn't trigger re-renders
const initialized = useRef(false) const initialized = useRef(false)
const assistantRef = useRef<Assistant | null>(null) const assistantRef = useRef<Assistant | null>(null)
const topicRef = useRef<Topic | null>(null) const topicRef = useRef<Topic | null>(topic)
const askId = useRef('') const askId = useRef('')
// update ref
useEffect(() => {
topicRef.current = topic
}, [topic])
useEffect(() => { useEffect(() => {
runAsyncFunction(async () => { runAsyncFunction(async () => {
const biDirectionLangPair = await db.settings.get({ id: 'translate:bidirectional:pair' }) const biDirectionLangPair = await db.settings.get({ id: 'translate:bidirectional:pair' })
@ -147,7 +153,7 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
fetchResult() fetchResult()
}, [fetchResult]) }, [fetchResult])
const allMessages = useTopicMessages(topicRef.current?.id || '') const allMessages = useTopicMessages(topic?.id || '')
const messageContent = useMemo(() => { const messageContent = useMemo(() => {
const assistantMessages = allMessages.filter((message) => message.role === 'assistant') const assistantMessages = allMessages.filter((message) => message.role === 'assistant')