Elffuss BraidHub

kikocis/continue public

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