-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathadmin.tsx
211 lines (198 loc) · 6.34 KB
/
admin.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { useState, useEffect } from 'react'
import { useApi, useValidSession } from '@hooks'
import { Disclosure } from '@headlessui/react'
import { Button, Nav, Input, Select, Container, Avatar } from '@components'
import { useRouter } from 'next/router'
import * as timeago from 'timeago.js'
import toast from 'react-hot-toast'
import cookie from 'js-cookie'
export default function Admin() {
const [user, setUser] = useState<any>(null)
const [admin, setAdmin] = useState<any>(null)
const [createUserForm, setCreateUserForm] = useState<any>({
name: '',
email: '',
password: '',
admin: false,
show: false,
})
const router = useRouter()
const hydrate = async () => {
setUser(await useApi(`/api/auth`))
setAdmin(await useApi(`/api/admin`))
}
useEffect(() => {
hydrate()
}, [])
if (!user) return null
if (user && !user.admin) {
router.push('/')
toast.error('You are not allowed to view that page')
}
async function clean() {
await toast.promise(useApi('/api/cleanup', 'POST'), {
loading: 'Please wait...',
success: 'Success',
error: 'Error, try again',
})
}
async function loginAs(id, name) {
await toast.promise(useApi('/api/admin', 'POST', { action: 'login_as', id }), {
loading: 'Please wait...',
success: ({ token }) => {
cookie.set('session', token)
router.push('/')
return `Logged in as ${name}`
},
error: 'Error, try again',
})
}
async function makeAdmin(id) {
await toast.promise(useApi('/api/admin', 'POST', { action: 'make_admin', id }), {
loading: 'Please wait...',
success: () => {
hydrate()
return 'Success'
},
error: 'Error, try again',
})
}
async function deleteUser(id) {
await toast.promise(useApi('/api/admin', 'POST', { action: 'delete_user', id }), {
loading: 'Please wait...',
success: () => {
hydrate()
return 'Deleted successfully'
},
error: 'Error, try again',
})
}
async function createUser() {
await toast.promise(useApi('/api/admin', 'POST', { action: 'create_user', id: '0', data: createUserForm }), {
loading: 'Please wait...',
success: () => {
setCreateUserForm({ name: '', email: '', password: '', admin: false, show: false })
hydrate()
return 'Created successfully'
},
error: 'Error, try again',
})
}
return (
<Container>
<Nav active={null} />
<div className='flex'>
<main className='bg-white rounded-lg shadow w-full p-10'>
<div className='mb-8 flex items-center justify-between'>
<b>Admin</b>
</div>
<div className='w-96 mb-8'>
<span>
<b>Cleanup</b>
<p>Cleans up exited or dead Docker containers and removes dangling images</p>
</span>
<Button onClick={() => clean()}>Clean</Button>
</div>
<div className='mb-8'>
<div className='mb-4'>
<div className='flex items-center gap-4'>
<b>Accounts</b>
<Button className='mt-0' onClick={() => setCreateUserForm({ ...createUserForm, show: true })}>
Create
</Button>
</div>
</div>
{createUserForm.show ? (
<div className='w-96 my-4'>
<Input
label='Name'
value={createUserForm.name}
onChange={({ target }) => setCreateUserForm({ ...createUserForm, name: target.value })}
/>
<Input
label='Email'
value={createUserForm.email}
type='email'
onChange={({ target }) => setCreateUserForm({ ...createUserForm, email: target.value })}
/>
<Input
label='Password'
value={createUserForm.password}
type='password'
onChange={({ target }) => setCreateUserForm({ ...createUserForm, password: target.value })}
/>
<Select
label='Admin'
value={createUserForm.admin}
onChange={({ target }) => setCreateUserForm({ ...createUserForm, admin: target.value })}
>
<option value='false'>No</option>
<option value='true'>Yes</option>
</Select>
<Button
disabled={!createUserForm.name || !createUserForm.email || !createUserForm.password}
className='w-full'
onClick={() => createUser()}
>
Add
</Button>
</div>
) : null}
<div>
{admin &&
admin.map((i) => {
return (
<Disclosure>
<Disclosure.Button className='flex items-center gap-4 w-full px-4 py-2 -ml-4 text-sm font-medium text-left rounded-lg'>
<Avatar name={i.name} image={i.avatar} />
{i.name} <span className='opacity-40'>{i.email}</span>{' '}
{i.admin && <span className='bg-gray-200 rounded-full py-1 px-2 text-xs'>Admin</span>}
</Disclosure.Button>
<Disclosure.Panel className='px-4 pt-4 pb-2 text-sm ml-14 w-1/2'>
<div className='grid grid-cols-2 gap-3'>
<p className='opacity-40'>ID</p>
<p className='font-mono'>{i.id}</p>
<p className='opacity-40'>Email</p>
<p>{i.email}</p>
<p className='opacity-40'>Has MFA Enabled</p>
<p>{i.mfa_enabled ? 'Yes' : 'No'}</p>
<p className='opacity-40'>Created</p>
<p>{timeago.format(i.created)}</p>
</div>
<hr className='my-6' />
<div className='grid grid-cols-2 gap-3'>
<p className='opacity-40'>Activity</p>
<p>{i.activity.length}</p>
<p className='opacity-40'>Projects</p>
<p>{i.projects.length}</p>
<p className='opacity-40'>Deployments</p>
<p>{i.deployments.length}</p>
<p className='opacity-40'>Linked Domains</p>
<p>{i.domains.length}</p>
<p className='opacity-40'>Databases</p>
<p>{i.databases.length}</p>
</div>
{i.id !== user.id && (
<div className='my-4 flex items-center gap-2'>
<Button onClick={() => loginAs(i.id, i.name)}>Login as {i.name}</Button>
<Button onClick={() => makeAdmin(i.id)}>Make Admin</Button>
<Button onClick={() => deleteUser(i.id)}>Delete User</Button>
</div>
)}
</Disclosure.Panel>
</Disclosure>
)
})}
</div>
</div>
</main>
</div>
</Container>
)
}
export async function getServerSideProps(context) {
return {
props: {},
...useValidSession(context),
}
}