| 1 | import { XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | import { useCallback, useEffect } from "react"; |
| 3 | import { useDispatch, useSelector } from "react-redux"; |
| 4 | import styled from "styled-components"; |
| 5 | import { defaultBorderRadius } from ".."; |
| 6 | import { newSession } from "../../redux/slices/sessionSlice"; |
| 7 | import { |
| 8 | addTab, |
| 9 | handleSessionChange, |
| 10 | removeTab, |
| 11 | setActiveTab, |
| 12 | setTabs, |
| 13 | } from "../../redux/slices/tabsSlice"; |
| 14 | import { AppDispatch, RootState } from "../../redux/store"; |
| 15 | import { loadSession, saveCurrentSession } from "../../redux/thunks/session"; |
| 16 | |
| 17 | const TabBarContainer = styled.div` |
| 18 | display: flex; |
| 19 | flex-wrap: wrap; |
| 20 | flex-shrink: 0; |
| 21 | flex-grow: 0; |
| 22 | background-color: var(--vscode-tab-inactiveBackground); |
| 23 | border-bottom: none; |
| 24 | position: relative; |
| 25 | margin-top: 2px; |
| 26 | |
| 27 | /* Hide scrollbar but keep functionality */ |
| 28 | scrollbar-width: none; |
| 29 | &::-webkit-scrollbar { |
| 30 | display: none; |
| 31 | } |
| 32 | `; |
| 33 | |
| 34 | const Tab = styled.div<{ isActive: boolean }>` |
| 35 | display: flex; |
| 36 | align-items: center; |
| 37 | box-sizing: border-box; |
| 38 | padding: 0 5px 0 12px; |
| 39 | flex-grow: 1; |
| 40 | width: 100px; |
| 41 | max-width: 150px; |
| 42 | height: 25px; |
| 43 | background-color: ${(props) => |
| 44 | props.isActive |
| 45 | ? "var(--vscode-tab-activeBackground)" |
| 46 | : "var(--vscode-tab-inactiveBackground)"}; |
| 47 | color: ${(props) => |
| 48 | props.isActive |
| 49 | ? "var(--vscode-tab-activeForeground)" |
| 50 | : "var(--vscode-tab-inactiveForeground)"}; |
| 51 | cursor: pointer; |
| 52 | border: 1px solid var(--vscode-tab-border); |
| 53 | border-bottom: ${(props) => |
| 54 | props.isActive ? "none" : `1px solid var(--vscode-tab-border)`}; |
| 55 | user-select: none; |
| 56 | position: relative; |
| 57 | transition: background-color 0.2s; |
| 58 | border-top: ${(props) => |
| 59 | props.isActive |
| 60 | ? `1px solid var(--vscode-tab-activeBorderTop, --vscode-tab-border)` |
| 61 | : `1px solid var(--vscode-tab-border)`}; |
| 62 | &:first-child { |
| 63 | border-left: none; |
| 64 | } |
| 65 | & + & { |
| 66 | border-left: none; |
| 67 | } |
| 68 | |
| 69 | &:hover { |
| 70 | background-color: ${(props) => |
| 71 | props.isActive |
| 72 | ? "var(--vscode-tab-activeBackground)" |
| 73 | : "var(--vscode-tab-hoverBackground)"}; |
| 74 | color: ${(props) => |
| 75 | props.isActive |
| 76 | ? "var(--vscode-tab-activeForeground)" |
| 77 | : "var(--vscode-tab-hoverForeground)"}; |
| 78 | } |
| 79 | `; |
| 80 | |
| 81 | const TabTitle = styled.span` |
| 82 | flex: 1; |
| 83 | white-space: nowrap; |
| 84 | overflow: hidden; |
| 85 | text-overflow: ellipsis; |
| 86 | font-size: 13px; |
| 87 | `; |
| 88 | |
| 89 | const CloseButton = styled.button` |
| 90 | display: flex; |
| 91 | align-items: center; |
| 92 | justify-content: center; |
| 93 | width: 16px; |
| 94 | height: 16px; |
| 95 | margin-left: 4px; |
| 96 | border: none; |
| 97 | background: transparent; |
| 98 | color: inherit; |
| 99 | opacity: 0.7; |
| 100 | cursor: pointer; |
| 101 | border-radius: ${defaultBorderRadius}; |
| 102 | padding: 2px; |
| 103 | visibility: hidden; |
| 104 | |
| 105 | &:hover { |
| 106 | opacity: 1; |
| 107 | background-color: var(--vscode-tab-hoverBackground); |
| 108 | } |
| 109 | |
| 110 | ${Tab}:hover & { |
| 111 | visibility: visible; |
| 112 | } |
| 113 | |
| 114 | &[disabled] { |
| 115 | display: none !important; |
| 116 | } |
| 117 | `; |
| 118 | |
| 119 | const TabBarSpace = styled.div` |
| 120 | flex: 1; |
| 121 | display: flex; |
| 122 | border-bottom: 1px solid var(--vscode-tab-border); |
| 123 | background-color: var(--vscode-tab-inactiveBackground); |
| 124 | `; |
| 125 | |
| 126 | const NewTabButton = styled.button` |
| 127 | display: flex; |
| 128 | align-items: center; |
| 129 | justify-content: center; |
| 130 | width: 35px; |
| 131 | height: 100%; |
| 132 | border: none; |
| 133 | background: transparent; |
| 134 | color: var(--vscode-tab-inactiveForeground); |
| 135 | cursor: pointer; |
| 136 | opacity: 0.7; |
| 137 | transition: opacity 0.2s; |
| 138 | |
| 139 | &:hover { |
| 140 | opacity: 1; |
| 141 | background-color: var(--vscode-tab-hoverBackground); |
| 142 | color: var(--vscode-tab-hoverForeground); |
| 143 | } |
| 144 | `; |
| 145 | |
| 146 | export function TabBar() { |
| 147 | const dispatch = useDispatch<AppDispatch>(); |
| 148 | const currentSession = useSelector((state: RootState) => state.session); |
| 149 | const currentSessionId = useSelector((state: RootState) => state.session.id); |
| 150 | const currentSessionTitle = useSelector( |
| 151 | (state: RootState) => state.session.title, |
| 152 | ); |
| 153 | const hasHistory = useSelector( |
| 154 | (state: RootState) => state.session.history.length > 0, |
| 155 | ); |
| 156 | const tabs = useSelector((state: RootState) => state.tabs.tabs); |
| 157 | |
| 158 | // Simple UUID generator for our needs |
| 159 | const generateId = useCallback(() => { |
| 160 | return Date.now().toString(36) + Math.random().toString(36).substring(2); |
| 161 | }, []); |
| 162 | |
| 163 | // Handle session changes |
| 164 | useEffect(() => { |
| 165 | if (!currentSessionId) return; |
| 166 | |
| 167 | dispatch( |
| 168 | handleSessionChange({ |
| 169 | currentSessionId, |
| 170 | currentSessionTitle, |
| 171 | newTabId: generateId(), // Pass the ID generator result |
| 172 | }), |
| 173 | ); |
| 174 | }, [currentSessionId, currentSessionTitle]); |
| 175 | |
| 176 | const handleNewTab = async () => { |
| 177 | // Save current session before creating new one |
| 178 | if (hasHistory) { |
| 179 | await dispatch( |
| 180 | saveCurrentSession({ openNewSession: false, generateTitle: true }), |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | dispatch(newSession()); |
| 185 | |
| 186 | dispatch( |
| 187 | addTab({ |
| 188 | id: generateId(), |
| 189 | title: `Chat ${tabs.length + 1}`, |
| 190 | isActive: true, |
| 191 | sessionId: undefined, |
| 192 | }), |
| 193 | ); |
| 194 | }; |
| 195 | |
| 196 | useEffect(() => { |
| 197 | if (!tabs.length) { |
| 198 | handleNewTab(); |
| 199 | } |
| 200 | }, [tabs.map((t) => t.id).join(",")]); |
| 201 | |
| 202 | const handleTabClick = async (id: string) => { |
| 203 | const targetTab = tabs.find((tab) => tab.id === id); |
| 204 | if (!targetTab) return; |
| 205 | |
| 206 | if (targetTab.sessionId) { |
| 207 | // Switch to existing session |
| 208 | await dispatch( |
| 209 | loadSession({ |
| 210 | sessionId: targetTab.sessionId, |
| 211 | saveCurrentSession: hasHistory, |
| 212 | }), |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | dispatch(setActiveTab(id)); |
| 217 | }; |
| 218 | |
| 219 | const handleTabClose = async (id: string) => { |
| 220 | //if (tabs.length <= 1) return; |
| 221 | |
| 222 | const isClosingActive = tabs.find((t) => t.id === id)?.isActive; |
| 223 | const filtered = tabs.filter((t) => t.id !== id); |
| 224 | |
| 225 | if (isClosingActive) { |
| 226 | const lastTab = filtered[filtered.length - 1]; |
| 227 | if (filtered.length) { |
| 228 | await handleTabClick(lastTab.id); |
| 229 | dispatch( |
| 230 | setTabs( |
| 231 | filtered.map((tab, i) => ({ |
| 232 | ...tab, |
| 233 | isActive: i === filtered.length - 1, |
| 234 | })), |
| 235 | ), |
| 236 | ); |
| 237 | } else { |
| 238 | dispatch(setTabs([])); |
| 239 | dispatch(newSession()); |
| 240 | } |
| 241 | } else { |
| 242 | dispatch(removeTab(id)); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | return tabs.length === 1 ? ( |
| 247 | <></> |
| 248 | ) : ( |
| 249 | <TabBarContainer> |
| 250 | {tabs.map((tab) => ( |
| 251 | <Tab |
| 252 | key={tab.id} |
| 253 | isActive={tab.isActive} |
| 254 | onClick={() => handleTabClick(tab.id)} |
| 255 | > |
| 256 | <TabTitle>{tab.title}</TabTitle> |
| 257 | <CloseButton |
| 258 | /* disabled={tabs.length === 1} */ |
| 259 | onClick={(e) => { |
| 260 | e.stopPropagation(); |
| 261 | handleTabClose(tab.id); |
| 262 | }} |
| 263 | > |
| 264 | <XMarkIcon width={12} height={12} /> |
| 265 | </CloseButton> |
| 266 | </Tab> |
| 267 | ))} |
| 268 | <TabBarSpace> |
| 269 | {/* <NewTabButton onClick={handleNewTab}> |
| 270 | <PlusIcon width={16} height={16} /> |
| 271 | </NewTabButton> */} |
| 272 | </TabBarSpace> |
| 273 | </TabBarContainer> |
| 274 | ); |
| 275 | } |