|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react' |
| 4 | +import { |
| 5 | + Plus, |
| 6 | + Search, |
| 7 | + Upload, |
| 8 | + RefreshCw, |
| 9 | + Trash2, |
| 10 | + Edit, |
| 11 | + Mail, |
| 12 | + User |
| 13 | +} from 'lucide-react' |
| 14 | +import { Button } from '@/components/ui/button' |
| 15 | +import { Input } from '@/components/ui/input' |
| 16 | +import { Card } from '@/components/ui/card' |
| 17 | + |
| 18 | +interface Contact { |
| 19 | + id: string |
| 20 | + email: string |
| 21 | + firstName: string |
| 22 | + lastName: string |
| 23 | + createdAt: string |
| 24 | +} |
| 25 | + |
| 26 | +const STORAGE_KEY = 'email-platform-contacts' |
| 27 | + |
| 28 | +export default function ContactsPage() { |
| 29 | + const [contacts, setContacts] = useState<Contact[]>([]) |
| 30 | + const [searchQuery, setSearchQuery] = useState('') |
| 31 | + const [showAddForm, setShowAddForm] = useState(false) |
| 32 | + const [newContact, setNewContact] = useState({ |
| 33 | + email: '', |
| 34 | + firstName: '', |
| 35 | + lastName: '', |
| 36 | + }) |
| 37 | + |
| 38 | + // 从 localStorage 加载联系人 |
| 39 | + useEffect(() => { |
| 40 | + const saved = localStorage.getItem(STORAGE_KEY) |
| 41 | + if (saved) { |
| 42 | + try { |
| 43 | + setContacts(JSON.parse(saved)) |
| 44 | + } catch (e) { |
| 45 | + console.error('Failed to load contacts:', e) |
| 46 | + } |
| 47 | + } |
| 48 | + }, []) |
| 49 | + |
| 50 | + // 保存联系人到 localStorage |
| 51 | + const saveContacts = (newContacts: Contact[]) => { |
| 52 | + setContacts(newContacts) |
| 53 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(newContacts)) |
| 54 | + } |
| 55 | + |
| 56 | + const handleAddContact = () => { |
| 57 | + if (!newContact.email) return |
| 58 | + |
| 59 | + const contact: Contact = { |
| 60 | + id: Date.now().toString(), |
| 61 | + email: newContact.email, |
| 62 | + firstName: newContact.firstName, |
| 63 | + lastName: newContact.lastName, |
| 64 | + createdAt: new Date().toISOString(), |
| 65 | + } |
| 66 | + |
| 67 | + saveContacts([...contacts, contact]) |
| 68 | + setNewContact({ email: '', firstName: '', lastName: '' }) |
| 69 | + setShowAddForm(false) |
| 70 | + } |
| 71 | + |
| 72 | + const handleDeleteContact = (id: string) => { |
| 73 | + saveContacts(contacts.filter(c => c.id !== id)) |
| 74 | + } |
| 75 | + |
| 76 | + const filteredContacts = contacts.filter(contact => { |
| 77 | + const query = searchQuery.toLowerCase() |
| 78 | + return ( |
| 79 | + contact.email.toLowerCase().includes(query) || |
| 80 | + contact.firstName.toLowerCase().includes(query) || |
| 81 | + contact.lastName.toLowerCase().includes(query) |
| 82 | + ) |
| 83 | + }) |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="p-8"> |
| 87 | + {/* Header */} |
| 88 | + <div className="flex items-center justify-between mb-8"> |
| 89 | + <div> |
| 90 | + <h1 className="text-3xl font-black uppercase tracking-tight mb-2"> |
| 91 | + Contacts |
| 92 | + </h1> |
| 93 | + <p className="text-gray-600"> |
| 94 | + Manage your email recipients |
| 95 | + </p> |
| 96 | + </div> |
| 97 | + <div className="flex gap-2"> |
| 98 | + <Button variant="outline" className="neo-border"> |
| 99 | + <Upload className="w-4 h-4 mr-2" /> |
| 100 | + Import CSV |
| 101 | + </Button> |
| 102 | + <Button variant="outline" className="neo-border"> |
| 103 | + <RefreshCw className="w-4 h-4 mr-2" /> |
| 104 | + Sync Resend |
| 105 | + </Button> |
| 106 | + <Button |
| 107 | + className="neo-button bg-neo-green text-white" |
| 108 | + onClick={() => setShowAddForm(true)} |
| 109 | + > |
| 110 | + <Plus className="w-4 h-4 mr-2" /> |
| 111 | + Add Contact |
| 112 | + </Button> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + |
| 116 | + {/* Search */} |
| 117 | + <div className="mb-6"> |
| 118 | + <div className="relative max-w-md"> |
| 119 | + <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> |
| 120 | + <Input |
| 121 | + placeholder="Search contacts..." |
| 122 | + value={searchQuery} |
| 123 | + onChange={(e) => setSearchQuery(e.target.value)} |
| 124 | + className="neo-border pl-10" |
| 125 | + /> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + |
| 129 | + {/* Add Contact Form */} |
| 130 | + {showAddForm && ( |
| 131 | + <Card className="neo-border neo-shadow p-6 mb-6"> |
| 132 | + <h3 className="font-bold text-lg mb-4">Add New Contact</h3> |
| 133 | + <div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4"> |
| 134 | + <Input |
| 135 | + placeholder="Email *" |
| 136 | + type="email" |
| 137 | + value={newContact.email} |
| 138 | + onChange={(e) => setNewContact({ ...newContact, email: e.target.value })} |
| 139 | + className="neo-border" |
| 140 | + /> |
| 141 | + <Input |
| 142 | + placeholder="First Name" |
| 143 | + value={newContact.firstName} |
| 144 | + onChange={(e) => setNewContact({ ...newContact, firstName: e.target.value })} |
| 145 | + className="neo-border" |
| 146 | + /> |
| 147 | + <Input |
| 148 | + placeholder="Last Name" |
| 149 | + value={newContact.lastName} |
| 150 | + onChange={(e) => setNewContact({ ...newContact, lastName: e.target.value })} |
| 151 | + className="neo-border" |
| 152 | + /> |
| 153 | + </div> |
| 154 | + <div className="flex gap-2"> |
| 155 | + <Button |
| 156 | + className="neo-button bg-neo-green text-white" |
| 157 | + onClick={handleAddContact} |
| 158 | + > |
| 159 | + Add Contact |
| 160 | + </Button> |
| 161 | + <Button |
| 162 | + variant="outline" |
| 163 | + className="neo-border" |
| 164 | + onClick={() => setShowAddForm(false)} |
| 165 | + > |
| 166 | + Cancel |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + </Card> |
| 170 | + )} |
| 171 | + |
| 172 | + {/* Contacts List */} |
| 173 | + <Card className="neo-border neo-shadow"> |
| 174 | + {contacts.length === 0 ? ( |
| 175 | + <div className="p-12 text-center"> |
| 176 | + <User className="w-12 h-12 mx-auto mb-4 text-gray-300" /> |
| 177 | + <h3 className="text-lg font-bold mb-2">No contacts yet</h3> |
| 178 | + <p className="text-gray-600 mb-4"> |
| 179 | + Add your first contact or import from CSV |
| 180 | + </p> |
| 181 | + <Button |
| 182 | + className="neo-button bg-neo-green text-white" |
| 183 | + onClick={() => setShowAddForm(true)} |
| 184 | + > |
| 185 | + <Plus className="w-4 h-4 mr-2" /> |
| 186 | + Add Contact |
| 187 | + </Button> |
| 188 | + </div> |
| 189 | + ) : ( |
| 190 | + <div className="overflow-x-auto"> |
| 191 | + <table className="w-full"> |
| 192 | + <thead className="bg-gray-50 border-b-4 border-black"> |
| 193 | + <tr> |
| 194 | + <th className="text-left p-4 font-bold uppercase text-sm">Email</th> |
| 195 | + <th className="text-left p-4 font-bold uppercase text-sm">First Name</th> |
| 196 | + <th className="text-left p-4 font-bold uppercase text-sm">Last Name</th> |
| 197 | + <th className="text-left p-4 font-bold uppercase text-sm">Added</th> |
| 198 | + <th className="text-right p-4 font-bold uppercase text-sm">Actions</th> |
| 199 | + </tr> |
| 200 | + </thead> |
| 201 | + <tbody> |
| 202 | + {filteredContacts.map((contact, index) => ( |
| 203 | + <tr |
| 204 | + key={contact.id} |
| 205 | + className={index % 2 === 0 ? 'bg-white' : 'bg-gray-50'} |
| 206 | + > |
| 207 | + <td className="p-4"> |
| 208 | + <div className="flex items-center gap-2"> |
| 209 | + <Mail className="w-4 h-4 text-gray-400" /> |
| 210 | + {contact.email} |
| 211 | + </div> |
| 212 | + </td> |
| 213 | + <td className="p-4">{contact.firstName || '-'}</td> |
| 214 | + <td className="p-4">{contact.lastName || '-'}</td> |
| 215 | + <td className="p-4 text-gray-500 text-sm"> |
| 216 | + {new Date(contact.createdAt).toLocaleDateString()} |
| 217 | + </td> |
| 218 | + <td className="p-4 text-right"> |
| 219 | + <div className="flex justify-end gap-2"> |
| 220 | + <Button size="sm" variant="ghost"> |
| 221 | + <Edit className="w-4 h-4" /> |
| 222 | + </Button> |
| 223 | + <Button |
| 224 | + size="sm" |
| 225 | + variant="ghost" |
| 226 | + className="text-red-500 hover:text-red-700" |
| 227 | + onClick={() => handleDeleteContact(contact.id)} |
| 228 | + > |
| 229 | + <Trash2 className="w-4 h-4" /> |
| 230 | + </Button> |
| 231 | + </div> |
| 232 | + </td> |
| 233 | + </tr> |
| 234 | + ))} |
| 235 | + </tbody> |
| 236 | + </table> |
| 237 | + </div> |
| 238 | + )} |
| 239 | + </Card> |
| 240 | + |
| 241 | + {/* Stats */} |
| 242 | + {contacts.length > 0 && ( |
| 243 | + <div className="mt-4 text-sm text-gray-500"> |
| 244 | + Showing {filteredContacts.length} of {contacts.length} contacts |
| 245 | + </div> |
| 246 | + )} |
| 247 | + </div> |
| 248 | + ) |
| 249 | +} |
0 commit comments