From 6bc35fc0bbd70dc4d4994bf50e3972b3b85c6fff Mon Sep 17 00:00:00 2001 From: Oronemu Date: Sun, 21 Dec 2025 03:46:37 +0700 Subject: [PATCH] http checking --- frontend/src/pages/LobbyPage.tsx | 46 ++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/frontend/src/pages/LobbyPage.tsx b/frontend/src/pages/LobbyPage.tsx index dd6b9aa..729031a 100644 --- a/frontend/src/pages/LobbyPage.tsx +++ b/frontend/src/pages/LobbyPage.tsx @@ -29,9 +29,43 @@ export function LobbyPage() { const [showAddGame, setShowAddGame] = useState(false) const [gameTitle, setGameTitle] = useState('') const [gameUrl, setGameUrl] = useState('') + const [gameUrlError, setGameUrlError] = useState(null) const [gameGenre, setGameGenre] = useState('') const [isAddingGame, setIsAddingGame] = useState(false) + const validateUrl = (url: string): boolean => { + if (!url.trim()) return true // Empty is ok, will be caught by required check + try { + const parsed = new URL(url.trim()) + if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { + return false + } + // Check that hostname has at least one dot (domain.tld) + const hostname = parsed.hostname + if (!hostname || !hostname.includes('.')) { + return false + } + // Check that TLD is valid (2-6 letters only, like com, ru, org, online) + const parts = hostname.split('.') + const tld = parts[parts.length - 1].toLowerCase() + if (tld.length < 2 || tld.length > 6 || !/^[a-z]+$/.test(tld)) { + return false + } + return true + } catch { + return false + } + } + + const handleGameUrlChange = (value: string) => { + setGameUrl(value) + if (value.trim() && !validateUrl(value)) { + setGameUrlError('Введите корректную ссылку (например: https://store.steampowered.com/...)') + } else { + setGameUrlError(null) + } + } + // Moderation const [moderatingGameId, setModeratingGameId] = useState(null) @@ -141,7 +175,7 @@ export function LobbyPage() { } const handleAddGame = async () => { - if (!id || !gameTitle.trim() || !gameUrl.trim()) return + if (!id || !gameTitle.trim() || !gameUrl.trim() || !validateUrl(gameUrl)) return setIsAddingGame(true) try { @@ -152,6 +186,7 @@ export function LobbyPage() { }) setGameTitle('') setGameUrl('') + setGameUrlError(null) setGameGenre('') setShowAddGame(false) await loadData() @@ -1696,9 +1731,10 @@ export function LobbyPage() { onChange={(e) => setGameTitle(e.target.value)} /> setGameUrl(e.target.value)} + onChange={(e) => handleGameUrlChange(e.target.value)} + error={gameUrlError || undefined} /> {isOrganizer ? 'Добавить' : 'Предложить'} - setShowAddGame(false)}> + { setShowAddGame(false); setGameUrlError(null) }}> Отмена