Branchwell

Lumen & Gesso / website · lumengesso.com / app / admin / page.tsx

1import { redirect } from 'next/navigation';
2import { getCustomerUser } from '@/app/lib/lumen-auth';
3import { commerceDb } from '@/app/lib/commerce-db';
4import { isAdmin } from '@/app/lib/admin';
5import { pendingCustomerMessage } from '@/app/lib/order-messages';
6import { AdminOrdersList } from './orders-list';
7import { AdminNav } from './AdminNav';
8import { prodigiDashboardUrl } from '@/app/lib/prodigi-dashboard';
9import { PasskeyPanel } from '@/app/components/PasskeyPanel';
10
11export const dynamic = 'force-dynamic';
12
13export default async function AdminHomePage() {
14 const user = await getCustomerUser();
15 if (!user) redirect('/login?next=/admin');
16 if (!isAdmin(user.email)) redirect('/orders');
17
18 const admin = commerceDb();
19 // Fetch a generous window and paginate client-side so search/filter
20 // stay instant across the whole set. A pet-portrait studio won't exceed
21 // this for a long while; revisit with server-side range paging if it does.
22 const { data: orders } = await admin
23 .from('orders')
24 .select('id, user_email, status, price_cents, config, created_at, custom_style_url, revision_note, updated_at, prodigi_order_id')
25 .order('created_at', { ascending: false })
26 .limit(500);
27
28 return (
29 <main className="orders">
30 <div className="orders__inner" style={{ maxWidth: 960 }}>
31 <h1 className="orders__title">Admin · commissions</h1>
32 <AdminNav email={user.email!} current="commissions" />
33 <PasskeyPanel compact />
34 <AdminOrdersList orders={(orders ?? []).map(({ config, revision_note, updated_at, ...order }) => ({
35 ...order,
36 config: { styleId: config?.styleId, productId: config?.productId, size: config?.size },
37 customerMessageAt: pendingCustomerMessage(config, revision_note, updated_at)?.at ?? null,
38 printerUrl: order.prodigi_order_id ? prodigiDashboardUrl(order.prodigi_order_id, process.env.PRODIGI_API_BASE_URL?.includes('sandbox')) : null,
39 }))} />
40 </div>
41 </main>
42 );
43}