contextQuotaResetOpportunityReminder.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { AlarmClock, GiftIcon, XIcon } from "lucide-react";
  2. import type { useZCodeIntl } from "@/i18n/IntlProvider.js";
  3. import { formatCodingPlanQuotaResetCountdown } from "@/components/coding-plan-quota-reset/CodingPlanQuotaResetDialog.js";
  4. import { Button } from "@/components/ui/button.js";
  5. export const CONTEXT_QUOTA_RESET_URGENT_SECONDS = 180;
  6. export type ContextQuotaResetOpportunityReminderPhase = "initial" | "urgent";
  7. export interface ContextQuotaResetOpportunityDismissal {
  8. opportunityKey: string | null;
  9. initial: boolean;
  10. urgent: boolean;
  11. }
  12. export interface ContextQuotaResetOpportunity {
  13. count: number;
  14. expiresAt: number | null;
  15. sourceKey: string | null;
  16. visible: boolean;
  17. }
  18. export interface ContextQuotaResetOpportunityReminder {
  19. count: number;
  20. opportunityKey: string;
  21. phase: ContextQuotaResetOpportunityReminderPhase;
  22. remainingSeconds: number;
  23. }
  24. export function createContextQuotaResetOpportunityDismissalStore() {
  25. let snapshot: ContextQuotaResetOpportunityDismissal = {
  26. opportunityKey: null,
  27. initial: false,
  28. urgent: false,
  29. };
  30. const listeners = new Set<() => void>();
  31. return {
  32. dismiss(reminder: ContextQuotaResetOpportunityReminder) {
  33. const sameOpportunity = snapshot.opportunityKey === reminder.opportunityKey;
  34. snapshot = {
  35. initial: sameOpportunity
  36. ? snapshot.initial || reminder.phase === "initial"
  37. : reminder.phase === "initial",
  38. opportunityKey: reminder.opportunityKey,
  39. urgent: sameOpportunity
  40. ? snapshot.urgent || reminder.phase === "urgent"
  41. : reminder.phase === "urgent",
  42. };
  43. listeners.forEach((listener) => listener());
  44. },
  45. getSnapshot: () => snapshot,
  46. subscribe(listener: () => void) {
  47. listeners.add(listener);
  48. return () => listeners.delete(listener);
  49. },
  50. };
  51. }
  52. // 会话切换会重新挂载 composer;已读状态必须归当前 renderer 窗口所有,
  53. // 否则同一个重置机会会在每次切换会话后重新弹出。模块实例天然按窗口隔离且不落盘。
  54. export const contextQuotaResetOpportunityDismissalStore =
  55. createContextQuotaResetOpportunityDismissalStore();
  56. export type ContextQuotaResetOpportunityTriggerTone = "available" | "urgent";
  57. export function resolveContextQuotaResetOpportunityTriggerTone({
  58. now,
  59. opportunity,
  60. }: {
  61. now: number;
  62. opportunity: ContextQuotaResetOpportunity;
  63. }): ContextQuotaResetOpportunityTriggerTone | null {
  64. const { count, expiresAt, sourceKey, visible } = opportunity;
  65. if (!visible || count <= 0 || expiresAt === null || !sourceKey || expiresAt <= now) {
  66. return null;
  67. }
  68. return expiresAt - now <= CONTEXT_QUOTA_RESET_URGENT_SECONDS * 1_000 ? "urgent" : "available";
  69. }
  70. export function resolveContextQuotaResetOpportunityReminder({
  71. dismissal,
  72. now,
  73. opportunity,
  74. }: {
  75. dismissal: ContextQuotaResetOpportunityDismissal;
  76. now: number;
  77. opportunity: ContextQuotaResetOpportunity;
  78. }): ContextQuotaResetOpportunityReminder | null {
  79. const { count, expiresAt, sourceKey, visible } = opportunity;
  80. if (!visible || count <= 0 || expiresAt === null || !sourceKey || expiresAt <= now) {
  81. return null;
  82. }
  83. const opportunityKey = `${sourceKey}:${expiresAt}`;
  84. const sameOpportunity = dismissal.opportunityKey === opportunityKey;
  85. const remainingSeconds = Math.max(0, Math.ceil((expiresAt - now) / 1_000));
  86. const phase: ContextQuotaResetOpportunityReminderPhase =
  87. remainingSeconds <= CONTEXT_QUOTA_RESET_URGENT_SECONDS ? "urgent" : "initial";
  88. if (sameOpportunity && dismissal[phase]) {
  89. return null;
  90. }
  91. return { count, opportunityKey, phase, remainingSeconds };
  92. }
  93. export function resolveContextTriggerTooltipKind(
  94. resetStatus: "processing" | "completed" | null,
  95. opportunityPhase: ContextQuotaResetOpportunityReminderPhase | null,
  96. ): "reset-status" | ContextQuotaResetOpportunityReminderPhase | null {
  97. return resetStatus ? "reset-status" : opportunityPhase;
  98. }
  99. export function shouldDismissContextQuotaResetOpportunityReminder(
  100. target: EventTarget | null,
  101. trigger: HTMLElement | null,
  102. ): boolean {
  103. if (!(target instanceof Node) || trigger?.contains(target)) {
  104. return false;
  105. }
  106. const targetElement = target instanceof Element ? target : target.parentElement;
  107. return !targetElement?.closest("[data-context-reset-reminder]");
  108. }
  109. export function ContextQuotaResetOpportunityReminderContent({
  110. count,
  111. intl,
  112. onDismiss,
  113. phase,
  114. remainingSeconds,
  115. }: {
  116. count: number;
  117. intl: ReturnType<typeof useZCodeIntl>["intl"];
  118. onDismiss: () => void;
  119. phase: ContextQuotaResetOpportunityReminderPhase;
  120. remainingSeconds: number;
  121. }) {
  122. if (phase === "initial") {
  123. return (
  124. <span
  125. role="status"
  126. className="inline-flex items-center gap-1.5 text-ui-sm"
  127. data-context-reset-reminder="initial"
  128. >
  129. <GiftIcon className="size-3.5 shrink-0 text-success" aria-hidden="true" />
  130. <span>
  131. {intl.formatMessage({ id: "codingPlan.quotaReset.contextReminder.available" }, { count })}
  132. </span>
  133. <ContextQuotaResetOpportunityDismissButton intl={intl} onDismiss={onDismiss} />
  134. </span>
  135. );
  136. }
  137. const countdown = formatCodingPlanQuotaResetCountdown(remainingSeconds, intl.formatMessage);
  138. return (
  139. <span
  140. role="status"
  141. className="inline-flex items-center gap-0 text-ui-sm"
  142. data-context-reset-reminder="urgent"
  143. >
  144. <span className="inline-flex items-center gap-1.5" data-context-reset-reminder-copy>
  145. <span
  146. className="inline-flex shrink-0 origin-bottom animate-zcode-alarm-ring text-warning motion-reduce:animate-none"
  147. aria-hidden="true"
  148. >
  149. <AlarmClock className="size-3.5" />
  150. </span>
  151. <span>
  152. {intl.formatMessage({ id: "codingPlan.quotaReset.contextReminder.expiresIn" })}{" "}
  153. <span className="inline-flex h-3.5 items-center rounded-full bg-warning/10 px-1.5 text-ui-xs leading-none font-medium tabular-nums text-warning">
  154. {countdown}
  155. </span>
  156. </span>
  157. </span>
  158. <ContextQuotaResetOpportunityDismissButton intl={intl} onDismiss={onDismiss} />
  159. </span>
  160. );
  161. }
  162. function ContextQuotaResetOpportunityDismissButton({
  163. intl,
  164. onDismiss,
  165. }: {
  166. intl: ReturnType<typeof useZCodeIntl>["intl"];
  167. onDismiss: () => void;
  168. }) {
  169. return (
  170. <Button
  171. type="button"
  172. variant="ghost"
  173. size="icon-xs"
  174. className="rounded-full text-foreground-subtle"
  175. aria-label={intl.formatMessage({ id: "codingPlan.quotaReset.contextReminder.dismiss" })}
  176. data-context-reset-reminder-dismiss
  177. onPointerDown={(event) => event.stopPropagation()}
  178. onClick={(event) => {
  179. event.stopPropagation();
  180. onDismiss();
  181. }}
  182. >
  183. <XIcon aria-hidden="true" />
  184. </Button>
  185. );
  186. }