import chapter

This commit is contained in:
2026-06-09 14:08:35 +02:00
parent 7b2bf3161b
commit fcb1433403
7 changed files with 240 additions and 16 deletions
+67 -11
View File
@@ -1,20 +1,76 @@
import { useLichess } from "#/hooks/useLichess.ts";
import { useEffect, useRef, useState } from "react";
import { type IUserStudyChapter, useLichess } from "#/hooks/useLichess.ts";
const SelectLichessStudy = () => {
const { getLichessUserStudies } = useLichess();
interface IProps {
setSelectedStudyId: (studyId: string) => void;
setStudyChapters: (chapters: IUserStudyChapter[]) => void;
}
const SelectLichessStudy = ({ setSelectedStudyId }: IProps) => {
const [isOpen, setIsOpen] = useState(false);
const { getLichessUserStudies, userStudies } = useLichess();
const dropdownRef = useRef<HTMLDivElement>(null);
const onClickHandler = async () => {
await getLichessUserStudies();
setIsOpen(!isOpen);
if (!userStudies.length) {
await getLichessUserStudies();
}
};
const handleStudyClick = async (studyId: string) => {
setSelectedStudyId(studyId);
setIsOpen(false);
};
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
isOpen &&
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setIsOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [isOpen]);
return (
<button
type="button"
className="btn btn-secondary"
onClick={onClickHandler}
>
Select Lichess Study
</button>
<div ref={dropdownRef} className="relative">
<button
type="button"
className="btn btn-secondary"
onClick={onClickHandler}
>
Select Lichess Study
</button>
{isOpen ? (
<div className="absolute z-3 bg-(--bg-base) mt-2 max-h-75 border rounded-lg p-2 overflow-y-scroll">
<input type="text" placeholder="Search..." />
<ul>
{userStudies.map((study) => (
<li key={study.id}>
<button
type="button"
className="p-2 cursor-pointer hover:bg-(--neutral-content)/20 text-left w-full"
onClick={() => handleStudyClick(study.id)}
>
{study.name}
</button>
</li>
))}
</ul>
</div>
) : null}
</div>
);
};
+63
View File
@@ -0,0 +1,63 @@
import { useRef, useState } from "react";
import { type IUserStudyChapter, useLichess } from "#/hooks/useLichess.ts";
interface IProps {
selectedStudyId: string;
gameDispatch: any;
}
const SelectStudyChapter = ({ selectedStudyId, gameDispatch }: IProps) => {
const [isOpen, setIsOpen] = useState(false);
const [studyChapters, setStudyChapters] = useState<IUserStudyChapter[]>([]);
const dropdownRef = useRef<HTMLDivElement>(null);
const { getLichessStudyChapters, loadLichessStudyChapter } = useLichess();
const onClickHandler = async () => {
const studies = await getLichessStudyChapters({ studyId: selectedStudyId });
if (!studies) return;
setStudyChapters(studies);
setIsOpen(!isOpen);
};
const handleChapterClick = async (chapter: IUserStudyChapter) => {
const { headers, pgn } = await loadLichessStudyChapter({ chapter });
gameDispatch({ type: "SET_PGN", payload: headers, pgn });
setIsOpen(false);
};
return (
<div ref={dropdownRef} className="relative">
<button
type="button"
className="btn btn-secondary"
onClick={onClickHandler}
>
Import Study Chapter/Game
</button>
{isOpen ? (
<div className="absolute z-3 bg-(--bg-base) mt-2 max-h-75 border rounded-lg p-2 overflow-y-scroll">
<input type="text" placeholder="Search..." />
<ul>
{studyChapters?.map((chapter) => (
<li key={`${chapter.chapterId}${chapter.name}`}>
<button
type="button"
className="p-2 cursor-pointer hover:bg-(--neutral-content)/20 text-left w-full"
onClick={() => handleChapterClick(chapter)}
>
{chapter.name}
</button>
</li>
))}
</ul>
</div>
) : null}
</div>
);
};
export default SelectStudyChapter;