Open source CapCut alternative.
TypeScript
refactor(site): one settle hook re-reads a query after a charge lands
David Le8ef74d935860
diff --git a/site/src/cut/components/CreditsPill.tsx b/site/src/cut/components/CreditsPill.tsxindex 0a0ec455..38b151bf 100644--- a/site/src/cut/components/CreditsPill.tsx+++ b/site/src/cut/components/CreditsPill.tsx@@ -14,7 +14,7 @@ import {TooltipTrigger,} from "@/components/ui/tooltip";import { useEngineUser } from "@/cut/lib/backend/hooks";-import { useHostedBalance } from "@/cut/lib/hosted";+import { useHostedBalance, useInvalidateOnSettle } from "@/cut/lib/hosted";import { useCutBase } from "@/cut/lib/nav";import { track } from "@/lib/analytics";import { formatUsd } from "@/lib/credits/format-usd";@@ -27,10 +27,6 @@ import { useAccountFlags } from "@/queries/featureFlags";const PILL ="flex cursor-pointer items-center gap-1.5 whitespace-nowrap rounded-full border border-transparent px-3 py-1.5 text-xs font-medium tabular-nums text-muted-foreground transition-colors hover:border-border hover:bg-card hover:shadow-xs";-/** A hosted call that answered with no balance was charged after its- * response left; the re-read waits for the charge to land. */-const SETTLE_DELAY_MS = 1500;-/** The bar's reading of a balance: cents while there is less than $10, where* each one changes what the next generation can be, and whole dollars past* that, where they are noise. A round amount drops the ".00" either way. */@@ -60,7 +56,6 @@ function BalancePill() {const queryClient = useQueryClient();const balance = useCreditBalance();const reported = useHostedBalance((s) => s.balance);- const settled = useHostedBalance((s) => s.settled);// A charged call reported the balance it left: that is the balance, with no// request to make.@@ -71,16 +66,7 @@ function BalancePill() {);}, [reported, queryClient]);// A call that answered without one: re-read once its charge has landed.- // Only the count moving matters; the mount value is whatever the page has- // done so far.- useEffect(() => {- if (settled === 0) return;- const id = setTimeout(- () => void queryClient.invalidateQueries({ queryKey: creditBalanceQueryKey }),- SETTLE_DELAY_MS- );- return () => clearTimeout(id);- }, [settled, queryClient]);+ useInvalidateOnSettle(creditBalanceQueryKey);// Background work — renders, cloud jobs — charges without a call from this// tab, and a top-up happens on another page; a slow poll keeps the number// honest between them, and a focus refetch catches the trip back from billing.diff --git a/site/src/cut/lib/hosted.ts b/site/src/cut/lib/hosted.tsindex f44a0bf3..a9b7f920 100644--- a/site/src/cut/lib/hosted.ts+++ b/site/src/cut/lib/hosted.ts@@ -1,6 +1,7 @@"use client";import { useEffect } from "react";+import { useQueryClient } from "@tanstack/react-query";import { create } from "zustand";import { offloadHostedMedia } from "./hostedBlobs";@@ -50,6 +51,22 @@ export const useHostedBalance = create<{ balance: string | null; settled: numbersettled: 0,}));+/** A hosted call that answered with no balance was charged after its+ * response left; the re-read waits for the charge to land. */+const SETTLE_DELAY_MS = 1500;++/** Re-reads a query once a charge from this tab has landed. Only the count+ * moving matters; the mount value is whatever the page has done so far. */+export function useInvalidateOnSettle(queryKey: readonly unknown[]): void {+ const queryClient = useQueryClient();+ const settled = useHostedBalance((s) => s.settled);+ useEffect(() => {+ if (settled === 0) return;+ const id = setTimeout(() => void queryClient.invalidateQueries({ queryKey }), SETTLE_DELAY_MS);+ return () => clearTimeout(id);+ }, [settled, queryClient, queryKey]);+}+export function reportBalance(balance: string | undefined) {if (balance === undefined) {useHostedBalance.setState((s) => ({ settled: s.settled + 1 }));