Files
the-sunken-reach/frontend/src/pages/CampaignListPage.tsx
T
joshuaandClaude Opus 5.5 9c97d61ce5 V1.12.0: worlds from a code, shops and cash in the seeds, up-to-date packages
- A DM can start a campaign from a ready-made world by typing its
  six-character code: "+ New Campaign" on the Campaigns page, then "Or
  start from a ready-made world". The whole world is built in one go
  with them as its DM: locations, weapons, items, spells, monsters,
  NPCs, encounters and notes. ALDENF is Aldenfell, HOLLOW is Hollowcrown.
- A world is one file in /backend/seeds. Drop another in and its code
  works straight away, with no restart. "npm run seeds:check" lists
  every world, or what is wrong with one: a misspelt field, a name used
  twice, an encounter pointing at something the file does not have. A
  file with a mistake in it is never offered. docs/SEEDS.md has the
  format.
- A seed can now hold everything a campaign can: gear effects, an NPC's
  wares and exchange rate, things for sale at an encounter, and rewards
  in silver, copper and cash.
- The database seed creates the admin account and nothing else. The
  Aldenfell and Hollowcrown seed commands are gone: worlds come from
  codes.
- Hollowcrown follows the V3 book: 4 starting items and 3 things
  equipped, the book's effects on every weapon and item, armor as items
  (library, modern and magic), worn gear at the book's levels, damage
  types on monsters, ten shop scenes with prices and stock, five money
  changers, Meridian loot as cash and mixed hoards as gold, silver and
  copper.
- Aldenfell: the Shield and Leather Armor change AC, Mireille and the
  Founding Fair stalls sell things, and the book's missing scenes are in
  (E5, E7, E15, E16, E18, E23-24, E26, the Drowned Shrine, Sevrisk's
  Hollow, a wyvern pass). The locket is found with Investigation, as the
  book has it.
- Packages are up to date: Prisma 7 (with the pg driver adapter and a
  prisma.config.js that holds the database URL), Express 5, zod 4,
  dotenv 18, jest 30, bcrypt 6 and TypeScript 7. "npm audit" finds
  nothing. Install scripts are approved for Prisma and bcrypt. On a
  server: "npm install", then "npm run deploy".
- The frontend's five lint warnings are fixed; useAuth and usePagination
  have files of their own.
- All packages at version 1.12.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 16:03:16 +02:00

186 lines
7.7 KiB
TypeScript

