-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Display the total amount of tokens currently consumed at the menu.
- Loading branch information
1 parent
0f3e5ac
commit 9c4cdc0
Showing
11 changed files
with
159 additions
and
13 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
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
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
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
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,40 @@ | ||
import * as React from "react"; | ||
import { cn } from "@/lib"; | ||
import { AiOutlinePieChart, AiOutlineLoading } from "react-icons/ai"; | ||
import { useTokens } from "@/hooks"; | ||
|
||
interface TokenProps { | ||
type: "pc" | "mobile"; | ||
} | ||
|
||
const Tokens: React.FC<TokenProps> = ({ type }) => { | ||
const [tokens, setTokens] = useTokens(); | ||
|
||
const { costTokens, loading } = tokens; | ||
|
||
React.useEffect(() => { | ||
setTokens(); | ||
}, []); | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
"h-11 text-sm cursor-pointer flex items-center gap-2 px-2 transition-colors", | ||
"hover:bg-gray-200/60 dark:hover:bg-slate-700/70", | ||
{ "rounded-md text-black/90 dark:text-white/90": type === "mobile" } | ||
)} | ||
> | ||
<AiOutlinePieChart size={16} /> | ||
<div className="flex items-center gap-2"> | ||
{!!(!costTokens && loading) ? ( | ||
<AiOutlineLoading className="animate-spin" /> | ||
) : ( | ||
costTokens | ||
)} | ||
<span>Tokens</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Tokens; |
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,47 @@ | ||
import { create } from "zustand"; | ||
|
||
interface ITokensState { | ||
loading: boolean; | ||
costTokens: number; | ||
costUSD: number; | ||
} | ||
|
||
interface ITokensAction { | ||
update: () => void; | ||
} | ||
|
||
type useTokensReturn = [ITokensState, ITokensAction["update"]]; | ||
|
||
let timeout: NodeJS.Timeout | null = null; | ||
|
||
const useStore = create<ITokensState & ITokensAction>((set) => ({ | ||
loading: false, | ||
costTokens: 0, | ||
costUSD: 0, | ||
update: () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
timeout = setTimeout(async () => { | ||
try { | ||
set(() => ({ loading: true })); | ||
const res = await fetch("/api/user").then((res) => res.json()); | ||
const { costTokens, costUSD } = res.data; | ||
set(() => ({ costTokens, costUSD })); | ||
} catch (error) { | ||
set(() => ({ costTokens: 0, costUSD: 0 })); | ||
} finally { | ||
set(() => ({ loading: false })); | ||
} | ||
}, 3000); | ||
}, | ||
})); | ||
|
||
export const useTokens = (): useTokensReturn => { | ||
const { costTokens, costUSD, loading } = useStore(); | ||
|
||
const update = useStore((state) => state.update); | ||
|
||
return [{ costTokens, costUSD, loading }, update]; | ||
}; |
9c4cdc0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
l-gpt – ./
l-gpt.vercel.app
l-gpt-peek-a-booo-s-team.vercel.app
gpt.ltopx.com
l-gpt-git-main-peek-a-booo-s-team.vercel.app