Files
EpicNext-Cms/src/app/(site)/shop/page.tsx
T
openhands 6cc45d7413
Gitea Actions Runner Test / test-job (push) Successful in 2s
CI / check (push) Successful in 30s
CI / tests-integration (push) Successful in 1m42s
CI / tests-unit (push) Failing after 1m49s
CI / tests-ui (push) Successful in 2m31s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
feat: harden atoms-nexst against review findings (37 items)
Second review pass covering security, performance, admin tooling and the
public/room flows. All HIGH and MEDIUM findings from the audit are resolved;
nothing in this commit changes the visible feature set.

Authentication & session security
- CSP is now set on the request headers in the proxy, which is what Next.js
  uses to derive the render nonce, so the nonce is effective.
- 2FA: an already-enabled user cannot re-enroll, the setup endpoint is
  rate-limited per account, and confirmed codes are persisted so the second
  secret no longer silently never applies.
- Password reset revokes the ticket, authTicket and all personal access
  tokens, and bumps the token version so existing sessions die. The same
  revocation is now wired into the staff-side password reset.
- /reset and /verify return a stable error code instead of raw text; the
  mail lookups are ordered by id so duplicates cannot vary between runs.
- Resending the verification mail gets a per-address cooldown on top of the
  per-user limit.
- Issue API tokens with the narrower radio/ticket ability set instead of "*".

Authorization & input handling
- Mid-rank staff can no longer keep dynamically granted non-view admin.*
  permissions: existing grants are revoked by migration and the grant lookup
  is restricted to "%.view". Rank guards use the dynamic super-admin check.
- Alerting a user is permission-checked and audited like the other tools.
- Material mutations (giveCredits/giveDuckets/giveDiamonds, the admin user
  actions route, bulk user actions) are capped and rank-guarded, and bulk
  ids are bounded.
- updateRoom / updateRoomItem write through a field allowlist, and items
  may only be edited through their own room.
- Classnames reaching the filesystem are validated before use so a crafted
  value cannot escape the asset directories.
- The word filter now also covers offline mails, guild forum threads and
  replies, and user mottos.
- Media uploads are validated by magic bytes, /api/media requires the page
  edit permission, APP_URL must be configured once mail is enabled, and the
  diagnostics error route checks the fetch site header.

Admin tooling
- Secret settings render masked and cannot be overwritten with a blank or
  an arbitrary raw key; radio credentials are new password inputs.
- Commandocentrum balance changes are audited.
- Admin list pagination reads the caller's per-page instead of the max, and
  the log exporter caps offset and search length.

Performance
- Catalog translations are cached per module, with a cheap revision hash;
  the public online count uses a stale window instead of hammering the DB.
- The cache warmup now primes the payload the home route actually reads.
- TopHeader batches its queries into one round trip, and LCP avatars load
  eagerly.
- motion/react and sonner are no longer part of the root layout; the nav
  dropdown and mobile nav panels are lazy client chunks. Anonymous visitors
  again get the navigation chrome, and public pages get an edge cacheable
  response.

Accessibility
- Nested <main> elements in phase pages became <section>; the page entrance
  and route progress animations are pure CSS that respect reduced motion.
2026-10-09 16:19:48 +02:00

346 lines
9.0 KiB
TypeScript

