| 1 | import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; |
| 2 | import { useContext, useEffect, useState } from "react"; |
| 3 | import { useNavigate } from "react-router-dom"; |
| 4 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 5 | import { useAppSelector } from "../../../../redux/hooks"; |
| 6 | import FreeTrialButton from "../../../FreeTrialButton"; |
| 7 | import { ToolTip } from "../../../gui/Tooltip"; |
| 8 | |
| 9 | import { usesFreeTrialApiKey } from "core/config/usesFreeTrialApiKey"; |
| 10 | import type { FreeTrialStatus } from "core/control-plane/client"; |
| 11 | import { getLocalStorage } from "../../../../util/localStorage"; |
| 12 | import { CONFIG_ROUTES } from "../../../../util/navigation"; |
| 13 | import { AssistantAndOrgListbox } from "../../../AssistantAndOrgListbox"; |
| 14 | |
| 15 | export function BlockSettingsTopToolbar() { |
| 16 | const navigate = useNavigate(); |
| 17 | |
| 18 | const configError = useAppSelector((store) => store.config.configError); |
| 19 | const config = useAppSelector((state) => state.config.config); |
| 20 | const ideMessenger = useContext(IdeMessengerContext); |
| 21 | |
| 22 | const [freeTrialStatus, setFreeTrialStatus] = |
| 23 | useState<FreeTrialStatus | null>(null); |
| 24 | const hasExitedFreeTrial = getLocalStorage("hasExitedFreeTrial"); |
| 25 | const isUsingFreeTrial = usesFreeTrialApiKey(config) && !hasExitedFreeTrial; |
| 26 | |
| 27 | useEffect(() => { |
| 28 | const fetchFreeTrialStatus = () => { |
| 29 | ideMessenger |
| 30 | .request("controlPlane/getFreeTrialStatus", undefined) |
| 31 | .then((resp) => { |
| 32 | if (resp.status === "success") { |
| 33 | setFreeTrialStatus(resp.content); |
| 34 | } |
| 35 | }) |
| 36 | .catch(() => {}); |
| 37 | }; |
| 38 | |
| 39 | fetchFreeTrialStatus(); |
| 40 | |
| 41 | let intervalId: NodeJS.Timeout | null = null; |
| 42 | |
| 43 | if (isUsingFreeTrial) { |
| 44 | intervalId = setInterval(fetchFreeTrialStatus, 15000); |
| 45 | } |
| 46 | |
| 47 | return () => { |
| 48 | if (intervalId) { |
| 49 | clearInterval(intervalId); |
| 50 | } |
| 51 | }; |
| 52 | }, [ideMessenger, isUsingFreeTrial]); |
| 53 | |
| 54 | const shouldShowError = configError && configError?.length > 0; |
| 55 | |
| 56 | return ( |
| 57 | <div className="flex flex-1 items-center justify-between gap-1.5"> |
| 58 | <div> |
| 59 | {shouldShowError && ( |
| 60 | <ToolTip delayShow={700} content="View configuration errors"> |
| 61 | <div |
| 62 | role="button" |
| 63 | tabIndex={0} |
| 64 | onClick={() => navigate(CONFIG_ROUTES.AGENTS)} |
| 65 | onKeyDown={(e) => { |
| 66 | if (e.key === "Enter" || e.key === " ") { |
| 67 | e.preventDefault(); |
| 68 | navigate(CONFIG_ROUTES.AGENTS); |
| 69 | } |
| 70 | }} |
| 71 | data-testid="block-settings-toolbar-icon-error" |
| 72 | className="relative flex cursor-pointer select-none items-center rounded-full px-1.5 py-1 sm:px-1.5" |
| 73 | > |
| 74 | <ExclamationTriangleIcon |
| 75 | className="text-warning h-[13px] w-[13px] flex-shrink-0" |
| 76 | aria-hidden="true" |
| 77 | /> |
| 78 | </div> |
| 79 | </ToolTip> |
| 80 | )} |
| 81 | </div> |
| 82 | <ToolTip |
| 83 | place="top" |
| 84 | content={isUsingFreeTrial ? "View free trial usage" : "Select Agent"} |
| 85 | > |
| 86 | <div> |
| 87 | {isUsingFreeTrial ? ( |
| 88 | <FreeTrialButton freeTrialStatus={freeTrialStatus} /> |
| 89 | ) : ( |
| 90 | <AssistantAndOrgListbox variant="lump" /> |
| 91 | )} |
| 92 | </div> |
| 93 | </ToolTip> |
| 94 | </div> |
| 95 | ); |
| 96 | } |