Skip to content

Commit

Permalink
show account balance, empty state for ai chat interface
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Oct 5, 2024
1 parent 9185738 commit 0340d81
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/components/AiChatInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ export default function AIChatInterface({
return (
<div className="flex flex-col h-[calc(100vh-60px)] max-w-2xl mx-auto p-4 bg-gray-100">
<div className="flex-1 overflow-y-auto space-y-4 mb-4">
{messages.length === 0 && (
<div className="text-gray-500 text-xl text-center mt-[30vh] flex flex-col items-center">
<div>Submit a prompt to get started!</div>
<div className="text-6xl mt-4"></div>
</div>
)}
{messages.map((message, index) => (
<AiChatMessage key={index} message={message} />
))}
Expand Down
9 changes: 8 additions & 1 deletion src/components/HeaderLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { Link, useLocation, useRouter } from "wouter"
import { User } from "lucide-react"
import { useSnippetsBaseApiUrl } from "@/hooks/use-snippets-base-api-url"
import { useGlobalStore } from "@/hooks/use-global-store"
import { useAccountBalance } from "@/hooks/use-account-balance"

export const HeaderLogin: React.FC = () => {
const [, setLocation] = useLocation()
const session = useGlobalStore((s) => s.session)
const isLoggedIn = Boolean(session)
const setSession = useGlobalStore((s) => s.setSession)
const snippetsBaseApiUrl = useSnippetsBaseApiUrl()
const { data: accountBalance } = useAccountBalance()

if (!isLoggedIn) {
return (
Expand Down Expand Up @@ -52,7 +54,7 @@ export const HeaderLogin: React.FC = () => {
}

return (
<div className="flex justify-end">
<div className="flex justify-end items-center">
<DropdownMenu>
<DropdownMenuTrigger>
<Avatar className="w-8 h-8">
Expand All @@ -65,6 +67,11 @@ export const HeaderLogin: React.FC = () => {
</Avatar>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem className="text-gray-500 text-xs" disabled>
AI Usage $
{accountBalance?.monthly_ai_budget_used_usd.toFixed(2) ?? "0.00"} /
$5.00
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setLocation("/dashboard")}>
Dashboard
</DropdownMenuItem>
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/use-account-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useQuery } from "react-query"
import { useAxios } from "./use-axios"

interface AccountBalance {
monthly_ai_budget_used_usd: number
}

export const useAccountBalance = () => {
const axios = useAxios()

return useQuery<AccountBalance, Error>(
"accountBalance",
async () => {
const { data } = await axios.get("/accounts/get_account_balance")
return data.account_balance
},
{
refetchInterval: 60000, // Refetch every minute
}
)
}
12 changes: 10 additions & 2 deletions src/hooks/use-ai-api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { Anthropic } from "@anthropic-ai/sdk"
import { useMemo } from "react"
import { useGlobalStore } from "./use-global-store"

export const useAiApi = ({
streaming = false,
}: { streaming?: boolean } = {}) => {
const sessionToken = useGlobalStore((state) => state.session?.token)
const anthropic = useMemo(
() =>
new Anthropic({
// apiKey: "{REPLACE_ON_SERVER}",
apiKey: import.meta.env.VITE_ANTHROPIC_API_KEY,
apiKey: "{REPLACE_ON_SERVER}",
// apiKey: import.meta.env.VITE_ANTHROPIC_API_KEY,
// baseURL: `${window.location.origin}/ai`,
baseURL: import.meta.env.VITE_SNIPPETS_API_URL
? `${import.meta.env.VITE_SNIPPETS_API_URL}/anthropic`
: "/api/anthropic",
defaultHeaders: {
Authorization: `Bearer ${sessionToken}`,
},
dangerouslyAllowBrowser: true,
}),
[],
Expand Down

0 comments on commit 0340d81

Please sign in to comment.