import { h } from 'preact'; import { useState, useEffect, useRef } from 'preact/hooks'; import styles from './NewGame.module.css'; import modalStyles from './PlayerSelectModal.module.css'; import { UI_CONSTANTS, WIZARD_STEPS, GAME_TYPE_OPTIONS, RACE_TO_QUICK_PICKS, RACE_TO_DEFAULT, RACE_TO_INFINITY, ERROR_MESSAGES, ARIA_LABELS, FORM_CONFIG, ERROR_STYLES } from '../utils/constants'; interface PlayerSelectModalProps { players: string[]; onSelect: (player: string) => void; onClose: () => void; } export const PlayerSelectModal = ({ players, onSelect, onClose }: PlayerSelectModalProps) => (
e.stopPropagation()}>

Alle Spieler

{players.map(player => ( ))}
); interface PlayerStepProps { playerNameHistory: string[]; onNext: (name: string) => void; onCancel: () => void; initialValue?: string; } /** * Player 1 input step for multi-step game creation wizard. */ const Player1Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }: PlayerStepProps) => { const [player1, setPlayer1] = useState(initialValue); const [error, setError] = useState(null); const [filteredNames, setFilteredNames] = useState(playerNameHistory); const [isModalOpen, setIsModalOpen] = useState(false); const inputRef = useRef(null); useEffect(() => { const el = inputRef.current; if (el) { el.focus(); const end = el.value.length; try { el.setSelectionRange(end, end); } catch {} } }, []); useEffect(() => { if (!player1) { setFilteredNames(playerNameHistory); } else { setFilteredNames( playerNameHistory.filter(name => name.toLowerCase().includes(player1.toLowerCase()) ) ); } }, [player1, playerNameHistory]); const handleSubmit = (e: Event) => { e.preventDefault(); const trimmedName = player1.trim(); if (!trimmedName) { setError(ERROR_MESSAGES.PLAYER1_REQUIRED); if (inputRef.current) { inputRef.current.focus(); inputRef.current.setAttribute('aria-invalid', 'true'); } return; } if (trimmedName.length > FORM_CONFIG.MAX_PLAYER_NAME_LENGTH) { setError(`Spielername darf maximal ${FORM_CONFIG.MAX_PLAYER_NAME_LENGTH} Zeichen lang sein`); if (inputRef.current) { inputRef.current.focus(); inputRef.current.setAttribute('aria-invalid', 'true'); } return; } setError(null); if (inputRef.current) { inputRef.current.setAttribute('aria-invalid', 'false'); } onNext(trimmedName); }; const handleQuickPick = (name: string) => { setError(null); onNext(name); }; const handleModalSelect = (name: string) => { setIsModalOpen(false); handleQuickPick(name); }; const handleClear = () => { setPlayer1(''); setError(null); if (inputRef.current) inputRef.current.focus(); }; return (
Neues Spiel – Schritt {WIZARD_STEPS.PLAYER1}/{UI_CONSTANTS.TOTAL_WIZARD_STEPS}
{ const target = e.target as HTMLInputElement; const value = target.value; setPlayer1(value); // Real-time validation feedback if (value.length > FORM_CONFIG.MAX_PLAYER_NAME_LENGTH) { setError(`Spielername darf maximal ${FORM_CONFIG.MAX_PLAYER_NAME_LENGTH} Zeichen lang sein`); target.setAttribute('aria-invalid', 'true'); } else if (value.trim() && error) { setError(null); target.setAttribute('aria-invalid', 'false'); } }} autoComplete="off" aria-label="Name Spieler 1" aria-describedby="player1-help" style={{ fontSize: UI_CONSTANTS.INPUT_FONT_SIZE, minHeight: UI_CONSTANTS.INPUT_MIN_HEIGHT, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: UI_CONSTANTS.INPUT_PADDING_RIGHT }} ref={inputRef} />
Geben Sie den Namen für Spieler 1 ein. Maximal {FORM_CONFIG.MAX_PLAYER_NAME_LENGTH} Zeichen erlaubt.
{player1.length > FORM_CONFIG.CHARACTER_COUNT_WARNING_THRESHOLD && (
FORM_CONFIG.MAX_PLAYER_NAME_LENGTH ? '#f44336' : '#ff9800', marginTop: '4px', textAlign: 'right' }}> {player1.length}/{FORM_CONFIG.MAX_PLAYER_NAME_LENGTH} Zeichen
)} {player1 && ( )}
{filteredNames.length > 0 && (
{filteredNames.slice(0, UI_CONSTANTS.MAX_QUICK_PICKS).map((name, idx) => ( ))} {playerNameHistory.length > UI_CONSTANTS.MAX_QUICK_PICKS && ( )}
)}
{error && (
⚠️ {error}
)} {isModalOpen && ( setIsModalOpen(false)} /> )}
); }; /** * Player 2 input step for multi-step game creation wizard. */ const Player2Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }: PlayerStepProps) => { const [player2, setPlayer2] = useState(initialValue); const [error, setError] = useState(null); const [filteredNames, setFilteredNames] = useState(playerNameHistory); const inputRef = useRef(null); useEffect(() => { const el = inputRef.current; if (el) { el.focus(); const end = el.value.length; try { el.setSelectionRange(end, end); } catch {} } }, []); useEffect(() => { if (!player2) { setFilteredNames(playerNameHistory); } else { setFilteredNames( playerNameHistory.filter(name => name.toLowerCase().includes(player2.toLowerCase()) ) ); } }, [player2, playerNameHistory]); const handleSubmit = (e: Event) => { e.preventDefault(); if (!player2.trim()) { setError('Bitte Namen für Spieler 2 eingeben'); return; } setError(null); onNext(player2.trim()); }; const handleQuickPick = (name: string) => { setError(null); onNext(name); }; const handleClear = () => { setPlayer2(''); setError(null); if (inputRef.current) inputRef.current.focus(); }; return (
Neues Spiel – Schritt 2/5
{ const target = e.target as HTMLInputElement; setPlayer2(target.value); }} autoComplete="off" aria-label="Name Spieler 2" style={{ fontSize: '1.2rem', minHeight: 48, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: 44 }} ref={inputRef} /> {player2 && ( )}
{filteredNames.length > 0 && (
{filteredNames.slice(0, 10).map((name, idx) => ( ))}
)}
{error &&
{error}
}
); }; /** * Player 3 input step for multi-step game creation wizard. */ const Player3Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }: PlayerStepProps) => { const [player3, setPlayer3] = useState(initialValue); const [filteredNames, setFilteredNames] = useState(playerNameHistory); const inputRef = useRef(null); useEffect(() => { const el = inputRef.current; if (el) { el.focus(); const end = el.value.length; try { el.setSelectionRange(end, end); } catch {} } }, []); useEffect(() => { if (!player3) { setFilteredNames(playerNameHistory); } else { setFilteredNames( playerNameHistory.filter(name => name.toLowerCase().includes(player3.toLowerCase()) ) ); } }, [player3, playerNameHistory]); const handleSubmit = (e: Event) => { e.preventDefault(); // Player 3 is optional, so always allow submission onNext(player3.trim()); }; const handleQuickPick = (name: string) => { onNext(name); }; const handleClear = () => { setPlayer3(''); if (inputRef.current) inputRef.current.focus(); }; const handleSkip = (e: Event) => { e.preventDefault(); onNext(''); }; return (
Neues Spiel – Schritt 3/5
{ const target = e.target as HTMLInputElement; setPlayer3(target.value); }} autoComplete="off" aria-label="Name Spieler 3" style={{ fontSize: '1.2rem', minHeight: 48, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: 44 }} ref={inputRef} /> {player3 && ( )}
{filteredNames.length > 0 && (
{filteredNames.slice(0, 10).map((name, idx) => ( ))}
)}
); }; interface GameTypeStepProps { onNext: (type: string) => void; onCancel: () => void; initialValue?: string; } /** * Game Type selection step for multi-step game creation wizard. */ const GameTypeStep = ({ onNext, onCancel, initialValue = '' }: GameTypeStepProps) => { const [gameType, setGameType] = useState(initialValue); const gameTypes = ['8-Ball', '9-Ball', '10-Ball']; const handleSelect = (selectedType: string) => { setGameType(selectedType); // Auto-advance to next step on selection onNext(selectedType); }; const handleSubmit = (e: Event) => { e.preventDefault(); if (gameType) { onNext(gameType); } }; return (
Neues Spiel – Schritt 4/5
{gameTypes.map(type => ( ))}
); }; interface RaceToStepProps { onNext: (raceTo: string | number) => void; onCancel: () => void; initialValue?: string | number; gameType?: string; } /** * Race To selection step for multi-step game creation wizard. */ const RaceToStep = ({ onNext, onCancel, initialValue = '', gameType }: RaceToStepProps) => { const quickPicks = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const defaultValue = 5; const [raceTo, setRaceTo] = useState(initialValue !== '' ? initialValue : defaultValue); useEffect(() => { if ((initialValue === '' || initialValue === undefined) && raceTo !== defaultValue) { setRaceTo(defaultValue); } if (initialValue !== '' && initialValue !== undefined && initialValue !== raceTo) { setRaceTo(initialValue); } }, [gameType, initialValue, defaultValue]); const handleQuickPick = (value: number) => { // For endlos (endless) games, use Infinity to prevent automatic completion const selected = value === 0 ? 'Infinity' : value; setRaceTo(selected); // Auto-advance to the next step (finalize) when a quick pick is chosen const raceToValue = selected === 'Infinity' ? Infinity : (parseInt(String(selected), 10) || 0); onNext(raceToValue); }; const handleInputChange = (e: Event) => { const target = e.target as HTMLInputElement; setRaceTo(target.value); }; const handleSubmit = (e: Event) => { e.preventDefault(); // Handle Infinity for endlos games, otherwise parse as integer const raceToValue = raceTo === 'Infinity' ? Infinity : (parseInt(String(raceTo), 10) || 0); onNext(raceToValue); }; return (
Neues Spiel – Schritt 5/5
{quickPicks.map(value => ( ))}
); }; export { Player1Step, Player2Step, Player3Step, GameTypeStep, RaceToStep };