2026-02-08 10:40:18 +01:00
|
|
|
import { useMemo, useState } from 'react'
|
2026-02-05 15:09:25 +01:00
|
|
|
import { useNavigate } from 'react-router-dom'
|
|
|
|
|
import { GameGrid, RulesConfiguration, StepIndicator } from '../components'
|
2026-02-08 10:40:18 +01:00
|
|
|
import { useGames, useGameRoutes } from '../hooks/useGames'
|
2026-02-11 21:36:50 +01:00
|
|
|
import { useCreateRun, useRuns, useNamingCategories } from '../hooks/useRuns'
|
2026-03-20 21:41:38 +01:00
|
|
|
import type { Game, NuzlockeRules, RunVisibility } from '../types'
|
2026-02-05 15:09:25 +01:00
|
|
|
import { DEFAULT_RULES } from '../types'
|
2026-02-08 10:40:18 +01:00
|
|
|
import { RULE_DEFINITIONS } from '../types/rules'
|
2026-02-05 15:09:25 +01:00
|
|
|
|
2026-02-07 21:29:14 +01:00
|
|
|
const DEFAULT_COLOR = '#6366f1'
|
|
|
|
|
|
2026-02-05 15:09:25 +01:00
|
|
|
export function NewRun() {
|
|
|
|
|
const navigate = useNavigate()
|
|
|
|
|
const { data: games, isLoading, error } = useGames()
|
2026-02-07 21:29:14 +01:00
|
|
|
const { data: runs } = useRuns()
|
2026-02-05 15:09:25 +01:00
|
|
|
const createRun = useCreateRun()
|
2026-02-11 21:36:50 +01:00
|
|
|
const { data: namingCategories } = useNamingCategories()
|
2026-02-05 15:09:25 +01:00
|
|
|
|
|
|
|
|
const [step, setStep] = useState(1)
|
|
|
|
|
const [selectedGame, setSelectedGame] = useState<Game | null>(null)
|
|
|
|
|
const [rules, setRules] = useState<NuzlockeRules>(DEFAULT_RULES)
|
|
|
|
|
const [runName, setRunName] = useState('')
|
2026-02-11 21:36:50 +01:00
|
|
|
const [namingScheme, setNamingScheme] = useState<string | null>(null)
|
2026-03-20 21:41:38 +01:00
|
|
|
const [visibility, setVisibility] = useState<RunVisibility>('public')
|
2026-02-08 10:40:18 +01:00
|
|
|
const { data: routes } = useGameRoutes(selectedGame?.id ?? null)
|
|
|
|
|
|
|
|
|
|
const hiddenRules = useMemo(() => {
|
|
|
|
|
const hidden = new Set<keyof NuzlockeRules>()
|
|
|
|
|
const hasPinwheelZones = routes?.some((r) => r.pinwheelZone != null)
|
|
|
|
|
if (!hasPinwheelZones) {
|
|
|
|
|
hidden.add('pinwheelClause')
|
|
|
|
|
}
|
|
|
|
|
return hidden.size > 0 ? hidden : undefined
|
|
|
|
|
}, [routes])
|
2026-02-05 15:09:25 +01:00
|
|
|
|
|
|
|
|
const handleGameSelect = (game: Game) => {
|
2026-02-07 14:20:26 +01:00
|
|
|
if (selectedGame?.id === game.id) {
|
|
|
|
|
setSelectedGame(null)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-05 15:09:25 +01:00
|
|
|
setSelectedGame(game)
|
|
|
|
|
if (!runName || runName === `${selectedGame?.name} Nuzlocke`) {
|
|
|
|
|
setRunName(`${game.name} Nuzlocke`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleCreate = () => {
|
|
|
|
|
if (!selectedGame) return
|
|
|
|
|
createRun.mutate(
|
2026-03-20 21:41:38 +01:00
|
|
|
{ gameId: selectedGame.id, name: runName, rules, namingScheme, visibility },
|
2026-02-14 16:41:24 +01:00
|
|
|
{ onSuccess: (data) => navigate(`/runs/${data.id}`) }
|
2026-02-05 15:09:25 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 20:39:41 +01:00
|
|
|
const visibleRuleKeys = RULE_DEFINITIONS.filter((r) => !hiddenRules?.has(r.key)).map((r) => r.key)
|
2026-02-08 10:40:18 +01:00
|
|
|
const enabledRuleCount = visibleRuleKeys.filter((k) => rules[k]).length
|
|
|
|
|
const totalRuleCount = visibleRuleKeys.length
|
2026-02-05 15:09:25 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="max-w-4xl mx-auto p-8">
|
2026-02-17 20:48:42 +01:00
|
|
|
<h1 className="text-3xl font-bold text-text-primary mb-2">New Nuzlocke Run</h1>
|
|
|
|
|
<p className="text-text-tertiary mb-6">Set up your run in a few steps.</p>
|
2026-02-05 15:09:25 +01:00
|
|
|
|
|
|
|
|
<StepIndicator currentStep={step} onStepClick={setStep} />
|
|
|
|
|
|
|
|
|
|
{step === 1 && (
|
|
|
|
|
<div>
|
2026-02-17 20:48:42 +01:00
|
|
|
<h2 className="text-xl font-semibold text-text-primary mb-4">Choose a Game</h2>
|
2026-02-05 15:09:25 +01:00
|
|
|
|
2026-02-17 20:48:42 +01:00
|
|
|
<div className="sticky top-0 z-10 bg-surface-0 py-3 mb-4 border-b border-border-default">
|
2026-02-07 21:29:14 +01:00
|
|
|
{selectedGame ? (
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
|
<SelectedGameThumb game={selectedGame} />
|
|
|
|
|
<div>
|
2026-02-17 20:48:42 +01:00
|
|
|
<p className="font-medium text-text-primary">{selectedGame.name}</p>
|
|
|
|
|
<p className="text-sm text-text-tertiary">
|
2026-02-16 20:39:41 +01:00
|
|
|
{selectedGame.region.charAt(0).toUpperCase() + selectedGame.region.slice(1)}
|
2026-02-07 21:29:14 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setStep(2)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="px-6 py-2 bg-blue-600 text-white rounded-lg font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-accent-400 focus:ring-offset-2 transition-colors"
|
2026-02-07 21:29:14 +01:00
|
|
|
>
|
|
|
|
|
Next
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex items-center justify-between">
|
2026-02-17 20:48:42 +01:00
|
|
|
<p className="text-sm text-text-tertiary">Select a game to continue</p>
|
2026-02-07 21:29:14 +01:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
disabled
|
|
|
|
|
className="px-6 py-2 bg-blue-600 text-white rounded-lg font-medium disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
|
|
|
>
|
|
|
|
|
Next
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-05 15:09:25 +01:00
|
|
|
{isLoading && (
|
|
|
|
|
<div className="flex items-center justify-center py-12">
|
|
|
|
|
<div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{error && (
|
2026-02-17 20:48:42 +01:00
|
|
|
<div className="rounded-lg bg-status-failed-bg p-4 text-status-failed">
|
2026-02-05 15:09:25 +01:00
|
|
|
Failed to load games. Please try again.
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{games && (
|
|
|
|
|
<GameGrid
|
|
|
|
|
games={games}
|
|
|
|
|
selectedId={selectedGame?.id ?? null}
|
|
|
|
|
onSelect={handleGameSelect}
|
2026-02-07 21:29:14 +01:00
|
|
|
runs={runs}
|
2026-02-05 15:09:25 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{step === 2 && (
|
|
|
|
|
<div>
|
2026-02-16 20:39:41 +01:00
|
|
|
<RulesConfiguration rules={rules} onChange={setRules} hiddenRules={hiddenRules} />
|
2026-02-05 15:09:25 +01:00
|
|
|
|
|
|
|
|
<div className="mt-6 flex justify-between">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setStep(1)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="px-6 py-2 text-text-secondary bg-surface-2 rounded-lg font-medium hover:bg-surface-3 focus:outline-none focus:ring-2 focus:ring-gray-400 focus:ring-offset-2 transition-colors"
|
2026-02-05 15:09:25 +01:00
|
|
|
>
|
|
|
|
|
Back
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setStep(3)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="px-6 py-2 bg-blue-600 text-white rounded-lg font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-accent-400 focus:ring-offset-2 transition-colors"
|
2026-02-05 15:09:25 +01:00
|
|
|
>
|
|
|
|
|
Next
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{step === 3 && (
|
|
|
|
|
<div>
|
2026-02-17 20:48:42 +01:00
|
|
|
<h2 className="text-xl font-semibold text-text-primary mb-4">Name Your Run</h2>
|
2026-02-05 15:09:25 +01:00
|
|
|
|
2026-02-17 20:48:42 +01:00
|
|
|
<div className="bg-surface-1 rounded-lg shadow p-6 space-y-4">
|
2026-02-05 15:09:25 +01:00
|
|
|
<div>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="run-name"
|
2026-02-17 20:48:42 +01:00
|
|
|
className="block text-sm font-medium text-text-secondary mb-1"
|
2026-02-05 15:09:25 +01:00
|
|
|
>
|
|
|
|
|
Run Name
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
id="run-name"
|
|
|
|
|
type="text"
|
|
|
|
|
value={runName}
|
|
|
|
|
onChange={(e) => setRunName(e.target.value)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="w-full px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-400 focus:border-transparent"
|
2026-02-05 15:09:25 +01:00
|
|
|
placeholder="My Nuzlocke Run"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-11 21:36:50 +01:00
|
|
|
{namingCategories && namingCategories.length > 0 && (
|
|
|
|
|
<div>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="naming-scheme"
|
2026-02-17 20:48:42 +01:00
|
|
|
className="block text-sm font-medium text-text-secondary mb-1"
|
2026-02-11 21:36:50 +01:00
|
|
|
>
|
|
|
|
|
Naming Scheme
|
|
|
|
|
</label>
|
|
|
|
|
<select
|
|
|
|
|
id="naming-scheme"
|
|
|
|
|
value={namingScheme ?? ''}
|
|
|
|
|
onChange={(e) => setNamingScheme(e.target.value || null)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="w-full px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-400 focus:border-transparent"
|
2026-02-11 21:36:50 +01:00
|
|
|
>
|
|
|
|
|
<option value="">None (manual nicknames)</option>
|
|
|
|
|
{namingCategories.map((cat) => (
|
|
|
|
|
<option key={cat} value={cat}>
|
|
|
|
|
{cat.charAt(0).toUpperCase() + cat.slice(1)}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
2026-02-17 20:48:42 +01:00
|
|
|
<p className="mt-1 text-xs text-text-tertiary">
|
2026-02-16 20:39:41 +01:00
|
|
|
Get nickname suggestions from a themed word list when catching Pokemon.
|
2026-02-11 21:36:50 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-03-20 21:41:38 +01:00
|
|
|
<div>
|
|
|
|
|
<label
|
|
|
|
|
htmlFor="visibility"
|
|
|
|
|
className="block text-sm font-medium text-text-secondary mb-1"
|
|
|
|
|
>
|
|
|
|
|
Visibility
|
|
|
|
|
</label>
|
|
|
|
|
<select
|
|
|
|
|
id="visibility"
|
|
|
|
|
value={visibility}
|
|
|
|
|
onChange={(e) => setVisibility(e.target.value as RunVisibility)}
|
|
|
|
|
className="w-full px-3 py-2 rounded-lg border border-border-default bg-surface-2 text-text-primary focus:outline-none focus:ring-2 focus:ring-accent-400 focus:border-transparent"
|
|
|
|
|
>
|
|
|
|
|
<option value="public">Public</option>
|
|
|
|
|
<option value="private">Private</option>
|
|
|
|
|
</select>
|
|
|
|
|
<p className="mt-1 text-xs text-text-tertiary">
|
|
|
|
|
{visibility === 'private'
|
|
|
|
|
? 'Only you will be able to see this run'
|
|
|
|
|
: 'Anyone can view this run'}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-17 20:48:42 +01:00
|
|
|
<div className="border-t border-border-default pt-4">
|
|
|
|
|
<h3 className="text-sm font-medium text-text-tertiary mb-2">Summary</h3>
|
2026-02-05 15:09:25 +01:00
|
|
|
<dl className="space-y-1 text-sm">
|
|
|
|
|
<div className="flex justify-between">
|
2026-02-17 20:48:42 +01:00
|
|
|
<dt className="text-text-tertiary">Game</dt>
|
|
|
|
|
<dd className="text-text-primary font-medium">{selectedGame?.name}</dd>
|
2026-02-05 15:09:25 +01:00
|
|
|
</div>
|
|
|
|
|
<div className="flex justify-between">
|
2026-02-17 20:48:42 +01:00
|
|
|
<dt className="text-text-tertiary">Region</dt>
|
|
|
|
|
<dd className="text-text-primary font-medium">
|
2026-02-14 16:41:24 +01:00
|
|
|
{selectedGame &&
|
2026-02-16 20:39:41 +01:00
|
|
|
selectedGame.region.charAt(0).toUpperCase() + selectedGame.region.slice(1)}
|
2026-02-05 15:09:25 +01:00
|
|
|
</dd>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex justify-between">
|
2026-02-17 20:48:42 +01:00
|
|
|
<dt className="text-text-tertiary">Rules</dt>
|
|
|
|
|
<dd className="text-text-primary font-medium">
|
2026-02-05 15:09:25 +01:00
|
|
|
{enabledRuleCount} of {totalRuleCount} enabled
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
2026-02-11 21:36:50 +01:00
|
|
|
<div className="flex justify-between">
|
2026-02-17 20:48:42 +01:00
|
|
|
<dt className="text-text-tertiary">Naming Scheme</dt>
|
|
|
|
|
<dd className="text-text-primary font-medium">
|
2026-02-11 21:36:50 +01:00
|
|
|
{namingScheme
|
2026-02-16 20:39:41 +01:00
|
|
|
? namingScheme.charAt(0).toUpperCase() + namingScheme.slice(1)
|
2026-02-11 21:36:50 +01:00
|
|
|
: 'None'}
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
2026-03-20 21:41:38 +01:00
|
|
|
<div className="flex justify-between">
|
|
|
|
|
<dt className="text-text-tertiary">Visibility</dt>
|
|
|
|
|
<dd className="text-text-primary font-medium capitalize">{visibility}</dd>
|
|
|
|
|
</div>
|
2026-02-05 15:09:25 +01:00
|
|
|
</dl>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{createRun.error && (
|
2026-02-17 20:48:42 +01:00
|
|
|
<div className="mt-4 rounded-lg bg-status-failed-bg p-4 text-status-failed">
|
2026-02-05 15:09:25 +01:00
|
|
|
Failed to create run. Please try again.
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="mt-6 flex justify-between">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setStep(2)}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="px-6 py-2 text-text-secondary bg-surface-2 rounded-lg font-medium hover:bg-surface-3 focus:outline-none focus:ring-2 focus:ring-gray-400 focus:ring-offset-2 transition-colors"
|
2026-02-05 15:09:25 +01:00
|
|
|
>
|
|
|
|
|
Back
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
disabled={!runName.trim() || createRun.isPending}
|
|
|
|
|
onClick={handleCreate}
|
2026-02-17 20:48:42 +01:00
|
|
|
className="px-6 py-2 bg-blue-600 text-white rounded-lg font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-accent-400 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
2026-02-05 15:09:25 +01:00
|
|
|
>
|
|
|
|
|
{createRun.isPending ? 'Creating...' : 'Create Run'}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-02-07 21:29:14 +01:00
|
|
|
|
|
|
|
|
function SelectedGameThumb({ game }: { game: Game }) {
|
2026-02-07 21:37:15 +01:00
|
|
|
const [imgIdx, setImgIdx] = useState(0)
|
2026-02-07 21:29:14 +01:00
|
|
|
const backgroundColor = game.color ?? DEFAULT_COLOR
|
2026-02-07 21:37:15 +01:00
|
|
|
const boxArtSrcs = [`/boxart/${game.slug}.png`, `/boxart/${game.slug}.jpg`]
|
2026-02-07 21:29:14 +01:00
|
|
|
|
2026-02-07 21:37:15 +01:00
|
|
|
if (imgIdx >= boxArtSrcs.length) {
|
2026-02-07 21:29:14 +01:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className="w-10 h-10 rounded flex items-center justify-center flex-shrink-0"
|
|
|
|
|
style={{ backgroundColor }}
|
|
|
|
|
>
|
|
|
|
|
<span className="text-white text-xs font-bold drop-shadow-md">
|
|
|
|
|
{game.name.replace('Pokemon ', '').slice(0, 3)}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<img
|
2026-02-07 21:37:15 +01:00
|
|
|
src={boxArtSrcs[imgIdx]}
|
2026-02-07 21:29:14 +01:00
|
|
|
alt={game.name}
|
|
|
|
|
className="w-10 h-10 rounded object-cover flex-shrink-0"
|
2026-02-07 21:37:15 +01:00
|
|
|
onError={() => setImgIdx((i) => i + 1)}
|
2026-02-07 21:29:14 +01:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|