Elffuss BraidHub

kikocis/continue public

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