Aplikacja: zdjecia silowni przez AuthImage (blob z chronionego endpointu) - wczesniej surowy <img> nie mial bearer i nie rysowal
This commit is contained in:
53
app/src/components/AuthImage.tsx
Normal file
53
app/src/components/AuthImage.tsx
Normal 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} />;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user