| 1 | import { OnboardingModes } from "core/protocol/core"; |
| 2 | import { FormProvider, useForm } from "react-hook-form"; |
| 3 | import { AddModelForm } from "../../../forms/AddModelForm"; |
| 4 | import { providers } from "../../../pages/AddNewModel/configs/providers"; |
| 5 | import { useAppDispatch } from "../../../redux/hooks"; |
| 6 | import { setDialogMessage, setShowDialog } from "../../../redux/slices/uiSlice"; |
| 7 | import { Button, Input } from "../../index"; |
| 8 | import { useSubmitOnboarding } from "../hooks/useSubmitOnboarding"; |
| 9 | |
| 10 | interface OnboardingProvidersTabProps { |
| 11 | /** Whether this is being shown in a dialog context */ |
| 12 | isDialog?: boolean; |
| 13 | } |
| 14 | |
| 15 | export function OnboardingProvidersTab({ |
| 16 | isDialog, |
| 17 | }: OnboardingProvidersTabProps) { |
| 18 | const formMethods = useForm(); |
| 19 | const dispatch = useAppDispatch(); |
| 20 | const { submitOnboarding } = useSubmitOnboarding( |
| 21 | OnboardingModes.API_KEY, |
| 22 | isDialog, |
| 23 | ); |
| 24 | |
| 25 | const providerConfigs = [ |
| 26 | providers["openai"], |
| 27 | providers["anthropic"], |
| 28 | providers["gemini"], |
| 29 | ]; |
| 30 | |
| 31 | const handleFormSubmit = () => { |
| 32 | // Find the first provider with an API key entered |
| 33 | for (const config of providerConfigs) { |
| 34 | const apiKey = formMethods.watch(`${config?.provider}_apiKey`); |
| 35 | if (apiKey?.trim()) { |
| 36 | submitOnboarding(config?.provider, apiKey); |
| 37 | return; |
| 38 | } |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | const handleClickMoreProviders = () => { |
| 43 | dispatch(setShowDialog(true)); |
| 44 | dispatch( |
| 45 | setDialogMessage( |
| 46 | <AddModelForm |
| 47 | onDone={() => { |
| 48 | dispatch(setShowDialog(false)); |
| 49 | submitOnboarding(); |
| 50 | }} |
| 51 | />, |
| 52 | ), |
| 53 | ); |
| 54 | }; |
| 55 | |
| 56 | const hasAnyApiKey = providerConfigs.some((config) => { |
| 57 | const apiKey = formMethods.watch(`${config?.provider}_apiKey`); |
| 58 | return apiKey?.trim(); |
| 59 | }); |
| 60 | |
| 61 | return ( |
| 62 | <div className="flex h-full w-full items-center justify-center"> |
| 63 | <div className="w-full max-w-md"> |
| 64 | <FormProvider {...formMethods}> |
| 65 | <div className="mt-5 space-y-6"> |
| 66 | <div className="space-y-4"> |
| 67 | {providerConfigs.map((config) => ( |
| 68 | <div key={config?.provider}> |
| 69 | <label className="mb-1 flex items-center gap-3 text-sm font-medium"> |
| 70 | {window.vscMediaUrl && ( |
| 71 | <img |
| 72 | src={`${window.vscMediaUrl}/logos/${config?.icon}`} |
| 73 | alt={config?.provider} |
| 74 | className="h-4 w-4 object-contain" |
| 75 | /> |
| 76 | )} |
| 77 | {config?.title} |
| 78 | </label> |
| 79 | <Input |
| 80 | id={`${config?.provider}_apiKey`} |
| 81 | type="password" |
| 82 | placeholder={`Enter your ${config?.title} API key`} |
| 83 | className="w-full" |
| 84 | {...formMethods.register(`${config?.provider}_apiKey`)} |
| 85 | /> |
| 86 | <span className="text-description-muted mt-1 block text-xs"> |
| 87 | <a |
| 88 | href={config?.apiKeyUrl} |
| 89 | target="_blank" |
| 90 | rel="noopener noreferrer" |
| 91 | className="cursor-pointer text-inherit underline hover:text-inherit hover:brightness-125" |
| 92 | > |
| 93 | Click here |
| 94 | </a>{" "} |
| 95 | to create a {config?.title} API key |
| 96 | </span> |
| 97 | </div> |
| 98 | ))} |
| 99 | </div> |
| 100 | |
| 101 | <div> |
| 102 | <Button |
| 103 | type="button" |
| 104 | onClick={handleFormSubmit} |
| 105 | disabled={!hasAnyApiKey} |
| 106 | className="w-full" |
| 107 | > |
| 108 | Connect |
| 109 | </Button> |
| 110 | |
| 111 | <div className="w-full text-center"> |
| 112 | <span className="text-description"> |
| 113 | <span |
| 114 | className="cursor-pointer underline hover:brightness-125" |
| 115 | onClick={handleClickMoreProviders} |
| 116 | > |
| 117 | Click here |
| 118 | </span>{" "} |
| 119 | to view more providers |
| 120 | </span> |
| 121 | </div> |
| 122 | </div> |
| 123 | </div> |
| 124 | </FormProvider> |
| 125 | </div> |
| 126 | </div> |
| 127 | ); |
| 128 | } |