-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from BYU-CPC/make-leaderboards-modular
make leaderboards modular and add progress bar
- Loading branch information
Showing
12 changed files
with
177 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from "react"; | ||
import { useLeaderboardIndex } from "../hooks/UseLeaderboard"; | ||
import { useDebounce } from "use-debounce"; | ||
import ProgressBar from "./ProgressBar"; | ||
|
||
const formatTime = (time: number) => { | ||
if (time < 0) { | ||
return "Ended"; | ||
} | ||
const seconds = String(Math.floor((time / 1000) % 60)).padStart(2, "0"); | ||
const minutes = String(Math.floor((time / (1000 * 60)) % 60)).padStart( | ||
2, | ||
"0" | ||
); | ||
const hours = Math.floor((time / (1000 * 60 * 60)) % 24); | ||
const days = Math.floor(time / (1000 * 60 * 60 * 24)); | ||
if (days) return `${days} days, ${hours}:${minutes}:${seconds}`; | ||
return `${hours}:${minutes}:${seconds}`; | ||
}; | ||
|
||
//Take in react props like classname | ||
function Countdown({ | ||
leaderboard, | ||
className, | ||
}: { | ||
leaderboard: string; | ||
className?: string; | ||
}) { | ||
const { data } = useLeaderboardIndex(); | ||
const board = data?.[leaderboard]; | ||
const currentTime = useDebounce(new Date(), 500)[0]; | ||
if (!board) return null; | ||
const timeDisplay = formatTime(board.end.getTime() - currentTime.getTime()); | ||
const progress = | ||
(currentTime.getTime() - (board.start.getTime() ?? 0)) / | ||
((board.end.getTime() ?? 1) - (board.start.getTime() ?? 0)); | ||
return ( | ||
<ProgressBar | ||
progress={progress} | ||
display={timeDisplay} | ||
className={className} | ||
/> | ||
); | ||
} | ||
|
||
export default Countdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from "react"; | ||
function ProgressBar({ | ||
progress, | ||
className, | ||
display, | ||
fontSize, | ||
}: { | ||
progress: number; | ||
display?: string; | ||
className?: string; | ||
fontSize?: string; | ||
}) { | ||
const size = fontSize ?? "10px"; | ||
return ( | ||
<div | ||
className={" center " + (className ?? "")} | ||
style={{ position: "relative" }} | ||
> | ||
<div | ||
className=" " | ||
style={{ | ||
position: "absolute", | ||
width: `${progress * 100}%`, | ||
minHeight: "1px", | ||
backgroundColor: "var(--accent-color)", | ||
background: `linear-gradient(90deg, var(--accent-color) 0%, var(--accent-color-two) 100%)`, | ||
color: "transparent", | ||
userSelect: "none", | ||
fontSize: size, | ||
zIndex: -1, | ||
}} | ||
> | ||
{display ?? ""} | ||
</div> | ||
<div className="w-full" style={{ fontSize: size }}> | ||
{display ?? ""} | ||
</div> | ||
</div> | ||
); | ||
} | ||
export default ProgressBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import axios from "axios"; | ||
import { BACKEND_URL } from "./base"; | ||
|
||
export type Leaderboard = { start: Date; end: Date }; | ||
|
||
export type LeaderboardIndex = Record<string, Leaderboard>; | ||
type LeaderboardResponse = Record< | ||
string, | ||
{ | ||
start: number; | ||
end: number; | ||
} | ||
>; | ||
|
||
const getLeaderboardIndex = async (): Promise<LeaderboardResponse> => { | ||
const response = await axios.get(`${BACKEND_URL}/leaderboard`); | ||
return response.data; | ||
}; | ||
|
||
export const useLeaderboardIndex = () => { | ||
const query = useQuery({ | ||
queryKey: ["leaderboardIndex"], | ||
queryFn: () => getLeaderboardIndex(), | ||
staleTime: 1000 * 60 * 60, | ||
}); | ||
const transformedData = query.data | ||
? Object.fromEntries( | ||
Object.entries(query.data).map(([key, value]) => [ | ||
key, | ||
{ | ||
start: new Date(value.start * 1000), | ||
end: new Date(value.end * 1000), | ||
}, | ||
]) | ||
) | ||
: undefined; | ||
return { ...query, data: transformedData }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,5 @@ export const useUsers = () => { | |
}); | ||
} | ||
} | ||
console.log(query.data); | ||
return query.data ?? []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.