import { asc, eq } from "drizzle-orm";
import type { Metadata } from "next";
import { getTranslations } from "next-intl/server";
import type { CSSProperties } from "react";
import { buyShopArticle } from "@/actions/shop";
import Link from "@/components/link";
import { ContentCard, EmptyState } from "@/components/public/ui";
import { CurrencyIcon } from "@/components/shared/currency-icon";
import { auth } from "@/lib/auth";
import { db, WebsiteShopArticles, WebsiteShopCategories } from "@/lib/db";
import { excerpt } from "@/lib/format";
import { creditsPerUnit } from "@/lib/services/paypal";
export async function generateMetadata(): Promise<Metadata> {
const t = await getTranslations("pages.shop");
return {
title: t("title"),
description: t("subtitle"),
openGraph: {
title: t("title"),
description: t("subtitle"),
type: "website",
},
};
}
function feedbackStyle(tone: "success" | "error"): CSSProperties {
const accent =
tone === "error" ? "var(--color-danger)" : "var(--color-primary)";
return {
margin: 0,
padding: "0.85rem 1rem",
borderRadius: "var(--radius-md)",
border: `1px solid ${accent}`,
color: "var(--color-text-readable, var(--color-text))",
fontSize: "0.9rem",
fontWeight: 600,
background: "var(--color-surface)",
borderLeft: `4px solid ${accent}`,
};
}
// Faithful to AtomCMS WebsiteShopArticle::price(): costs are stored in cents,
// the displayed price is costs/100 with a floor of 1.
function priceLabel(costs: number): string {
const price = costs < 100 ? 1 : costs / 100;
return `$${price.toFixed(2)}`;
}
function creditPriceLabel(costs: number): string {
const rate = creditsPerUnit();
const dollars = costs < 100 ? 1 : costs / 100;
const credits = Math.max(1, Math.floor(dollars * rate));
return credits.toLocaleString();
}
type ShopArticle = {
id: bigint;
name: string;
info: string;
iconUrl: string;
color: string;
costs: number;
credits: number | null;
duckets: number | null;
diamonds: number | null;
badges: string | null;
position: number;
};
type ShopCategory = {
id: bigint;
name: string;
icon: string | null;
};
const articleSelect = {
id: WebsiteShopArticles.id,
name: WebsiteShopArticles.name,
info: WebsiteShopArticles.info,
iconUrl: WebsiteShopArticles.iconUrl,
color: WebsiteShopArticles.color,
costs: WebsiteShopArticles.costs,
credits: WebsiteShopArticles.credits,
duckets: WebsiteShopArticles.duckets,
diamonds: WebsiteShopArticles.diamonds,
badges: WebsiteShopArticles.badges,
position: WebsiteShopArticles.position,
} as const;
export default async function ShopPage({
searchParams,
}: {
searchParams: Promise<{
category?: string;
bought?: string;
error?: string;
package?: string;
}>;
}) {
const t = await getTranslations("pages.shop");
const sp = await searchParams;
const rawCategory = (sp.category ?? "").trim();
const categoryId = /^\d+$/.test(rawCategory) ? rawCategory : "";
const session = await auth();
const isLoggedIn = Boolean(session?.user?.id);
const errorMessage =
sp.error === "invalid"
? t("errors.invalid")
: sp.error === "credits"
? t("errors.credits")
: sp.error === "ratelimit"
? t("errors.ratelimit")
: sp.error
? t("errors.error")
: null;
const boughtMessage =
sp.bought === "1"
? sp.package
? t("success.boughtNamed", { name: sp.package })
: t("success.bought")
: null;
// Categories for the filter bar and the article grid are independent —
// fetch them concurrently.
const [categories, articles] = await Promise.all([
db
.select({
id: WebsiteShopCategories.id,
name: WebsiteShopCategories.name,
icon: WebsiteShopCategories.icon,
})
.from(WebsiteShopCategories)
.orderBy(asc(WebsiteShopCategories.name))
.catch((): ShopCategory[] => []),
(categoryId
? db
.select(articleSelect)
.from(WebsiteShopArticles)
.where(eq(WebsiteShopArticles.categoryId, BigInt(categoryId)))
.orderBy(asc(WebsiteShopArticles.position))
: db
.select(articleSelect)
.from(WebsiteShopArticles)
.orderBy(asc(WebsiteShopArticles.position))
).catch((): ShopArticle[] => []),
]);
const activeCategory = categoryId
? categories.find((c) => String(c.id) === categoryId)
: undefined;
return (
<section className="page-grid">
{boughtMessage ? (
<div role="status" style={feedbackStyle("success")}>
{boughtMessage}
</div>
) : null}
{errorMessage ? (
<div role="alert" style={feedbackStyle("error")}>
{errorMessage}
{sp.error === "credits" ? (
<>
{" "}
<Link href="/shop/topup">{t("topUpLink")}</Link>
</>
) : null}
</div>
) : null}
<ContentCard
icon="🛒"
title={t("title")}
subtitle={
activeCategory
? t("browsingCategory", { category: activeCategory.name })
: t("subtitle")
}
>
<p className="muted" style={{ margin: "0 0 0.75rem" }}>
{t("creditsHint")} <Link href="/shop/topup">{t("topUpLink")}</Link>
</p>
{categories.length > 0 ? (
<div
style={{
display: "flex",
flexWrap: "wrap",
gap: "0.5rem",
}}
>
<Link
href="/shop"
className={`btn ${categoryId ? "btn-outline" : "btn-primary"}`}
>
{t("all")}
</Link>
{categories.map((c) => (
<Link
key={String(c.id)}
href={`/shop?category=${String(c.id)}`}
className={`btn ${categoryId === String(c.id) ? "btn-primary" : "btn-outline"}`}
>
{c.name}
</Link>
))}
</div>
) : null}
</ContentCard>
<ContentCard
icon="📦"
title={activeCategory ? activeCategory.name : t("allPackages")}
subtitle={
articles.length > 0
? t("packagesAvailable", { count: articles.length })
: undefined
}
padded={articles.length === 0}
>
{articles.length === 0 ? (
<EmptyState icon="🛒">{t("empty")}</EmptyState>
) : (
<div className="card-grid sm-2 lg-3" style={{ padding: "1rem" }}>
{articles.map((a) => (
<article key={String(a.id)} className="card hover">
<div
style={{
display: "flex",
alignItems: "center",
gap: "0.75rem",
marginBottom: "0.6rem",
}}
>
<div
style={{
width: 56,
height: 56,
borderRadius: "var(--radius-sm)",
display: "grid",
placeItems: "center",
background: "var(--color-background)",
border: `2px solid ${a.color || "var(--border-color)"}`,
flexShrink: 0,
overflow: "hidden",
}}
>
{a.iconUrl ? (
// eslint-disable-next-line @next/next/no-img-element
<img
src={a.iconUrl}
alt=""
width={48}
height={48}
style={{ objectFit: "contain", display: "block" }}
/>
) : null}
</div>
<div style={{ minWidth: 0 }}>
<h3 style={{ margin: 0 }}>{a.name}</h3>
<span className="muted">
{t("priceCredits", {
credits: creditPriceLabel(a.costs),
usd: priceLabel(a.costs),
})}
</span>
</div>
</div>
<p style={{ margin: "0 0 0.75rem" }}>{excerpt(a.info, 160)}</p>
<div
style={{
display: "flex",
flexWrap: "wrap",
gap: "0.4rem",
marginBottom: "0.85rem",
}}
>
{a.credits ? (
<span className="currency">
<CurrencyIcon kind="credits" size={22} alt="" />
{a.credits.toLocaleString()}
</span>
) : null}
{a.duckets ? (
<span className="currency">
<CurrencyIcon kind="duckets" size={22} alt="" />
{a.duckets.toLocaleString()}
</span>
) : null}
{a.diamonds ? (
<span className="currency">
<CurrencyIcon kind="diamonds" size={22} alt="" />
{a.diamonds.toLocaleString()}
</span>
) : null}
</div>
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "space-between",
gap: "0.5rem",
}}
>
<span style={{ fontWeight: 800 }}>
{t("priceCreditsShort", {
credits: creditPriceLabel(a.costs),
})}
</span>
{isLoggedIn ? (
<form action={buyShopArticle} style={{ margin: 0 }}>
<input
type="hidden"
name="articleId"
value={String(a.id)}
/>
<input
type="hidden"
name="categoryId"
value={categoryId}
/>
<button type="submit" className="btn btn-primary">
{t("buy")}
</button>
</form>
) : (
<Link href="/login" className="btn btn-primary">
{t("loginToBuy")}
</Link>
)}
</div>
</article>
))}
</div>
)}
</ContentCard>
</section>
);
}