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
196 lines
6.5 KiB
TypeScript
196 lines
6.5 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import Link from "next/link";
|
|
import type { AppPromo } from "@/app/lib/types";
|
|
import { useTheme } from "@/app/lib/ThemeContext";
|
|
|
|
const isExternal = (url: string) => /^https?:\/\//i.test(url);
|
|
|
|
/** Fixed cadence for hands-off slide advance; matches AnnouncementMarquee's constant-feel pacing. */
|
|
const AUTO_ADVANCE_MS = 5000;
|
|
/** Minimum horizontal drag (px) before a touch gesture counts as a swipe rather than a tap. */
|
|
const SWIPE_THRESHOLD_PX = 40;
|
|
|
|
const prefersReducedMotion = () =>
|
|
typeof window !== "undefined" && !!window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
|
|
/** Tracks the OS-level "reduce motion" preference; used to pause auto-advance and video autoplay.
|
|
* Reads the media query synchronously on first render (rather than defaulting to false and
|
|
* correcting in an effect) so a reduced-motion video never autoplays even for one tick. */
|
|
function useReducedMotion(): boolean {
|
|
const [reduced, setReduced] = useState(prefersReducedMotion);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined" || !window.matchMedia) return;
|
|
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
const onChange = () => setReduced(mq.matches);
|
|
mq.addEventListener("change", onChange);
|
|
return () => mq.removeEventListener("change", onChange);
|
|
}, []);
|
|
|
|
return reduced;
|
|
}
|
|
|
|
function Media({ promo, active, reducedMotion }: { promo: AppPromo; active: boolean; reducedMotion: boolean }) {
|
|
const videoRef = useRef<HTMLVideoElement>(null);
|
|
|
|
useEffect(() => {
|
|
const video = videoRef.current;
|
|
if (!video) return;
|
|
if (active && !reducedMotion) video.play().catch(() => {});
|
|
else video.pause();
|
|
}, [active, reducedMotion]);
|
|
|
|
if (promo.mediaType === "VIDEO") {
|
|
return (
|
|
<video
|
|
ref={videoRef}
|
|
src={promo.mediaUrl}
|
|
muted
|
|
loop
|
|
playsInline
|
|
preload="none"
|
|
aria-label={promo.altText || promo.caption || undefined}
|
|
className="w-full h-full object-cover"
|
|
/>
|
|
);
|
|
}
|
|
|
|
return (
|
|
// eslint-disable-next-line @next/next/no-img-element -- arbitrary remote image URLs; next/image would require per-host remotePatterns config and fixed dimensions.
|
|
<img src={promo.mediaUrl} alt={promo.altText ?? ""} className="w-full h-full object-cover" />
|
|
);
|
|
}
|
|
|
|
/** Visible text overlay for a slide's caption; non-interactive so it never blocks the slide's click-through. */
|
|
function CaptionOverlay({ caption }: { caption: string }) {
|
|
return (
|
|
<div
|
|
data-testid="home-promo-caption"
|
|
className="absolute inset-x-0 bottom-0 px-4 pt-8 pb-3 pointer-events-none"
|
|
style={{
|
|
pointerEvents: "none",
|
|
background: "linear-gradient(to top, rgba(0,0,0,0.65), rgba(0,0,0,0))",
|
|
}}
|
|
>
|
|
<span className="text-xs font-medium text-white drop-shadow-sm">{caption}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Slide({ promo, active, reducedMotion }: { promo: AppPromo; active: boolean; reducedMotion: boolean }) {
|
|
const media = <Media promo={promo} active={active} reducedMotion={reducedMotion} />;
|
|
const caption = promo.caption?.trim();
|
|
const overlay = caption ? <CaptionOverlay caption={caption} /> : null;
|
|
|
|
if (!promo.linkUrl) {
|
|
return (
|
|
<div className="relative block w-full h-full">
|
|
{media}
|
|
{overlay}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (isExternal(promo.linkUrl)) {
|
|
return (
|
|
<a
|
|
href={promo.linkUrl}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="relative block w-full h-full"
|
|
tabIndex={active ? undefined : -1}
|
|
>
|
|
{media}
|
|
{overlay}
|
|
</a>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Link href={promo.linkUrl} className="relative block w-full h-full" tabIndex={active ? undefined : -1}>
|
|
{media}
|
|
{overlay}
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
export default function HomePromoCarousel({ promos, height }: { promos: AppPromo[]; height?: number }) {
|
|
const [index, setIndex] = useState(0);
|
|
const { isDark } = useTheme();
|
|
const reducedMotion = useReducedMotion();
|
|
const touchStartX = useRef<number | null>(null);
|
|
const count = promos.length;
|
|
// Derived (not stored) so a shrinking promo list never leaves a stale, out-of-range index.
|
|
const activeIndex = count === 0 ? 0 : ((index % count) + count) % count;
|
|
|
|
useEffect(() => {
|
|
if (reducedMotion || count <= 1) return;
|
|
const id = setInterval(() => setIndex((i) => i + 1), AUTO_ADVANCE_MS);
|
|
return () => clearInterval(id);
|
|
}, [reducedMotion, count]);
|
|
|
|
if (count === 0) return null;
|
|
|
|
const goTo = (i: number) => setIndex(i);
|
|
|
|
const onTouchStart = (e: React.TouchEvent) => {
|
|
touchStartX.current = e.touches[0]?.clientX ?? null;
|
|
};
|
|
|
|
const onTouchEnd = (e: React.TouchEvent) => {
|
|
const startX = touchStartX.current;
|
|
touchStartX.current = null;
|
|
if (startX == null) return;
|
|
const dx = (e.changedTouches[0]?.clientX ?? startX) - startX;
|
|
if (Math.abs(dx) < SWIPE_THRESHOLD_PX) return;
|
|
goTo(activeIndex + (dx < 0 ? 1 : -1));
|
|
};
|
|
|
|
return (
|
|
<div
|
|
role="region"
|
|
aria-label="Promotions"
|
|
data-testid="home-promo-carousel"
|
|
className="relative"
|
|
onTouchStart={onTouchStart}
|
|
onTouchEnd={onTouchEnd}
|
|
>
|
|
<div
|
|
data-testid="home-promo-viewport"
|
|
className={`relative w-full rounded-[1.25rem] overflow-hidden${height == null ? " aspect-[16/9]" : ""}`}
|
|
style={height == null ? undefined : { height }}
|
|
>
|
|
{promos.map((promo, i) => (
|
|
<div
|
|
key={promo.id}
|
|
className="absolute inset-0 transition-opacity duration-300"
|
|
style={{ opacity: i === activeIndex ? 1 : 0, pointerEvents: i === activeIndex ? "auto" : "none" }}
|
|
aria-hidden={i === activeIndex ? undefined : "true"}
|
|
>
|
|
<Slide promo={promo} active={i === activeIndex} reducedMotion={reducedMotion} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
{count > 1 && (
|
|
<div className="flex items-center justify-center gap-1.5 mt-2">
|
|
{promos.map((promo, i) => (
|
|
<button
|
|
key={promo.id}
|
|
type="button"
|
|
aria-label={`Go to slide ${i + 1}`}
|
|
aria-current={i === activeIndex ? "true" : undefined}
|
|
data-testid={`home-promo-dot-${i}`}
|
|
onClick={() => goTo(i)}
|
|
className="w-1.5 h-1.5 rounded-full"
|
|
style={{ background: i === activeIndex ? "#FF6B00" : isDark ? "rgba(255,255,255,0.35)" : "rgba(0,0,0,0.2)" }}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|