Item.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import NovelCover from "components/NovelCover";
  2. import { delHistoryItem, putHistoryItem } from "libs/db/history";
  3. import Link from "next/link";
  4. import styles from "./index.module.scss";
  5. interface ItemProps {
  6. type: string;
  7. data: HistoryItem;
  8. onRemoved: () => void;
  9. }
  10. export default function Item({ type, data, onRemoved }: ItemProps) {
  11. const isHistory = type === "History";
  12. const url = `/novel/${data.uri}`;
  13. const chapterUrl = `/novel/${data.chapterUri}`;
  14. const handleRemoveItem = () => {
  15. const item = { ...data };
  16. if (isHistory) {
  17. item.isReading = 0;
  18. } else {
  19. item.isFavorite = 0;
  20. }
  21. (!item.isReading && !item.isFavorite
  22. ? delHistoryItem(item.uri)
  23. : putHistoryItem(item)
  24. ).then(onRemoved);
  25. };
  26. return (
  27. <li key={data.uri} className={styles.item}>
  28. <NovelCover
  29. className={styles.cover}
  30. component={Link}
  31. href={url}
  32. title={`${data.name} ${data.chapterName}`}
  33. src={data.img}
  34. />
  35. <div className={styles.body}>
  36. <div className={styles.title}>
  37. <Link href={url}>{data.name}</Link>
  38. </div>
  39. <div className={styles.progressTitle}>
  40. <Link href={chapterUrl}>
  41. You have read {data.chapterIndex}/{data.chapterCount}
  42. </Link>
  43. </div>
  44. <div role="progressbar" className={styles.progressbar}>
  45. <div
  46. style={{
  47. width: `${(data.chapterIndex / data.chapterCount) * 100}%`,
  48. }}
  49. ></div>
  50. </div>
  51. <div className={styles.btns}>
  52. <Link className={styles.btn} href={chapterUrl}>
  53. {data.isReading ? "Continue Reading" : "Start Reading"}
  54. </Link>
  55. </div>
  56. </div>
  57. <button
  58. className={styles.close}
  59. onClick={handleRemoveItem}
  60. title={isHistory ? "Remove History" : "Remove Favorite"}
  61. >
  62. <svg>
  63. <use xlinkHref="/icons.svg#close"></use>
  64. </svg>
  65. </button>
  66. </li>
  67. );
  68. }