Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): Error when API key is not entered is not clear #4429

Merged
merged 14 commits into from
Oct 22, 2024
Merged
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions frontend/src/components/form/settings-form.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import clsx from "clsx";
import React from "react";
import {
Autocomplete,
AutocompleteItem,
Input,
Switch,
} from "@nextui-org/react";
import React from "react";
import clsx from "clsx";
import { useFetcher, useLocation, useNavigate } from "@remix-run/react";
import { organizeModelsAndProviders } from "#/utils/organizeModelsAndProviders";
import { ModelSelector } from "#/components/modals/settings/ModelSelector";
import { Settings } from "#/services/settings";
import { ModalBackdrop } from "#/components/modals/modal-backdrop";
import ModalButton from "../buttons/ModalButton";
import { ModelSelector } from "#/components/modals/settings/ModelSelector";
import { clientAction } from "#/routes/settings";
import { Settings } from "#/services/settings";
import { extractModelAndProvider } from "#/utils/extractModelAndProvider";
import { organizeModelsAndProviders } from "#/utils/organizeModelsAndProviders";
import ModalButton from "../buttons/ModalButton";
import { DangerModal } from "../modals/confirmation-modals/danger-modal";

interface SettingsFormProps {
Expand Down Expand Up @@ -44,9 +44,8 @@ export function SettingsForm({
navigate("/");
onClose();
}
}, [fetcher.data]);
}, [fetcher.data, navigate, onClose]);

// Figure out if the advanced options should be enabled by default
const advancedAlreadyInUse = React.useMemo(() => {
if (models.length > 0) {
const organizedModels = organizeModelsAndProviders(models);
Expand Down Expand Up @@ -79,6 +78,7 @@ export function SettingsForm({
React.useState(false);
const [confirmEndSessionModalOpen, setConfirmEndSessionModalOpen] =
React.useState(false);
const [showWarningModal, setShowWarningModal] = React.useState(false);

const submitForm = (formData: FormData) => {
if (location.pathname === "/app") formData.set("end-session", "true");
Expand All @@ -98,15 +98,38 @@ export function SettingsForm({

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const apiKey = formData.get("api-key");

if (location.pathname === "/app") {
if (!apiKey) {
setShowWarningModal(true);
} else if (location.pathname === "/app") {
setConfirmEndSessionModalOpen(true);
} else {
const formData = new FormData(event.currentTarget);
submitForm(formData);
}
};

const handleCloseClick = () => {
const formData = new FormData(formRef.current ?? undefined);
const apiKey = formData.get("api-key");

if (!apiKey) {
setShowWarningModal(true);
} else {
onClose();
}
};

const handleWarningConfirm = () => {
setShowWarningModal(false);
onClose();
};

const handleWarningCancel = () => {
setShowWarningModal(false);
};

return (
<div>
<fetcher.Form
Expand All @@ -125,7 +148,7 @@ export function SettingsForm({
onValueChange={setShowAdvancedOptions}
classNames={{
thumb: clsx(
"bg-[#5D5D5D] w-3 h-3",
"bg-[#5D5D5D] w-3 h-3 z-0",
"group-data-[selected=true]:bg-white",
),
wrapper: clsx(
Expand Down Expand Up @@ -325,7 +348,7 @@ export function SettingsForm({
<ModalButton
text="Close"
className="bg-[#737373] w-full"
onClick={onClose}
onClick={handleCloseClick}
/>
</div>
<ModalButton
Expand Down Expand Up @@ -373,6 +396,24 @@ export function SettingsForm({
/>
</ModalBackdrop>
)}
{showWarningModal && (
<ModalBackdrop>
<DangerModal
title="Are you sure?"
description="You haven't set an API key. Without an API key, you won't be able to use the AI features. Are you sure you want to close the settings?"
buttons={{
danger: {
text: "Yes, close settings",
onClick: handleWarningConfirm,
},
cancel: {
text: "Cancel",
onClick: handleWarningCancel,
},
}}
/>
</ModalBackdrop>
)}
</div>
);
}