額度用量
以百分比顯示剩餘額度,並估算還能生成幾張圖,提供三種形式:側邊欄圓環、手機面板長條與方案頁卡片。
適合使用的時機
- 在 app 側邊欄或帳號面板顯示剩餘的生成額度
- 方案頁的額度摘要,附上每日獎勵的提醒
注意事項
- 顯示張數與百分比,不要顯示原始點數,與原產品用語一致
- 傳入在任務完成時會改變的 refreshKey,用量才會更新
交給你的 agent 安裝
一次複製就包含安裝指令、使用時機、props 與設計規則。貼進 Claude Code、Cursor 或 Codex 即可。
用 CLI 安裝
使用 shadcn CLI。原始碼會複製到你的專案裡,每一行都由你掌握。
$ npx shadcn@latest add https://sunui.synvize.com/r/quota-meter.json或手動安裝
- 1. 安裝相依套件。
$ npm install clsx tailwind-merge - 2. 如果還沒有,在
lib/utils.ts加上cn輔助函式。沒有用 shadcn/ui?再加上 sunui-theme.css 提供顏色變數(做法)。然後複製元件程式碼。quota-meter.tsx"use client"; import { useEffect, useState, type CSSProperties, type MouseEventHandler, type ReactNode } from "react"; import { cn } from "@/lib/utils"; // Quota usage: a percentage plus the estimated number of images left. If today's check-in is not done yet, the second line becomes a check-in reminder. // Shows only image counts and percentages, never the word "points". /** Default global event name, sent when checkin-streak succeeds */ export const REWARDS_EVENT = "sunui:rewards"; export type QuotaUsage = ({ unlimited: true } | { unlimited: false; pct: number; left: number }) & { /** Number of images available to claim when today's check-in is not done */ checkin?: number; }; /** Free quota and plan quota are counted together: remaining images divided by all images */ export function toQuotaUsage(a: { enforced: boolean; total: number; left: number; checkin?: number }): QuotaUsage { if (!a.enforced) return { unlimited: true, checkin: a.checkin }; const pct = a.total > 0 ? Math.max(0, Math.min(100, Math.round((a.left * 100) / a.total))) : 0; return { unlimited: false, left: a.left, pct, checkin: a.checkin }; } /** * Loads the quota: on mount, when refreshKey changes (page change, fewer running tasks), * on the check-in event, and when the tab regains focus. */ export function useQuotaUsage( load: () => Promise<QuotaUsage>, { refreshKey, event = REWARDS_EVENT }: { refreshKey?: unknown; event?: string | null } = {}, ) { const [usage, setUsage] = useState<QuotaUsage | null>(null); useEffect(() => { let alive = true; const run = () => load() .then((u) => alive && setUsage(u)) .catch(() => undefined); run(); window.addEventListener("focus", run); if (event) window.addEventListener(event, run); return () => { alive = false; window.removeEventListener("focus", run); if (event) window.removeEventListener(event, run); }; // load is usually an inline function, so rebind only on refreshKey and the event name // eslint-disable-next-line react-hooks/exhaustive-deps }, [refreshKey, event]); return usage; } export interface QuotaMeterLabels { /** Default title of the card variant */ title?: string; unlimitedHead?: string; unlimitedSub?: string; /** Description of the card variant when the quota is unlimited */ unlimitedBody?: string; head?: (pct: number) => string; sub?: (left: string) => string; checkin?: (n: number) => string; /** Card variant: the check-in reminder bar at the bottom */ nudge?: (n: number) => ReactNode; separator?: string; } type SunLocale = "en" | "zh-TW" | "zh-CN" | "ja"; /** Maps any language tag to one of the four built-in languages, falling back to English */ function resolveLocale(tag?: string): SunLocale { const t = (tag ?? "en").toLowerCase(); if (t.startsWith("zh")) return /hant|tw|hk|mo/.test(t) ? "zh-TW" : "zh-CN"; if (t.startsWith("ja")) return "ja"; return "en"; } const MESSAGES: Record<SunLocale, Required<QuotaMeterLabels>> = { en: { title: "Today's free quota", unlimitedHead: "Unlimited for now", unlimitedSub: "Remaining percent shows once quotas start", unlimitedBody: "Generation has no image limit right now.", head: (pct) => `${pct}% of quota left`, sub: (left) => `About ${left} images left`, checkin: (n) => `Check in today for ${n} images`, nudge: (n) => <>You haven't checked in today. Check in for {n} more images</>, separator: " · ", }, "zh-TW": { title: "今日免費額度", unlimitedHead: "目前不限量", unlimitedSub: "開始計算額度後會顯示剩餘百分比", unlimitedBody: "目前生成圖片沒有張數限制。", head: (pct) => `額度剩餘 ${pct}%`, sub: (left) => `約可再生成 ${left} 張`, checkin: (n) => `今天簽到可領 ${n} 張`, nudge: (n) => <>今天還沒簽到,簽到可再領 {n} 張</>, separator: "・", }, "zh-CN": { title: "今日免费额度", unlimitedHead: "目前不限量", unlimitedSub: "开始计算额度后会显示剩余百分比", unlimitedBody: "目前生成图片没有张数限制。", head: (pct) => `额度剩余 ${pct}%`, sub: (left) => `约可再生成 ${left} 张`, checkin: (n) => `今天签到可领 ${n} 张`, nudge: (n) => <>今天还没签到,签到可再领 {n} 张</>, separator: "・", }, ja: { title: "本日の無料枠", unlimitedHead: "いまは無制限", unlimitedSub: "上限が始まると残りの割合を表示します", unlimitedBody: "現在、画像の生成枚数に上限はありません。", head: (pct) => `残り ${pct}%`, sub: (left) => `あと約 ${left} 枚生成できます`, checkin: (n) => `チェックインで +${n} 枚`, nudge: (n) => <>今日はまだチェックインしていません。チェックインであと {n} 枚もらえます</>, separator: "・", }, }; export interface QuotaMeterProps { usage: QuotaUsage | null; /** sidebar: a ring at the bottom of the sidebar; sheet: a bar in the mobile panel; card: a quota card on the plans page */ variant?: "sidebar" | "sheet" | "card"; /** When the sidebar variant is collapsed only the ring remains; hover or focus shows the description */ collapsed?: boolean; /** Plans page to go to on click */ href?: string; onClick?: MouseEventHandler<HTMLAnchorElement>; /** Currently on the plans page */ current?: boolean; /** Title and note of the card variant; falls back to labels.title when omitted */ title?: ReactNode; hint?: ReactNode; /** Link of the check-in reminder bar in the card variant */ nudgeHref?: string; /** Turns to the warning color at or below this percentage */ lowAt?: number; /** Replaces the gift icon of the check-in reward, for example <Coins />; any SVG is scaled to the original 12 or 16px */ rewardIcon?: ReactNode; /** Accent color (check-in reminder), follows the theme's primary by default */ accent?: string; /** UI language and number format: en, zh-TW, zh-CN, ja. Tags like zh-Hant-HK and ja-JP are also accepted */ locale?: string; /** Overrides built-in strings, takes precedence over locale */ labels?: QuotaMeterLabels; className?: string; } // zinc palette from the original project (Yumecanvas), light and dark const tokens = "[--yc-card:#FFFFFF] [--yc-fg:#18181B] [--yc-muted:#F4F4F5] [--yc-muted-fg:#6B6B73] [--yc-border:#E4E4E7] [--yc-sidebar:#FAFAFA] [--yc-ring:#71717A] [--yc-brand-default:var(--primary,#18181B)] [--yc-destructive:#DC2626] [--yc-success:#15803D] " + "dark:[--yc-card:#111113] dark:[--yc-fg:#FAFAFA] dark:[--yc-muted:#27272A] dark:[--yc-muted-fg:#A1A1AA] dark:[--yc-border:rgba(255,255,255,0.08)] dark:[--yc-sidebar:#09090B] dark:[--yc-ring:#A1A1AA] dark:[--yc-brand-default:var(--primary,#FAFAFA)] dark:[--yc-destructive:#F87171] dark:[--yc-success:#4ADE80]"; const brand = "[--yc-brand-c:var(--yc-brand,var(--yc-brand-default))]"; const focus = "outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--yc-ring)]"; function GiftIcon({ className }: { className?: string }) { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden className={cn("shrink-0", className)}> <rect x="3" y="8" width="18" height="4" rx="1" /> <path d="M12 8v13" /> <path d="M19 12v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7" /> <path d="M7.5 8a2.5 2.5 0 0 1 0-5A4.8 8 0 0 1 12 8a4.8 8 0 0 1 4.5-5 2.5 2.5 0 0 1 0 5" /> </svg> ); } /** Uses rewardIcon if provided, otherwise the built-in gift */ function RewardIcon({ icon, className }: { icon?: ReactNode; className?: string }) { if (!icon) return <GiftIcon className={className} />; return ( <span aria-hidden className={cn("grid shrink-0 place-items-center [&_svg]:size-full", className)}> {icon} </span> ); } function ChevronDownIcon({ className }: { className?: string }) { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden className={cn("shrink-0", className)}> <path d="m6 9 6 6 6-6" /> </svg> ); } function texts(usage: QuotaUsage, lowAt: number, t: Required<QuotaMeterLabels>, locale?: string) { const base = usage.unlimited ? { pct: 100, low: false, head: t.unlimitedHead, sub: t.unlimitedSub } : { pct: usage.pct, low: usage.pct <= lowAt, head: t.head(usage.pct), sub: t.sub(usage.left.toLocaleString(locale)) }; // When not checked in yet, the second line becomes a reminder; clicking through to the plans page lets you check in return usage.checkin ? { ...base, sub: t.checkin(usage.checkin), gift: true } : { ...base, gift: false }; } function Bar({ pct, low, label }: { pct: number; low: boolean; label?: string }) { return ( <span className="block h-1.5 overflow-hidden rounded-full bg-[var(--yc-muted)]" role="progressbar" aria-label={label} aria-valuenow={pct} aria-valuemin={0} aria-valuemax={100} > <span className={cn( "block h-full rounded-full transition-[width] duration-700 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none", low ? "bg-[var(--yc-destructive)]" : "bg-[var(--yc-fg)]", )} style={{ width: `${pct}%` }} /> </span> ); } export function QuotaMeter({ usage, variant = "sidebar", collapsed = false, href = "/plans/", onClick, current = false, title: titleProp, hint, nudgeHref = "#rewards", lowAt = 10, rewardIcon, accent, locale, labels, className, }: QuotaMeterProps) { const [tip, setTip] = useState(false); if (!usage) return null; const l = { ...MESSAGES[resolveLocale(locale)], ...labels }; const title = titleProp ?? l.title; const t = texts(usage, lowAt, l, locale); const style = accent ? ({ "--yc-brand": accent } as CSSProperties) : undefined; const root = cn(tokens, brand, "text-[var(--yc-fg)]"); if (variant === "sheet") { return ( <a href={href} onClick={onClick} style={style} className={cn(root, focus, "flex flex-col gap-2 rounded-[0.7rem] border border-[var(--yc-border)] px-3 py-3 hover:bg-[var(--yc-muted)]", className)} > <span className="flex items-baseline justify-between gap-2 text-sm"> <span className="font-medium">{t.head}</span> <span className={cn("flex min-w-0 items-center gap-1 text-xs tabular-nums", t.gift ? "text-[var(--yc-brand-c)]" : "text-[var(--yc-muted-fg)]")}> {t.gift ? <RewardIcon icon={rewardIcon} className="size-3" /> : null} <span className="truncate">{t.sub}</span> </span> </span> <Bar pct={t.pct} low={t.low} /> </a> ); } if (variant === "card") { const label = typeof title === "string" ? title : undefined; return ( <section style={style} className={cn(root, "overflow-hidden rounded-[0.9rem] border border-[var(--yc-border)] bg-[var(--yc-card)]", className)}> <div className="p-5"> {usage.unlimited ? ( <div className="flex flex-col gap-2"> <span className="inline-flex w-fit items-center gap-1 rounded-full bg-[color-mix(in_oklab,var(--yc-success)_15%,transparent)] px-2 py-0.5 text-xs font-medium text-[var(--yc-success)]"> <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden className="size-3.5 shrink-0"> <path d="M6 16c5 0 7-8 12-8a4 4 0 0 1 0 8c-5 0-7-8-12-8a4 4 0 1 0 0 8" /> </svg> {l.unlimitedHead} </span> <p className="text-sm text-[var(--yc-muted-fg)]">{l.unlimitedBody}</p> </div> ) : ( <div className="flex flex-col gap-2 rounded-[0.7rem] bg-[color-mix(in_oklab,var(--yc-muted)_40%,transparent)] px-4 py-3"> <div className="flex items-baseline justify-between gap-2"> <span className="text-sm">{title}</span> <span className={cn("text-lg font-semibold tabular-nums", t.low && "text-[var(--yc-destructive)]")}>{t.pct}%</span> </div> <Bar pct={t.pct} low={t.low} label={label} /> <div className="flex justify-between gap-2 text-xs text-[var(--yc-muted-fg)]"> <span className="tabular-nums">{l.sub(usage.left.toLocaleString(locale))}</span> {hint ? <span className="max-sm:hidden">{hint}</span> : null} </div> </div> )} </div> {usage.checkin ? ( <a href={nudgeHref} className={cn( focus, "flex items-center gap-2 border-t border-[var(--yc-border)] bg-[color-mix(in_oklab,var(--yc-brand-c)_10%,transparent)] px-5 py-2.5 text-sm transition-colors hover:bg-[color-mix(in_oklab,var(--yc-brand-c)_15%,transparent)] focus-visible:-outline-offset-2", )} > <RewardIcon icon={rewardIcon} className="size-4 text-[var(--yc-brand-c)]" /> <span className="min-w-0 flex-1">{l.nudge(usage.checkin)}</span> <ChevronDownIcon className="size-4 text-[var(--yc-muted-fg)]" /> </a> ) : null} </section> ); } // Sidebar variant: when collapsed only a small ring remains; hover shows the description const ring = 2 * Math.PI * 9; return ( <span style={style} className={cn(root, "relative block", className)} onMouseEnter={() => setTip(true)} onMouseLeave={() => setTip(false)}> <a href={href} onClick={onClick} aria-label={`${t.head}${l.separator}${t.sub}`} aria-current={current ? "page" : undefined} onFocus={() => setTip(true)} onBlur={() => setTip(false)} onKeyDown={(e) => e.key === "Escape" && setTip(false)} className={cn(focus, "flex items-center gap-2.5 rounded-[0.4rem] px-0.5 py-1.5 hover:bg-[var(--yc-muted)]", current && "bg-[var(--yc-muted)]")} > <span className="relative shrink-0"> <svg viewBox="0 0 24 24" className="size-7 -rotate-90" aria-hidden> <circle cx="12" cy="12" r="9" fill="none" strokeWidth="3" className="stroke-[var(--yc-muted)]" /> <circle cx="12" cy="12" r="9" fill="none" strokeWidth="3" strokeLinecap="round" className={cn("transition-[stroke-dashoffset] duration-700 motion-reduce:transition-none", t.low ? "stroke-[var(--yc-destructive)]" : "stroke-[var(--yc-fg)]")} strokeDasharray={ring} strokeDashoffset={ring * (1 - t.pct / 100)} /> </svg> {t.gift ? ( <span aria-hidden className="absolute -top-0.5 -right-0.5 size-2 rounded-full bg-[var(--yc-brand-c)] ring-2 ring-[var(--yc-sidebar)]" /> ) : null} </span> <span className={cn( "flex min-w-0 flex-1 flex-col gap-1 whitespace-nowrap transition-opacity duration-200 motion-reduce:transition-none", collapsed && "pointer-events-none opacity-0", )} aria-hidden > <span className="text-xs font-medium">{t.head}</span> <span className={cn("flex items-center gap-1 truncate text-[11px] tabular-nums", t.gift ? "text-[var(--yc-brand-c)]" : "text-[var(--yc-muted-fg)]")}> {t.gift ? <RewardIcon icon={rewardIcon} className="size-3" /> : null} {t.sub} </span> </span> </a> {collapsed && tip ? ( <span role="tooltip" className="absolute top-1/2 left-[calc(1.75rem+0.75rem)] z-50 -translate-y-1/2 rounded-[0.4rem] bg-[var(--yc-fg)] px-3 py-1.5 text-xs whitespace-nowrap text-[var(--yc-card)]" > {t.head}・{t.sub} </span> ) : null} </span> ); }
相似區塊
- Pro





