2022-11-14 04:28:40 -05:00
|
|
|
'use client';
|
2022-04-20 02:36:56 -04:00
|
|
|
import SettingsGroup from "@components/settings-group"
|
2022-11-09 21:38:05 -05:00
|
|
|
import { Fieldset, useToasts } from "@geist-ui/core/dist"
|
2022-04-20 02:36:56 -04:00
|
|
|
import byteToMB from "@lib/byte-to-mb"
|
2022-11-14 04:28:40 -05:00
|
|
|
import { Post } from "@lib/server/prisma";
|
2022-04-20 02:36:56 -04:00
|
|
|
import Table from "rc-table"
|
2022-04-13 00:14:10 -04:00
|
|
|
import { useEffect, useMemo, useState } from "react"
|
2022-04-20 02:36:56 -04:00
|
|
|
import ActionDropdown from "./action-dropdown"
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const PostTable = ({
|
|
|
|
posts,
|
|
|
|
}: {
|
|
|
|
posts: Post[]
|
|
|
|
}) => {
|
2022-04-13 00:14:10 -04:00
|
|
|
const tablePosts = useMemo(
|
|
|
|
() =>
|
|
|
|
posts?.map((post) => {
|
|
|
|
return {
|
|
|
|
id: post.id,
|
|
|
|
title: post.title,
|
|
|
|
files: post.files?.length || 0,
|
|
|
|
createdAt: `${new Date(
|
|
|
|
post.createdAt
|
|
|
|
).toLocaleDateString()} ${new Date(
|
|
|
|
post.createdAt
|
|
|
|
).toLocaleTimeString()}`,
|
|
|
|
visibility: post.visibility,
|
|
|
|
size: post.files
|
|
|
|
? byteToMB(
|
2022-04-22 01:01:59 -04:00
|
|
|
post.files.reduce((acc, file) => acc + file.html.length, 0)
|
|
|
|
)
|
2022-04-13 00:14:10 -04:00
|
|
|
: 0,
|
|
|
|
actions: ""
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[posts]
|
|
|
|
)
|
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
const deletePost = async (/* id: string */) => {
|
|
|
|
return alert("Not implemented")
|
|
|
|
|
|
|
|
// const confirm = window.confirm("Are you sure you want to delete this post?")
|
|
|
|
// if (!confirm) return
|
|
|
|
// const res = await adminFetcher(`/posts/${id}`, {
|
|
|
|
// method: "DELETE",
|
|
|
|
// })
|
|
|
|
|
|
|
|
// const json = await res.json()
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
// if (res.status === 200) {
|
|
|
|
// setToast({
|
|
|
|
// text: "Post deleted",
|
|
|
|
// type: "success"
|
|
|
|
// })
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
// setPosts((posts) => {
|
|
|
|
// const newPosts = posts?.filter((post) => post.id !== id)
|
|
|
|
// return newPosts
|
|
|
|
// })
|
|
|
|
// } else {
|
|
|
|
// setToast({
|
|
|
|
// text: json.error || "Something went wrong",
|
|
|
|
// type: "error"
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
}
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
const tableColumns = [
|
|
|
|
{
|
|
|
|
title: "Title",
|
|
|
|
dataIndex: "title",
|
|
|
|
key: "title",
|
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Files",
|
|
|
|
dataIndex: "files",
|
|
|
|
key: "files",
|
|
|
|
width: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Created",
|
|
|
|
dataIndex: "createdAt",
|
|
|
|
key: "createdAt",
|
|
|
|
width: 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Visibility",
|
|
|
|
dataIndex: "visibility",
|
|
|
|
key: "visibility",
|
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Size (MB)",
|
|
|
|
dataIndex: "size",
|
|
|
|
key: "size",
|
|
|
|
width: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Actions",
|
|
|
|
dataIndex: "",
|
|
|
|
key: "actions",
|
|
|
|
width: 50,
|
2022-04-22 01:01:59 -04:00
|
|
|
render() {
|
2022-04-13 00:14:10 -04:00
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<ActionDropdown
|
|
|
|
title="Actions"
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: "Delete",
|
2022-04-22 01:01:59 -04:00
|
|
|
onClick: () => deletePost()
|
2022-04-20 02:36:56 -04:00
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<SettingsGroup title="Posts">
|
2022-04-13 00:14:10 -04:00
|
|
|
{!posts && <Fieldset.Subtitle>Loading...</Fieldset.Subtitle>}
|
2022-04-22 01:01:59 -04:00
|
|
|
{posts && (
|
|
|
|
<Fieldset.Subtitle>
|
|
|
|
<h5>{posts.length} posts</h5>
|
|
|
|
</Fieldset.Subtitle>
|
|
|
|
)}
|
2022-04-13 00:14:10 -04:00
|
|
|
{posts && <Table columns={tableColumns} data={tablePosts} />}
|
2022-04-20 02:36:56 -04:00
|
|
|
</SettingsGroup>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PostTable
|