Skip to content

Commit 50912f4

Browse files
committed
Show last updated time
1 parent a4a8cef commit 50912f4

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Dashboard.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Link, useParams } from "react-router";
66
import { Exercise, useGetExercisesQuery } from "./api/queries/get_exercises";
77
import { useGetUserQuery } from "./api/queries/get_user";
88
import { useGetUserProgressQuery } from "./api/queries/get_user_progress";
9+
import { useGetUserUpdateTimeQuery } from "./api/queries/get_user_update_time";
910
import Spinner from "./components/Spinner";
1011

1112
type UserProblemSetStatus = string;
@@ -73,6 +74,32 @@ function Dashboard() {
7374
await refetchUserProgress()
7475
}, [refetchUserProgress])
7576

77+
const {
78+
data: latestActionRun
79+
} = useGetUserUpdateTimeQuery(user?.id)
80+
81+
const formatTimeWithTimezone = useCallback((dateObj: Date) => {
82+
const formatter = new Intl.DateTimeFormat('en-GB', {
83+
day: '2-digit',
84+
month: 'short',
85+
year: 'numeric',
86+
hour: '2-digit',
87+
minute: '2-digit',
88+
hour12: false,
89+
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
90+
});
91+
92+
return formatter.format(dateObj)
93+
}, [])
94+
95+
const expectedNextUpdateTime = useMemo(() => {
96+
if (latestActionRun == null) {
97+
return null
98+
}
99+
const expectedTime = new Date(latestActionRun.getTime() + 30 * 60 * 1000)
100+
return expectedTime
101+
}, [latestActionRun])
102+
76103
return (
77104
<div className="lg:w-[40%] my-16 mx-auto md:w-[60%] w-[80%]">
78105
<h3 className="text-2xl font-bold mb-4">Git Mastery Progress Dashboard</h3>
@@ -89,6 +116,12 @@ function Dashboard() {
89116
</div>
90117
<p className="text-gray-700 font-semibold">Find your progress for the various Git Mastery exercises.</p>
91118
<p className="text-gray-700">To view all exercises, visit the <a className="text-blue-800 underline" href="https://git-mastery.github.io/exercises">exercises directory</a>.</p>
119+
{latestActionRun != null && expectedNextUpdateTime != null && (
120+
<p className="mt-2 italic">Last updated on {formatTimeWithTimezone(latestActionRun)}.
121+
<br />Next update around {formatTimeWithTimezone(expectedNextUpdateTime)}
122+
<br />If there is a discrepancy, open a ticket with the Git-Mastery team <a className="text-blue-800 underline" href="https://github.com/git-mastery/git-mastery">here</a>
123+
</p>
124+
)}
92125
</div>
93126
<div>
94127
{(isUserLoading || isUserProgressLoading || isUserProgressRefetching || isProblemSetsLoading) ? (
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import axios from "axios"
2+
import { useQuery } from "react-query"
3+
4+
export interface UserCommit {
5+
commit: {
6+
author: {
7+
date: string
8+
}
9+
}
10+
}
11+
12+
export const getUserUpdateTime = async (userId: number) => {
13+
try {
14+
const result = await axios.get<UserCommit[]>(`https://api.github.com/repos/git-mastery/progress/commits?path=students/${userId}.json&sha=tracker&per_page=1`);
15+
return new Date(result.data[0].commit.author.date);
16+
} catch {
17+
return null;
18+
}
19+
}
20+
21+
export const useGetUserUpdateTimeQuery = (userId: number | undefined) => {
22+
return useQuery<Date | null>({
23+
queryKey: ["get-user-update-time", userId],
24+
queryFn: () => getUserUpdateTime(userId!),
25+
enabled: userId != null,
26+
});
27+
}
28+

0 commit comments

Comments
 (0)