|
| 1 | +import { useQuery, useQueryClient } from '@tanstack/react-query' |
| 2 | +import React, { useEffect, useState } from 'react' |
| 3 | + |
| 4 | +import { usageQueryKeys, useUsageQuery } from '../hooks/use-usage-query' |
| 5 | +import { useChatStore } from '../state/chat-store' |
| 6 | +import { useTheme } from '../hooks/use-theme' |
| 7 | +import { BORDER_CHARS } from '../utils/ui-constants' |
| 8 | +import { ShimmerText } from './shimmer-text' |
| 9 | + |
| 10 | +const CREDIT_POLL_INTERVAL = 5 * 1000 // Poll every 5 seconds |
| 11 | + |
| 12 | +// Track credits restored state globally so keyboard handler can access it |
| 13 | +let creditsRestoredGlobal = false |
| 14 | + |
| 15 | +export const areCreditsRestored = () => creditsRestoredGlobal |
| 16 | + |
| 17 | +export const OutOfCreditsBanner = () => { |
| 18 | + const sessionCreditsUsed = useChatStore((state) => state.sessionCreditsUsed) |
| 19 | + const queryClient = useQueryClient() |
| 20 | + const [creditsRestored, setCreditsRestored] = useState(false) |
| 21 | + |
| 22 | + const { data: apiData } = useUsageQuery({ |
| 23 | + enabled: true, |
| 24 | + }) |
| 25 | + |
| 26 | + const { data: cachedUsageData } = useQuery<{ |
| 27 | + type: 'usage-response' |
| 28 | + usage: number |
| 29 | + remainingBalance: number | null |
| 30 | + balanceBreakdown?: { free: number; paid: number; ad?: number } |
| 31 | + next_quota_reset: string | null |
| 32 | + }>({ |
| 33 | + queryKey: usageQueryKeys.current(), |
| 34 | + enabled: false, |
| 35 | + }) |
| 36 | + |
| 37 | + const theme = useTheme() |
| 38 | + const activeData = apiData || cachedUsageData |
| 39 | + const remainingBalance = activeData?.remainingBalance ?? 0 |
| 40 | + |
| 41 | + // Poll for credit updates |
| 42 | + useEffect(() => { |
| 43 | + const interval = setInterval(() => { |
| 44 | + queryClient.invalidateQueries({ queryKey: usageQueryKeys.current() }) |
| 45 | + }, CREDIT_POLL_INTERVAL) |
| 46 | + return () => clearInterval(interval) |
| 47 | + }, [queryClient]) |
| 48 | + |
| 49 | + // Track if we've confirmed the zero-balance state to avoid false positives from stale cache |
| 50 | + const [confirmedZeroBalance, setConfirmedZeroBalance] = useState(false) |
| 51 | + |
| 52 | + // Reset global flag when component mounts (handles re-entry to out-of-credits mode) |
| 53 | + useEffect(() => { |
| 54 | + creditsRestoredGlobal = false |
| 55 | + }, []) |
| 56 | + |
| 57 | + // Confirm zero balance on first fetch to avoid race condition with cached data |
| 58 | + useEffect(() => { |
| 59 | + if (apiData && !confirmedZeroBalance) { |
| 60 | + if ((apiData.remainingBalance ?? 0) <= 0) { |
| 61 | + setConfirmedZeroBalance(true) |
| 62 | + } |
| 63 | + } |
| 64 | + }, [apiData, confirmedZeroBalance]) |
| 65 | + |
| 66 | + // Check if credits have been restored - show celebratory message |
| 67 | + useEffect(() => { |
| 68 | + // Only check for restoration after we've confirmed zero balance |
| 69 | + if (!confirmedZeroBalance || remainingBalance <= 0 || creditsRestored) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Credits restored! Show the success state |
| 74 | + setCreditsRestored(true) |
| 75 | + creditsRestoredGlobal = true |
| 76 | + }, [remainingBalance, creditsRestored, confirmedZeroBalance]) |
| 77 | + |
| 78 | + // Build stats text |
| 79 | + const statsText = activeData |
| 80 | + ? `Session: ${sessionCreditsUsed.toLocaleString()} credits used · Balance: ${remainingBalance.toLocaleString()} credits` |
| 81 | + : `Session: ${sessionCreditsUsed.toLocaleString()} credits used` |
| 82 | + |
| 83 | + // Show celebratory success state when credits are restored |
| 84 | + if (creditsRestored) { |
| 85 | + return ( |
| 86 | + <box |
| 87 | + style={{ |
| 88 | + width: '100%', |
| 89 | + borderStyle: 'single', |
| 90 | + borderColor: theme.success, |
| 91 | + customBorderChars: BORDER_CHARS, |
| 92 | + paddingLeft: 1, |
| 93 | + paddingRight: 1, |
| 94 | + paddingTop: 0, |
| 95 | + paddingBottom: 0, |
| 96 | + flexDirection: 'column', |
| 97 | + gap: 0, |
| 98 | + }} |
| 99 | + > |
| 100 | + <box |
| 101 | + style={{ |
| 102 | + flexDirection: 'column', |
| 103 | + justifyContent: 'center', |
| 104 | + minHeight: 3, |
| 105 | + gap: 0, |
| 106 | + }} |
| 107 | + > |
| 108 | + <text style={{ fg: theme.success }}> |
| 109 | + <ShimmerText |
| 110 | + text="✨ Credits acquired! ✨" |
| 111 | + primaryColor={theme.success} |
| 112 | + interval={120} |
| 113 | + /> |
| 114 | + </text> |
| 115 | + <text style={{ fg: theme.muted }}> |
| 116 | + Balance: {remainingBalance.toLocaleString()} credits |
| 117 | + </text> |
| 118 | + <text style={{ fg: theme.foreground }}> |
| 119 | + Press Enter to continue |
| 120 | + </text> |
| 121 | + </box> |
| 122 | + </box> |
| 123 | + ) |
| 124 | + } |
| 125 | + |
| 126 | + return ( |
| 127 | + <box |
| 128 | + style={{ |
| 129 | + width: '100%', |
| 130 | + borderStyle: 'single', |
| 131 | + borderColor: theme.warning, |
| 132 | + customBorderChars: BORDER_CHARS, |
| 133 | + paddingLeft: 1, |
| 134 | + paddingRight: 1, |
| 135 | + paddingTop: 0, |
| 136 | + paddingBottom: 0, |
| 137 | + flexDirection: 'column', |
| 138 | + gap: 0, |
| 139 | + }} |
| 140 | + > |
| 141 | + <box |
| 142 | + style={{ |
| 143 | + flexDirection: 'column', |
| 144 | + justifyContent: 'center', |
| 145 | + minHeight: 3, |
| 146 | + gap: 0, |
| 147 | + }} |
| 148 | + > |
| 149 | + <text style={{ fg: theme.warning }}> |
| 150 | + Out of credits |
| 151 | + </text> |
| 152 | + <text style={{ fg: theme.muted }}> |
| 153 | + {statsText} |
| 154 | + </text> |
| 155 | + <text style={{ fg: theme.foreground }}> |
| 156 | + Press Enter to buy more credits |
| 157 | + </text> |
| 158 | + </box> |
| 159 | + </box> |
| 160 | + ) |
| 161 | +} |
0 commit comments