|
| 1 | +import { Article, ArticleList } from "@/types/Article.type"; |
| 2 | +import styles from "./BestPostBoard.module.css"; |
| 3 | +import Image from "next/image"; |
| 4 | +import formatDate from "../lib/formatDate"; |
| 5 | +import { useDeviceType } from "@/contexts/DeviceTypeContext"; |
| 6 | +import { useState } from "react"; |
| 7 | + |
| 8 | +const PAGE_SIZE = { |
| 9 | + desktop: 3, |
| 10 | + tablet: 2, |
| 11 | + mobile: 1, |
| 12 | +}; |
| 13 | + |
| 14 | +const IMAGE_PLACEHOLDER = "/images/landscape-placeholder.svg"; |
| 15 | + |
| 16 | +export default function BestPostBoard({ articles }: { articles: ArticleList }) { |
| 17 | + const deviceType = useDeviceType(); |
| 18 | + |
| 19 | + return ( |
| 20 | + <div className={styles.Board}> |
| 21 | + <header className={styles.BoardHeader}> |
| 22 | + <h2 className={styles.BoardTitle}>베스트 게시글</h2> |
| 23 | + </header> |
| 24 | + <div className={styles.PostItemList}> |
| 25 | + {articles.list.map((article, i) => { |
| 26 | + if (i < PAGE_SIZE[deviceType]) { |
| 27 | + return <PostItem key={article.id} article={article} />; |
| 28 | + } |
| 29 | + })} |
| 30 | + </div> |
| 31 | + </div> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function PostItem({ article }: { article: Article }) { |
| 36 | + const [imgSrc, setImgSrc] = useState(article.image || IMAGE_PLACEHOLDER); |
| 37 | + |
| 38 | + const createdAt = formatDate(article.createdAt); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className={styles.Item}> |
| 42 | + <div className={styles.badge}> |
| 43 | + <div className={styles.medal}> |
| 44 | + <Image fill src="/images/ic_medal.svg" alt="베스트" /> |
| 45 | + </div> |
| 46 | + <span>Best</span> |
| 47 | + </div> |
| 48 | + <div className={styles.main}> |
| 49 | + <h3 className={styles.title}>{article.title}</h3> |
| 50 | + <div className={styles.preview}> |
| 51 | + <Image |
| 52 | + fill |
| 53 | + src={article.image} |
| 54 | + alt={article.title} |
| 55 | + style={{ |
| 56 | + objectFit: "cover", |
| 57 | + }} |
| 58 | + onError={() => setImgSrc(IMAGE_PLACEHOLDER)} |
| 59 | + /> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + <div className={styles.util}> |
| 63 | + <span>{article.writer.nickname}</span> |
| 64 | + <div className={styles.likeCount}> |
| 65 | + <div className={styles.heart}> |
| 66 | + <Image fill src="/images/ic_heart.svg" alt="베스트" /> |
| 67 | + </div> |
| 68 | + <span>{article.likeCount < 10000 ? article.likeCount : "9999+"}</span> |
| 69 | + </div> |
| 70 | + <span>{createdAt}</span> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + ); |
| 74 | +} |
0 commit comments