2022-03-28 15:13:22 -04:00
|
|
|
import { Text, Fieldset, Spacer, Link } from '@geist-ui/core'
|
|
|
|
import { Post, User } from '@lib/types'
|
|
|
|
import Cookies from 'js-cookie'
|
2022-03-29 03:11:02 -04:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-03-28 15:13:22 -04:00
|
|
|
import useSWR from 'swr'
|
|
|
|
import styles from './admin.module.css'
|
2022-03-29 03:11:02 -04:00
|
|
|
import PostModal from './post-modal-link'
|
|
|
|
|
|
|
|
export const adminFetcher = (url: string) => fetch(url, {
|
2022-03-28 15:13:22 -04:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": `Bearer ${Cookies.get('drift-token')}`,
|
|
|
|
}
|
|
|
|
}).then(res => res.json())
|
|
|
|
|
|
|
|
const Admin = () => {
|
2022-03-29 03:11:02 -04:00
|
|
|
const { data: posts, error: postsError } = useSWR<Post[]>('/server-api/admin/posts', adminFetcher)
|
|
|
|
const { data: users, error: usersError } = useSWR<User[]>('/server-api/admin/users', adminFetcher)
|
|
|
|
const [postSizes, setPostSizes] = useState<{ [key: string]: number }>({})
|
|
|
|
const byteToMB = (bytes: number) => Math.round(bytes / 1024 / 1024 * 100) / 100
|
|
|
|
useEffect(() => {
|
|
|
|
if (posts) {
|
|
|
|
// sum the sizes of each file per post
|
|
|
|
const sizes = posts.reduce((acc, post) => {
|
2022-04-01 19:26:42 -04:00
|
|
|
const size = post.files?.reduce((acc, file) => acc + file.html.length, 0) || 0
|
2022-03-29 03:11:02 -04:00
|
|
|
return { ...acc, [post.id]: byteToMB(size) }
|
|
|
|
}, {})
|
|
|
|
setPostSizes(sizes)
|
|
|
|
}
|
|
|
|
}, [posts])
|
|
|
|
|
2022-03-28 15:13:22 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.adminWrapper}>
|
|
|
|
<Text h2>Administration</Text>
|
|
|
|
<Fieldset>
|
|
|
|
<Fieldset.Title>Users</Fieldset.Title>
|
|
|
|
{users && <Fieldset.Subtitle>{users.length} users</Fieldset.Subtitle>}
|
|
|
|
{!users && <Fieldset.Subtitle>Loading...</Fieldset.Subtitle>}
|
2022-03-29 03:11:02 -04:00
|
|
|
{usersError && <Fieldset.Subtitle>An error occured</Fieldset.Subtitle>}
|
2022-03-28 15:13:22 -04:00
|
|
|
{users && <table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Username</th>
|
|
|
|
<th>Posts</th>
|
|
|
|
<th>Created</th>
|
|
|
|
<th>Role</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{users?.map(user => (
|
|
|
|
<tr key={user.id}>
|
|
|
|
<td>{user.username}</td>
|
|
|
|
<td>{user.posts?.length}</td>
|
|
|
|
<td>{new Date(user.createdAt).toLocaleDateString()} {new Date(user.createdAt).toLocaleTimeString()}</td>
|
|
|
|
<td>{user.role}</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>}
|
|
|
|
|
|
|
|
</Fieldset>
|
|
|
|
<Spacer height={1} />
|
|
|
|
<Fieldset>
|
|
|
|
<Fieldset.Title>Posts</Fieldset.Title>
|
|
|
|
{posts && <Fieldset.Subtitle>{posts.length} posts</Fieldset.Subtitle>}
|
|
|
|
{!posts && <Fieldset.Subtitle>Loading...</Fieldset.Subtitle>}
|
2022-03-29 03:11:02 -04:00
|
|
|
{postsError && <Fieldset.Subtitle>An error occured</Fieldset.Subtitle>}
|
2022-03-28 15:13:22 -04:00
|
|
|
{posts && <table>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Title</th>
|
|
|
|
<th>Visibility</th>
|
|
|
|
<th>Created</th>
|
|
|
|
<th>Author</th>
|
2022-03-29 03:11:02 -04:00
|
|
|
<th>Size</th>
|
2022-03-28 15:13:22 -04:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-03-29 03:19:33 -04:00
|
|
|
{posts?.map((post) => (
|
2022-03-28 15:13:22 -04:00
|
|
|
<tr key={post.id}>
|
2022-03-29 03:11:02 -04:00
|
|
|
<td><PostModal id={post.id} /></td>
|
2022-03-28 15:13:22 -04:00
|
|
|
<td>{post.visibility}</td>
|
|
|
|
<td>{new Date(post.createdAt).toLocaleDateString()} {new Date(post.createdAt).toLocaleTimeString()}</td>
|
2022-03-29 03:11:02 -04:00
|
|
|
<td>{post.users?.length ? post.users[0].username : <i>Deleted</i>}</td>
|
|
|
|
<td>{postSizes[post.id] ? `${postSizes[post.id]} MB` : ''}</td>
|
2022-03-28 15:13:22 -04:00
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>}
|
2022-03-29 03:11:02 -04:00
|
|
|
{Object.keys(postSizes).length && <div style={{ float: 'right' }}>
|
|
|
|
<Text>Total size: {Object.values(postSizes).reduce((prev, curr) => prev + curr)} MB</Text>
|
|
|
|
</div>}
|
2022-03-28 15:13:22 -04:00
|
|
|
</Fieldset>
|
|
|
|
|
|
|
|
</div >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Admin
|