Files
Epicure/apps/web/app/admin/users/[id]/page.tsx
T
Arnaud 4c3880e07f feat: moderator-scoped admin access + fix push notifications not displaying (v0.66.0)
Moderator role existed in the schema and was already respected by
comment deletion, but every admin page/route treated moderator
identically to a regular user (403/redirect). Wires it up narrowly:
admin/layout.tsx now lets admin+moderator through and filters the
nav by role, while every admin-only page (users, tiers, settings,
webhooks, insights, etc.) explicitly redirects moderators away via a
new requireFullAdminPage() helper -- the nav filter is UX, this is
the actual gate. Moderators land on Reports and Recipes: reports
GET/PATCH now accept requireAdmin({allowModerator: true}), and a new
PATCH /api/v1/admin/recipes/[id] lets admin+moderator unpublish a
public recipe (flip to private) as a takedown action, audit-logged.

Also found and fixed a real bug while auditing the PWA push pipeline
for a "push click-through" gap: public/sw.js had no `push` event
listener at all, so incoming push messages never displayed anything
-- push was silently non-functional end-to-end despite the
subscribe/send plumbing all working. Added the push listener
(showNotification) and a notificationclick listener that focuses an
existing tab or opens one at the payload's url.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 23:31:47 +02:00

176 lines
6.2 KiB
TypeScript

import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { db, users, recipes, userUsage, eq, desc, count, and } from "@epicure/db";
import { getStorageUsedMb } from "@/lib/tiers";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
import { UserEditor } from "@/components/admin/user-editor";
import { ResetUsageButton } from "@/components/admin/reset-usage-button";
import Link from "next/link";
import { ArrowLeft } from "lucide-react";
import { requireFullAdminPage } from "@/lib/require-admin-page";
export const metadata: Metadata = {};
interface PageProps {
params: Promise<{ id: string }>;
}
const ROLE_COLORS = {
user: "secondary",
moderator: "outline",
admin: "destructive",
} as const;
const TIER_COLORS = {
free: "secondary",
pro: "default",
family: "default",
} as const;
export default async function AdminUserDetailPage({ params }: PageProps) {
await requireFullAdminPage();
const { id } = await params;
const [user] = await db.select().from(users).where(eq(users.id, id)).limit(1);
if (!user) notFound();
const currentMonth = new Date().toISOString().slice(0, 7);
const [usage] = await db
.select()
.from(userUsage)
.where(and(eq(userUsage.userId, id), eq(userUsage.month, currentMonth)))
.limit(1);
const [[recipeCountRow], storageUsedMb] = await Promise.all([
db.select({ count: count() }).from(recipes).where(eq(recipes.authorId, id)),
getStorageUsedMb(id),
]);
const recentRecipes = await db
.select({
id: recipes.id,
title: recipes.title,
visibility: recipes.visibility,
createdAt: recipes.createdAt,
})
.from(recipes)
.where(eq(recipes.authorId, id))
.orderBy(desc(recipes.createdAt))
.limit(10);
return (
<div className="space-y-6">
<div className="flex items-center gap-3">
<Link
href="/admin/users"
className="flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground"
>
<ArrowLeft className="h-4 w-4" />
Back to Users
</Link>
</div>
<div className="flex items-center gap-4">
<Avatar className="h-16 w-16">
<AvatarImage src={user.avatarUrl ?? ""} alt={user.name} />
<AvatarFallback className="text-lg">
{user.name.slice(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div>
<h1 className="text-2xl font-bold tracking-tight">{user.name}</h1>
<p className="text-muted-foreground">{user.email}</p>
<p className="text-xs text-muted-foreground mt-1">
Joined {user.createdAt.toLocaleDateString()}
</p>
</div>
<div className="ml-auto flex gap-2">
<Badge variant={ROLE_COLORS[user.role]}>{user.role}</Badge>
<Badge variant={TIER_COLORS[user.tier]}>{user.tier}</Badge>
</div>
</div>
<Separator />
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<Card>
<CardHeader>
<CardTitle className="text-base">Edit Role &amp; Tier</CardTitle>
</CardHeader>
<CardContent>
<UserEditor userId={user.id} currentRole={user.role} currentTier={user.tier} />
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-base">Usage &amp; Limits</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">AI Calls Used ({currentMonth})</span>
<span className="font-medium">{usage?.aiCallsUsed ?? 0}</span>
</div>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Total Recipes</span>
<span className="font-medium">{recipeCountRow?.count ?? 0}</span>
</div>
<div className="flex justify-between text-sm">
<span className="text-muted-foreground">Storage Used</span>
<span className="font-medium">{storageUsedMb} MB</span>
</div>
<div className="pt-2">
<ResetUsageButton userId={user.id} />
</div>
</CardContent>
</Card>
</div>
<div>
<h2 className="text-lg font-semibold mb-3">Recent Recipes</h2>
{recentRecipes.length === 0 ? (
<p className="text-muted-foreground text-sm">No recipes yet.</p>
) : (
<div className="rounded-md border">
<table className="w-full text-sm">
<thead className="border-b bg-muted/50">
<tr>
<th className="px-4 py-3 text-left font-medium">Title</th>
<th className="px-4 py-3 text-left font-medium">Visibility</th>
<th className="px-4 py-3 text-left font-medium">Created</th>
<th className="px-4 py-3 text-left font-medium">Link</th>
</tr>
</thead>
<tbody>
{recentRecipes.map((recipe) => (
<tr key={recipe.id} className="border-b last:border-0 hover:bg-muted/30">
<td className="px-4 py-3 font-medium">{recipe.title}</td>
<td className="px-4 py-3">
<Badge variant="outline">{recipe.visibility}</Badge>
</td>
<td className="px-4 py-3 text-muted-foreground">
{recipe.createdAt.toLocaleDateString()}
</td>
<td className="px-4 py-3">
<Link
href={`/r/${recipe.id}`}
className="text-primary underline-offset-4 hover:underline text-xs"
>
View
</Link>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
}