| 7 | const sql = postgres(process.env.POSTGRES_URL!, { ssl: "require" }); |
| 8 | |
| 9 | export default async function Table() { |
| 10 | let data |
| 11 | let startTime = Date.now() |
| 12 | |
| 13 | try { |
| 14 | data = await sql`SELECT * FROM profiles` |
| 15 | } catch (e: any) { |
| 16 | if (e.message.includes('relation "profiles" does not exist')) { |
| 17 | console.log( |
| 18 | 'Table does not exist, creating and seeding it with dummy data now...' |
| 19 | ) |
| 20 | // Table is not created yet |
| 21 | await seed() |
| 22 | startTime = Date.now() |
| 23 | data = await sql`SELECT * FROM profiles` |
| 24 | } else { |
| 25 | throw e |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | const profiles = data |
| 30 | const duration = Date.now() - startTime |
| 31 | |
| 32 | return ( |
| 33 | <div className="bg-white/30 p-12 shadow-xl ring-1 ring-gray-900/5 rounded-lg backdrop-blur-lg max-w-xl mx-auto w-full"> |
| 34 | <div className="flex justify-between items-center mb-4"> |
| 35 | <div className="space-y-1"> |
| 36 | <h2 className="text-xl font-semibold">Recent Users</h2> |
| 37 | <p className="text-sm text-gray-500"> |
| 38 | Fetched {profiles.length} users in {duration}ms |
| 39 | </p> |
| 40 | </div> |
| 41 | <RefreshButton /> |
| 42 | </div> |
| 43 | <div className="divide-y divide-gray-900/5"> |
| 44 | {profiles.map((user) => ( |
| 45 | <div |
| 46 | key={user.name} |
| 47 | className="flex items-center justify-between py-3" |
| 48 | > |
| 49 | <div className="flex items-center space-x-4"> |
| 50 | <Image |
| 51 | src={user.image} |
| 52 | alt={user.name} |
| 53 | width={48} |
| 54 | height={48} |
| 55 | className="rounded-full ring-1 ring-gray-900/5" |
| 56 | /> |
| 57 | <div className="space-y-1"> |
| 58 | <p className="font-medium leading-none">{user.name}</p> |
| 59 | <p className="text-sm text-gray-500">{user.email}</p> |
| 60 | </div> |
| 61 | </div> |
| 62 | <p className="text-sm text-gray-500">{timeAgo(user.createdAt)}</p> |
| 63 | </div> |
| 64 | ))} |
| 65 | </div> |
| 66 | </div> |