-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddBucketKeyModal.tsx
More file actions
158 lines (142 loc) · 4.8 KB
/
AddBucketKeyModal.tsx
File metadata and controls
158 lines (142 loc) · 4.8 KB
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
import { useState } from 'react';
import type { AccessKeyPermission, CreateAccessKeyResponse } from '@filone/shared';
import { apiRequest } from '../lib/api.js';
import { expiresAtFromForm } from '../lib/time.js';
import { AccessKeyExpirationFields } from './AccessKeyExpirationFields.js';
import type { ExpirationOption } from './AccessKeyExpirationFields.js';
import { AccessKeyPermissionsFields } from './AccessKeyPermissionsFields.js';
import { Button } from './Button';
import { Input } from './Input/index.js';
import { Label } from './Label/index.js';
import { Dialog, DialogBody, DialogFooter, DialogHeader } from './Dialog';
import { SaveCredentialsModal } from './SaveCredentialsModal.js';
import { useToast } from './Toast/index.js';
// ---------------------------------------------------------------------------
// Props
// ---------------------------------------------------------------------------
export type AddBucketKeyModalProps = {
open: boolean;
onClose: () => void;
bucketName: string;
onKeyAdded: () => void;
};
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
export function AddBucketKeyModal({
open,
onClose,
bucketName,
onKeyAdded,
}: AddBucketKeyModalProps) {
const { toast } = useToast();
const [keyName, setKeyName] = useState('');
const [permissions, setPermissions] = useState<AccessKeyPermission[]>([
'read',
'write',
'list',
'delete',
]);
const [expiration, setExpiration] = useState<ExpirationOption>('never');
const [customDate, setCustomDate] = useState<string | null>(null);
const [creating, setCreating] = useState(false);
const [credentials, setCredentials] = useState<{
accessKeyId: string;
secretAccessKey: string;
} | null>(null);
function reset() {
setKeyName('');
setPermissions(['read', 'write', 'list', 'delete']);
setExpiration('never');
setCustomDate(null);
setCreating(false);
setCredentials(null);
}
function handleClose() {
reset();
onClose();
}
async function handleCreate() {
if (!keyName.trim() || permissions.length === 0) return;
setCreating(true);
try {
const response = await apiRequest<CreateAccessKeyResponse>('/access-keys', {
method: 'POST',
body: JSON.stringify({
keyName: keyName.trim(),
permissions,
bucketScope: 'specific',
buckets: [bucketName],
expiresAt: expiresAtFromForm(expiration, customDate),
}),
});
setCredentials({
accessKeyId: response.accessKeyId,
secretAccessKey: response.secretAccessKey,
});
onKeyAdded();
} catch (err) {
console.error('Failed to create access key:', err);
toast.error(err instanceof Error ? err.message : 'Failed to create access key');
} finally {
setCreating(false);
}
}
if (credentials) {
return (
<SaveCredentialsModal
open={open}
onClose={handleClose}
onDone={handleClose}
credentials={credentials}
/>
);
}
const canSubmit = keyName.trim().length > 0 && permissions.length > 0 && !creating;
return (
<Dialog open={open} onClose={handleClose} size="md">
<DialogHeader onClose={handleClose}>Create API key for {bucketName}</DialogHeader>
<DialogBody>
<div className="flex flex-col gap-4">
{/* Key name */}
<div className="flex flex-col gap-1.5">
<Label>Key name</Label>
<Input
value={keyName}
onChange={(e) => setKeyName(e.target.value)}
placeholder="e.g., Production API Key"
/>
</div>
{/* Permissions */}
<div className="flex flex-col gap-2">
<Label>Permissions</Label>
<AccessKeyPermissionsFields value={permissions} onChange={setPermissions} />
{permissions.length === 0 && (
<p className="text-xs text-red-600">Select at least one permission.</p>
)}
</div>
{/* Expiration */}
<div className="flex flex-col gap-2">
<Label>Expiration</Label>
<AccessKeyExpirationFields
value={expiration}
customDate={customDate}
onChange={setExpiration}
onDateChange={setCustomDate}
/>
</div>
</div>
</DialogBody>
<DialogFooter>
<div className="flex justify-end gap-2">
<Button variant="ghost" onClick={handleClose}>
Cancel
</Button>
<Button variant="default" disabled={!canSubmit} onClick={handleCreate}>
{creating ? 'Creating...' : 'Create & add key'}
</Button>
</div>
</DialogFooter>
</Dialog>
);
}