| 1 | import { ChevronDownIcon } from "@heroicons/react/24/outline"; |
| 2 | import { FreeTrialStatus } from "core/control-plane/client"; |
| 3 | import { useContext } from "react"; |
| 4 | import { Button, SecondaryButton, vscButtonBackground } from "."; |
| 5 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 6 | import { fontSize } from "../util"; |
| 7 | import { setLocalStorage } from "../util/localStorage"; |
| 8 | import { Listbox, ListboxButton, ListboxOptions, Transition } from "./ui"; |
| 9 | |
| 10 | interface ProgressBarProps { |
| 11 | label: string; |
| 12 | current: number; |
| 13 | total: number; |
| 14 | } |
| 15 | |
| 16 | function ProgressBar({ label, current, total }: ProgressBarProps) { |
| 17 | const percentage = Math.min((current / total) * 100, 100); |
| 18 | |
| 19 | return ( |
| 20 | <div className="mb-4"> |
| 21 | <div className="mb-1 flex justify-between text-xs"> |
| 22 | <span>{label}</span> |
| 23 | <span> |
| 24 | {current}/{total} |
| 25 | </span> |
| 26 | </div> |
| 27 | <div className="h-2 w-full rounded-full bg-gray-700"> |
| 28 | <div |
| 29 | className="h-2 rounded-full transition-all duration-300" |
| 30 | style={{ |
| 31 | width: `${percentage}%`, |
| 32 | backgroundColor: vscButtonBackground, |
| 33 | }} |
| 34 | /> |
| 35 | </div> |
| 36 | </div> |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | interface FreeTrialProgressBarsProps { |
| 41 | freeTrialStatus: FreeTrialStatus; |
| 42 | } |
| 43 | |
| 44 | function FreeTrialProgressBars({ |
| 45 | freeTrialStatus, |
| 46 | }: FreeTrialProgressBarsProps) { |
| 47 | // Use data from freeTrialStatus |
| 48 | const autocompleteUsage = { |
| 49 | current: freeTrialStatus.autocompleteCount, |
| 50 | total: freeTrialStatus.autocompleteLimit, |
| 51 | }; |
| 52 | const chatUsage = { |
| 53 | current: freeTrialStatus.chatCount, |
| 54 | total: freeTrialStatus.chatLimit, |
| 55 | }; |
| 56 | |
| 57 | return ( |
| 58 | <> |
| 59 | <ProgressBar |
| 60 | label="Autocomplete usage" |
| 61 | current={autocompleteUsage.current ?? 0} |
| 62 | total={autocompleteUsage.total} |
| 63 | /> |
| 64 | |
| 65 | <ProgressBar |
| 66 | label="Chat usage" |
| 67 | current={chatUsage.current ?? 0} |
| 68 | total={chatUsage.total} |
| 69 | /> |
| 70 | </> |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | interface FreeTrialButtonProps { |
| 75 | freeTrialStatus?: FreeTrialStatus | null; |
| 76 | } |
| 77 | |
| 78 | export default function FreeTrialButton({ |
| 79 | freeTrialStatus, |
| 80 | }: FreeTrialButtonProps) { |
| 81 | const ideMessenger = useContext(IdeMessengerContext); |
| 82 | |
| 83 | const onExitFreeTrial = async () => { |
| 84 | setLocalStorage("hasExitedFreeTrial", true); |
| 85 | |
| 86 | await ideMessenger.request("controlPlane/openUrl", { |
| 87 | path: "setup-models", |
| 88 | orgSlug: undefined, |
| 89 | }); |
| 90 | }; |
| 91 | |
| 92 | return ( |
| 93 | <Listbox> |
| 94 | <div className="relative"> |
| 95 | <ListboxButton |
| 96 | className="text-description border-none bg-transparent hover:brightness-125" |
| 97 | style={{ fontSize: fontSize(-3) }} |
| 98 | > |
| 99 | <div className="flex flex-row items-center gap-1.5"> |
| 100 | <span className="line-clamp-1 select-none">Free trial usage</span> |
| 101 | </div> |
| 102 | <ChevronDownIcon |
| 103 | className="h-2 w-2 flex-shrink-0 select-none" |
| 104 | aria-hidden="true" |
| 105 | /> |
| 106 | </ListboxButton> |
| 107 | |
| 108 | <Transition> |
| 109 | <ListboxOptions className="pb-0"> |
| 110 | <div className="max-w-96 px-4 pb-4"> |
| 111 | <h3 className="mb-4 text-sm font-semibold"> |
| 112 | Free trial of the Models Add-On |
| 113 | </h3> |
| 114 | |
| 115 | <div className="mb-4"> |
| 116 | <span className="text-description"> |
| 117 | You are currently using a free trial the Models Add-On, which |
| 118 | allows you to use a variety of frontier models for a flat |
| 119 | monthly fee. Read more about usage limits and what models are |
| 120 | included{" "} |
| 121 | <span |
| 122 | onClick={async () => { |
| 123 | await ideMessenger.request("controlPlane/openUrl", { |
| 124 | path: "pricing", |
| 125 | orgSlug: undefined, |
| 126 | }); |
| 127 | }} |
| 128 | className="cursor-pointer text-blue-400 underline hover:text-blue-300" |
| 129 | > |
| 130 | here |
| 131 | </span> |
| 132 | . |
| 133 | </span> |
| 134 | </div> |
| 135 | |
| 136 | {!freeTrialStatus ? ( |
| 137 | <div className="mb-4 flex items-center justify-center py-8"> |
| 138 | <span className="text-description"> |
| 139 | Loading trial usage... |
| 140 | </span> |
| 141 | </div> |
| 142 | ) : ( |
| 143 | <FreeTrialProgressBars freeTrialStatus={freeTrialStatus} /> |
| 144 | )} |
| 145 | |
| 146 | <div className="mt-4 flex gap-2"> |
| 147 | <SecondaryButton className="flex-1" onClick={onExitFreeTrial}> |
| 148 | Exit trial |
| 149 | </SecondaryButton> |
| 150 | <Button |
| 151 | className="flex-1" |
| 152 | onClick={async () => { |
| 153 | await ideMessenger.request("controlPlane/openUrl", { |
| 154 | path: "settings/billing", |
| 155 | orgSlug: undefined, |
| 156 | }); |
| 157 | }} |
| 158 | > |
| 159 | Upgrade |
| 160 | </Button> |
| 161 | </div> |
| 162 | </div> |
| 163 | </ListboxOptions> |
| 164 | </Transition> |
| 165 | </div> |
| 166 | </Listbox> |
| 167 | ); |
| 168 | } |