|
| 1 | +import { Link, useParams } from "react-router"; |
| 2 | +import { useGetUserQuery } from "./api/queries/get_user"; |
| 3 | +import Spinner from "./components/Spinner"; |
| 4 | +import { useGetUserProgressQuery } from "./api/queries/get_user_progress"; |
| 5 | +import { ProblemSet, useGetProblemSetsQuery } from "./api/queries/get_problem_sets"; |
| 6 | +import { useMemo } from "react"; |
| 7 | + |
| 8 | +type UserProblemSetStatus = string; |
| 9 | + |
| 10 | +function Dashboard() { |
| 11 | + let { username } = useParams(); |
| 12 | + |
| 13 | + const { |
| 14 | + data: user, |
| 15 | + isLoading: isUserLoading, |
| 16 | + } = useGetUserQuery(username); |
| 17 | + |
| 18 | + const { |
| 19 | + data: userProgress, |
| 20 | + isLoading: isUserProgressLoading, |
| 21 | + } = useGetUserProgressQuery(user?.id); |
| 22 | + |
| 23 | + const parsedUserProgress = useMemo(() => { |
| 24 | + if (isUserProgressLoading || userProgress == null) { |
| 25 | + return new Map<string, UserProblemSetStatus>(); |
| 26 | + } |
| 27 | + |
| 28 | + const progress = new Map<string, UserProblemSetStatus>(); |
| 29 | + for (const up of userProgress) { |
| 30 | + if (!progress.has(up.exercise_name)) { |
| 31 | + progress.set(up.exercise_name, up.status) |
| 32 | + } else if (progress.get(up.exercise_name) !== "SUCCESSFUL" && up.status === "SUCCESSFUL") { |
| 33 | + // Take any success |
| 34 | + progress.set(up.exercise_name, up.status) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return progress |
| 39 | + }, [userProgress, isUserProgressLoading]) |
| 40 | + |
| 41 | + const { |
| 42 | + data: problemSets, |
| 43 | + isLoading: isProblemSetsLoading, |
| 44 | + } = useGetProblemSetsQuery(); |
| 45 | + |
| 46 | + const problemSetGroups = useMemo(() => { |
| 47 | + if (isProblemSetsLoading || problemSets == null) { |
| 48 | + return new Map<string, ProblemSet[]>(); |
| 49 | + } |
| 50 | + |
| 51 | + const repoGroups = new Map<string, ProblemSet[]>(); |
| 52 | + for (const repo of problemSets) { |
| 53 | + for (const topic of repo.topics) { |
| 54 | + if (topic !== "problem-set") { |
| 55 | + if (parsedUserProgress.has(repo.name)) { |
| 56 | + if (!repoGroups.has(topic)) { |
| 57 | + repoGroups.set(topic, []) |
| 58 | + } |
| 59 | + repoGroups.get(topic)!.push(repo) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const sortedGroups = new Map([...repoGroups.entries()].sort()) |
| 66 | + return sortedGroups |
| 67 | + }, [problemSets, isProblemSetsLoading, parsedUserProgress]) |
| 68 | + |
| 69 | + console.log(problemSetGroups) |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="w-[40%] my-28 mx-auto"> |
| 73 | + <h3 className="text-2xl font-bold mb-4">Git Mastery Progress Dashboard</h3> |
| 74 | + <div className="mb-6"> |
| 75 | + <Link to="/" className="text-gray-500 italic mb-2">← Back to search</Link> |
| 76 | + <h1 className="text-4xl font-bold mb-4">@{username}</h1> |
| 77 | + <p className="text-gray-700 font-semibold">Find your progress for the various Git Mastery problem sets.</p> |
| 78 | + <p className="text-gray-700">To view all problem sets, visit the <a className="text-blue-800 underline" href="https://github.com/git-mastery/problems-directory">problems directory</a>.</p> |
| 79 | + </div> |
| 80 | + <div> |
| 81 | + {(isUserLoading || isUserProgressLoading || isProblemSetsLoading) ? ( |
| 82 | + <div className="flex justify-center"> |
| 83 | + <Spinner /> |
| 84 | + </div> |
| 85 | + ) : ( |
| 86 | + user == null ? ( |
| 87 | + <div className="text-center"> |
| 88 | + <p className="mb-4 text-red-700">User <strong>{username}</strong> does not exist</p> |
| 89 | + <Link to="/" className="hover:cursor-pointer border-1 border-red-700 bg-red-700 text-white rounded-sm px-4 py-2 font-semibold">← Return to search</Link> |
| 90 | + </div> |
| 91 | + ) : ( |
| 92 | + problemSetGroups.size === 0 ? ( |
| 93 | + <div className="text-center"> |
| 94 | + <p className="mb-4">You have not completed any problem sets yet</p> |
| 95 | + <a href="https://github.com/git-mastery/problems-directory" target="_blank" className="hover:cursor-pointer border-1 border-blue-800 bg-blue-800 text-white rounded-sm px-4 py-2 font-semibold">Go to problems directory ↗</a> |
| 96 | + </div> |
| 97 | + ) : ( |
| 98 | + Array.from(problemSetGroups) |
| 99 | + .map(([key, value]) => { |
| 100 | + return ( |
| 101 | + <div key={key} className="not-last:mb-4"> |
| 102 | + <h2 className="text-2xl font-bold mb-2"><code>{key}</code></h2> |
| 103 | + <table className="table-fixed w-full bg-white border border-gray-300 rounded-sm"> |
| 104 | + <thead> |
| 105 | + <tr> |
| 106 | + <th className="bg-gray-200 border border-gray-300 px-4 py-2 text-left">Exercise name</th> |
| 107 | + <th className="bg-gray-200 border border-gray-300 px-4 py-2 text-left">Completion Status</th> |
| 108 | + </tr> |
| 109 | + </thead> |
| 110 | + <tbody> |
| 111 | + {value |
| 112 | + .filter(problemSet => parsedUserProgress.has(problemSet.name)) |
| 113 | + .map(problemSet => ( |
| 114 | + <tr key={problemSet.id}> |
| 115 | + <td className="border border-gray-300 px-4 py-2 text-left"><a target="_blank" href={problemSet.html_url}><code className="underline text-blue-800">{problemSet.name}</code></a></td> |
| 116 | + <td className="border border-gray-300 px-4 py-2 text-left">{parsedUserProgress.get(problemSet.name)}</td> |
| 117 | + </tr> |
| 118 | + ))} |
| 119 | + </tbody> |
| 120 | + </table> |
| 121 | + </div> |
| 122 | + ) |
| 123 | + }) |
| 124 | + ) |
| 125 | + ) |
| 126 | + )} |
| 127 | + </div> |
| 128 | + </div > |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +export default Dashboard; |
0 commit comments