| 1 | import { PayloadAction, createSlice } from "@reduxjs/toolkit"; |
| 2 | |
| 3 | export interface Tab { |
| 4 | id: string; |
| 5 | title: string; |
| 6 | isActive: boolean; |
| 7 | sessionId?: string; |
| 8 | } |
| 9 | |
| 10 | interface TabsState { |
| 11 | tabs: Tab[]; |
| 12 | } |
| 13 | |
| 14 | const initialState: TabsState = { |
| 15 | tabs: [{ |
| 16 | id: Date.now().toString(36) + Math.random().toString(36).substring(2), |
| 17 | title: "Chat 1", |
| 18 | isActive: true |
| 19 | }] |
| 20 | }; |
| 21 | |
| 22 | export const tabsSlice = createSlice({ |
| 23 | name: "tabs", |
| 24 | initialState, |
| 25 | reducers: { |
| 26 | setTabs: (state, action: PayloadAction<Tab[]>) => { |
| 27 | state.tabs = action.payload; |
| 28 | }, |
| 29 | updateTab: (state, action: PayloadAction<{ id: string; updates: Partial<Tab> }>) => { |
| 30 | const { id, updates } = action.payload; |
| 31 | state.tabs = state.tabs.map(tab => |
| 32 | tab.id === id ? { ...tab, ...updates } : tab |
| 33 | ); |
| 34 | }, |
| 35 | addTab: (state, action: PayloadAction<Tab>) => { |
| 36 | state.tabs = state.tabs.map(tab => ({ ...tab, isActive: action.payload.isActive ? false : tab.isActive })) |
| 37 | .concat(action.payload); |
| 38 | }, |
| 39 | removeTab: (state, action: PayloadAction<string>) => { |
| 40 | state.tabs = state.tabs.filter(tab => tab.id !== action.payload); |
| 41 | }, |
| 42 | setActiveTab: (state, action: PayloadAction<string>) => { |
| 43 | state.tabs = state.tabs.map(tab => ({ |
| 44 | ...tab, |
| 45 | isActive: tab.id === action.payload |
| 46 | })); |
| 47 | }, |
| 48 | handleSessionChange: ( |
| 49 | state, |
| 50 | action: PayloadAction<{ |
| 51 | currentSessionId: string; |
| 52 | currentSessionTitle: string; |
| 53 | newTabId?: string; |
| 54 | }> |
| 55 | ) => { |
| 56 | const { currentSessionId, currentSessionTitle, newTabId } = action.payload; |
| 57 | |
| 58 | const activeTab = state.tabs.find(tab => tab.isActive); |
| 59 | if (!activeTab) return; |
| 60 | |
| 61 | // Current session matches active tab's session |
| 62 | if (activeTab.sessionId === currentSessionId) { |
| 63 | state.tabs = state.tabs.map(tab => |
| 64 | tab.id === activeTab.id |
| 65 | ? { ...tab, title: currentSessionTitle } |
| 66 | : tab |
| 67 | ); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Check if there's another tab with the same session ID |
| 72 | const existingTabWithSession = state.tabs.find( |
| 73 | tab => tab.sessionId === currentSessionId |
| 74 | ); |
| 75 | if (existingTabWithSession) { |
| 76 | // Activate the existing tab and update its title |
| 77 | // Remove any unassigned tabs |
| 78 | state.tabs = state.tabs |
| 79 | .filter(tab => tab.sessionId || tab.id === existingTabWithSession.id) |
| 80 | .map(tab => ({ |
| 81 | ...tab, |
| 82 | isActive: tab.id === existingTabWithSession.id, |
| 83 | title: tab.sessionId === currentSessionId |
| 84 | ? currentSessionTitle |
| 85 | : tab.title, |
| 86 | })); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Active tab has no session ID |
| 91 | if (!activeTab.sessionId) { |
| 92 | state.tabs = state.tabs.map(tab => |
| 93 | tab.id === activeTab.id |
| 94 | ? { |
| 95 | ...tab, |
| 96 | sessionId: currentSessionId, |
| 97 | title: currentSessionTitle, |
| 98 | } |
| 99 | : tab |
| 100 | ); |
| 101 | } else { |
| 102 | // Active tab has a session ID, create new tab |
| 103 | state.tabs = state.tabs.map(tab => ({ ...tab, isActive: false })).concat({ |
| 104 | id: newTabId || Date.now().toString(36) + Math.random().toString(36).substring(2), |
| 105 | title: currentSessionTitle, |
| 106 | isActive: true, |
| 107 | sessionId: currentSessionId, |
| 108 | }); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | }); |
| 113 | |
| 114 | export const { setTabs, updateTab, addTab, removeTab, setActiveTab, handleSessionChange } = tabsSlice.actions; |
| 115 | |
| 116 | export default tabsSlice.reducer; |