codingPlanResetOpportunityBadge.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { CodingPlanUsageRemainingState } from "@/CodingPlanUsageRemainingPanel.js";
  2. import type { CodingPlanQuotaResetUiController } from "@/hooks/useCodingPlanQuotaResetUi.js";
  3. import { findCodingPlanQuotaLimit } from "@/lib/codingPlanQuotaPresentation.js";
  4. import {
  5. mergeCodingPlanQuotaResetOpportunityBadges,
  6. resolveCodingPlanQuotaResetLimit,
  7. } from "@/lib/codingPlanQuotaResetUi.js";
  8. export function resolveChatCodingPlanResetOpportunityBadge(
  9. state: CodingPlanUsageRemainingState | null,
  10. resetUi: Pick<CodingPlanQuotaResetUiController, "entry" | "opportunityVisible" | "week">,
  11. ) {
  12. const limits = state?.visibleSnapshot?.quota?.limits ?? [];
  13. const fiveHourTokenLimit = resolveCodingPlanQuotaResetLimit(
  14. findCodingPlanQuotaLimit(limits, "TOKENS_LIMIT", 3, 5),
  15. resetUi.entry,
  16. );
  17. const weeklyTokenLimit = resolveCodingPlanQuotaResetLimit(
  18. findCodingPlanQuotaLimit(limits, "TOKENS_LIMIT", 6),
  19. resetUi.week.entry,
  20. );
  21. // Bugfix:徽标曾在额度剩余 100% 时隐藏(重置无收益)。但核销会把剩余改写成 100%,
  22. // 余下机会的徽标随即消失,用户以为卡被吞掉。只要该额度存在且有可用机会就展示张数。
  23. return mergeCodingPlanQuotaResetOpportunityBadges([
  24. {
  25. count: resetUi.entry?.opportunityCount ?? 0,
  26. expiresAt: resetUi.entry?.opportunityExpiresAt ?? null,
  27. visible: Boolean(fiveHourTokenLimit) && resetUi.opportunityVisible,
  28. },
  29. {
  30. count: resetUi.week.entry?.opportunityCount ?? 0,
  31. expiresAt: resetUi.week.entry?.opportunityExpiresAt ?? null,
  32. visible: Boolean(weeklyTokenLimit) && resetUi.week.opportunityVisible,
  33. },
  34. ]);
  35. }