Elffuss BraidHub

kikocis/continue public

d0cd3fef8227 · 7.2 KiBRawPermalink
1import {
2 BookOpenIcon,
3 ChatBubbleLeftIcon,
4 ChevronLeftIcon,
5 CubeIcon,
6 EllipsisHorizontalIcon,
7 ExclamationTriangleIcon,
8 PencilIcon,
9 Squares2X2Icon,
10 WrenchScrewdriverIcon,
11} from "@heroicons/react/24/outline";
12import { ReactNode, useContext, useEffect, useState } from "react";
13import { vscBadgeForeground } from "../../..";
14import { IdeMessengerContext } from "../../../../context/IdeMessenger";
15import { useAppSelector } from "../../../../redux/hooks";
16import { ToolTip } from "../../../gui/Tooltip";
17import AssistantSelect from "../../../modelSelection/platform/AssistantSelect";
18import FreeTrialButton from "../../../modelSelection/platform/FreeTrialButton";
19import { useFontSize } from "../../../ui/font";
20import HoverItem from "../../InputToolbar/HoverItem";
21import { useLump } from "../LumpContext";
22import { ErrorsSectionTooltip } from "../sections/errors/ErrorsSectionTooltip";
23import { McpSectionTooltip } from "../sections/mcp/MCPTooltip";
24import { ToolsSectionTooltip } from "../sections/tool-policies/ToolPoliciesSectionTooltip";
25
26import { usesFreeTrialApiKey } from "core/config/usesFreeTrialApiKey";
27import type { FreeTrialStatus } from "core/control-plane/client";
28
29interface BlockSettingsToolbarIcon {
30 title: string;
31 tooltip: ReactNode;
32 icon: React.ComponentType<any>;
33 itemCount?: number;
34 onClick: () => void;
35 isSelected?: boolean;
36 className?: string;
37}
38
39interface Section {
40 id: string;
41 title: string;
42 tooltip: ReactNode;
43 icon: React.ComponentType<any>;
44}
45
46const sections: Section[] = [
47 { id: "models", title: "Models", tooltip: "Models", icon: CubeIcon },
48 { id: "rules", title: "Rules", tooltip: "Rules", icon: PencilIcon },
49 { id: "docs", title: "Docs", tooltip: "Docs", icon: BookOpenIcon },
50 {
51 id: "prompts",
52 title: "Prompts",
53 tooltip: "Prompts",
54 icon: ChatBubbleLeftIcon,
55 },
56 {
57 id: "tools",
58 title: "Tools",
59 tooltip: <ToolsSectionTooltip />,
60 icon: WrenchScrewdriverIcon,
61 },
62 {
63 id: "mcp",
64 title: "MCP",
65 tooltip: <McpSectionTooltip />,
66 icon: Squares2X2Icon,
67 },
68 {
69 id: "error",
70 title: "Errors",
71 tooltip: <ErrorsSectionTooltip />,
72 icon: ExclamationTriangleIcon,
73 },
74];
75
76function BlockSettingsToolbarIcon(
77 props: BlockSettingsToolbarIcon & { sectionId?: string },
78) {
79 const isErrorSection = props.sectionId === "error";
80
81 const id = `block-settings-toolbar-icon-${props.sectionId}`;
82
83 const fontSize = useFontSize(-3);
84 return (
85 <>
86 <HoverItem
87 px={0}
88 onClick={props.onClick}
89 data-testid={id}
90 data-tooltip-id={id}
91 >
92 <div
93 role="button"
94 tabIndex={0}
95 onKeyDown={(e) => {
96 if (e.key === "Enter" || e.key === " ") {
97 e.preventDefault();
98 props.onClick();
99 }
100 }}
101 className={`${
102 props.isSelected
103 ? isErrorSection
104 ? "bg-error"
105 : "bg-badge"
106 : undefined
107 } relative flex select-none items-center rounded-full px-[3px] py-0.5 transition-all duration-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500/50 sm:px-1 ${props.className || ""}`}
108 >
109 <props.icon
110 className={`h-[13px] w-[13px] flex-shrink-0 hover:brightness-125 ${
111 isErrorSection ? "text-error" : ""
112 }`}
113 style={{
114 color: props.isSelected ? vscBadgeForeground : undefined,
115 }}
116 aria-hidden="true"
117 />
118 <div
119 style={{ fontSize }}
120 className={`overflow-hidden transition-all duration-200 ${
121 props.isSelected ? "ml-1 w-auto opacity-100" : "w-0 opacity-0"
122 }`}
123 >
124 <span
125 className="whitespace-nowrap"
126 style={{ color: vscBadgeForeground }}
127 >
128 {props.title}
129 </span>
130 </div>
131 </div>
132 </HoverItem>
133 <ToolTip delayShow={700} id={id}>
134 {props.tooltip}
135 </ToolTip>
136 </>
137 );
138}
139
140export function BlockSettingsTopToolbar() {
141 const {
142 isToolbarExpanded,
143 toggleToolbar,
144 selectedSection,
145 setSelectedSection,
146 } = useLump();
147
148 const configError = useAppSelector((store) => store.config.configError);
149 const config = useAppSelector((state) => state.config.config);
150 const ideMessenger = useContext(IdeMessengerContext);
151
152 const [freeTrialStatus, setFreeTrialStatus] =
153 useState<FreeTrialStatus | null>(null);
154 const isUsingFreeTrial = usesFreeTrialApiKey(config);
155
156 useEffect(() => {
157 const fetchFreeTrialStatus = () => {
158 ideMessenger
159 .request("controlPlane/getFreeTrialStatus", undefined)
160 .then((resp) => {
161 if (resp.status === "success") {
162 setFreeTrialStatus(resp.content);
163 }
164 })
165 .catch(() => {});
166 };
167
168 fetchFreeTrialStatus();
169
170 let intervalId: NodeJS.Timeout | null = null;
171
172 if (isUsingFreeTrial) {
173 intervalId = setInterval(fetchFreeTrialStatus, 15000);
174 }
175
176 return () => {
177 if (intervalId) {
178 clearInterval(intervalId);
179 }
180 };
181 }, [ideMessenger, isUsingFreeTrial]);
182
183 const handleEllipsisClick = () => {
184 if (isToolbarExpanded) {
185 setSelectedSection(null);
186 }
187 toggleToolbar();
188 };
189
190 const visibleSections = sections.filter(
191 (section) =>
192 section.id !== "error" ||
193 (section.id === "error" && configError && configError?.length > 0),
194 );
195
196 return (
197 <div className="flex flex-1 items-center justify-between gap-2">
198 <div className="flex flex-row">
199 <div className="xs:flex text-description hidden items-center justify-center gap-0.5">
200 <BlockSettingsToolbarIcon
201 className="-ml-1.5"
202 icon={isToolbarExpanded ? ChevronLeftIcon : EllipsisHorizontalIcon}
203 tooltip={isToolbarExpanded ? "Collapse Toolbar" : "Expand Toolbar"}
204 title=""
205 isSelected={false}
206 onClick={handleEllipsisClick}
207 />
208 <div
209 className={`${isToolbarExpanded ? "w-min" : "w-0"} flex overflow-hidden transition-all duration-200`}
210 >
211 <div className="flex">
212 {visibleSections.map((section) => (
213 <BlockSettingsToolbarIcon
214 key={section.id}
215 sectionId={section.id}
216 icon={section.icon}
217 tooltip={section.tooltip}
218 title={section.title}
219 isSelected={selectedSection === section.id}
220 onClick={() =>
221 setSelectedSection(
222 selectedSection === section.id ? null : section.id,
223 )
224 }
225 />
226 ))}
227 </div>
228 </div>
229 </div>
230 </div>
231 <div className="flex gap-0.5">
232 <HoverItem
233 data-tooltip-id="assistant-select-tooltip"
234 className="!m-0 !p-0"
235 >
236 {isUsingFreeTrial ? (
237 <FreeTrialButton freeTrialStatus={freeTrialStatus} />
238 ) : (
239 <AssistantSelect />
240 )}
241 <ToolTip id="assistant-select-tooltip" place="top">
242 {isUsingFreeTrial ? "View free trial usage" : "Select Assistant"}
243 </ToolTip>
244 </HoverItem>
245 </div>
246 </div>
247 );
248}