diff --git a/app/src/components/AuthImage.tsx b/app/src/components/AuthImage.tsx new file mode 100644 index 0000000..00cec0f --- /dev/null +++ b/app/src/components/AuthImage.tsx @@ -0,0 +1,53 @@ +import { useEffect, useState } from 'react'; +import { apiBlob } from '../api/client'; + +// Obiekty URL trzymamy w buforze na czas życia karty — galeria potrafi +// renderować ten sam obraz wiele razy (cover + miniatura + lightbox). +const srcCache = new Map>(); + +function loadObjectUrl(url: string): Promise { + let pending = srcCache.get(url); + if (!pending) { + pending = apiBlob(url).then((blob) => URL.createObjectURL(blob)); + srcCache.set(url, pending); + pending.catch(() => srcCache.delete(url)); + } + return pending; +} + +interface AuthImageProps { + /** Ścieżka względna API (np. `/gyms/x/photos/y`) — jak dawniej `gymPhotoSrc`. */ + src: string | null | undefined; + alt: string; + className?: string; +} + +/** + * Zdjęcia siłowni serwuje chroniony endpoint (Authorization), więc zwykły + * nie przejdzie — pobieramy blob przez `api()` i wyświetlamy z + * URL.createObjectURL. Błąd poboru = pusty render (bez errorów w konsoli). + */ +export function AuthImage({ src, alt, className }: AuthImageProps) { + const [objectUrl, setObjectUrl] = useState(null); + + useEffect(() => { + if (!src) { + setObjectUrl(null); + return; + } + let active = true; + loadObjectUrl(src) + .then((url) => { + if (active) setObjectUrl(url); + }) + .catch(() => { + if (active) setObjectUrl(null); + }); + return () => { + active = false; + }; + }, [src]); + + if (!src || !objectUrl) return null; + return {alt}; +} diff --git a/app/src/components/gyms/GymGallery.tsx b/app/src/components/gyms/GymGallery.tsx index 03ebd63..38cf0e6 100644 --- a/app/src/components/gyms/GymGallery.tsx +++ b/app/src/components/gyms/GymGallery.tsx @@ -1,7 +1,8 @@ import { useRef, useState } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import { ImagePlus, Star, Trash2 } from 'lucide-react'; -import { addGymPhotos, deleteGymPhoto, gymPhotoSrc, setGymPhotoCover } from '../../api/queries'; +import { addGymPhotos, deleteGymPhoto, setGymPhotoCover } from '../../api/queries'; +import { AuthImage } from '../AuthImage'; import type { Gym, GymPhoto } from '../../api/types'; import { Badge, Button, ErrorText } from '../ui'; import { useToast } from '../toast'; @@ -110,7 +111,12 @@ export default function GymGallery({ Zdjęcia siłowni. Pierwsze zdjęcie (najmniejsza kolejność) jest okładką.

{canManage && ( - @@ -140,7 +146,7 @@ export default function GymGallery({ key={p.id} className="relative rounded-xl overflow-hidden border border-line bg-input group" > - {gym.name} + {isCover && ( @@ -192,7 +198,11 @@ export default function GymGallery({

Usuń zdjęcie

Czy na pewno chcesz usunąć to zdjęcie?

- } diff --git a/app/src/pages/gyms/Gyms.tsx b/app/src/pages/gyms/Gyms.tsx index 7032ff6..9573d3f 100644 --- a/app/src/pages/gyms/Gyms.tsx +++ b/app/src/pages/gyms/Gyms.tsx @@ -12,7 +12,6 @@ import { Users, } from 'lucide-react'; import { - gymPhotoSrc, joinGym, rateGym, useGyms, @@ -22,6 +21,7 @@ import { withdrawGymClaim, withdrawGymInvitation, } from '../../api/queries'; +import { AuthImage } from '../../components/AuthImage'; import type { Gym, GymClaim, GymInvitation, VirtualGym } from '../../api/types'; import { Badge, @@ -93,8 +93,8 @@ function GymCard({ {/* okładka: zdjęcie albo gradientowy placeholder z piktogramem */}
{cover ? ( - {gym.name}