AlbumListLayout.tsx 4.0 KB

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