|
| 1 | +import { useState } from 'react'; |
| 2 | +import { apiFetch } from '@/lib/api'; |
| 3 | +import { Button } from '@/components/ui/button'; |
| 4 | +import { Input } from '@/components/ui/input'; |
| 5 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; |
| 6 | +import { Label } from '@/components/ui/label'; |
| 7 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; |
| 8 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; |
| 9 | +import { Copy, Mail, RefreshCw, Sparkles } from 'lucide-react'; |
| 10 | +import toast from 'react-hot-toast'; |
| 11 | + |
| 12 | +interface GeneratedEmail { |
| 13 | + email: string; |
| 14 | + expires: number; |
| 15 | +} |
| 16 | + |
| 17 | +export function TempMailGenerator() { |
| 18 | + const [generatedEmail, setGeneratedEmail] = useState<string>(''); |
| 19 | + const [customLocal, setCustomLocal] = useState(''); |
| 20 | + const [selectedDomain, setSelectedDomain] = useState('gxtend.vip'); |
| 21 | + const [isGenerating, setIsGenerating] = useState(false); |
| 22 | + const [isCreating, setIsCreating] = useState(false); |
| 23 | + |
| 24 | + const handleRandomGenerate = async () => { |
| 25 | + setIsGenerating(true); |
| 26 | + try { |
| 27 | + const data = await apiFetch<GeneratedEmail>('/api/generate?length=8&domainIndex=0'); |
| 28 | + if (data?.email) { |
| 29 | + setGeneratedEmail(data.email); |
| 30 | + toast.success('Random email generated!'); |
| 31 | + } |
| 32 | + } catch (error) { |
| 33 | + console.error('Failed to generate email', error); |
| 34 | + toast.error('Failed to generate email'); |
| 35 | + } finally { |
| 36 | + setIsGenerating(false); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const handleCustomCreate = async () => { |
| 41 | + if (!customLocal.trim()) { |
| 42 | + toast.error('Please enter a local part'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (!/^[a-z0-9._-]{1,64}$/i.test(customLocal.trim())) { |
| 47 | + toast.error('Invalid format. Use only letters, numbers, dots, hyphens, and underscores'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + setIsCreating(true); |
| 52 | + try { |
| 53 | + const data = await apiFetch<GeneratedEmail>('/api/create', { |
| 54 | + method: 'POST', |
| 55 | + body: JSON.stringify({ |
| 56 | + local: customLocal.trim(), |
| 57 | + domainIndex: 0 |
| 58 | + }) |
| 59 | + }); |
| 60 | + |
| 61 | + if (data?.email) { |
| 62 | + setGeneratedEmail(data.email); |
| 63 | + setCustomLocal(''); |
| 64 | + toast.success('Custom email created!'); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + console.error('Failed to create email', error); |
| 68 | + toast.error('Failed to create email'); |
| 69 | + } finally { |
| 70 | + setIsCreating(false); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const handleCopy = () => { |
| 75 | + if (generatedEmail) { |
| 76 | + navigator.clipboard.writeText(generatedEmail); |
| 77 | + toast.success('Email copied to clipboard!'); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <Card> |
| 83 | + <CardHeader> |
| 84 | + <CardTitle className="flex items-center gap-2"> |
| 85 | + <Mail className="h-5 w-5" /> |
| 86 | + Temp Mail Generator |
| 87 | + </CardTitle> |
| 88 | + <CardDescription> |
| 89 | + Generate temporary email addresses for testing or privacy |
| 90 | + </CardDescription> |
| 91 | + </CardHeader> |
| 92 | + <CardContent className="space-y-4"> |
| 93 | + <Tabs defaultValue="random" className="w-full"> |
| 94 | + <TabsList className="grid w-full grid-cols-2"> |
| 95 | + <TabsTrigger value="random">Random</TabsTrigger> |
| 96 | + <TabsTrigger value="custom">Custom</TabsTrigger> |
| 97 | + </TabsList> |
| 98 | + |
| 99 | + <TabsContent value="random" className="space-y-4"> |
| 100 | + <div className="space-y-2"> |
| 101 | + <Label>Generate Random Email</Label> |
| 102 | + <p className="text-sm text-muted-foreground"> |
| 103 | + Click the button below to generate a random temporary email address |
| 104 | + </p> |
| 105 | + </div> |
| 106 | + <Button |
| 107 | + onClick={handleRandomGenerate} |
| 108 | + disabled={isGenerating} |
| 109 | + className="w-full" |
| 110 | + > |
| 111 | + {isGenerating ? ( |
| 112 | + <> |
| 113 | + <RefreshCw className="mr-2 h-4 w-4 animate-spin" /> |
| 114 | + Generating... |
| 115 | + </> |
| 116 | + ) : ( |
| 117 | + <> |
| 118 | + <Sparkles className="mr-2 h-4 w-4" /> |
| 119 | + Generate Random Email |
| 120 | + </> |
| 121 | + )} |
| 122 | + </Button> |
| 123 | + </TabsContent> |
| 124 | + |
| 125 | + <TabsContent value="custom" className="space-y-4"> |
| 126 | + <div className="space-y-2"> |
| 127 | + <Label htmlFor="custom-local">Custom Email Address</Label> |
| 128 | + <div className="flex gap-2"> |
| 129 | + <Input |
| 130 | + id="custom-local" |
| 131 | + placeholder="yourname" |
| 132 | + value={customLocal} |
| 133 | + onChange={(e) => setCustomLocal(e.target.value)} |
| 134 | + onKeyPress={(e) => { |
| 135 | + if (e.key === 'Enter') { |
| 136 | + handleCustomCreate(); |
| 137 | + } |
| 138 | + }} |
| 139 | + /> |
| 140 | + <span className="flex items-center text-muted-foreground">@</span> |
| 141 | + <Select value={selectedDomain} onValueChange={setSelectedDomain}> |
| 142 | + <SelectTrigger className="w-[180px]"> |
| 143 | + <SelectValue /> |
| 144 | + </SelectTrigger> |
| 145 | + <SelectContent> |
| 146 | + <SelectItem value="gxtend.vip">gxtend.vip</SelectItem> |
| 147 | + </SelectContent> |
| 148 | + </Select> |
| 149 | + </div> |
| 150 | + <p className="text-xs text-muted-foreground"> |
| 151 | + Use letters, numbers, dots, hyphens, and underscores (1-64 characters) |
| 152 | + </p> |
| 153 | + </div> |
| 154 | + <Button |
| 155 | + onClick={handleCustomCreate} |
| 156 | + disabled={isCreating} |
| 157 | + className="w-full" |
| 158 | + > |
| 159 | + {isCreating ? ( |
| 160 | + <> |
| 161 | + <RefreshCw className="mr-2 h-4 w-4 animate-spin" /> |
| 162 | + Creating... |
| 163 | + </> |
| 164 | + ) : ( |
| 165 | + <> |
| 166 | + <Mail className="mr-2 h-4 w-4" /> |
| 167 | + Create Custom Email |
| 168 | + </> |
| 169 | + )} |
| 170 | + </Button> |
| 171 | + </TabsContent> |
| 172 | + </Tabs> |
| 173 | + |
| 174 | + {generatedEmail && ( |
| 175 | + <div className="mt-4 p-4 bg-muted rounded-lg space-y-2"> |
| 176 | + <Label className="text-sm font-medium">Generated Email</Label> |
| 177 | + <div className="flex items-center gap-2"> |
| 178 | + <Input |
| 179 | + value={generatedEmail} |
| 180 | + readOnly |
| 181 | + className="font-mono bg-background" |
| 182 | + /> |
| 183 | + <Button |
| 184 | + variant="outline" |
| 185 | + size="icon" |
| 186 | + onClick={handleCopy} |
| 187 | + title="Copy to clipboard" |
| 188 | + > |
| 189 | + <Copy className="h-4 w-4" /> |
| 190 | + </Button> |
| 191 | + </div> |
| 192 | + <p className="text-xs text-muted-foreground"> |
| 193 | + Click the copy button to copy the email address to your clipboard |
| 194 | + </p> |
| 195 | + </div> |
| 196 | + )} |
| 197 | + </CardContent> |
| 198 | + </Card> |
| 199 | + ); |
| 200 | +} |
0 commit comments