11import { XMarkIcon } from "@heroicons/react/24/outline" ;
22import React , { useCallback , useEffect } from "react" ;
33import { useDispatch , useSelector } from "react-redux" ;
4+ import { useAppSelector } from "../../redux/hooks" ;
45import styled from "styled-components" ;
56import { defaultBorderRadius } from ".." ;
67import { newSession } from "../../redux/slices/sessionSlice" ;
@@ -10,10 +11,14 @@ import {
1011 removeTab ,
1112 setActiveTab ,
1213 setTabs ,
14+ setTabModel ,
1315} from "../../redux/slices/tabsSlice" ;
1416import { AppDispatch , RootState } from "../../redux/store" ;
1517import { loadSession , saveCurrentSession } from "../../redux/thunks/session" ;
1618import { varWithFallback } from "../../styles/theme" ;
19+ import { useAuth } from "../../context/Auth" ;
20+ import { selectSelectedChatModel } from "../../redux/slices/configSlice" ;
21+ import { updateSelectedModelByRole } from "../../redux/thunks/updateSelectedModelByRole" ;
1722
1823// Haven't set up theme colors for tabs yet
1924// Will keep it simple and choose from existing ones. Comments show vars we could use
@@ -137,6 +142,11 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
137142 ( state : RootState ) => state . session . history . length > 0 ,
138143 ) ;
139144 const tabs = useSelector ( ( state : RootState ) => state . tabs . tabs ) ;
145+ const selectedModel = useAppSelector ( selectSelectedChatModel ) ;
146+ const { selectedProfile } = useAuth ( ) ;
147+ const activeTab = tabs . find ( ( tab ) => tab . isActive ) ;
148+ const activeTabId = activeTab ?. id ;
149+ const activeTabModel = activeTab ?. modelTitle ;
140150
141151 // Simple UUID generator for our needs
142152 const generateId = useCallback ( ( ) => {
@@ -155,6 +165,15 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
155165 ) ;
156166 } , [ currentSessionId , currentSessionTitle ] ) ;
157167
168+ // Persist selected model into the active tab in Redux
169+ useEffect ( ( ) => {
170+ if ( activeTabId && selectedModel ?. title ) {
171+ dispatch (
172+ setTabModel ( { id : activeTabId , modelTitle : selectedModel . title } ) ,
173+ ) ;
174+ }
175+ } , [ activeTabId , selectedModel ?. title , dispatch ] ) ;
176+
158177 const handleNewTab = async ( ) => {
159178 // Save current session before creating new one
160179 if ( hasHistory ) {
@@ -171,6 +190,7 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
171190 title : `Chat ${ tabs . length + 1 } ` ,
172191 isActive : true ,
173192 sessionId : undefined ,
193+ modelTitle : selectedModel ?. title ,
174194 } ) ,
175195 ) ;
176196 } ;
@@ -196,6 +216,16 @@ export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => {
196216 }
197217
198218 dispatch ( setActiveTab ( id ) ) ;
219+ // restore model for this tab
220+ if ( targetTab . modelTitle && selectedProfile ) {
221+ void dispatch (
222+ updateSelectedModelByRole ( {
223+ selectedProfile,
224+ role : "chat" ,
225+ modelTitle : targetTab . modelTitle ,
226+ } ) ,
227+ ) ;
228+ }
199229 } ;
200230
201231 const handleTabClose = async ( id : string ) => {
0 commit comments