mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-10 07:19:02 +08:00
refactor(action): simplify assistant and topic initialization
Move assistant and topic initialization to useState hooks and sync with refs Remove redundant initialization code from useEffect fix: Error: Cannot access refs during render fix: Error: Calling setState synchronously within an effect can trigger cascading renders
This commit is contained in:
parent
76bf78b810
commit
cf9bfce43c
@ -36,32 +36,38 @@ const ActionGeneral: FC<Props> = React.memo(({ 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 [assistant] = useState<Assistant>(() => {
|
||||||
|
const assistant = action.assistantId
|
||||||
|
? getAssistantById(action.assistantId) || getDefaultAssistant()
|
||||||
|
: getDefaultAssistant()
|
||||||
|
if (!assistant.model) {
|
||||||
|
return { ...assistant, model: getDefaultModel() }
|
||||||
|
} else {
|
||||||
|
return assistant
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const [topic] = useState<Topic | null>(getDefaultTopic(assistant.id))
|
||||||
const initialized = useRef(false)
|
const initialized = useRef(false)
|
||||||
|
|
||||||
// Use useRef for values that shouldn't trigger re-renders
|
// Use useRef for values that shouldn't trigger re-renders
|
||||||
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 promptContentRef = useRef('')
|
const promptContentRef = useRef('')
|
||||||
const askId = useRef('')
|
const askId = useRef('')
|
||||||
|
|
||||||
|
// Sync refs
|
||||||
|
useEffect(() => {
|
||||||
|
assistantRef.current = assistant
|
||||||
|
}, [assistant])
|
||||||
|
useEffect(() => {
|
||||||
|
topicRef.current = topic
|
||||||
|
}, [assistant, topic])
|
||||||
|
|
||||||
// Initialize values only once when action changes
|
// Initialize values only once when action changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (initialized.current) return
|
if (initialized.current) return
|
||||||
initialized.current = true
|
initialized.current = true
|
||||||
|
|
||||||
// Initialize assistant
|
|
||||||
const currentAssistant = action.assistantId
|
|
||||||
? getAssistantById(action.assistantId) || getDefaultAssistant()
|
|
||||||
: getDefaultAssistant()
|
|
||||||
|
|
||||||
assistantRef.current = {
|
|
||||||
...currentAssistant,
|
|
||||||
model: currentAssistant.model || getDefaultModel()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize topic
|
|
||||||
topicRef.current = getDefaultTopic(currentAssistant.id)
|
|
||||||
|
|
||||||
// Initialize prompt content
|
// Initialize prompt content
|
||||||
let userContent = ''
|
let userContent = ''
|
||||||
switch (action.id) {
|
switch (action.id) {
|
||||||
@ -128,7 +134,7 @@ const ActionGeneral: FC<Props> = React.memo(({ action, scrollToBottom }) => {
|
|||||||
fetchResult()
|
fetchResult()
|
||||||
}, [fetchResult])
|
}, [fetchResult])
|
||||||
|
|
||||||
const allMessages = useTopicMessages(topicRef.current?.id || '')
|
const allMessages = useTopicMessages(topic?.id || '')
|
||||||
|
|
||||||
// Memoize the messages to prevent unnecessary re-renders
|
// Memoize the messages to prevent unnecessary re-renders
|
||||||
const messageContent = useMemo(() => {
|
const messageContent = useMemo(() => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user