mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
103 lines
5.2 KiB
TypeScript
103 lines
5.2 KiB
TypeScript
import { FC } from 'react';
|
|
import Image from 'next/image';
|
|
import { Dialog, DialogContent, DialogHeader, DialogTrigger } from './ui/dialog';
|
|
import { cn } from '@/lib/utils';
|
|
import { DialogDescription, DialogTitle } from '@radix-ui/react-dialog';
|
|
|
|
type Props = {
|
|
avatar: string
|
|
className?: string,
|
|
isPresence?: boolean
|
|
sizes?: string;
|
|
quality?: number
|
|
};
|
|
|
|
const ViewAvatarNextImage: FC<Props> = ({ avatar, className, isPresence, quality = 65, sizes = "(max-width: 640px) 36px, 40px" }) => {
|
|
return (
|
|
<>
|
|
{avatar ?
|
|
<Dialog>
|
|
<DialogTrigger asChild>
|
|
{/* <img loading='lazy' alt="avatar" src={process.env.BASE_URL_IMG + avatar} className={cn("h-9 w-9 sm:h-10 sm:w-10 shadow-sm rounded-full object-cover border border-border cursor-pointer hover:scale-110 duration-200", className)} /> */}
|
|
<div className={cn("relative h-9 w-9 sm:h-10 sm:w-10", className)}>
|
|
<Image
|
|
src={`${process.env.BASE_URL_IMG}${avatar}`}
|
|
alt="avatar"
|
|
fill
|
|
sizes={sizes}
|
|
quality={quality}
|
|
className="rounded-full object-cover shadow-sm border border-border cursor-pointer hover:scale-110 duration-200"
|
|
/>
|
|
</div>
|
|
</DialogTrigger>
|
|
<DialogContent className={`overflow-hidden p-0 rounded-2xl ${isPresence ? 'h-auto w-full' : 'w-full h-[400px] sm:w-[500px] sm:h-[500px]'}`}>
|
|
<DialogHeader className='sr-only'>
|
|
<DialogTitle>Profile</DialogTitle>
|
|
<DialogDescription>Description of profile avatar</DialogDescription>
|
|
</DialogHeader>
|
|
{/* <img loading='lazy' alt="avatar" src={process.env.BASE_URL_IMG + avatar} className="w-full h-full shadow-sm rounded-2xl border border-border" /> */}
|
|
{/* 2. Logic Switch for Image Mode */}
|
|
{isPresence ? (
|
|
// MODE A: Presence (Natural Aspect Ratio)
|
|
// We use width=0 height=0 + sizes=100vw to emulate "width: 100%, height: auto"
|
|
<Image
|
|
src={process.env.BASE_URL_IMG + avatar}
|
|
alt="avatar"
|
|
width={0}
|
|
height={0}
|
|
sizes="100vw"
|
|
quality={90}
|
|
className="w-full h-auto object-contain" // h-auto lets it grow naturally
|
|
/>
|
|
) : (
|
|
// MODE B: Default Profile (Fixed Box with Crop)
|
|
// We use fill to cover the specific 500x500 box
|
|
<div className="relative w-full h-full">
|
|
<Image
|
|
src={process.env.BASE_URL_IMG + avatar}
|
|
alt="avatar"
|
|
fill
|
|
className="object-cover shadow-sm border border-border"
|
|
sizes="(max-width: 640px) 100vw, 500px"
|
|
quality={90}
|
|
/>
|
|
</div>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
:
|
|
<div className={cn("relative h-9 w-9 sm:h-10 sm:w-10", className)}>
|
|
<Image
|
|
alt='avatar'
|
|
src='/assets/no-avatar.jpg'
|
|
fill
|
|
sizes={sizes}
|
|
className='shadow-sm rounded-full object-cover border border-border'
|
|
/>
|
|
</div>
|
|
}
|
|
</>
|
|
);
|
|
}
|
|
|
|
|
|
export const Avatar: FC<Props> = ({ avatar, className, sizes = "(max-width: 640px) 36px, 40px" }) => {
|
|
return (
|
|
<>
|
|
{avatar ?
|
|
// <img loading='lazy' alt="avatar" src={process.env.BASE_URL_IMG + avatar} className={cn("h-9 w-9 sm:h-10 sm:w-10 shadow-sm rounded-full object-cover border border-border cursor-pointer hover:scale-110 duration-200", className)} />
|
|
<div className={cn("relative h-9 w-9 sm:h-10 sm:w-10", className)}>
|
|
<Image src={`${process.env.BASE_URL_IMG}${avatar}`} alt="avatar"
|
|
fill // This replaces width/height
|
|
sizes={sizes} // Optimization (explained below)
|
|
className="rounded-full object-cover shadow-sm border border-border cursor-pointer hover:scale-110 duration-200"
|
|
/>
|
|
</div>
|
|
:
|
|
<Image alt='avatar' height={0} width={0} quality={50} src='/assets/no-avatar.jpg' className={cn('h-9 w-9 sm:h-10 sm:w-10 shadow-sm rounded-full object-cover border border-border', className)} />
|
|
}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default ViewAvatarNextImage; |