1
0
Fork 0
polaris-task-force/src/app/(frontend)/polls/[id]/page.tsx
Z8MB1E d9d4ef1c08 feat(polls): add the polls page with live tallies and tie decisions
`/polls` lists live polls first, then ones waiting on a decision, then closed
history; `/polls/[id]` is the vote surface. Both under Operations.

- Voting, the live tally and the superuser tie decision live in one client
  panel so the pages stay server components.
- The tally refreshes on a scoped 10s timer instead of joining the app-wide SSE
  bus: every game event currently makes each client re-render its whole layout,
  and a poll page does not need to be part of that fan-out.
- Anonymity is applied in the read model, not the view: counts always render,
  identities only for a public poll or a superuser.
- A pending tie shows its deadline and one Decide button per tied option; the
  service refuses a key outside the frozen tie set.
- The outcome line renders only for a settled poll, so a tied poll does not
  claim a decision it has not made.
2026-12-01 14:17:51 -05:00

56 lines
2 KiB
TypeScript

import config from "@payload-config";
import { getPayload } from "payload";
import { headers as nextHeaders } from "next/headers";
import Link from "next/link";
import { notFound } from "next/navigation";
import { ArrowLeft } from "lucide-react";
import { PollVotePanel } from "@/components/frontend/polls/PollVotePanel";
import { loadUserPermissions } from "@/utils/access-control/loadUserPermissions";
import { advancePoll } from "@/lib/polls/service";
import { buildPollView } from "@/lib/polls/surface";
import type { Poll } from "@/payload-types";
export const metadata = {
title: "Poll: Polaris Task Force",
};
export default async function PollPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const pollId = Number(id);
if (!Number.isInteger(pollId)) notFound();
const headers = await nextHeaders();
const payload = await getPayload({ config });
const { user } = await payload.auth({ headers, canSetHeaders: false });
// The (frontend) layout renders LandingPage for guests; this only guards the
// authed render path's types.
if (!user || user.collection !== "users") return null;
// Opening the page settles anything that is due, so a poll closes on time
// even when the bot is down.
const poll = (await advancePoll(payload, { pollId })) as Poll | null;
if (!poll || !poll.id) notFound();
const { isSuperuser } = await loadUserPermissions(payload, user);
const roleDocs = (user as { roleDocs?: (number | { id: number })[] | null }).roleDocs ?? [];
const view = await buildPollView(payload, poll, {
userId: Number(user.id),
isSuperuser,
roleIds: roleDocs.map((ref) => (typeof ref === "number" ? ref : ref.id)),
});
return (
<div className="mx-auto flex w-full max-w-3xl flex-col gap-4 p-5">
<Link
href="/polls"
className="flex items-center gap-1 text-xs text-muted-foreground underline-offset-4 hover:underline"
>
<ArrowLeft className="size-3.5" />
All polls
</Link>
<PollVotePanel initialView={view} />
</div>
);
}