index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import EmptyResult from "components/EmptyResult";
  2. import Pagination from "components/Pagination";
  3. import useDbList from "libs/hooks/useDbList";
  4. import Link from "next/link";
  5. import styles from "./index.module.scss";
  6. import Item from "./Item";
  7. interface LibrayListProps {
  8. type: string;
  9. }
  10. export default function LibrayList({ type }: LibrayListProps) {
  11. const isHistory = type === "History";
  12. const [historyList, historyPages, fetchHistory] = useDbList(
  13. "history",
  14. isHistory ? "historyIsReading" : "historyIsFavorite",
  15. "prev",
  16. isHistory
  17. ? [
  18. [1, 0],
  19. [1, new Date()],
  20. ]
  21. : [[1], [1]]
  22. );
  23. const handlePageChange = (
  24. event: React.ChangeEvent<unknown>,
  25. value: number
  26. ) => {
  27. fetchHistory(value - 1);
  28. };
  29. const refreshList = () => {
  30. fetchHistory(historyPages.page);
  31. };
  32. return (
  33. <>
  34. {historyList.length ? (
  35. <ul className={styles.list}>
  36. {historyList.map((item) => (
  37. <Item
  38. key={item.uri}
  39. data={item}
  40. type={type}
  41. onRemoved={refreshList}
  42. />
  43. ))}
  44. </ul>
  45. ) : (
  46. <EmptyResult
  47. icon={isHistory ? "browseGallery" : "autoStories"}
  48. title={
  49. isHistory
  50. ? "You have no read history."
  51. : "You haven't added any books yet."
  52. }
  53. >
  54. <p>
  55. <Link href="/">EXPLORE NOW</Link>
  56. </p>
  57. </EmptyResult>
  58. )}
  59. {historyPages.total > historyPages.pageSize ? (
  60. <Pagination
  61. className="mt-5"
  62. count={Math.ceil(historyPages.total / historyPages.pageSize)}
  63. page={historyPages.page}
  64. onChange={handlePageChange}
  65. />
  66. ) : null}
  67. </>
  68. );
  69. }