quivr/frontend/app/user/components/ApiKeyConfig/ApiKeyConfig.tsx
Nick Guernsey 253e866a86
feat: enhance user page UI (#1319)
# Description

Hope ya'll don't mind, I updated the User Profile's UI to be a bit more
readable. Kept the design neutral to not impose styles.

I also kept the original toggle buttons for Theme and Language in case
we still want to use those in other places in the app, where a select
menu isn't necessary.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):


https://github.com/StanGirard/quivr/assets/64866427/d0320852-ece1-4002-a180-0005df132e71


https://github.com/StanGirard/quivr/assets/64866427/be3c67ec-8d42-44f9-8131-35a70aec20ef

---------

Co-authored-by: Zineb El Bachiri <100568984+gozineb@users.noreply.github.com>
Co-authored-by: Stan Girard <girard.stanislas@gmail.com>
2023-10-05 17:50:02 +02:00

118 lines
3.4 KiB
TypeScript

/* eslint-disable max-lines */
"use client";
import { useTranslation } from "react-i18next";
import { FaCopy, FaInfoCircle } from "react-icons/fa";
import Button from "@/lib/components/ui/Button";
import Field from "@/lib/components/ui/Field";
import copyToClipboard from "@/lib/helpers/copyToClipboard";
import { useApiKeyConfig } from "./hooks/useApiKeyConfig";
export const ApiKeyConfig = (): JSX.Element => {
const {
apiKey,
handleCopyClick,
handleCreateClick,
openAiApiKey,
setOpenAiApiKey,
changeOpenAiApiKey,
changeOpenAiApiKeyRequestPending,
userIdentity,
removeOpenAiApiKey,
hasOpenAiApiKey,
} = useApiKeyConfig();
const { t } = useTranslation(["config"]);
return (
<>
<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>
</div>
)}
</div>
<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>
)}
<Button
data-testid="save-open-ai-api-key"
isLoading={changeOpenAiApiKeyRequestPending}
disabled={openAiApiKey === userIdentity?.openai_api_key}
>
Save Key
</Button>
</div>
</form>
<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>
</div>
</div>
</>
);
};