PostList.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Button from "../components/form/Button.tsx";
  2. interface PostListProps {
  3. posts: {
  4. id: string;
  5. title: string;
  6. content: string;
  7. shared: boolean;
  8. hasPassword?: boolean;
  9. }[];
  10. readOnly?: boolean;
  11. }
  12. export default function PostList(props: PostListProps) {
  13. const onEdit = (id: string) => {
  14. location.href = `/${id}`;
  15. };
  16. const onDelete = (id: string, title: string) => {
  17. globalThis.$modal?.show(
  18. "Confirm delete",
  19. `Are you sure you want to delete ${title}?`,
  20. [
  21. {
  22. text: "Confirm",
  23. onClick: async () => {
  24. const resp = await fetch("/api/post", {
  25. method: "DELETE",
  26. headers: { "Content-Type": "application/json" },
  27. body: JSON.stringify({
  28. id,
  29. }),
  30. });
  31. const respJson = await resp.json();
  32. if (respJson.success) {
  33. location.reload();
  34. }
  35. },
  36. },
  37. {
  38. text: "Cancel",
  39. },
  40. ],
  41. );
  42. };
  43. return (
  44. <div className="w-full grid grid-cols-4 gap-4 mt-4 pb-4 overflow-auto">
  45. {props.posts.map((post) => (
  46. <div
  47. className="w-full min-w-[180px] border border-gray-200 dark:border-gray-700 dark:bg-gray-800 rounded box-border relative p-4 text-base flex flex-col"
  48. key={post.id}
  49. >
  50. <span className="font-medium mb-2">
  51. {post.hasPassword && <i className="bi bi-lock-fill text-sm mr-1" />}
  52. <a href={`/${post.id}`}>{post.title || "Untitled"}</a>
  53. </span>
  54. {!props.readOnly && (
  55. <div className="absolute right-4 top-3 text-2xl z-[1]">
  56. <i
  57. className="bi bi-x cursor-pointer"
  58. onClick={() => {
  59. onDelete(post.id, post.title);
  60. }}
  61. />
  62. </div>
  63. )}
  64. <span className="overflow-hidden text-ellipsis whitespace-nowrap mb-2">
  65. {post.content || "No content"}
  66. </span>
  67. {!props.readOnly && (
  68. <Button
  69. type="button"
  70. onClick={() => {
  71. onEdit(post.id);
  72. }}
  73. >
  74. Edit
  75. </Button>
  76. )}
  77. </div>
  78. ))}
  79. </div>
  80. );
  81. }