Top panel: Expert / Intermediate / Beginner. Bottom: that difficulty's saved
games, best first, click to play one on the board.
── the selection lives in the URL ──
`/minesweeper/expert`, not a channel and not panel state. It survives a
refresh, it can be linked, and the two panels never pass anything between
themselves — both read `useParams`. The plugin section route already existed;
nothing had used it.
The side effect hangs off the URL too: an effect on the selected level posts
the difficulty, so arriving at `/minesweeper/expert` by click, deep link or
back button all leave the game on expert. `POST /difficulty` verifies and is
idempotent, so a repeat changes nothing. One `<Navigate replace>` after the
hooks canonicalises both the bare route and a bogus section, and it waits for
`/status` so the URL lands on what the game IS rather than a guess — which is
why `difficulty` is now part of that payload.
Levels are NavLinks so cmd-click works and the active style comes from the
router. Replay rows are buttons, because they do not navigate anywhere, they
make the game do something. That is the line navigation-audit.md draws.
── two things on the server ──
`GET /replays` reads the directory rather than any index, because the filename
IS the metadata — Minesweeper X names a recording `<Lvl> <secs> - <date>.mvr`
and keeps no catalogue. A name that does not parse is skipped rather than
guessed at. Ordering is the server's: a loss is never "best" however short it
was, so wins rank by time and losses fall to the end, still watchable.
`POST /replay` drives Extras > Play Recording... and TYPES the filename into a
file dialog, which is why it only accepts a name `isKnownReplay` just matched
against the directory. An unchecked client string reaching `xdotool type` can
carry a path, a wildcard, or a newline that submits something else. Verified by
the dialog closing, not by the window size — a beginner replay leaves the
window beginner-sized, which is also what nothing-happened looks like.
Verified end to end against the running session: the expert list comes back
ranked 58.63 through 82.06 with losses after, playing 58.63 starts on the
board, and `{"file":"../../../etc/passwd"}` is a 404.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Officer-Session: de3ed4e6-f88a-429c-94e6-a760336ba83b
76 lines
3.1 KiB
TypeScript
76 lines
3.1 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { Navigate, NavLink, useParams } from 'react-router';
|
|
import { toast } from 'sonner';
|
|
import { useClient } from 'hooks/useClient';
|
|
import { LEVELS, isLevel, type Level } from './levels';
|
|
|
|
/**
|
|
* The difficulty list — and the URL is what holds the choice.
|
|
*
|
|
* `/minesweeper/expert` rather than a channel or panel state, because it is exactly the kind of thing
|
|
* `docs/navigation-audit.md` calls addressable: it survives a refresh, it can be linked, and the replay
|
|
* panel reads it from the URL instead of having it passed across. Rows are real `NavLink`s, so cmd-click
|
|
* and middle-click behave and the active style comes from the router rather than a hand-rolled compare.
|
|
*
|
|
* The SIDE EFFECT hangs off the URL too. An effect on the selected level posts the difficulty, so
|
|
* arriving at `/minesweeper/expert` by any route — click, deep link, back button — leaves the game on
|
|
* expert. `POST /difficulty` verifies and is idempotent, so a repeat costs a round trip and changes
|
|
* nothing.
|
|
*/
|
|
export const LevelsPanel = () => {
|
|
const { section } = useParams();
|
|
const client = useClient();
|
|
// useClient() returns fresh verbs every render; a ref keeps the effect below from re-running forever.
|
|
const clientRef = useRef(client);
|
|
clientRef.current = client;
|
|
|
|
const selected = isLevel(section) ? section : null;
|
|
|
|
const { data: status } = useQuery({
|
|
queryKey: ['minesweeper', 'status'],
|
|
queryFn: () => client.get<{ difficulty: Level | null; running: boolean }>('/minesweeper/status'),
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!selected) return;
|
|
clientRef.current
|
|
.post('/minesweeper/difficulty', { level: selected })
|
|
.catch(() => toast.error(`Could not switch the game to ${selected}.`));
|
|
}, [selected]);
|
|
|
|
// After the hooks, never before: one guard canonicalising both the bare route and a bogus section.
|
|
// It waits for status so the URL lands on what the game IS rather than on a guess.
|
|
if (!selected) {
|
|
if (!status) return <Shell />;
|
|
return <Navigate to={`/minesweeper/${status.difficulty ?? 'beginner'}`} replace />;
|
|
}
|
|
|
|
return (
|
|
<Shell>
|
|
{LEVELS.map((level) => (
|
|
<NavLink
|
|
key={level.key}
|
|
to={`/minesweeper/${level.key}`}
|
|
// Re-asserting the level it is already on is the point: a replay leaves the board at the
|
|
// replay's size, and clicking the current level is the obvious way back.
|
|
onClick={() => {
|
|
if (level.key === selected) void clientRef.current.post('/minesweeper/difficulty', { level: level.key });
|
|
}}
|
|
className={({ isActive }) =>
|
|
`rounded-md px-3 py-2 text-sm transition-colors ${
|
|
isActive ? 'bg-accent font-medium text-accent-foreground' : 'text-muted-foreground hover:bg-accent/50'
|
|
}`
|
|
}
|
|
>
|
|
{level.label}
|
|
</NavLink>
|
|
))}
|
|
</Shell>
|
|
);
|
|
};
|
|
|
|
const Shell = ({ children }: { children?: React.ReactNode }) => (
|
|
<div className="flex size-full flex-col gap-1 p-2">{children}</div>
|
|
);
|