Categories.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React from "react";
  2. import { Button } from "react-bootstrap";
  3. import { Category, Thread } from "../../types";
  4. const Categories = (props: {
  5. categories: Category[];
  6. threads: Thread[];
  7. selectCategory: (id: number) => void;
  8. latest: number[];
  9. }) => {
  10. const categories = props.categories.map((c) => {
  11. const threads: Thread[] = props.threads.filter(
  12. (t) => t.categoryId === c.id
  13. );
  14. return { ...c, threads };
  15. });
  16. const getColor = (id: number) => {
  17. return props.latest.find((i) => i === id) ? "bg-secondary" : "";
  18. };
  19. return (
  20. <div className="overflow-auto" style={{ height: "90%" }}>
  21. <div className="box">
  22. {categories
  23. .filter((c) => c.threads.length)
  24. .sort((a, b) => (a.title < b.title ? -1 : a.title > b.title ? 1 : 0))
  25. .map((c) => (
  26. <Button
  27. variant="dark"
  28. className={`btn-sm m-1 ${getColor(c.id)}`}
  29. key={c.id}
  30. onClick={() => props.selectCategory(c.id)}
  31. >
  32. {c.title} ({c.threads.length})
  33. </Button>
  34. ))}
  35. </div>
  36. </div>
  37. );
  38. };
  39. export default Categories;