index.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Pagination from "components/Pagination";
  2. import useDbList from "libs/hooks/useDbList";
  3. import styles from "./index.module.scss";
  4. import Item from "./Item";
  5. interface LibrayListProps {
  6. type: string;
  7. }
  8. export default function LibrayList({ type }: LibrayListProps) {
  9. const isHistory = type === "History";
  10. const [historyList, historyPages, fetchHistory] = useDbList(
  11. "history",
  12. isHistory ? "historyIsReading" : "historyIsFavorite",
  13. "prev",
  14. isHistory
  15. ? [
  16. [1, 0],
  17. [1, new Date()],
  18. ]
  19. : [[1], [1]]
  20. );
  21. const handlePageChange = (
  22. event: React.ChangeEvent<unknown>,
  23. value: number
  24. ) => {
  25. fetchHistory(value - 1);
  26. };
  27. const refreshList = () => {
  28. fetchHistory(historyPages.page);
  29. };
  30. return (
  31. <>
  32. <ul className={styles.list}>
  33. {historyList.map((item) => (
  34. <Item
  35. key={item.uri}
  36. data={item}
  37. type={type}
  38. onRemoved={refreshList}
  39. />
  40. ))}
  41. </ul>
  42. {historyPages.total > historyPages.pageSize ? (
  43. <Pagination
  44. className="mt-5"
  45. count={Math.ceil(historyPages.total / historyPages.pageSize)}
  46. page={historyPages.page}
  47. onChange={handlePageChange}
  48. />
  49. ) : null}
  50. </>
  51. );
  52. }