-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import saveSettings from "@/utils/saveSettings"; | ||
|
||
export async function POST(req: Request) { | ||
const { key, value } = (await req.json()) as { | ||
key: string; | ||
value: string; | ||
}; | ||
|
||
await saveSettings(key, value); | ||
|
||
return Response.json({}); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useAppContext } from "@/context/AppContext"; | ||
import { Popover } from "./Popover"; | ||
import { useRef, useState } from "react"; | ||
import classNames from "@/utils/classNames"; | ||
import PROVIDERS from "@/constants/PROVIDERS"; | ||
import { saveSetting } from "@/app/action"; | ||
import RightArrowIcon from "./icons/RightArrowIcon"; | ||
import ExpandArrowIcon from "./icons/ExpandArrowIcon"; | ||
|
||
export default function Header() { | ||
const { selectedModel, setSelectedModel } = useAppContext(); | ||
|
||
const targetRef = useRef<HTMLButtonElement>(null); | ||
const [showPopover, setShowPopover] = useState(false); | ||
|
||
return ( | ||
<header className="flex justify-center p-4"> | ||
<button | ||
className="flex gap-2 items-center" | ||
onClick={() => setShowPopover(!showPopover)} | ||
ref={targetRef} | ||
> | ||
<span>{selectedModel}</span> | ||
<span | ||
className={classNames("transition-transform", { | ||
"rotate-90": showPopover, | ||
})} | ||
> | ||
<ExpandArrowIcon color="gray" size={16} /> | ||
</span> | ||
</button> | ||
<Popover | ||
isOpen={showPopover} | ||
onClose={() => setShowPopover(false)} | ||
target={targetRef.current} | ||
> | ||
<ul> | ||
{PROVIDERS.map((provider) => ( | ||
<li key={provider.name}> | ||
<div className="font-bold">{provider.name}</div> | ||
<ul> | ||
{provider.models.map((model) => ( | ||
<li key={model}> | ||
<button | ||
onClick={async () => { | ||
void fetch("/api/saveSetting", { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
key: "model", | ||
value: model, | ||
}), | ||
}); | ||
setSelectedModel(model); | ||
setShowPopover(false); | ||
}} | ||
> | ||
{model} | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
</li> | ||
))} | ||
</ul> | ||
</Popover> | ||
</header> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { useRef, useEffect, Ref, useState } from "react"; | ||
import { createPortal } from "react-dom"; | ||
|
||
interface PopoverProps { | ||
children: React.ReactNode; | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
position?: "top" | "bottom" | "left" | "right"; | ||
target?: HTMLElement | null; | ||
} | ||
|
||
export const Popover: React.FC<PopoverProps> = ({ | ||
children, | ||
onClose = () => {}, | ||
isOpen = false, | ||
position = "bottom", | ||
target, | ||
}) => { | ||
const contentRef = useRef<HTMLDivElement>(null); | ||
|
||
const [left, setLeft] = useState(0); | ||
const [top, setTop] = useState(0); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
contentRef.current && | ||
!contentRef.current.contains(event.target as Node) && | ||
target && | ||
!target.contains(event.target as Node) | ||
) { | ||
onClose(); | ||
} | ||
}; | ||
|
||
const handleResize = () => { | ||
const left = | ||
(target?.getBoundingClientRect().left || 0) - | ||
((contentRef.current?.getBoundingClientRect().width || 0) - | ||
(target?.getBoundingClientRect().width || 0)) / | ||
2; | ||
|
||
const top = | ||
(target?.getBoundingClientRect().top || 0) + | ||
(target?.getBoundingClientRect().height || 0) + | ||
5; | ||
|
||
setLeft(left); | ||
setTop(top); | ||
}; | ||
|
||
handleResize(); | ||
|
||
document.addEventListener("mousedown", handleClickOutside); | ||
window.addEventListener("resize", handleResize); | ||
|
||
return () => { | ||
document.removeEventListener("mousedown", handleClickOutside); | ||
window.removeEventListener("resize", handleResize); | ||
}; | ||
}, [onClose, target]); | ||
|
||
if (!isOpen) { | ||
return null; | ||
} | ||
|
||
return createPortal( | ||
<div | ||
className="bg-gray-900 fixed p-4 rounded-2xl" | ||
ref={contentRef} | ||
style={{ | ||
left, | ||
top, | ||
}} | ||
> | ||
{children} | ||
</div>, | ||
document.body | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default function ExpandArrowIcon({ color = "white", size = 32 }) { | ||
return ( | ||
<svg | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
height={size} | ||
width={size} | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
clipRule="evenodd" | ||
d="m7.2072 20.7072c-.39052-.3905-.39052-1.0237 0-1.4142l7.2929-7.2929-7.2929-7.29292c-.39052-.39052-.39052-1.02369 0-1.41421l.70711-.70711c.39052-.39052 1.02368-.39052 1.41421 0l8.35358 8.35354c.5858.5858.5858 1.5356 0 2.1213l-8.35358 8.3536c-.39052.3905-1.02369.3905-1.41421 0z" | ||
fill={color} | ||
fillRule="evenodd" | ||
/> | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const PROVIDERS = [ | ||
{ | ||
name: "OpenAI", | ||
models: ["gpt-3.5-turbo", "gpt-4o", "gpt-4-turbo", "gpt-4"], | ||
}, | ||
]; | ||
|
||
export default PROVIDERS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React, { createContext, useContext, useState, ReactNode } from "react"; | ||
|
||
interface AppContextType { | ||
selectedModel: string; | ||
setSelectedModel: (model: string) => void; | ||
} | ||
|
||
const AppContext = createContext<AppContextType | undefined>(undefined); | ||
|
||
export const AppContextProvider: React.FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [selectedModel, setSelectedModel] = useState("gpt-4"); | ||
|
||
return ( | ||
<AppContext.Provider value={{ selectedModel, setSelectedModel }}> | ||
{children} | ||
</AppContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAppContext = (): AppContextType => { | ||
const context = useContext(AppContext); | ||
if (context === undefined) { | ||
throw new Error("useAppContext must be used within a AppContextProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters