File size: 6,678 Bytes
eeb9404 | 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 | 'use client';
import React, { useState, useEffect } from 'react';
import { Secret } from '@/lib/vfs/types';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Loader2, AlertCircle, Eye, EyeOff, Info } from 'lucide-react';
interface SecretEditorProps {
secret: Secret | null;
isOpen: boolean;
onClose: () => void;
onSave: (data: { name: string; value?: string; description?: string }) => Promise<void>;
}
export function SecretEditor({
secret,
isOpen,
onClose,
onSave,
}: SecretEditorProps) {
const [name, setName] = useState(secret?.name || '');
const [value, setValue] = useState('');
const [description, setDescription] = useState(secret?.description || '');
const [showValue, setShowValue] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (isOpen) {
setName(secret?.name || '');
setValue('');
setDescription(secret?.description || '');
setShowValue(false);
setError(null);
}
}, [secret, isOpen]);
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// Convert to SCREAMING_SNAKE_CASE
const newValue = e.target.value
.toUpperCase()
.replace(/[^A-Z0-9_]/g, '')
.replace(/^[0-9]+/, ''); // Remove leading numbers
setName(newValue);
};
const handleSave = async () => {
setError(null);
// Basic validation
if (!name.trim()) {
setError('Secret name is required');
return;
}
// Validate SCREAMING_SNAKE_CASE
if (!/^[A-Z][A-Z0-9_]*$/.test(name)) {
setError('Name must be SCREAMING_SNAKE_CASE (uppercase letters, numbers, underscores; must start with letter)');
return;
}
// Value required for new secrets
if (!secret && !value.trim()) {
setError('Secret value is required');
return;
}
setSaving(true);
try {
await onSave({
name: name.trim(),
value: value.trim() || undefined,
description: description.trim() || undefined,
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to save secret');
} finally {
setSaving(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>
{secret ? 'Edit Secret' : 'Create Secret'}
</DialogTitle>
<DialogDescription>
Store sensitive values like API keys securely. Edge functions can access them via secrets.get('{name || 'NAME'}').
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* Name */}
<div className="space-y-2">
<Label htmlFor="name">Secret Name</Label>
<Input
id="name"
value={name}
onChange={handleNameChange}
placeholder="STRIPE_API_KEY"
disabled={!!secret}
className="font-mono"
/>
<p className="text-xs text-muted-foreground">
Use SCREAMING_SNAKE_CASE (e.g., API_KEY, SENDGRID_TOKEN)
</p>
</div>
{/* Value */}
<div className="space-y-2">
<Label htmlFor="value">
{secret ? 'New Value (leave empty to keep current)' : 'Secret Value'}
</Label>
<div className="relative">
<Input
id="value"
type={showValue ? 'text' : 'password'}
value={value}
onChange={e => setValue(e.target.value)}
placeholder={secret ? 'Enter new value to change...' : 'sk_live_...'}
className="pr-10 font-mono"
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-1 top-1/2 -translate-y-1/2 h-7 w-7 p-0"
onClick={() => setShowValue(!showValue)}
>
{showValue ? (
<EyeOff className="h-4 w-4" />
) : (
<Eye className="h-4 w-4" />
)}
</Button>
</div>
<p className="text-xs text-muted-foreground">
{secret
? 'Leave empty to keep the existing value'
: 'This value will be encrypted and never displayed again'}
</p>
</div>
{/* Description */}
<div className="space-y-2">
<Label htmlFor="description">Description (optional)</Label>
<Input
id="description"
value={description}
onChange={e => setDescription(e.target.value)}
placeholder="Production Stripe API key"
/>
</div>
{/* Usage Reference */}
<div className="bg-muted/30 border rounded-lg p-4 space-y-2">
<div className="flex items-center gap-2 text-sm font-medium">
<Info className="h-4 w-4" />
Usage in Edge Functions
</div>
<pre className="text-xs font-mono bg-background p-2 rounded overflow-x-auto">
{`// Get secret value
const apiKey = secrets.get('${name || 'STRIPE_API_KEY'}');
// Check if secret exists
if (secrets.has('${name || 'STRIPE_API_KEY'}')) {
// Use the secret
}
// List all available secrets
const allSecrets = secrets.list(); // ['${name || 'STRIPE_API_KEY'}', ...]`}
</pre>
</div>
{/* Error */}
{error && (
<div className="flex items-center gap-2 text-sm text-destructive bg-destructive/10 p-3 rounded-lg">
<AlertCircle className="h-4 w-4" />
{error}
</div>
)}
</div>
{/* Footer */}
<div className="flex items-center justify-end gap-2 pt-4 border-t">
<Button variant="outline" onClick={onClose} disabled={saving}>
Cancel
</Button>
<Button onClick={handleSave} disabled={saving}>
{saving ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
Saving...
</>
) : (
secret ? 'Save Changes' : 'Create Secret'
)}
</Button>
</div>
</DialogContent>
</Dialog>
);
}
|