Skip to content

Commit

Permalink
fix : post iamge
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubinquitous committed Mar 5, 2024
1 parent 1e144bd commit bf533cc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/components/atoms/FallbackImgImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react";
import Image, { ImageProps, StaticImageData } from "next/image";
import styled, { css } from "styled-components";
import { StaticImport } from "next/dist/shared/lib/get-img-props";

interface FallbackImgImageProps extends ImageProps {
fallbackSrc: StaticImageData | string;
alt: string;
sizes?: string;
rounded?: boolean;
isShouldHide?: boolean;
}

const FallbackImgImage = ({
src,
fallbackSrc,
alt,
sizes,
isShouldHide,
rounded,
...props
}: FallbackImgImageProps) => {
const [imgSrc, setImgSrc] = React.useState<
StaticImageData | StaticImport | string
>("");

React.useEffect(() => {
setImgSrc(src ?? "/");
}, [src]);

return (
<StyledImage
src={imgSrc}
{...props}
alt={alt}
isshouldhide={isShouldHide}
onError={() => setImgSrc(fallbackSrc)}
isrounded={rounded?.toString()}
/>
);
};

const StyledImage = styled(Image)<{
isshouldhide?: boolean;
isrounded?: string;
}>`
height: auto;
${({ isshouldhide }) =>
isshouldhide &&
css`
display: none;
`}
${({ isrounded }) =>
isrounded === "true" &&
css`
border-radius: 50%;
`}
`;

export default FallbackImgImage;
3 changes: 2 additions & 1 deletion src/templates/post/layouts/list/PostListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PostListProperty } from "../../types";
import PostListItemInformationBar from "./PostListItemInformationBar";
import { EmptyImage } from "../../assets/images";
import { CATEGORY } from "../../constants";
import FallbackImgImage from "@/components/atoms/FallbackImgImage";

const PostListItem = ({
category,
Expand All @@ -26,7 +27,7 @@ const PostListItem = ({
return (
<Layout href={`${ROUTER.POST.LIST}/${id}`}>
{is카테고리가분실물찾기라면 && (
<FallbackImage
<FallbackImgImage
src={lostThingImage || "/"}
alt="분실물 이미지"
fallbackSrc={EmptyImage}
Expand Down

0 comments on commit bf533cc

Please sign in to comment.