import React, { useCallback, useEffect, useState } from "react"; import { apiService } from "../../lib/api-client"; import { __ } from "../../lib/i18n"; import { Crown, Star, Zap } from "lucide-react"; type NoticeAction = { label: string; url: string; target?: string; }; type NoticeItem = { id: string; type?: "info" | "success" | "warning" | "error"; title?: string; message: string; actions?: NoticeAction[]; }; function typeToClasses(type: NoticeItem["type"]) { switch (type) { case "success": return "bg-green-50 dark:bg-green-950/30 border-green-200 dark:border-green-900/40 text-green-900 dark:text-green-100"; case "warning": return "bg-amber-50 dark:bg-amber-950/25 border-amber-200 dark:border-amber-900/40 text-amber-900 dark:text-amber-100"; case "error": return "bg-red-50 dark:bg-red-950/30 border-red-200 dark:border-red-900/40 text-red-900 dark:text-red-100"; default: return "bg-blue-50 dark:bg-blue-950/25 border-blue-200 dark:border-blue-900/40 text-blue-900 dark:text-blue-100"; } } export const InlineNotices: React.FC = () => { const [notices, setNotices] = useState([]); const load = useCallback(async () => { try { const res = await apiService.getNotices(); const data = (res as any)?.data; setNotices(Array.isArray(data) ? data : []); } catch { // Fail silent: notices should never block the UI. setNotices([]); } }, []); useEffect(() => { load(); }, [load]); const dismiss = useCallback( async (id: string) => { // Optimistic UI. setNotices((prev) => prev.filter((n) => n.id !== id)); try { await apiService.dismissNotice(id); } catch { // Reload if dismiss failed (permissions / nonce / etc.) load(); } }, [load], ); if (notices.length === 0) { return null; } const renderUpgradeNotice = (n: NoticeItem) => { const primary = Array.isArray(n.actions) ? n.actions[0] : undefined; return (
{n.title || __("Upgrade to Pro", "yatra")}
{n.message}
{__("LIMITED TIME", "yatra")}
{__( "Special Offer: Upgrade today and save 30%+ with premium features and priority support.", "yatra", )}
{primary?.url && primary?.label && ( {primary.label} )}
); }; return (
{notices.map((n) => { if (n.id === "buy_pro") { return renderUpgradeNotice(n); } const primary = Array.isArray(n.actions) ? n.actions[0] : undefined; return (
{n.title && (
{n.title}
)}
{n.message}
{primary?.url && primary?.label && ( {primary.label} )}
); })}
); };