2023-08-01 10:24:57 +03:00
|
|
|
/* eslint-disable max-lines */
|
2023-07-26 00:12:46 +03:00
|
|
|
"use client";
|
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { FaCopy, FaInfoCircle } from "react-icons/fa";
|
|
|
|
|
2023-07-26 00:12:46 +03:00
|
|
|
import Button from "@/lib/components/ui/Button";
|
2023-08-01 10:24:57 +03:00
|
|
|
import Field from "@/lib/components/ui/Field";
|
2023-10-05 18:50:02 +03:00
|
|
|
import copyToClipboard from "@/lib/helpers/copyToClipboard";
|
2023-07-26 00:12:46 +03:00
|
|
|
|
|
|
|
import { useApiKeyConfig } from "./hooks/useApiKeyConfig";
|
|
|
|
|
|
|
|
export const ApiKeyConfig = (): JSX.Element => {
|
2023-08-01 10:24:57 +03:00
|
|
|
const {
|
|
|
|
apiKey,
|
|
|
|
handleCopyClick,
|
|
|
|
handleCreateClick,
|
|
|
|
openAiApiKey,
|
|
|
|
setOpenAiApiKey,
|
|
|
|
changeOpenAiApiKey,
|
|
|
|
changeOpenAiApiKeyRequestPending,
|
|
|
|
userIdentity,
|
|
|
|
removeOpenAiApiKey,
|
|
|
|
hasOpenAiApiKey,
|
|
|
|
} = useApiKeyConfig();
|
2023-10-05 18:50:02 +03:00
|
|
|
const { t } = useTranslation(["config"]);
|
2023-07-26 00:12:46 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-10-05 18:50:02 +03:00
|
|
|
<h3 className="font-semibold mb-2">Quivr {t("apiKey")}</h3>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
{apiKey === "" ? (
|
|
|
|
<Button
|
|
|
|
data-testid="create-new-key"
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => void handleCreateClick()}
|
|
|
|
>
|
|
|
|
Create New Key
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Field name="quivrApiKey" disabled={true} value={apiKey} />
|
|
|
|
<button data-testid="copy-api-key-button" onClick={handleCopyClick}>
|
|
|
|
<FaCopy />
|
|
|
|
</button>
|
2023-07-26 00:12:46 +03:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-08-01 10:24:57 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
<hr className="my-8" />
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<h3 className="font-semibold mb-2">OpenAI {t("apiKey")}</h3>
|
|
|
|
<form
|
|
|
|
className="mb-4"
|
|
|
|
onSubmit={(event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
void changeOpenAiApiKey();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Field
|
|
|
|
name="openAiApiKey"
|
|
|
|
placeholder="Open AI Key"
|
|
|
|
className="w-full"
|
|
|
|
value={openAiApiKey ?? ""}
|
|
|
|
data-testid="open-ai-api-key-input"
|
|
|
|
onChange={(e) => setOpenAiApiKey(e.target.value)}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
hidden={!hasOpenAiApiKey}
|
|
|
|
data-testid="copy-openai-api-key-button"
|
|
|
|
onClick={() => void copyToClipboard(openAiApiKey)}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
<FaCopy />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="mt-4 flex flex-row justify-between">
|
|
|
|
{hasOpenAiApiKey && (
|
|
|
|
<Button
|
|
|
|
isLoading={changeOpenAiApiKeyRequestPending}
|
|
|
|
variant="secondary"
|
|
|
|
onClick={() => void removeOpenAiApiKey()}
|
|
|
|
>
|
|
|
|
Remove Key
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
|
2023-08-01 10:24:57 +03:00
|
|
|
<Button
|
2023-10-05 18:50:02 +03:00
|
|
|
data-testid="save-open-ai-api-key"
|
2023-08-01 10:24:57 +03:00
|
|
|
isLoading={changeOpenAiApiKeyRequestPending}
|
2023-10-05 18:50:02 +03:00
|
|
|
disabled={openAiApiKey === userIdentity?.openai_api_key}
|
2023-08-01 10:24:57 +03:00
|
|
|
>
|
2023-10-05 18:50:02 +03:00
|
|
|
Save Key
|
2023-08-01 10:24:57 +03:00
|
|
|
</Button>
|
2023-10-05 18:50:02 +03:00
|
|
|
</div>
|
|
|
|
</form>
|
2023-08-01 10:24:57 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
<div className="flex space-x-2 bg-sky-100 dark:bg-gray-900 border border-sky-200 dark:border-gray-700 px-4 py-3 rounded relative max-w-md">
|
|
|
|
<div className="text-xl font-semibold text-sky-600">
|
|
|
|
<FaInfoCircle />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
We will store your OpenAI API key, but we will not use it for any
|
|
|
|
other purpose. However,{" "}
|
|
|
|
<strong>we have not implemented any encryption logic yet</strong>
|
|
|
|
</p>
|
|
|
|
</div>
|
2023-08-01 10:24:57 +03:00
|
|
|
</div>
|
2023-10-05 18:50:02 +03:00
|
|
|
</div>
|
2023-07-26 00:12:46 +03:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|