-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathApiKeyList.tsx
233 lines (220 loc) · 6.7 KB
/
ApiKeyList.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
import * as API from '@api';
import { ArrowPathIcon, PencilSquareIcon, TrashIcon } from '@heroicons/react/20/solid';
import DeleteModal from 'components/DeleteModal';
import ErrorAlert from 'components/ErrorAlert';
import { useAccountContext } from 'context/account';
import cn from 'utils/cn';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import { CopyToClipboardButton } from './chat/message';
import ConfirmModal from './ConfirmModal';
export default function ApiKeyList({ keys, fetchKeys }: { keys: any[]; fetchKeys?: any }) {
const [accountContext]: any = useAccountContext();
const { csrf } = accountContext as any;
const router = useRouter();
const { resourceSlug } = router.query;
const [deletingModel, setDeletingModel] = useState(null);
const [regeneratingKey, setRegeneratingKey] = useState(null);
const [deletingMap, setDeletingMap] = useState({});
const [error, setError] = useState(null);
const [deleteOpen, setDeleteOpen] = useState(false);
const [regenerateOpen, setRegenerateOpen] = useState(false);
const handleCopyClick = async dataToCopy => {
try {
await navigator.clipboard.writeText(dataToCopy);
toast.success('Copied to clipboard');
} catch {
/* ignored for now */
}
};
async function deleteKey(keyId, ownerId) {
setDeleteOpen(false);
API.deleteKey(
{
_csrf: csrf,
ownerId,
keyId
},
() => {
fetchKeys();
toast('Deleted Key');
},
() => {
toast.error('Error deleting Key');
},
router
);
}
async function regenerateKey(keyId, ownerId) {
//TODO: implement regeneration of key
API.incrementKeyVersion(
{
_csrf: csrf,
ownerId,
keyId
},
() => {
fetchKeys();
toast('Regenerated Key');
setRegenerateOpen(false);
},
() => {
toast.error('Error Regenerating Key');
setRegenerateOpen(false);
},
router
);
}
useEffect(() => {
let timeout;
if (!deleteOpen) {
timeout = setTimeout(() => {
setDeletingModel(null);
}, 500);
}
return () => clearTimeout(timeout);
}, [deleteOpen]);
return (
<>
<DeleteModal
open={deleteOpen}
confirmFunction={() => {
deleteKey(deletingModel?._id, deletingModel?.ownerId);
fetchKeys();
}}
cancelFunction={() => {
setDeleteOpen(false);
}}
title={'Delete Key'}
message={`Are you sure you want to delete the key "${deletingModel?.name}". This action cannot be undone and this key will cease to function.`}
/>
<ConfirmModal
open={regenerateOpen}
setOpen={setRegenerateOpen}
confirmFunction={() => {
regenerateKey(regeneratingKey?._id, regeneratingKey?.ownerId);
fetchKeys();
}}
cancelFunction={() => {
setRegenerateOpen(false);
}}
title={'Regenerate Key'}
message={`Are you sure you want to regenerate the key "${regeneratingKey?.name}". This action cannot be undone and old versions of this key will cease to function.`}
/>
{error && (
<div className='my-4'>
<ErrorAlert error={error} />
</div>
)}
<div className='rounded-lg overflow-hidden shadow overflow-x-auto'>
<table className='min-w-full divide-y divide-gray-200'>
<thead className='bg-gray-50 dark:bg-gray-700'>
<tr>
<th
scope='col'
className='w-min px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:text-gray-50'
>
Name
</th>
<th
scope='col'
className='w-min px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:text-gray-50'
>
Description
</th>
<th
scope='col'
className='w-min px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:text-gray-50'
>
Token
</th>
<th
scope='col'
className='w-min px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider dark:text-gray-50'
>
Expiry
</th>
{/* Add more columns as necessary */}
<th
scope='col'
className='w-min px-6 py-3 text-xs font-medium text-gray-500 uppercase tracking-wider dark:text-gray-50'
>
Actions
</th>
</tr>
</thead>
<tbody className='bg-white divide-y divide-gray-200 dark:bg-gray-700'>
{keys.map(key => {
let keyDate: string;
if (key?.expirationDate === null) {
keyDate = 'Never';
} else {
keyDate = new Date(key?.expirationDate).toDateString();
}
return (
<tr
key={key._id}
className={cn(
'transition-all opacity-1 duration-700',
deletingMap[key._id]
? 'bg-red-400'
: 'hover:bg-gray-50 dark:hover:bg-gray-500 dark:text-gray-50'
)}
style={{ borderColor: deletingMap[key._id] ? 'red' : '' }}
>
<td className={'px-6 py-4 whitespace-nowrap'} onClick={() => router.push(``)}>
<div className='text-sm text-gray-900 dark:text-white'>{key.name}</div>
</td>
<td className='px-6 py-4 text-wrap' onClick={() => router.push(``)}>
<div className='text-sm text-gray-900 dark:text-white'>
{key?.description || '-'}
</div>
</td>
<td
className='px-6 py-4 whitespace-nowrap'
onClick={() => handleCopyClick(key?.token)}
>
<div className='text-sm max-w-36 text-gray-900 dark:text-white truncate hover:text-blue-600 hover:underline cursor-pointer'>
{key?.token}
</div>
</td>
<td className='px-6 py-4 whitespace-nowrap' onClick={() => router.push(``)}>
<div className='text-sm max-w-36 text-gray-900 dark:text-white truncate'>
{keyDate}
</div>
</td>
<td className='flex flex-row justify-center items-center w-full gap-5 px-6 py-4 whitespace-nowrap text-right text-sm font-medium'>
<button
onClick={() => {
setRegeneratingKey(key);
setRegenerateOpen(true);
}}
className='text-gray-900 hover:text-gray-400'
>
<ArrowPathIcon className='h-5 w-5' aria-hidden='true' />
</button>
<CopyToClipboardButton dataToCopy={key?.token} />
<>
<button
onClick={() => {
setDeletingModel(key);
setDeleteOpen(true);
}}
className='text-red-500 hover:text-red-700'
data-tooltip-target='delete-tooltip'
>
<TrashIcon className='h-5 w-5' aria-hidden='true' />
</button>
</>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</>
);
}