diff --git a/src/games/GamesBrowser.tsx b/src/games/GamesBrowser.tsx index 1ba5e48..8f1eef7 100644 --- a/src/games/GamesBrowser.tsx +++ b/src/games/GamesBrowser.tsx @@ -3,11 +3,14 @@ import games from "./games"; import { useUser } from "../user-context"; import Link from "../components/Link"; import SmoothCollapse from "react-smooth-collapse"; +import { + AT, +} from "@lingdocs/lingdocs-main"; function GamesBrowser() { const { user } = useUser(); const [opened, setOpened] = useState(undefined); - function handleTitleClick(id: string) { + function handleChapterClick(id: string) { setOpened(prev => ( prev === id ? undefined : id )); @@ -15,35 +18,101 @@ function GamesBrowser() { return
{games.map((chapter) => (
-

{chapter.chapter}

- {chapter.items.map(({ id, title, Game, studyLink }) => { - const done = user && user.tests.some(t => t.id === id); - const open = opened === id; - return
-
-
-

handleTitleClick(id)}> - {title} - {` `} -

-
-
-

- {done ? "✅" - : - {"📚"} - } -

-
-
- - - -
- })} +
))}
} +function ChapterDisplay({ chapter, user, handleClick, expanded }: { + chapter: { chapter: string, items: GameRecord[] }, + user: AT.LingdocsUser | undefined, + handleClick: (chapter: string) => void, + expanded: boolean, +}) { + const [opened, setOpened] = useState(undefined); + const progress = getPercentageComplete(chapter, user?.tests); + function handleTitleClick(id: string) { + setOpened(prev => ( + prev === id ? undefined : id + )); + } + return
+
handleClick(chapter.chapter)}> +
+

{chapter.chapter}

+ +
+
+ + {chapter.items.map(({ id, title, Game, studyLink }) => { + const done = user && user.tests.some(t => t.id === id); + const open = opened === id; + return
+
+
+

handleTitleClick(id)}> + {title} + {` `} +

+
+
+

+ {done ? "✅" + : + {"📚"} + } +

+
+
+ + + +
+ })} +
+
+ +} + +function ChapterProgress({ progress }: { progress: "not logged in" | number }) { + if (progress === "not logged in") { + return
Log in to see progress
; + } + return
+
{progress}% mastered
+
+
+
+
; +} + +function getPercentageComplete( + chapter: { chapter: string, items: GameRecord[] }, + tests: undefined | AT.TestResult[], +): "not logged in" | number { + if (!tests) return "not logged in"; + const chapterTestIds = chapter.items.map(gr => gr.id); + const userCompletedIds = tests.map(t => t.id); + const required = chapterTestIds.length; + const completed = chapterTestIds + .filter(userCompletedIds.includes) + .length; + + return Math.round( + (completed / (required + 1)) * 100 + ); +} + export default GamesBrowser; \ No newline at end of file