import { useState, type FormEvent } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useCampaigns, useCreateCampaign, useCreateCampaignFromSeed, useJoinCampaign, useSetCampaignArchived } from '../api/queries';
import { Spinner, ErrorBanner } from '../components/Spinner';
import { extractErrorMessage } from '../lib/format';
export function CampaignListPage() {
const { data: campaigns, isLoading, error } = useCampaigns();
const createCampaign = useCreateCampaign();
const createFromSeed = useCreateCampaignFromSeed();
const joinCampaign = useJoinCampaign();
const setArchived = useSetCampaignArchived();
const navigate = useNavigate();
const [showCreate, setShowCreate] = useState(false);
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [inviteCode, setInviteCode] = useState('');
const [worldCode, setWorldCode] = useState('');
const [worldError, setWorldError] = useState<string | null>(null);
const [formError, setFormError] = useState<string | null>(null);
const [showArchived, setShowArchived] = useState(false);
const activeCampaigns = campaigns?.filter((c) => !c.archivedAt);
const archivedCampaigns = campaigns?.filter((c) => c.archivedAt) || [];
async function handleCreate(e: FormEvent) {
e.preventDefault();
setFormError(null);
try {
const campaign = await createCampaign.mutateAsync({ name, description: description || undefined });
navigate(`/campaigns/${campaign.id}`);
} catch (err) {
setFormError(extractErrorMessage(err));
}
}
async function handleCreateFromSeed(e: FormEvent) {
e.preventDefault();
setWorldError(null);
try {
const campaign = await createFromSeed.mutateAsync(worldCode.trim().toUpperCase());
navigate(`/campaigns/${campaign.id}`);
} catch (err) {
setWorldError(extractErrorMessage(err));
}
}
async function handleJoin(e: FormEvent) {
e.preventDefault();
setFormError(null);
try {
const campaign = await joinCampaign.mutateAsync(inviteCode.trim().toUpperCase());
navigate(`/campaigns/${campaign.id}`);
} catch (err) {
setFormError(extractErrorMessage(err));
}
}
return (
<div>
<div className="page-header">
<div>
<h1>Your Campaigns</h1>
<p className="subtitle">The tales you're telling, and the ones you're living.</p>
</div>
<button className="btn btn-primary" onClick={() => setShowCreate((s) => !s)}>
{showCreate ? 'Cancel' : '+ New Campaign'}
</button>
</div>
{showCreate && (
<div className="card mt-md">
<div className="card-title">Found a New Campaign</div>
<form onSubmit={handleCreate}>
<div className="field">
<label htmlFor="name">Campaign name</label>
<input id="name" required value={name} onChange={(e) => setName(e.target.value)} />
</div>
<div className="field">
<label htmlFor="description">Description (optional)</label>
<textarea id="description" rows={3} value={description} onChange={(e) => setDescription(e.target.value)} />
</div>
{formError && <ErrorBanner message={formError} />}
<button type="submit" className="btn btn-primary" disabled={createCampaign.isPending}>
{createCampaign.isPending ? 'Founding…' : 'Found Campaign'}
</button>
</form>
<h2 className="section-label mt-lg">Or start from a ready-made world</h2>
<p style={{ color: 'var(--text-muted)', marginTop: 0 }}>
Have a world code? Enter it and the whole world is set up for you as its DM: locations, monsters, NPCs,
gear, spells and encounters.
</p>
<form onSubmit={handleCreateFromSeed} className="flex-row flex-wrap">
<input
aria-label="World code"
placeholder="e.g. ALDENF"
maxLength={12}
value={worldCode}
onChange={(e) => setWorldCode(e.target.value)}
style={{ flex: 1, minWidth: 160, textTransform: 'uppercase' }}
/>
<button type="submit" className="btn" disabled={createFromSeed.isPending || !worldCode.trim()}>
{createFromSeed.isPending ? 'Building the world…' : 'Create From Code'}
</button>
</form>
{worldError && <div className="mt-sm"><ErrorBanner message={worldError} /></div>}
</div>
)}
<div className="card mt-md">
<div className="card-title">Join With an Invite Code</div>
<form onSubmit={handleJoin} className="flex-row flex-wrap">
<input
placeholder="e.g. AB3XQ9"
value={inviteCode}
onChange={(e) => setInviteCode(e.target.value)}
style={{ flex: 1, minWidth: 160, textTransform: 'uppercase' }}
/>
<button type="submit" className="btn" disabled={joinCampaign.isPending || !inviteCode}>
{joinCampaign.isPending ? 'Joining…' : 'Join'}
</button>
</form>
{formError && <div className="mt-sm"><ErrorBanner message={formError} /></div>}
</div>
<div className="mt-lg">
{isLoading && <Spinner label="Consulting the tavern board…" />}
{error && <ErrorBanner message="Could not load your campaigns." />}
{activeCampaigns && activeCampaigns.length === 0 && (
<div className="empty-state">
{archivedCampaigns.length > 0
? 'No active campaigns — found one, join with an invite code above, or restore one from the archive below.'
: 'No campaigns yet — found one, or join with an invite code above.'}
</div>
)}
<div className="grid grid-cols-2">
{activeCampaigns?.map((c) => (
<Link key={c.id} to={`/campaigns/${c.id}`} className="card" style={{ display: 'block' }}>
<div className="card-title">
<span>{c.name}</span>
<span className={`badge ${c.role === 'DM' ? 'badge-dm' : 'badge-player'}`}>{c.role}</span>
</div>
<p style={{ color: 'var(--text-muted)', margin: 0 }}>{c.description || 'No description yet.'}</p>
</Link>
))}
</div>
</div>
{archivedCampaigns.length > 0 && (
<div className="mt-lg">
<div className="flex-between">
<h2 className="section-label" style={{ margin: 0 }}>Archived ({archivedCampaigns.length})</h2>
<button className="btn btn-ghost" onClick={() => setShowArchived((s) => !s)}>
{showArchived ? 'Hide' : 'Show'}
</button>
</div>
{showArchived && (
<div className="grid grid-cols-2 mt-sm">
{archivedCampaigns.map((c) => (
<div key={c.id} className="card">
<div className="card-title">
<Link to={`/campaigns/${c.id}`}>{c.name}</Link>
<span className={`badge ${c.role === 'DM' ? 'badge-dm' : 'badge-player'}`}>{c.role}</span>
</div>
<p style={{ color: 'var(--text-muted)', marginTop: 0 }}>{c.description || 'No description yet.'}</p>
{c.role === 'DM' && (
<button
className="btn"
disabled={setArchived.isPending}
onClick={() => setArchived.mutate({ campaignId: c.id, archived: false })}
>
Restore
</button>
)}
</div>
))}
</div>
)}
</div>
)}
</div>
);
}