Skip to content

Commit

Permalink
Add API key retrieval from cookies in SearchBar
Browse files Browse the repository at this point in the history
The code now tries to retrieve the OpenAI API key from the user's cookies during a search. If the API key is not present, a modal is launched to request it. This provides a seamless experience when the API key is not available, preventing unnecessary search attempts.
  • Loading branch information
Supernova3339 committed Apr 27, 2024
1 parent 1733229 commit 702faa5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import React, {useState} from 'react';
import Message from './Message';
import {Tooltip, TooltipContent, TooltipProvider, TooltipTrigger} from "@/components/ui/tooltip";
import {useModal} from "@/hooks/use-modal-store";
import {parse} from "cookie";

const SearchBar: React.FC = () => {
const [searchDisabled, setSearchDisabled] = React.useState(false);
const [searchQuery, setSearchQuery] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [response, setResponse] = useState<string | null>(null);
const [showSearch, setShowSearch] = useState<boolean>(true);
const { onOpen } = useModal();

const handleSearch = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setIsLoading(true);

try {
// Retrieve API key from cookies
const cookies = parse(document.cookie);
const apiKey = cookies.openaiApiKey || null;

if (!apiKey) {
// Open the API modal if the API Key is not present.
setIsLoading(false);
onOpen("api");
return;
}
setSearchDisabled(true)
const res = await fetch('/api/search', {
method: 'POST',
Expand Down

0 comments on commit 702faa5

Please sign in to comment.