Skip to content

Commit

Permalink
Adds successfully used API key to local storage (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPGill authored May 28, 2024
1 parent e4f26c8 commit d54a2cf
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions apps/www/src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { useChat } from "ai/react";
import * as React from "react";
import * as R from "remeda";

const IS_SERVER = typeof window === "undefined";
const CHAT_OPENAI_API_KEY = "CHAT_OPENAI_API_KEY";

const ChatMessage: React.FC<{
message: Message;
isStreamFinished: boolean;
Expand Down Expand Up @@ -38,7 +41,10 @@ const ChatMessage: React.FC<{
};

export const Chat = () => {
const [currentApiKey, setCurrentApiKey] = React.useState<string>("");
const storage = !IS_SERVER ? window.localStorage : null;
const [currentApiKey, setCurrentApiKey] = React.useState<string>(
storage?.getItem(CHAT_OPENAI_API_KEY) ?? "",
);
const { messages, input, handleInputChange, handleSubmit, isLoading } =
useChat({
api: "/api/chat",
Expand Down Expand Up @@ -73,7 +79,10 @@ export const Chat = () => {
</div>
<form
autoComplete="off"
onSubmit={handleSubmit}
onSubmit={(e) => {
storage?.setItem(CHAT_OPENAI_API_KEY, currentApiKey);
handleSubmit(e);
}}
className="p-2 flex flex-col"
>
{reversedMessagesWithoutSystem.length != 0 && (
Expand Down

0 comments on commit d54a2cf

Please sign in to comment.