PostList.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 mt-4 pb-4 overflow-auto">
  45. {props.posts.map((post) => (
  46. <div
  47. className="w-full 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. <div className="flex items-center justify-between mb-2">
  51. <span className="font-medium line-clamp-1">
  52. <a href={`/${post.id}`}>{post.title || "Untitled"}</a>
  53. </span>
  54. <div className="flex items-center gap-1 shrink-0">
  55. {!props.readOnly && post.shared && (
  56. <i className="bi bi-share-fill text-gray-500 dark:text-gray-400 text-sm" />
  57. )}
  58. {post.hasPassword && (
  59. <i className="bi bi-lock-fill text-gray-500 dark:text-gray-400 text-sm" />
  60. )}
  61. </div>
  62. </div>
  63. <p className="mb-2 line-clamp-2">
  64. {post.content || "No content"}
  65. </p>
  66. {!props.readOnly && (
  67. <div className="flex gap-2 mt-auto">
  68. <Button
  69. type="button"
  70. onClick={() => {
  71. onEdit(post.id);
  72. }}
  73. >
  74. Edit
  75. </Button>
  76. <Button
  77. type="button"
  78. variant="danger-outline"
  79. onClick={() => {
  80. onDelete(post.id, post.title);
  81. }}
  82. >
  83. Delete
  84. </Button>
  85. </div>
  86. )}
  87. </div>
  88. ))}
  89. </div>
  90. );
  91. }