Skip to content

Commit

Permalink
add time sync warning
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed Jul 28, 2024
1 parent a586337 commit 7b0657a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/api/endpoints/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import client from "../client";

export interface getServerInfoResponse {
datetime: string;
}

export async function getServerInfo(): Promise<getServerInfoResponse> {
const response = await client.get<getServerInfoResponse>(`/api/info`);
return response.data;
}
14 changes: 14 additions & 0 deletions src/utilities/time.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getServerInfo } from "../api/endpoints/time";

export const secondsToHHMMSS = (seconds: number): string => {
return new Date(seconds).toISOString().slice(11, 19);
};
Expand All @@ -14,3 +16,15 @@ export const datetimeToddmmHHMMSS = (datetime: string): string => {
const date = new Date(datetime);
return date.toLocaleDateString() + " " + date.toLocaleTimeString();
};

const maxDiffAllowedMiliseconds = 10 * 1000; // 10 seconds

export const isLocalTimeSynchronized = async (): Promise<[boolean, number]> => {
const info = await getServerInfo();
const diff = Date.now() - new Date(info.datetime).getTime();
const absDiff = Math.abs(diff);

let isSynchronized = absDiff < maxDiffAllowedMiliseconds;

return [isSynchronized, diff];
};
66 changes: 66 additions & 0 deletions src/views/Login/components/TimeSynchronizationWarningDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
Button,
Dialog,
DialogActions,
DialogContent,
Stack,
useTheme,
} from "@mui/material";
import { FunctionComponent, useEffect, useState } from "react";
import { TbClockX } from "react-icons/tb";
import { isLocalTimeSynchronized } from "../../../utilities/time";

interface TimeSynchronizationWarningDialogProps {}

const TimeSynchronizationWarningDialog: FunctionComponent<
TimeSynchronizationWarningDialogProps
> = () => {
const theme = useTheme();

const [showTimeWarning, setShowTimeWarning] = useState(false);
const [timeDifference, setTimeDifference] = useState<number | null>(null);

useEffect(() => {
(async () => {
const [isSynchronized, difference] = await isLocalTimeSynchronized();

setTimeDifference(difference);
setShowTimeWarning(!isSynchronized);
})();
}, []);

return (
<Dialog
open={showTimeWarning}
PaperProps={{
sx: {
maxWidth: "400px",
},
}}
>
<DialogContent>
<Stack alignItems="center" textAlign="center" spacing={2}>
<TbClockX size={96} color={theme.palette.error.dark} />

<p>Your local time is not synchronized with the server time.</p>

<p>
Your are {timeDifference && timeDifference > 0 ? "ahead" : "behind"}{" "}
by {timeDifference} ms.
</p>

<p>
Please make sure your system clock is synchronized to avoid any
problems.
</p>
</Stack>
</DialogContent>

<DialogActions sx={{ justifyContent: "center" }}>
<Button onClick={() => setShowTimeWarning(false)}>Dismiss</Button>
</DialogActions>
</Dialog>
);
};

export default TimeSynchronizationWarningDialog;
3 changes: 3 additions & 0 deletions src/views/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ConfirmDialog from "../../components/ConfirmDialog";
import BottomGamesCount from "./components/BottomGamesCount";
import SoundMuteFab from "./components/SoundMuteFab";
import ThemeModeFab from "./components/ThemeModeFab";
import TimeSynchronizationWarningDialog from "./components/TimeSynchronizationWarningDialog";

const LoginView: FunctionComponent = () => {
return (
Expand Down Expand Up @@ -43,6 +44,8 @@ const LoginView: FunctionComponent = () => {
onConfirm={() => {}}
/>
</Container>

<TimeSynchronizationWarningDialog />
</>
);
};
Expand Down

0 comments on commit 7b0657a

Please sign in to comment.