mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
Improve image fallbacks and status display
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.
This commit is contained in:
@@ -1,37 +1,49 @@
|
||||
import { Badge, STATUS_BADGE_MAP } from "@/components/ui/badge"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
export function formatStatusText(status?: string): string {
|
||||
if (!status) return "";
|
||||
const trimmed = status.trim();
|
||||
if (!trimmed) return "";
|
||||
|
||||
// Tetap UPPERCASE jika seluruh teks status hanya 3 huruf (SIT, UAT, PTR, dll) atau akronim BAST
|
||||
const upperAcronyms = new Set(["SIT", "UAT", "PTR", "BAST", "POC", "WIP", "WO", "IM"]);
|
||||
if (trimmed.length === 3 || upperAcronyms.has(trimmed.toUpperCase())) {
|
||||
return trimmed.toUpperCase();
|
||||
}
|
||||
|
||||
// Capitalize setiap kata untuk teks status lainnya
|
||||
return trimmed
|
||||
.split(/\s+/)
|
||||
.map(word => {
|
||||
if (!word) return "";
|
||||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
||||
})
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
export default function BadgeStatus({ status, className }: { status: string, className?: string }) { // e.g. status = "UAT"
|
||||
const formatted = formatStatusText(status);
|
||||
const lookupKey = (status || "").toLowerCase().trim();
|
||||
|
||||
return (
|
||||
<Badge
|
||||
variant={STATUS_BADGE_MAP[lookupKey] || "default"}
|
||||
className={cn("text-[10px] font-semibold tracking-wide px-2 py-0.5", className)}
|
||||
>
|
||||
{formatted}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
import { Badge, STATUS_BADGE_MAP } from "@/components/ui/badge"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function extractStatusString(status?: any): string {
|
||||
if (!status) return "";
|
||||
if (typeof status === "string") return status;
|
||||
if (typeof status === "object") {
|
||||
return status.name || status.status || status.title || "";
|
||||
}
|
||||
return String(status);
|
||||
}
|
||||
|
||||
export function formatStatusText(status?: any): string {
|
||||
const raw = extractStatusString(status);
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return "";
|
||||
|
||||
// Tetap UPPERCASE jika seluruh teks status hanya 3 huruf (SIT, UAT, PTR, dll) atau akronim BAST
|
||||
const upperAcronyms = new Set(["SIT", "UAT", "PTR", "BAST", "POC", "WIP", "WO", "IM"]);
|
||||
if (trimmed.length === 3 || upperAcronyms.has(trimmed.toUpperCase())) {
|
||||
return trimmed.toUpperCase();
|
||||
}
|
||||
|
||||
// Capitalize setiap kata untuk teks status lainnya
|
||||
return trimmed
|
||||
.split(/\s+/)
|
||||
.map(word => {
|
||||
if (!word) return "";
|
||||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
||||
})
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
export default function BadgeStatus({ status, className }: { status: any, className?: string }) { // e.g. status = "UAT"
|
||||
const raw = extractStatusString(status);
|
||||
const formatted = formatStatusText(raw);
|
||||
if (!formatted) return null;
|
||||
|
||||
const lookupKey = raw.toLowerCase().trim();
|
||||
|
||||
return (
|
||||
<Badge
|
||||
variant={STATUS_BADGE_MAP[lookupKey] || "default"}
|
||||
className={cn("text-[10px] font-semibold tracking-wide px-2 py-0.5", className)}
|
||||
>
|
||||
{formatted}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"use client"
|
||||
"use client";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
@@ -8,31 +8,47 @@ export function LazyImage({
|
||||
alt,
|
||||
className,
|
||||
imgClassName,
|
||||
fallbackSrc,
|
||||
}: {
|
||||
src: string;
|
||||
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);
|
||||
|
||||
// Check if the image is already loaded from the cache
|
||||
useEffect(() => {
|
||||
if (imgRef.current?.complete) {
|
||||
const nextSrc = src || fallbackSrc || "";
|
||||
setCurrentSrc(nextSrc);
|
||||
setIsLoaded(false);
|
||||
}, [src, fallbackSrc]);
|
||||
|
||||
useEffect(() => {
|
||||
if (imgRef.current?.complete && (imgRef.current?.naturalWidth ?? 0) > 0) {
|
||||
setIsLoaded(true);
|
||||
}
|
||||
}, [src]); // Re-run this check if the src prop changes
|
||||
}, [currentSrc]);
|
||||
|
||||
const handleError = () => {
|
||||
if (fallbackSrc && currentSrc !== fallbackSrc) {
|
||||
setCurrentSrc(fallbackSrc);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn("relative overflow-hidden bg-secondary/50", className)}>
|
||||
<img
|
||||
ref={imgRef}
|
||||
src={src}
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user