Aplikacja: zdjecia silowni przez AuthImage (blob z chronionego endpointu) - wczesniej surowy <img> nie mial bearer i nie rysowal

This commit is contained in:
tisachi
2026-10-11 17:56:54 +02:00
parent 21c7d2a484
commit 8a8e44c83d
4 changed files with 135 additions and 33 deletions

View File

@@ -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<string, Promise<string>>();
function loadObjectUrl(url: string): Promise<string> {
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
* <img src> 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<string | null>(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 <img src={objectUrl} alt={alt} className={className} />;
}

View File

@@ -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ą.
</p>
{canManage && (
<Button size="sm" variant="secondary" onClick={() => fileRef.current?.click()} disabled={busy}>
<Button
size="sm"
variant="secondary"
onClick={() => fileRef.current?.click()}
disabled={busy}
>
<ImagePlus className="w-4 h-4" />
Dodaj zdjęcia
</Button>
@@ -140,7 +146,7 @@ export default function GymGallery({
key={p.id}
className="relative rounded-xl overflow-hidden border border-line bg-input group"
>
<img src={gymPhotoSrc(p.url)} alt={gym.name} className="h-32 w-full object-cover" />
<AuthImage src={p.url} alt={gym.name} className="h-32 w-full object-cover" />
{isCover && (
<span className="absolute top-1.5 left-1.5">
<Badge color="green">
@@ -192,7 +198,11 @@ export default function GymGallery({
<h3 className="mb-2 text-[1.05rem] font-semibold">Usuń zdjęcie</h3>
<p className="text-[0.875rem] text-dim">Czy na pewno chcesz usunąć to zdjęcie?</p>
<div className="flex flex-col-reverse sm:flex-row sm:items-center sm:justify-end gap-3 mt-5">
<Button variant="secondary" onClick={() => setConfirmDelete(null)} className="w-full sm:w-auto">
<Button
variant="secondary"
onClick={() => setConfirmDelete(null)}
className="w-full sm:w-auto"
>
Anuluj
</Button>
<Button

View File

@@ -15,7 +15,6 @@ import {
createGymException,
deleteGym,
deleteGymException,
gymPhotoSrc,
useGymDetail,
useGymExceptions,
useGymInvitations,
@@ -40,6 +39,7 @@ import {
} from '../../components/ui';
import { useToast } from '../../components/toast';
import { Avatar } from '../../components/Avatar';
import { AuthImage } from '../../components/AuthImage';
import { useTopbar, type TopbarAction, type TopbarCustomGroup } from '../../topbar';
import { normalizeAvailability, TIME_SLOTS } from '../../lib/availability';
import ClaimModal from '../../components/gyms/ClaimModal';
@@ -52,16 +52,15 @@ function HoursList({ gym }: { gym: Gym }) {
const availability = normalizeAvailability(gym.availability);
const todayIdx = (new Date().getDay() + 6) % 7;
const DOWS = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'];
const fmt = (r: { start: string; end: string } | null) => (r ? `${r.start}–${r.end}` : 'zamknięte');
const fmt = (r: { start: string; end: string } | null) =>
r ? `${r.start}–${r.end}` : 'zamknięte';
return (
<div className="flex flex-col gap-1">
{availability.map((day, i) => (
<div
key={i}
className={`flex items-center justify-between gap-3 rounded-[10px] px-3 py-1.5 text-[0.85rem] ${
i === todayIdx
? 'bg-accent/10 outline outline-1 outline-accent/40'
: ''
i === todayIdx ? 'bg-accent/10 outline outline-1 outline-accent/40' : ''
}`}
>
<span className={`flex items-center gap-2 ${day ? 'font-semibold' : 'text-dim/70'}`}>
@@ -72,7 +71,9 @@ function HoursList({ gym }: { gym: Gym }) {
</span>
)}
</span>
<span className={`tabular-nums ${day ? 'font-semibold' : 'text-dim/60'}`}>{fmt(day)}</span>
<span className={`tabular-nums ${day ? 'font-semibold' : 'text-dim/60'}`}>
{fmt(day)}
</span>
</div>
))}
</div>
@@ -89,11 +90,14 @@ function GalleryMosaic({ gym }: { gym: Gym & { photos: { id: string; url: string
return (
<div className="grid grid-cols-3 gap-2">
<div className="relative col-span-2 row-span-2 overflow-hidden rounded-[10px] border border-line bg-input">
<img src={gymPhotoSrc(first.url)} alt={gym.name} className="h-full w-full object-cover" />
<AuthImage src={first.url} alt={gym.name} className="h-full w-full object-cover" />
</div>
{rest.slice(0, 3).map((p) => (
<div key={p.id} className="relative overflow-hidden rounded-[10px] border border-line bg-input">
<img src={gymPhotoSrc(p.url)} alt={gym.name} className="h-full min-h-20 w-full object-cover" />
<div
key={p.id}
className="relative overflow-hidden rounded-[10px] border border-line bg-input"
>
<AuthImage src={p.url} alt={gym.name} className="h-full min-h-20 w-full object-cover" />
</div>
))}
{rest.length > 3 && (
@@ -363,9 +367,7 @@ export default function GymDetail() {
id: 'edit',
label: 'Edycja',
icon: Pencil,
actions: [
{ label: 'Dane, godziny i wyjątki', onClick: () => setEditOpen(true) },
],
actions: [{ label: 'Dane, godziny i wyjątki', onClick: () => setEditOpen(true) }],
},
...(gym.kind === 'REAL'
? [
@@ -441,8 +443,8 @@ export default function GymDetail() {
{/* HERO: okładka (zdjęcie lub placeholder) + nazwa + chipy metadanych */}
<div className="relative mb-5 flex min-h-[190px] items-end overflow-hidden rounded-2xl border border-line shadow-card">
{gym.coverPhoto ? (
<img
src={gymPhotoSrc(gym.coverPhoto)}
<AuthImage
src={gym.coverPhoto}
alt={gym.name}
className="absolute inset-0 h-full w-full object-cover"
/>
@@ -467,7 +469,10 @@ export default function GymDetail() {
<Dumbbell className="absolute left-1/2 top-1/2 h-10 w-10 -translate-x-1/2 -translate-y-1/2 text-ink/20" />
</>
)}
<div aria-hidden="true" className="absolute inset-0 bg-[linear-gradient(180deg,transparent_30%,rgb(0_0_0/0.6))]" />
<div
aria-hidden="true"
className="absolute inset-0 bg-[linear-gradient(180deg,transparent_30%,rgb(0_0_0/0.6))]"
/>
<div className="relative z-10 flex w-full flex-wrap items-end justify-between gap-3 p-5">
<div>
<h1 className="font-display text-[1.5rem] font-bold tracking-[-0.02em] text-white drop-shadow">
@@ -517,7 +522,10 @@ export default function GymDetail() {
</Card>
)}
<Card>
<SectionHeader title="Urządzenia" description="Maszyny i urządzenia dostępne w tej siłowni." />
<SectionHeader
title="Urządzenia"
description="Maszyny i urządzenia dostępne w tej siłowni."
/>
<EquipmentTiles gym={gym} />
</Card>
</div>
@@ -526,7 +534,9 @@ export default function GymDetail() {
<Card>
<SectionHeader title="Godziny otwarcia" />
<HoursList gym={gym} />
<div className="mt-4 mb-2 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">Wyjątki</div>
<div className="mt-4 mb-2 text-[11px] font-bold uppercase tracking-[0.1em] text-dim">
Wyjątki
</div>
<ExceptionsList gymId={gym.id} canManage={isOwner} />
</Card>
<Card>
@@ -540,14 +550,21 @@ export default function GymDetail() {
<b className="font-display tabular-nums">{gym.capacity ?? '—'}</b>
</div>
</Card>
{isOwner && gym.kind === 'VIRTUAL' && gym.isPrivate && <GymInvitationsSection gym={gym} />}
{isOwner && gym.kind === 'VIRTUAL' && gym.isPrivate && (
<GymInvitationsSection gym={gym} />
)}
</div>
</div>
{/* MODALE właściciela (otwierane z grup menu kontekstowego) */}
{gym && (
<>
<Modal wide open={editOpen} onClose={() => setEditOpen(false)} title="Dane, godziny i wyjątki">
<Modal
wide
open={editOpen}
onClose={() => setEditOpen(false)}
title="Dane, godziny i wyjątki"
>
<GymEditForm
key={gym.id}
gym={gym}
@@ -567,7 +584,11 @@ export default function GymDetail() {
<Modal open={galleryOpen} onClose={() => setGalleryOpen(false)} title="Zarządzaj galerią">
<GymGallery gym={gym} canManage={canManage} onChanged={() => {}} />
</Modal>
<Modal open={equipmentOpen} onClose={() => setEquipmentOpen(false)} title="Zarządzaj urządzeniami">
<Modal
open={equipmentOpen}
onClose={() => setEquipmentOpen(false)}
title="Zarządzaj urządzeniami"
>
<EquipmentSection key={gym.id} gym={gym} />
</Modal>
{gym.kind === 'VIRTUAL' && (
@@ -578,7 +599,11 @@ export default function GymDetail() {
/>
)}
{inviteOpen && (
<InviteGymModal open onClose={() => setInviteOpen(false)} gym={{ id: gym.id, name: gym.name }} />
<InviteGymModal
open
onClose={() => setInviteOpen(false)}
gym={{ id: gym.id, name: gym.name }}
/>
)}
</>
)}
@@ -602,7 +627,9 @@ function GymInvitationsSection({ gym }: { gym: Gym }) {
const queryClient = useQueryClient();
const { push } = useToast();
const { data } = useGymInvitations();
const sent = (data?.invitations ?? []).filter((i) => i.direction === 'sent' && i.gym.id === gym.id);
const sent = (data?.invitations ?? []).filter(
(i) => i.direction === 'sent' && i.gym.id === gym.id,
);
const withdraw = async (id: string) => {
try {
@@ -616,12 +643,19 @@ function GymInvitationsSection({ gym }: { gym: Gym }) {
return (
<Card>
<SectionHeader title="Zaproszenia" description="Prywatna siłownia jest ukryta — zapraszaj określonych userów." />
<SectionHeader
title="Zaproszenia"
description="Prywatna siłownia jest ukryta — zapraszaj określonych userów."
/>
<div className="divide-y divide-line">
{sent.length === 0 && <span className="py-3 text-[0.875rem] text-dim">Brak wysłanych zaproszeń.</span>}
{sent.length === 0 && (
<span className="py-3 text-[0.875rem] text-dim">Brak wysłanych zaproszeń.</span>
)}
{sent.map((inv) => {
const name =
[inv.user?.firstName, inv.user?.lastName].filter(Boolean).join(' ') || inv.user?.email || '—';
[inv.user?.firstName, inv.user?.lastName].filter(Boolean).join(' ') ||
inv.user?.email ||
'—';
return (
<ListRow
key={inv.id}
@@ -632,7 +666,12 @@ function GymInvitationsSection({ gym }: { gym: Gym }) {
</span>
}
actions={
<Button variant="danger" size="sm" type="button" onClick={() => void withdraw(inv.id)}>
<Button
variant="danger"
size="sm"
type="button"
onClick={() => void withdraw(inv.id)}
>
Wycofaj
</Button>
}

View File

@@ -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 */}
<div className="relative grid h-28 place-items-center overflow-hidden sm:h-32">
{cover ? (
<img
src={gymPhotoSrc(cover)}
<AuthImage
src={cover}
alt={gym.name}
className="absolute inset-0 h-full w-full object-cover"
/>