import { useState } from "react"; import { useLichess } from "#/hooks/useLichess.ts"; const parseStudyId = (url: string): string | null => { const match = url.match( /(?:https?:\/\/)?(?:www\.)?lichess\.org\/study\/([a-zA-Z0-9]+)/, ); return match?.[1] ?? null; }; const LichessStudyUrlInput = () => { const [url, setUrl] = useState(""); const [feedback, setFeedback] = useState<{ type: "success" | "error"; message: string; } | null>(null); const { setSelectedStudyId, selectedStudyId } = useLichess(); const handleSubmit = () => { if (!url.trim()) { setFeedback({ type: "error", message: "Please enter a study URL" }); return; } const studyId = parseStudyId(url.trim()); if (!studyId) { setFeedback({ type: "error", message: "Invalid Lichess study URL", }); return; } if (studyId === selectedStudyId) { setFeedback({ type: "success", message: "Study already selected", }); return; } setSelectedStudyId(studyId); setFeedback({ type: "success", message: "Study loaded — pick a chapter" }); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") handleSubmit(); }; return (