|
| 1 | +import { Circle } from "@phosphor-icons/react"; |
| 2 | +import { |
| 3 | + formatResetTime, |
| 4 | + isUsageExceeded, |
| 5 | +} from "@posthog/core/billing/usageDisplay"; |
| 6 | +import { |
| 7 | + Button, |
| 8 | + Popover, |
| 9 | + PopoverContent, |
| 10 | + PopoverTrigger, |
| 11 | + Progress, |
| 12 | +} from "@posthog/quill"; |
| 13 | +import { BILLING_FLAG } from "@posthog/shared"; |
| 14 | +import { |
| 15 | + ANALYTICS_EVENTS, |
| 16 | + type UpgradePromptClickedSurface, |
| 17 | +} from "@posthog/shared/analytics-events"; |
| 18 | +import { Link } from "@tanstack/react-router"; |
| 19 | +import { useState } from "react"; |
| 20 | +import { track } from "../../shell/analytics"; |
| 21 | +import { useFeatureFlag } from "../feature-flags/useFeatureFlag"; |
| 22 | +import { |
| 23 | + openSettings, |
| 24 | + prepareSettingsPage, |
| 25 | +} from "../settings/hooks/useOpenSettings"; |
| 26 | +import { useFreeUsage } from "./useFreeUsage"; |
| 27 | + |
| 28 | +// Title-bar usage entry point (replaces the old sidebar usage bar): a compact |
| 29 | +// "Usage: N%" button whose hover card carries the full plan card — plan name, |
| 30 | +// progress bar, reset time, and the Upgrade action. Built on quill's Popover |
| 31 | +// with `openOnHover` on the trigger, so it behaves as a hover card (Base UI |
| 32 | +// keeps it open while the pointer travels into the card to click Upgrade). |
| 33 | +// The card body styles with quill tokens (foreground/muted-foreground/primary, |
| 34 | +// quill Progress) — radix scale classes don't resolve inside the data-quill |
| 35 | +// popover portal. |
| 36 | +export function UsageButton() { |
| 37 | + const billingEnabled = useFeatureFlag(BILLING_FLAG); |
| 38 | + const { usage, isLoading } = useFreeUsage(billingEnabled); |
| 39 | + // Controlled so the trigger click can close the card before navigating to |
| 40 | + // settings — uncontrolled, the same click would also toggle the popover open |
| 41 | + // over the settings view. Hover open/close still flows through onOpenChange. |
| 42 | + const [open, setOpen] = useState(false); |
| 43 | + |
| 44 | + if (!billingEnabled) return null; |
| 45 | + |
| 46 | + // Same-size placeholder while usage loads, so the button doesn't pop in and |
| 47 | + // shift the PostHog Web button after boot. |
| 48 | + if (!usage) { |
| 49 | + if (!isLoading) return null; |
| 50 | + return ( |
| 51 | + <Button variant="outline" size="sm" disabled aria-hidden> |
| 52 | + <span className="animate-pulse">Usage: --%</span> |
| 53 | + </Button> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const exceeded = isUsageExceeded(usage); |
| 58 | + const dominant = |
| 59 | + usage.sustained.used_percent >= usage.burst.used_percent |
| 60 | + ? usage.sustained |
| 61 | + : usage.burst; |
| 62 | + const usagePercent = Math.min(Math.round(dominant.used_percent), 100); |
| 63 | + const resetLabel = formatResetTime(dominant.reset_at); |
| 64 | + |
| 65 | + const handleOpenChange = (nextOpen: boolean) => { |
| 66 | + // The hover card has real impressions now (the old sidebar bar was |
| 67 | + // always-visible); count each open as a shown upgrade prompt. |
| 68 | + if (nextOpen && !open) { |
| 69 | + track(ANALYTICS_EVENTS.UPGRADE_PROMPT_SHOWN, { |
| 70 | + surface: "titlebar_card", |
| 71 | + }); |
| 72 | + } |
| 73 | + setOpen(nextOpen); |
| 74 | + }; |
| 75 | + |
| 76 | + const handleOpenPlan = (surface: UpgradePromptClickedSurface) => { |
| 77 | + track(ANALYTICS_EVENTS.UPGRADE_PROMPT_CLICKED, { surface }); |
| 78 | + setOpen(false); |
| 79 | + openSettings("plan-usage"); |
| 80 | + }; |
| 81 | + |
| 82 | + // The trigger is a real <Link> (render={<Link/>} per convention), so the |
| 83 | + // router owns the navigation; this click handler carries the side effects |
| 84 | + // openSettings would have done — tracking, closing the card, and resetting |
| 85 | + // the settings-page store so no stale context/one-shot action leaks in. |
| 86 | + const handleTriggerClick = () => { |
| 87 | + track(ANALYTICS_EVENTS.UPGRADE_PROMPT_CLICKED, { surface: "titlebar" }); |
| 88 | + setOpen(false); |
| 89 | + prepareSettingsPage(); |
| 90 | + }; |
| 91 | + |
| 92 | + return ( |
| 93 | + <Popover open={open} onOpenChange={handleOpenChange}> |
| 94 | + <PopoverTrigger |
| 95 | + openOnHover |
| 96 | + delay={300} |
| 97 | + closeDelay={150} |
| 98 | + render={ |
| 99 | + <Button |
| 100 | + variant="outline" |
| 101 | + size="sm" |
| 102 | + render={ |
| 103 | + <Link |
| 104 | + to="/settings/$category" |
| 105 | + params={{ category: "plan-usage" }} |
| 106 | + onClick={handleTriggerClick} |
| 107 | + /> |
| 108 | + } |
| 109 | + > |
| 110 | + {exceeded ? "Usage: limit reached" : `Usage: ${usagePercent}%`} |
| 111 | + </Button> |
| 112 | + } |
| 113 | + /> |
| 114 | + {/* no-drag: the popover opens under the title bar's drag region; without |
| 115 | + the opt-out a click near its top edge is swallowed as a window drag. */} |
| 116 | + <PopoverContent |
| 117 | + side="bottom" |
| 118 | + align="end" |
| 119 | + sideOffset={6} |
| 120 | + className="no-drag gap-2" |
| 121 | + > |
| 122 | + <div className="flex items-center justify-between"> |
| 123 | + <span className="font-medium text-foreground text-xs"> |
| 124 | + Free plan |
| 125 | + <Circle |
| 126 | + size={4} |
| 127 | + weight="fill" |
| 128 | + className="mx-1.5 inline text-muted-foreground" |
| 129 | + /> |
| 130 | + <span className="font-normal text-muted-foreground"> |
| 131 | + {exceeded ? "Limit reached" : `${usagePercent}% used`} |
| 132 | + </span> |
| 133 | + </span> |
| 134 | + <Button |
| 135 | + variant="link" |
| 136 | + size="sm" |
| 137 | + className="h-auto p-0" |
| 138 | + onClick={() => handleOpenPlan("titlebar_card")} |
| 139 | + > |
| 140 | + Upgrade |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + <Progress |
| 144 | + value={usagePercent} |
| 145 | + variant={exceeded ? "destructive" : "default"} |
| 146 | + /> |
| 147 | + <div className="font-normal text-[11px] text-muted-foreground"> |
| 148 | + {resetLabel} |
| 149 | + </div> |
| 150 | + </PopoverContent> |
| 151 | + </Popover> |
| 152 | + ); |
| 153 | +} |
0 commit comments