Files
co-lite/solvway-customer-portal/app/page.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

330 lines
15 KiB
TypeScript

"use client";
import { useState, useEffect, useRef } from "react";
import { motion } from "framer-motion";
import Link from "next/link";
import { useRouter } from "next/navigation";
import {
Coins, ChevronRight,
ShoppingBag, Music2, Sparkles, Map, Gift,
Maximize2,
} from "lucide-react";
import { useTheme } from "@/app/lib/ThemeContext";
import { useMember } from "@/app/lib/MemberContext";
import { useToast } from "@/app/lib/ToastContext";
import { apiGet } from "@/app/lib/clientApi";
import QRModal from "@/app/components/QRModal";
import HomeProfileReminder from "@/app/components/HomeProfileReminder";
import MemberQR from "@/app/components/ui/MemberQR";
import MemberAvatar from "@/app/components/ui/MemberAvatar";
import AnnouncementMarquee from "@/app/components/ui/AnnouncementMarquee";
import HomePromoCarousel from "@/app/components/ui/HomePromoCarousel";
import type { AppBanner, AppPromo, CurrentEventConfig } from "@/app/lib/types";
const MotionLink = motion(Link);
// A tile links only when the current event has its content, as in member-portal home.html —
// except Taste Rewards+, which links when the event has reward products (an empty list stays inert).
const QUICK_LINKS: { label: string; icon: typeof Gift; href: string; color: string; bg: string; when: keyof CurrentEventConfig }[] = [
{ label: "Welcome Gift", icon: ShoppingBag, href: "/attendance", color: "#FF6B00", bg: "rgba(255,107,0,0.12)", when: "currentEvent" },
{ label: "Stage Activities", icon: Music2, href: "/stage", color: "#FF6B00", bg: "rgba(255,107,0,0.12)", when: "currentEventStageActivities" },
{ label: "Lucky Draw", icon: Sparkles, href: "/draw", color: "#FF6B00", bg: "rgba(255,107,0,0.12)", when: "currentEventLuckyDraw" },
{ label: "Floor Plan", icon: Map, href: "/floorplan", color: "#FF6B00", bg: "rgba(255,107,0,0.12)", when: "currentEventFloorPlan" },
{ label: "Taste Rewards +", icon: Gift, href: "/store", color: "#FF6B00", bg: "rgba(255,107,0,0.12)", when: "currentEventTasteRewardsProductList" },
];
/* ── Animations ───────────────────────────────────────────────────── */
const stagger = {
hidden: {},
visible: { transition: { staggerChildren: 0.07 } },
};
const fadeUp = {
hidden: { opacity: 0, y: 16 },
visible: { opacity: 1, y: 0, transition: { type: "spring" as const, stiffness: 340, damping: 28 } },
};
/* ── Component ────────────────────────────────────────────────────── */
export default function HomePage() {
const { isDark } = useTheme();
const { member } = useMember();
const router = useRouter();
const { showToast } = useToast();
const [qrOpen, setQrOpen] = useState(false);
const [balance, setBalance] = useState(0);
const [earnedToday, setEarnedToday] = useState(0);
const [banners, setBanners] = useState<AppBanner[]>([]);
const [promos, setPromos] = useState<AppPromo[]>([]);
const [eventConfig, setEventConfig] = useState<CurrentEventConfig>({});
const pointsRef = useRef<HTMLDivElement>(null);
const [promoHeight, setPromoHeight] = useState<number>();
useEffect(() => {
const el = pointsRef.current;
if (!el || typeof ResizeObserver === "undefined") return;
const update = () => {
const h = el.offsetHeight;
if (h > 0) setPromoHeight(h);
};
const ro = new ResizeObserver(update);
ro.observe(el);
update();
return () => ro.disconnect();
}, []);
useEffect(() => {
// Edit Profile leaves the pre-save balance here after a first completion; the reward lands a few seconds later.
// The key is removed only when the check ends, so a Strict Mode remount still finds it.
const clearBaseline = () => { try { sessionStorage.removeItem("profileRewardBaseline"); } catch {} };
let baseline: number | undefined;
let deadline = 0;
try {
const raw = sessionStorage.getItem("profileRewardBaseline");
if (raw !== null) {
let note: { balance?: unknown; at?: unknown } | null = null;
try { note = JSON.parse(raw); } catch {}
if (typeof note?.balance === "number" && Number.isFinite(note.balance) && typeof note.at === "number" && Number.isFinite(note.at) && Date.now() < note.at + 12000) {
baseline = note.balance;
deadline = note.at + 12000;
} else clearBaseline();
}
} catch {}
let timer: ReturnType<typeof setTimeout> | undefined;
let cancelled = false;
const retryOrStop = () => {
if (Date.now() < deadline) timer = setTimeout(readBalance, 1500);
else clearBaseline();
};
function readBalance() {
apiGet<{ balance: number }>("/api/points/balance")
.then((d) => {
if (cancelled) return;
setBalance(d.balance);
if (baseline === undefined || d.balance > baseline) {
if (baseline === undefined) return;
clearBaseline();
apiGet<{ earnedToday: number }>("/api/points/today").then((t) => !cancelled && setEarnedToday(t.earnedToday)).catch(() => {});
showToast(`Profile updated · +${(d.balance - baseline).toLocaleString()} points earned`);
} else retryOrStop();
})
.catch(() => { if (!cancelled && baseline !== undefined) retryOrStop(); });
}
readBalance();
apiGet<{ earnedToday: number }>("/api/points/today").then((d) => setEarnedToday(d.earnedToday)).catch(() => {});
// Global announcement banner — non-critical; render nothing on failure.
apiGet<AppBanner[]>("/api/banner").then(setBanners).catch(() => {});
// Home promo carousel — non-critical; render nothing on failure.
apiGet<AppPromo[]>("/api/promos").then(setPromos).catch(() => {});
// On failure every event tile stays inert.
apiGet<CurrentEventConfig>("/api/event/current-config").then(setEventConfig).catch(() => {});
return () => { cancelled = true; clearTimeout(timer); };
}, [showToast]);
const t = {
bg: isDark ? "rgba(10,10,10,0.4)" : "var(--bg-primary)",
card: isDark ? "rgba(255,255,255,0.06)" : "var(--bg-card)",
cardAlt: isDark ? "rgba(255,255,255,0.03)" : "var(--bg-card-alt)",
border: isDark ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.07)",
shadow: isDark ? "0 8px 32px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06)"
: "0 2px 20px rgba(0,0,0,0.06), 0 1px 4px rgba(0,0,0,0.04)",
text: isDark ? "#FFFFFF" : "#1a1a1a",
textSec: isDark ? "rgba(255,255,255,0.8)" : "#5C5C5C",
textMuted: isDark ? "rgba(255,255,255,0.6)" : "#6B6B6B",
};
const card = {
background: t.card,
border: `1px solid ${t.border}`,
boxShadow: t.shadow,
borderRadius: "1.25rem",
};
return (
<div className="min-h-full" style={{ background: "var(--bg-primary)" }}>
<HomeProfileReminder />
<motion.div
variants={stagger}
initial="hidden"
animate="visible"
className="px-4 pt-5 space-y-4 pb-2"
>
{/* ── 0. Announcement banner (running marquee) ──────────────── */}
{banners.length > 0 && (
<motion.div variants={fadeUp}>
<AnnouncementMarquee banners={banners} />
</motion.div>
)}
{/* ── 1. Profile Card ───────────────────────────────────────── */}
<motion.div variants={fadeUp} style={card} className="p-4 flex items-center gap-3">
{/* Avatar */}
<MemberAvatar className="w-14 h-14 rounded-2xl" />
{/* Info */}
<div className="flex-1 min-w-0">
<p className="font-bold text-base leading-tight truncate uppercase" style={{ color: t.text }}>{member?.name ?? "—"}</p>
<p className="text-xs font-mono mt-0.5 truncate" style={{ color: t.textMuted }}>{member?.memberCode ?? ""}</p>
<p className="text-xs mt-0.5 truncate" style={{ color: t.textSec }}>{member?.phone ?? ""}</p>
</div>
{/* QR thumbnail — tap to enlarge */}
<motion.button
onClick={() => setQrOpen(true)}
whileTap={{ scale: 0.94 }}
whileHover={{ scale: 1.03 }}
transition={{ type: "spring", stiffness: 420, damping: 24 }}
className="flex-shrink-0 flex flex-col items-center gap-1.5"
aria-label="Show my member QR code"
>
<div
className="rounded-xl p-1.5 flex items-center justify-center"
style={{
width: 58,
height: 58,
background: "#FFFFFF",
border: `1px solid ${isDark ? "rgba(255,255,255,0.12)" : "rgba(0,0,0,0.06)"}`,
boxShadow: "0 2px 10px rgba(255,107,0,0.12)",
}}
>
<MemberQR value={member?.memberCode ?? ""} size={48} />
</div>
<span className="flex items-center gap-0.5 text-xs font-semibold tracking-wide leading-none" style={{ color: "var(--orange-text)" }}>
<Maximize2 strokeWidth={2.5} className="w-2.5 h-2.5" />
Tap to view
</span>
</motion.button>
</motion.div>
{/* ── 3. Points Section ─────────────────────────────────────── */}
<motion.div
ref={pointsRef}
variants={fadeUp}
className="p-4 rounded-[1.25rem] overflow-hidden relative"
style={{
background: "linear-gradient(135deg, #C94D00 0%, #A83F00 100%)",
boxShadow: "0 8px 32px rgba(255,107,0,0.35), 0 2px 8px rgba(0,0,0,0.15)",
}}
>
{/* Subtle noise/shimmer overlay */}
<div
className="absolute inset-0 pointer-events-none rounded-[1.25rem]"
style={{
background: "radial-gradient(ellipse at 80% 20%, rgba(255,255,255,0.12) 0%, transparent 60%)",
}}
/>
{/* Top row: earned today + total */}
<div className="relative flex items-start justify-between mb-4">
<div>
<p className="text-xs font-semibold uppercase tracking-widest mb-1.5" style={{ color: "rgba(255,255,255,0.65)" }}>
Earned Today
</p>
<div className="flex items-center gap-1.5">
<Coins strokeWidth={2} className="w-4 h-4 stroke-white" />
<span className="text-lg font-extrabold text-white">
+{earnedToday.toLocaleString()} pts
</span>
</div>
</div>
<div className="text-right">
<p className="text-xs font-semibold uppercase tracking-widest mb-1.5" style={{ color: "rgba(255,255,255,0.65)" }}>
Total Balance
</p>
<p className="text-3xl font-extrabold tracking-tight text-white leading-none">
{balance.toLocaleString()}
<span className="text-sm font-medium ml-1" style={{ color: "rgba(255,255,255,0.6)" }}>pts</span>
</p>
</div>
</div>
{/* Redeem button */}
<motion.button
onClick={() => router.push("/store")}
whileTap={{ scale: 0.97 }}
transition={{ type: "spring", stiffness: 420, damping: 26 }}
className="relative w-full flex items-center justify-center gap-2 py-2.5 rounded-xl font-bold text-sm"
style={{
background: "rgba(255,255,255,0.18)",
border: "1px solid rgba(255,255,255,0.3)",
color: "#ffffff",
backdropFilter: "blur(8px)",
}}
>
<Gift strokeWidth={2} className="w-4 h-4" />
Redeem Points
<ChevronRight strokeWidth={2} className="w-3.5 h-3.5" />
</motion.button>
</motion.div>
{/* ── 4. Quick Links ─────────────────────────────────────────── */}
<motion.section variants={fadeUp} className="py-3">
<div className="grid grid-cols-3 gap-2.5">
{QUICK_LINKS.map(({ label, icon: Icon, href, color, bg, when }) => {
const content = (
<>
{/* Duotone icon container */}
<div
className="w-11 h-11 rounded-xl flex items-center justify-center"
style={{ background: bg }}
>
<Icon
strokeWidth={2}
style={{ width: 22, height: 22, stroke: color, fill: `${color}25` }}
/>
</div>
<span
className="text-xs font-semibold text-center leading-tight px-1"
style={{ color: t.textSec }}
>
{label}
</span>
</>
);
const cls = "flex flex-col items-center gap-2 py-4 rounded-2xl";
const value = eventConfig[when];
return (Array.isArray(value) ? value.length > 0 : value) ? (
<MotionLink
key={label}
href={href}
whileTap={{ scale: 0.93 }}
whileHover={{ scale: 1.03 }}
transition={{ type: "spring", stiffness: 420, damping: 24 }}
className={cls}
style={card}
>
{content}
</MotionLink>
) : (
<div key={label} className={cls} style={card}>
{content}
</div>
);
})}
</div>
</motion.section>
{/* ── 5. Highlights (promo carousel) ─────────────────────────── */}
{promos.length > 0 && (
<motion.section variants={fadeUp}>
<h2 className="text-xs font-bold tracking-widest uppercase mb-2" style={{ color: t.textMuted }}>
Highlights
</h2>
<HomePromoCarousel promos={promos} height={promoHeight} />
</motion.section>
)}
</motion.div>
{/* ── QR Modal ────────────────────────────────────────────────── */}
<QRModal
open={qrOpen}
onClose={() => setQrOpen(false)}
member={{ name: member?.name ?? "", id: member?.memberCode ?? "" }}
/>
</div>
);
}