Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : apps/web/src/components/Gallery.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { AnimatePresence, motion } from "motion/react";44import { useCallback, useEffect, useRef, useState } from "react";45import { imageSrcSet, imageUrl } from "../lib/api";46import { IconChevronLeft, IconChevronRight, IconCross, IconExpand } from "./Icons";4748export interface GalleryImage {49 id: string;50 alt: string;51}5253/**54 * Galerie de photographies : balayage tactile, flèches, vignettes, plein écran.55 * Les proportions sont préservées ; une photo indisponible est simplement retirée.56 */57export function Gallery({ images, resetKey }: { images: GalleryImage[]; resetKey: string }) {58 const [broken, setBroken] = useState<Set<string>>(new Set());59 const [index, setIndex] = useState(0);60 const [full, setFull] = useState(false);61 const [loaded, setLoaded] = useState<Set<string>>(new Set());62 const drag = useRef<{ x: number; y: number } | null>(null);63 const thumbs = useRef<HTMLDivElement>(null);64 const usable = images.filter((image) => !broken.has(image.id));65 const count = usable.length;66 const current = usable[Math.min(index, Math.max(0, count - 1))];6768 useEffect(() => {69 setIndex(0);70 setBroken(new Set());71 setFull(false);72 }, [resetKey]);7374 const go = useCallback(75 (delta: number) => {76 if (count > 0) setIndex((i) => (Math.min(i, count - 1) + delta + count) % count);77 },78 [count],79 );8081 useEffect(() => {82 const onKey = (event: KeyboardEvent): void => {83 const target = event.target as HTMLElement | null;84 if (target && /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName)) return;85 if (event.key === "ArrowLeft") go(-1);86 else if (event.key === "ArrowRight") go(1);87 else if (event.key === "Escape") setFull(false);88 else if (event.key.toLowerCase() === "f") setFull((v) => !v);89 };90 window.addEventListener("keydown", onKey);91 return () => window.removeEventListener("keydown", onKey);92 }, [go]);9394 // Précharge la photo suivante de la galerie.95 useEffect(() => {96 const next = usable[(index + 1) % Math.max(1, count)];97 if (next && next.id !== current?.id) {98 const img = new Image();99 img.src = imageUrl(next.id, 960);100 }101 }, [index, count, current?.id]);102103 useEffect(() => {104 const strip = thumbs.current;105 const active = strip?.querySelector<HTMLElement>('[aria-current="true"]');106 if (strip && active) strip.scrollTo({ left: active.offsetLeft - strip.clientWidth / 2 + active.clientWidth / 2, behavior: "smooth" });107 }, [index]);108109 const onPointerDown = (event: React.PointerEvent): void => {110 drag.current = { x: event.clientX, y: event.clientY };111 };112 const onPointerUp = (event: React.PointerEvent): void => {113 const start = drag.current;114 drag.current = null;115 if (!start) return;116 const dx = event.clientX - start.x;117 const dy = event.clientY - start.y;118 if (Math.abs(dx) > 44 && Math.abs(dx) > Math.abs(dy) * 1.4) go(dx < 0 ? 1 : -1);119 };120121 if (!current) {122 return (123 <div className="gallery gallery-empty">124 <p className="muted">Photographies indisponibles pour cette propriété.</p>125 </div>126 );127 }128129 const stage = (large: boolean) => (130 <div className={`gallery-stage${large ? " is-full" : ""}`} onPointerDown={onPointerDown} onPointerUp={onPointerUp} onPointerCancel={() => (drag.current = null)}>131 <AnimatePresence mode="popLayout" initial={false}>132 <motion.img133 key={current.id}134 src={imageUrl(current.id, large ? 1600 : 960)}135 srcSet={imageSrcSet(current.id)}136 sizes={large ? "100vw" : "(min-width: 960px) 60vw, 100vw"}137 alt={current.alt}138 draggable={false}139 decoding="async"140 fetchPriority={index === 0 ? "high" : "auto"}141 onLoad={() => setLoaded((s) => new Set(s).add(current.id))}142 onError={() => setBroken((s) => new Set(s).add(current.id))}143 initial={{ opacity: 0, scale: 1.02 }}144 animate={{ opacity: 1, scale: 1 }}145 exit={{ opacity: 0 }}146 transition={{ duration: 0.35, ease: [0.16, 1, 0.3, 1] }}147 className={large ? "contain" : ""}148 />149 </AnimatePresence>150 {!loaded.has(current.id) && <div className="gallery-loading" aria-hidden="true"><span className="spinner" /></div>}151 {count > 1 && (152 <>153 <button type="button" className="gallery-nav prev" onClick={() => go(-1)} aria-label="Photographie précédente"><IconChevronLeft /></button>154 <button type="button" className="gallery-nav next" onClick={() => go(1)} aria-label="Photographie suivante"><IconChevronRight /></button>155 </>156 )}157 <div className="gallery-count tabular" aria-live="polite">158 {Math.min(index, count - 1) + 1} / {count}159 </div>160 <button type="button" className="gallery-full" onClick={() => setFull(!large)} aria-label={large ? "Quitter le plein écran" : "Voir en plein écran"}>161 {large ? <IconCross /> : <IconExpand />}162 </button>163 </div>164 );165166 return (167 <div className="gallery" role="group" aria-roledescription="galerie" aria-label="Photographies de la propriété">168 {stage(false)}169 {count > 1 && (170 <div className="gallery-thumbs" ref={thumbs}>171 {usable.map((image, i) => (172 <button key={image.id} type="button" aria-current={i === Math.min(index, count - 1)} aria-label={`Photographie ${i + 1}`} onClick={() => setIndex(i)}>173 <img src={imageUrl(image.id, 480)} alt="" loading="lazy" decoding="async" draggable={false} onError={() => setBroken((s) => new Set(s).add(image.id))} />174 </button>175 ))}176 </div>177 )}178 <AnimatePresence>179 {full && (180 <motion.div className="lightbox" role="dialog" aria-modal="true" aria-label="Photographie en plein écran" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>181 {stage(true)}182 </motion.div>183 )}184 </AnimatePresence>185 </div>186 );187}188