import type { CodingPlanBillingDiscountConfig, CodingPlanBillingDiscountLocaleCopy, Locale, } from "@zcode/shared"; import type { ICodingPlanSubscriptionService } from "@zcode/services"; import { InfoIcon, SparklesIcon, TrendingUpIcon } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { cn } from "@/components/lib/utils.js"; import { MessageResponse } from "@/components/ai-elements/message.js"; import { Dialog, DialogContent, DialogTrigger, } from "@/components/ui/dialog.js"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { useOptionalServices } from "@/hooks/useServices.js"; import { useZCodeStoreWithDefault } from "@/store/StoreProvider.js"; import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js"; import { logger } from "@/logger.js"; const BILLING_DISCOUNT_CACHE_TTL_MS = 60 * 60 * 1000; export const CODING_PLAN_BILLING_DISCOUNT_DIALOG_CONTENT_CLASS = "max-h-[min(82vh,640px)] max-w-lg grid-rows-[auto_minmax(0,1fr)] overflow-hidden"; export const CODING_PLAN_BILLING_DISCOUNT_DIALOG_BODY_CLASS = "p-3 !space-y-3"; let billingDiscountCache: { value: CodingPlanBillingDiscountConfig | undefined; expiresAt: number; } | null = null; let billingDiscountRequest: Promise< CodingPlanBillingDiscountConfig | undefined > | null = null; export function useCodingPlanBillingDiscount(): { active: boolean; config: CodingPlanBillingDiscountConfig | undefined; loading: boolean; refresh: () => Promise; } { const services = useOptionalServices(); return useCodingPlanBillingDiscountFromService( services?.codingPlanSubscriptionService, ); } export function useCodingPlanBillingDiscountFromService( service: | Pick | undefined, ): { active: boolean; config: CodingPlanBillingDiscountConfig | undefined; loading: boolean; refresh: () => Promise; } { const { locale } = useZCodeIntl(); const [state, setState] = useState({ active: hasLocaleCopy(billingDiscountCache?.value, locale, ["badgeBody"]), config: billingDiscountCache?.value, loading: Boolean(service) && !billingDiscountCache, }); const refresh = useCallback(async () => { if (!service || typeof service.getBillingDiscount !== "function") { setState({ active: false, config: undefined, loading: false }); return; } setState((current) => ({ ...current, loading: true })); try { const discount = await loadCodingPlanBillingDiscount(service); setState({ active: hasLocaleCopy(discount, locale, ["badgeBody"]), config: discount, loading: false, }); } catch (error) { logger.warn("[CodingPlanBillingDiscount] 读取 Coding Plan 活动配置失败", { error: error instanceof Error ? error.message : String(error), }); setState({ active: false, config: undefined, loading: false }); } }, [locale, service]); useEffect(() => { void refresh(); }, [refresh]); return { active: state.active, config: state.config, loading: state.loading, refresh, }; } export function resolveCodingPlanBillingDiscountCopy( config: CodingPlanBillingDiscountConfig | undefined, locale: Locale, ): CodingPlanBillingDiscountLocaleCopy { const copy = config?.[locale]; if (!copy || typeof copy !== "object") { return {}; } return { badgeBody: readNonEmptyString(copy.badgeBody), cardTitle: readNonEmptyString(copy.cardTitle), cardBody: readNonEmptyString(copy.cardBody), infoTitle: readNonEmptyString(copy.infoTitle), infoBody: readNonEmptyString(copy.infoBody), }; } function hasLocaleCopy( config: CodingPlanBillingDiscountConfig | undefined, locale: Locale, keys: Array, ): boolean { const copy = resolveCodingPlanBillingDiscountCopy(config, locale); return keys.every((key) => Boolean(copy[key])); } type CodingPlanBillingDiscountBadgePillProps = { config?: CodingPlanBillingDiscountConfig; iconVisible?: boolean; size?: "default" | "compact"; variant?: "gradient" | "surface" | "tag"; }; export function CodingPlanBillingDiscountBadgePill({ config, iconVisible = true, size = "default", variant = "gradient", }: CodingPlanBillingDiscountBadgePillProps) { const { locale } = useZCodeIntl(); const copy = resolveCodingPlanBillingDiscountCopy(config, locale); if (!copy.badgeBody) { return null; } return (
{iconVisible ? ( ) : null} {copy.badgeBody}
); } export function CodingPlanBillingDiscountBadge( props: CodingPlanBillingDiscountBadgePillProps, ) { const { locale } = useZCodeIntl(); const copy = resolveCodingPlanBillingDiscountCopy(props.config, locale); if (!copy.badgeBody) { return null; } const badge = ; return ( <> {badge} ); } export function CodingPlanBillingDiscountBanner({ config, }: { config?: CodingPlanBillingDiscountConfig; }) { const { locale } = useZCodeIntl(); const copy = resolveCodingPlanBillingDiscountCopy(config, locale); if (!copy.cardTitle || !copy.cardBody) { return null; } return (
{copy.cardTitle}
{copy.cardBody}
); } export function CodingPlanBillingDiscountInfoDialog({ config, tone = "default", }: { config?: CodingPlanBillingDiscountConfig; tone?: "default" | "onGradient"; }) { const { intl, locale } = useZCodeIntl(); const copy = resolveCodingPlanBillingDiscountCopy(config, locale); // M5②.5 store 耦合剥离:MessageResponse 不再自取 store,主题/代码预览设置由调用方传入。 const theme = useZCodeStoreWithDefault((state) => state.theme, "system"); const codePreviewSettings = useZCodeStoreWithDefault( (state) => state.codePreviewSettings, DEFAULT_CODE_PREVIEW_SETTINGS, ); const openLabel = intl.formatMessage({ id: "settings.modelProvider.codingPlan.billingDiscountInfo.open", }); // 活动规则会随服务端运营配置变化,必须整体使用远端标题和 Markdown 正文, // 缺失时隐藏入口,避免本地旧规则与当前活动不一致。 if (!copy.infoTitle || !copy.infoBody) { return null; } const markdown = `## ${copy.infoTitle}\n\n${copy.infoBody}`; return (
{markdown}
); } export function clearCodingPlanBillingDiscountCacheForTest() { billingDiscountCache = null; billingDiscountRequest = null; } export function primeCodingPlanBillingDiscountCacheForTest( config: CodingPlanBillingDiscountConfig | undefined, ) { billingDiscountCache = { value: config, expiresAt: Date.now() + BILLING_DISCOUNT_CACHE_TTL_MS, }; billingDiscountRequest = null; } async function loadCodingPlanBillingDiscount( service: Pick, ): Promise { const now = Date.now(); if (billingDiscountCache && billingDiscountCache.expiresAt > now) { return billingDiscountCache.value; } if (billingDiscountRequest) { return billingDiscountRequest; } const request = service.getBillingDiscount(); billingDiscountRequest = request; try { const discount = await request; billingDiscountCache = { value: discount, expiresAt: now + BILLING_DISCOUNT_CACHE_TTL_MS, }; return discount; } finally { if (billingDiscountRequest === request) { billingDiscountRequest = null; } } } function readNonEmptyString(value: unknown): string | undefined { if (typeof value !== "string") { return undefined; } const trimmed = value.trim(); return trimmed.length > 0 ? trimmed : undefined; }