Img extends img

This commit is contained in:
DrMint 2022-08-14 02:04:54 +02:00
parent 93645a2f53
commit eaef34a766
12 changed files with 45 additions and 42 deletions

View File

@ -1,47 +1,40 @@
import { ImageProps } from "next/image";
import { MouseEventHandler } from "react";
import { DetailedHTMLProps, ImgHTMLAttributes } from "react";
import { UploadImageFragment } from "graphql/generated";
import { getAssetURL, getImgSizesByQuality, ImageQuality } from "helpers/img";
import { getAssetURL, ImageQuality } from "helpers/img";
/*
*
* CONSTANTS
*/
interface Props {
className?: string;
image?: UploadImageFragment | string;
interface Props
extends Omit<
DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>,
"src"
> {
src: UploadImageFragment | string;
quality?: ImageQuality;
alt?: ImageProps["alt"];
onClick?: MouseEventHandler<HTMLImageElement>;
}
// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
export const Img = ({
className,
image,
src: rawSrc,
quality = ImageQuality.Small,
alt,
onClick,
loading = "lazy",
...otherProps
}: Props): JSX.Element => {
if (typeof image === "string") {
return (
<img className={className} src={image} alt={alt ?? ""} loading="lazy" />
);
} else if (image?.width && image.height) {
const imgSize = getImgSizesByQuality(image.width, image.height, quality);
return (
<img
className={className}
src={getAssetURL(image.url, quality)}
alt={alt ?? image.alternativeText ?? ""}
width={imgSize.width}
height={imgSize.height}
loading="lazy"
onClick={onClick}
/>
);
}
return <></>;
const src =
typeof rawSrc === "string" ? rawSrc : getAssetURL(rawSrc.url, quality);
return (
<img
className={className}
src={src}
alt={alt}
loading={loading}
{...otherProps}
/>
);
};

View File

@ -235,7 +235,7 @@ export const ScanSet = ({
openLightBox(images, index);
}}
>
<Img image={page.attributes} quality={ImageQuality.Small} />
<Img src={page.attributes} quality={ImageQuality.Small} />
</div>
))}
</div>

View File

@ -180,7 +180,7 @@ export const ScanSetCover = ({
openLightBox(imgs, index);
}}
>
<Img image={image} quality={ImageQuality.Small} />
<Img src={image} quality={ImageQuality.Small} />
</div>
))}
</div>

View File

@ -90,7 +90,7 @@ export const LightBox = ({
<Img
className="max-h-full min-h-fit [grid-area:image]"
image={images[index]}
src={images[index]}
/>
<div className="[grid-area:right]">

View File

@ -284,7 +284,7 @@ export const Markdawn = ({
}}
>
<Img
image={
src={
compProps.src.startsWith("/uploads/")
? getAssetURL(compProps.src, ImageQuality.Small)
: compProps.src

View File

@ -148,7 +148,7 @@ export const PreviewCard = ({
{thumbnail && (
<Img
className="opacity-30"
image={thumbnail}
src={thumbnail}
quality={ImageQuality.Medium}
/>
)}
@ -164,7 +164,7 @@ export const PreviewCard = ({
{thumbnail && (
<Img
className="opacity-70"
image={thumbnail}
src={thumbnail}
quality={ImageQuality.Medium}
/>
)}
@ -193,7 +193,7 @@ export const PreviewCard = ({
),
cIf(thumbnailForceAspectRatio, "h-full w-full object-cover")
)}
image={thumbnail}
src={thumbnail}
quality={ImageQuality.Medium}
/>
{stackNumber > 0 && (

View File

@ -41,7 +41,7 @@ export const PreviewLine = ({
<div className="aspect-[3/2] h-full">
<Img
className="h-full object-cover"
image={thumbnail}
src={thumbnail}
quality={ImageQuality.Medium}
/>
</div>

View File

@ -29,7 +29,7 @@ export const RecorderChip = ({ recorder, langui }: Props): JSX.Element => (
{recorder.avatar?.data?.attributes && (
<Img
className="w-20 rounded-full border-4 border-mid"
image={recorder.avatar.data.attributes}
src={recorder.avatar.data.attributes}
quality={ImageQuality.Small}
/>
)}

View File

@ -53,7 +53,7 @@ export const ThumbnailHeader = ({
{thumbnail ? (
<Img
className="cursor-pointer rounded-xl"
image={thumbnail}
src={thumbnail}
quality={ImageQuality.Medium}
onClick={() => {
openLightBox([getAssetURL(thumbnail.url, ImageQuality.Large)]);

View File

@ -24,3 +24,13 @@ export const randomInt = (min: number, max: number): number =>
export const isInteger = (value: string): boolean =>
/^[+-]?[0-9]+$/u.test(value);
export const clamp = (
value: number,
minValue: number,
maxValue: number
): number => {
if (value > maxValue) return maxValue;
if (value < minValue) return minValue;
return value;
};

View File

@ -172,7 +172,7 @@ const LibrarySlug = ({
>
{item.thumbnail?.data?.attributes ? (
<Img
image={item.thumbnail.data.attributes}
src={item.thumbnail.data.attributes}
quality={ImageQuality.Large}
className="h-full w-full object-contain"
/>
@ -273,7 +273,7 @@ const LibrarySlug = ({
<Img
className="h-full w-full rounded-lg
bg-light object-cover drop-shadow-shade-md"
image={galleryItem.attributes}
src={galleryItem.attributes}
/>
</div>
</Fragment>

View File

@ -112,7 +112,7 @@ const WikiPage = ({
>
{page.thumbnail?.data?.attributes && (
<Img
image={page.thumbnail.data.attributes}
src={page.thumbnail.data.attributes}
quality={ImageQuality.Medium}
className="w-full cursor-pointer"
onClick={() => {