Skip to content

Commit 77535b0

Browse files
authored
feat(Inputbar): cache mentioned models state between renders (#10197)
Use a ref to track mentioned models state and cache it in a module-level variable when component unmounts to preserve state between renders
1 parent dec68ee commit 77535b0

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/renderer/src/pages/home/Inputbar/Inputbar.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ interface Props {
7171

7272
let _text = ''
7373
let _files: FileType[] = []
74+
let _mentionedModelsCache: Model[] = []
7475

7576
const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic, topic }) => {
7677
const [text, setText] = useState(_text)
@@ -103,7 +104,8 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic, topic }) =
103104
const spaceClickTimer = useRef<NodeJS.Timeout>(null)
104105
const [isTranslating, setIsTranslating] = useState(false)
105106
const [selectedKnowledgeBases, setSelectedKnowledgeBases] = useState<KnowledgeBase[]>([])
106-
const [mentionedModels, setMentionedModels] = useState<Model[]>([])
107+
const [mentionedModels, setMentionedModels] = useState<Model[]>(_mentionedModelsCache)
108+
const mentionedModelsRef = useRef(mentionedModels)
107109
const [isDragging, setIsDragging] = useState(false)
108110
const [isFileDragging, setIsFileDragging] = useState(false)
109111
const [textareaHeight, setTextareaHeight] = useState<number>()
@@ -114,6 +116,10 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic, topic }) =
114116
const isGenerateImageAssistant = useMemo(() => isGenerateImageModel(model), [model])
115117
const { setTimeoutTimer } = useTimer()
116118

119+
useEffect(() => {
120+
mentionedModelsRef.current = mentionedModels
121+
}, [mentionedModels])
122+
117123
const isVisionSupported = useMemo(
118124
() =>
119125
(mentionedModels.length > 0 && isVisionModels(mentionedModels)) ||
@@ -179,6 +185,13 @@ const Inputbar: FC<Props> = ({ assistant: _assistant, setActiveTopic, topic }) =
179185
_text = text
180186
_files = files
181187

188+
useEffect(() => {
189+
// 利用useEffect清理函数在卸载组件时更新状态缓存
190+
return () => {
191+
_mentionedModelsCache = mentionedModelsRef.current
192+
}
193+
}, [])
194+
182195
const focusTextarea = useCallback(() => {
183196
textareaRef.current?.focus()
184197
}, [])

0 commit comments

Comments
 (0)