import { useQuery } from "@tanstack/react-query"; import { useLocation } from "@tanstack/react-router"; import { getAccountNotices } from "~/fns/notices"; import type { AccountNotice, QuotaNotice } from "~/fns/notices"; import { useViewer } from "~/hooks/use-viewer"; import { NagBanner } from "~/ui/NagBanner"; import { safeDecode } from "~/lib/safe-decode"; const NOTICE_STALE_MS = 5 * 60 * 1000; function QuotaBanner({ notice }: { notice: QuotaNotice }) { const whose = notice.accountKind === "org" ? notice.accountName : "You"; const title = notice.spend ? `${whose} reached ${notice.accountKind === "org" ? "its" : "your"} monthly spending limit` : `${whose} used all of ${notice.accountKind === "org" ? "its" : "your"} ${notice.plan} ${notice.meterLabel.toLowerCase()} this month`; const body = notice.spend ? "Usage that costs money is paused until next month or until the limit is raised." : notice.canUpgrade ? `Upgrade to Pro for more, or buy prepaid credit${notice.rate ? ` (${notice.rate})` : ""}.` : notice.rate ? `More is ${notice.rate}; add credit or raise the spending limit.` : "It resets at the start of next month."; return ( {body} ); } function Notice({ notice }: { notice: AccountNotice }) { if (notice.kind === "quota") return ; return ( An authenticator app or a passkey keeps your repositories safe even if your password leaks. ); } /** Account-level nags under the site header: maxed-out plans and missing two-factor. */ export function AccountNotices() { const viewer = useViewer(); const location = useLocation(); const notices = useQuery({ queryKey: ["account-notices", viewer?.id ?? null], queryFn: () => getAccountNotices(), enabled: Boolean(viewer), staleTime: NOTICE_STALE_MS, }); if (!viewer || !notices.data?.length) return null; const onSecurity = location.pathname.startsWith("/settings/security"); const path = safeDecode(location.pathname) ?? location.pathname; return (
{notices.data .filter( (notice) => !(notice.kind === "mfa" && onSecurity) && !(notice.kind === "quota" && (safeDecode(notice.billingPath) ?? notice.billingPath) === path), ) .map((notice) => ( ))}
); }