AlbumListLayout.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use client';
  2. import './style.scss';
  3. import Link from 'next/link';
  4. import { useSearchParams } from 'next/navigation';
  5. import Image, { ImageProps } from 'next/image';
  6. import { useState } from 'react';
  7. import Post from '@/types/forum/post';
  8. import { BoardListMeta } from '@/types/forum/boardMeta';
  9. import { formatDate, isHotPost, isNewPost } from '@/lib/utils/client';
  10. import { faThumbsUp, faEye, faClock } from '@fortawesome/free-regular-svg-icons';
  11. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  12. import NoticeListLayout from './NoticeListLayout';
  13. interface Props {
  14. boardListMeta: BoardListMeta;
  15. speaker: Post[];
  16. notice: Post[];
  17. list: Post[];
  18. startIndex: number;
  19. onChange: (_: number|undefined) => void;
  20. }
  21. function ImageWithFallback({
  22. src,
  23. fallbackSrc = '/resources/no-image.png',
  24. alt,
  25. ...option
  26. }: ImageProps & { fallbackSrc?: string }) {
  27. const [imgSrc, setImgSrc] = useState(src);
  28. return (
  29. <Image
  30. {...option}
  31. src={imgSrc}
  32. alt={alt}
  33. onError={() => setImgSrc(fallbackSrc)}
  34. />
  35. );
  36. }
  37. export default function AlbumListLayout({boardListMeta, speaker, notice, list, onChange}: Props)
  38. {
  39. const searchParams = useSearchParams();
  40. return (
  41. <>
  42. {!boardListMeta.exceptSpeaker && !boardListMeta.exceptNotice ? (
  43. <section className='notice-list-layout' aria-label='공지사항'>
  44. <article>
  45. <ul>
  46. <li>번호</li>
  47. <li>제목</li>
  48. <li>작성자</li>
  49. <li>작성일</li>
  50. <li>조회수</li>
  51. </ul>
  52. </article>
  53. <article>
  54. {/* 전체 공지 */}
  55. <NoticeListLayout isEnabled={!boardListMeta.exceptSpeaker} list={speaker} layout={boardListMeta.layout} />
  56. {/* 일반 공지 */}
  57. <NoticeListLayout isEnabled={!boardListMeta.exceptNotice} list={notice} layout={boardListMeta.layout} />
  58. </article>
  59. </section>
  60. ) : (
  61. <br hidden/>
  62. )}
  63. <div className={list.length > 0 ? 'album-list-layout' : 'grid p-10 text-center border-b mb-3'} aria-label='사진/영산 게시판'>
  64. {list.length > 0 ? (
  65. list.map(row => {
  66. const query = Object.fromEntries(searchParams.entries());
  67. const href = `/post/${row.id}${window.location.search}`;
  68. const isNew = isNewPost(boardListMeta.isNewIcon, row);
  69. const isHot = isHotPost(boardListMeta.isHotIcon, row);
  70. const createdAt = formatDate(row.createdAt);
  71. return (
  72. <div key={row.id}>
  73. <figure>
  74. <article>
  75. <Link href={href}>
  76. {row.thumbnail ? (
  77. <ImageWithFallback src={row.thumbnail} alt={row.subject} fill style={{ objectFit: 'cover' }} loading="lazy" />
  78. ) : (
  79. <Image src='/resources/no-image.png' alt={row.subject} loading='lazy' fill />
  80. )}
  81. </Link>
  82. </article>
  83. <dl>
  84. <dt>
  85. {row.boardPrefix && row.boardPrefixID && (
  86. <button type="button" onClick={() => onChange(row.boardPrefixID || undefined)}>
  87. [{row.boardPrefix.name}]
  88. </button>
  89. )}
  90. <Link href={{
  91. pathname: '/post/' + row.id,
  92. query: {
  93. ...query
  94. }
  95. }}
  96. >
  97. <em>{row.subject} {row.comments > 0 && (<span>[{row.comments}]</span>)}</em>
  98. {isNew && <span><img src='/resources/new.gif' alt='NEW'/></span>}
  99. {isHot && <span><img src='/resources/hot.gif' alt='HOT'/></span>}
  100. </Link>
  101. </dt>
  102. <dd>{row.name}</dd>
  103. </dl>
  104. <figcaption>
  105. <ul>
  106. <li><FontAwesomeIcon icon={faThumbsUp} /> {row.likes}</li>
  107. <li><FontAwesomeIcon icon={faEye} /> {row.views}</li>
  108. <li><FontAwesomeIcon icon={faClock} /> {createdAt}</li>
  109. </ul>
  110. </figcaption>
  111. </figure>
  112. </div>
  113. );
  114. })
  115. ) : (
  116. <p>등록된 게시글이 없습니다.</p>
  117. )}
  118. </div>
  119. </>
  120. );
  121. }