import { AlarmClock, GiftIcon, XIcon } from "lucide-react"; import type { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { formatCodingPlanQuotaResetCountdown } from "@/components/coding-plan-quota-reset/CodingPlanQuotaResetDialog.js"; import { Button } from "@/components/ui/button.js"; export const CONTEXT_QUOTA_RESET_URGENT_SECONDS = 180; export type ContextQuotaResetOpportunityReminderPhase = "initial" | "urgent"; export interface ContextQuotaResetOpportunityDismissal { opportunityKey: string | null; initial: boolean; urgent: boolean; } export interface ContextQuotaResetOpportunity { count: number; expiresAt: number | null; sourceKey: string | null; visible: boolean; } export interface ContextQuotaResetOpportunityReminder { count: number; opportunityKey: string; phase: ContextQuotaResetOpportunityReminderPhase; remainingSeconds: number; } export function createContextQuotaResetOpportunityDismissalStore() { let snapshot: ContextQuotaResetOpportunityDismissal = { opportunityKey: null, initial: false, urgent: false, }; const listeners = new Set<() => void>(); return { dismiss(reminder: ContextQuotaResetOpportunityReminder) { const sameOpportunity = snapshot.opportunityKey === reminder.opportunityKey; snapshot = { initial: sameOpportunity ? snapshot.initial || reminder.phase === "initial" : reminder.phase === "initial", opportunityKey: reminder.opportunityKey, urgent: sameOpportunity ? snapshot.urgent || reminder.phase === "urgent" : reminder.phase === "urgent", }; listeners.forEach((listener) => listener()); }, getSnapshot: () => snapshot, subscribe(listener: () => void) { listeners.add(listener); return () => listeners.delete(listener); }, }; } // 会话切换会重新挂载 composer;已读状态必须归当前 renderer 窗口所有, // 否则同一个重置机会会在每次切换会话后重新弹出。模块实例天然按窗口隔离且不落盘。 export const contextQuotaResetOpportunityDismissalStore = createContextQuotaResetOpportunityDismissalStore(); export type ContextQuotaResetOpportunityTriggerTone = "available" | "urgent"; export function resolveContextQuotaResetOpportunityTriggerTone({ now, opportunity, }: { now: number; opportunity: ContextQuotaResetOpportunity; }): ContextQuotaResetOpportunityTriggerTone | null { const { count, expiresAt, sourceKey, visible } = opportunity; if (!visible || count <= 0 || expiresAt === null || !sourceKey || expiresAt <= now) { return null; } return expiresAt - now <= CONTEXT_QUOTA_RESET_URGENT_SECONDS * 1_000 ? "urgent" : "available"; } export function resolveContextQuotaResetOpportunityReminder({ dismissal, now, opportunity, }: { dismissal: ContextQuotaResetOpportunityDismissal; now: number; opportunity: ContextQuotaResetOpportunity; }): ContextQuotaResetOpportunityReminder | null { const { count, expiresAt, sourceKey, visible } = opportunity; if (!visible || count <= 0 || expiresAt === null || !sourceKey || expiresAt <= now) { return null; } const opportunityKey = `${sourceKey}:${expiresAt}`; const sameOpportunity = dismissal.opportunityKey === opportunityKey; const remainingSeconds = Math.max(0, Math.ceil((expiresAt - now) / 1_000)); const phase: ContextQuotaResetOpportunityReminderPhase = remainingSeconds <= CONTEXT_QUOTA_RESET_URGENT_SECONDS ? "urgent" : "initial"; if (sameOpportunity && dismissal[phase]) { return null; } return { count, opportunityKey, phase, remainingSeconds }; } export function resolveContextTriggerTooltipKind( resetStatus: "processing" | "completed" | null, opportunityPhase: ContextQuotaResetOpportunityReminderPhase | null, ): "reset-status" | ContextQuotaResetOpportunityReminderPhase | null { return resetStatus ? "reset-status" : opportunityPhase; } export function shouldDismissContextQuotaResetOpportunityReminder( target: EventTarget | null, trigger: HTMLElement | null, ): boolean { if (!(target instanceof Node) || trigger?.contains(target)) { return false; } const targetElement = target instanceof Element ? target : target.parentElement; return !targetElement?.closest("[data-context-reset-reminder]"); } export function ContextQuotaResetOpportunityReminderContent({ count, intl, onDismiss, phase, remainingSeconds, }: { count: number; intl: ReturnType["intl"]; onDismiss: () => void; phase: ContextQuotaResetOpportunityReminderPhase; remainingSeconds: number; }) { if (phase === "initial") { return ( ); } const countdown = formatCodingPlanQuotaResetCountdown(remainingSeconds, intl.formatMessage); return ( {intl.formatMessage({ id: "codingPlan.quotaReset.contextReminder.expiresIn" })}{" "} {countdown} ); } function ContextQuotaResetOpportunityDismissButton({ intl, onDismiss, }: { intl: ReturnType["intl"]; onDismiss: () => void; }) { return ( ); }