Skip to content

Commit 6573304

Browse files
fix(frontend): Error when API key is not entered is not clear (#4429)
1 parent 29ddcda commit 6573304

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

frontend/src/components/form/settings-form.tsx

+56-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import {
44
Input,
55
Switch,
66
} from "@nextui-org/react";
7-
import React from "react";
8-
import clsx from "clsx";
97
import { useFetcher, useLocation, useNavigate } from "@remix-run/react";
10-
import { organizeModelsAndProviders } from "#/utils/organizeModelsAndProviders";
11-
import { ModelSelector } from "#/components/modals/settings/ModelSelector";
12-
import { Settings } from "#/services/settings";
8+
import clsx from "clsx";
9+
import React from "react";
1310
import { ModalBackdrop } from "#/components/modals/modal-backdrop";
14-
import ModalButton from "../buttons/ModalButton";
11+
import { ModelSelector } from "#/components/modals/settings/ModelSelector";
1512
import { clientAction } from "#/routes/settings";
13+
import { Settings } from "#/services/settings";
1614
import { extractModelAndProvider } from "#/utils/extractModelAndProvider";
15+
import { organizeModelsAndProviders } from "#/utils/organizeModelsAndProviders";
16+
import ModalButton from "../buttons/ModalButton";
1717
import { DangerModal } from "../modals/confirmation-modals/danger-modal";
1818

1919
interface SettingsFormProps {
@@ -44,9 +44,8 @@ export function SettingsForm({
4444
navigate("/");
4545
onClose();
4646
}
47-
}, [fetcher.data]);
47+
}, [fetcher.data, navigate, onClose]);
4848

49-
// Figure out if the advanced options should be enabled by default
5049
const advancedAlreadyInUse = React.useMemo(() => {
5150
if (models.length > 0) {
5251
const organizedModels = organizeModelsAndProviders(models);
@@ -79,6 +78,7 @@ export function SettingsForm({
7978
React.useState(false);
8079
const [confirmEndSessionModalOpen, setConfirmEndSessionModalOpen] =
8180
React.useState(false);
81+
const [showWarningModal, setShowWarningModal] = React.useState(false);
8282

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

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

102-
if (location.pathname === "/app") {
104+
if (!apiKey) {
105+
setShowWarningModal(true);
106+
} else if (location.pathname === "/app") {
103107
setConfirmEndSessionModalOpen(true);
104108
} else {
105-
const formData = new FormData(event.currentTarget);
106109
submitForm(formData);
107110
}
108111
};
109112

113+
const handleCloseClick = () => {
114+
const formData = new FormData(formRef.current ?? undefined);
115+
const apiKey = formData.get("api-key");
116+
117+
if (!apiKey) {
118+
setShowWarningModal(true);
119+
} else {
120+
onClose();
121+
}
122+
};
123+
124+
const handleWarningConfirm = () => {
125+
setShowWarningModal(false);
126+
const formData = new FormData(formRef.current ?? undefined);
127+
formData.set("api-key", ""); // Set null value for API key
128+
submitForm(formData);
129+
onClose();
130+
};
131+
132+
const handleWarningCancel = () => {
133+
setShowWarningModal(false);
134+
};
135+
110136
return (
111137
<div>
112138
<fetcher.Form
@@ -125,7 +151,7 @@ export function SettingsForm({
125151
onValueChange={setShowAdvancedOptions}
126152
classNames={{
127153
thumb: clsx(
128-
"bg-[#5D5D5D] w-3 h-3",
154+
"bg-[#5D5D5D] w-3 h-3 z-0",
129155
"group-data-[selected=true]:bg-white",
130156
),
131157
wrapper: clsx(
@@ -325,7 +351,7 @@ export function SettingsForm({
325351
<ModalButton
326352
text="Close"
327353
className="bg-[#737373] w-full"
328-
onClick={onClose}
354+
onClick={handleCloseClick}
329355
/>
330356
</div>
331357
<ModalButton
@@ -373,6 +399,24 @@ export function SettingsForm({
373399
/>
374400
</ModalBackdrop>
375401
)}
402+
{showWarningModal && (
403+
<ModalBackdrop>
404+
<DangerModal
405+
title="Are you sure?"
406+
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?"
407+
buttons={{
408+
danger: {
409+
text: "Yes, close settings",
410+
onClick: handleWarningConfirm,
411+
},
412+
cancel: {
413+
text: "Cancel",
414+
onClick: handleWarningCancel,
415+
},
416+
}}
417+
/>
418+
</ModalBackdrop>
419+
)}
376420
</div>
377421
);
378422
}

0 commit comments

Comments
 (0)