import { h } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import styles from '../NewGame.module.css'; import type { BreakRule } from '@lib/domain/types'; import { WizardNav } from '../components/WizardNav'; import { WizardStepForm } from '../components/WizardStepForm'; interface BreakOrderStepProps { players: string[]; rule: BreakRule; onNext: (first: number, second?: number) => void; onCancel: () => void; initialFirst?: number; initialSecond?: number; } export const BreakOrderStep = ({ players, rule, onNext, onCancel, initialFirst = 1, initialSecond }: BreakOrderStepProps) => { const playerCount = players.filter(Boolean).length; const [first, setFirst] = useState(initialFirst); const [second, setSecond] = useState(initialSecond); useEffect(() => { if (!initialSecond && rule === 'wechselbreak' && playerCount === 3) { setSecond(2); } }, [initialSecond, rule, playerCount]); const handleFirst = (idx: number) => { setFirst(idx); const isImmediateFlow = rule !== 'wechselbreak' || playerCount !== 3; if (isImmediateFlow) { onNext(idx); } }; const handleSecond = (idx: number) => { setSecond(idx); if (rule === 'wechselbreak' && playerCount === 3 && first > 0) { onNext(first, idx); } }; const handleSubmit = (e: Event) => { e.preventDefault(); if (rule === 'wechselbreak' && playerCount === 3) { if (first > 0 && (second ?? 0) > 0) { onNext(first, second); } return; } if (first > 0) { onNext(first); } }; return ( 0 && (second ?? 0) > 0) : !(first > 0) } /> } >
Wer hat den ersten Anstoss?
{players.filter(Boolean).map((name, idx) => ( ))}
{rule === 'wechselbreak' && playerCount === 3 && ( <>
Wer hat den zweiten Anstoss?
{players.filter(Boolean).map((name, idx) => ( ))}
)}
); };