mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
3f0ed29f0c
This commit adds reusable client and end-user fallback assets and updates profile cards, lists, and assignment views to render graceful placeholders when images are missing or fail to load. It also hardens the shared LazyImage component with fallback handling and improves BadgeStatus so it accepts object-like status values and still renders clean labels. Minor UX fixes include dismissing upload toasts after completion and clarifying end-user form labels/copy.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
import { useState, useRef, useEffect } from "react";
|
|
|
|
export function LazyImage({
|
|
src,
|
|
alt,
|
|
className,
|
|
imgClassName,
|
|
fallbackSrc,
|
|
}: {
|
|
src?: string | null;
|
|
alt: string;
|
|
className?: string;
|
|
imgClassName?: string;
|
|
fallbackSrc?: string;
|
|
}) {
|
|
const effectiveSrc = src || fallbackSrc || "";
|
|
const [currentSrc, setCurrentSrc] = useState(effectiveSrc);
|
|
const [isLoaded, setIsLoaded] = useState(false);
|
|
const imgRef = useRef<HTMLImageElement>(null);
|
|
|
|
useEffect(() => {
|
|
const nextSrc = src || fallbackSrc || "";
|
|
setCurrentSrc(nextSrc);
|
|
setIsLoaded(false);
|
|
}, [src, fallbackSrc]);
|
|
|
|
useEffect(() => {
|
|
if (imgRef.current?.complete && (imgRef.current?.naturalWidth ?? 0) > 0) {
|
|
setIsLoaded(true);
|
|
}
|
|
}, [currentSrc]);
|
|
|
|
const handleError = () => {
|
|
if (fallbackSrc && currentSrc !== fallbackSrc) {
|
|
setCurrentSrc(fallbackSrc);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className={cn("relative overflow-hidden bg-secondary/50", className)}>
|
|
<img
|
|
ref={imgRef}
|
|
src={currentSrc}
|
|
alt={alt}
|
|
loading="lazy"
|
|
referrerPolicy="no-referrer"
|
|
onLoad={() => setIsLoaded(true)}
|
|
onError={handleError}
|
|
className={cn(
|
|
"w-full h-full object-cover transition-all duration-700 ease-in-out",
|
|
imgClassName,
|
|
isLoaded ? "blur-0 scale-100 opacity-100" : "blur-xl scale-110 opacity-0"
|
|
)}
|
|
/>
|
|
</div>
|
|
);
|
|
} |