This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
8 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,34 @@ | ||
import React from "react"; | ||
import dayjs from "dayjs"; | ||
|
||
import { Box } from "@chakra-ui/react"; | ||
import { Heading } from "@chakra-ui/react"; | ||
|
||
import { Api, Track } from "./Api"; | ||
import { Colors } from "./theme"; | ||
import { useLightningTimer } from "./LightningTimer"; | ||
|
||
export type Props = { | ||
track: Track; | ||
}; | ||
|
||
export const ControlLightningTimer: React.FC<Props> = ({ track }) => { | ||
const { data: latency } = Api.useStreamLatencyMark(); | ||
const timer = useLightningTimer(track?.card?.lightning_timer); | ||
|
||
if (!timer) return <></>; | ||
|
||
return ( | ||
<Box border="1px solid" borderColor={Colors.chatBorder2} backgroundColor="white"> | ||
<Heading as="h4" fontSize="1.1rem"> | ||
Lightning Timer | ||
</Heading> | ||
<Box fontSize="5rem" textAlign="center"> | ||
{timer.m}:{timer.s} | ||
</Box> | ||
<Box>Latency: {latency?.delta || "-"} ms</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default ControlLightningTimer; |
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,49 @@ | ||
import dayjs from "dayjs"; | ||
import { useEffect, useState } from "react"; | ||
import { Api, LightningTimer } from "./Api"; | ||
|
||
export type LightningTimerData = { | ||
tick: dayjs.Dayjs; | ||
remaining: number; | ||
m: string; | ||
s: string; | ||
}; | ||
|
||
export function useLightningTimer(timer?: LightningTimer, tickCorrection?: boolean) { | ||
const correction = tickCorrection ?? true; | ||
const { data: latency } = Api.useStreamLatencyMark(); | ||
|
||
const [tick, setTick] = useState(dayjs()); | ||
useEffect(() => { | ||
if (timer) { | ||
const updateTick = () => { | ||
const t = dayjs(); | ||
if (timer.expires_at + 10 >= t.unix()) { | ||
setTick(t); | ||
} | ||
}; | ||
updateTick(); | ||
const id = setInterval(updateTick, 500); | ||
return () => clearInterval(id); | ||
} else { | ||
return () => null; | ||
} | ||
}, [timer]); | ||
|
||
if (!timer) return undefined; | ||
|
||
const correctedTick = correction ? tick.add((latency?.delta ?? 0) * -1, "ms") : tick; | ||
|
||
if (timer.expires_at <= correctedTick.unix()) return undefined; | ||
|
||
const remaining = Math.max(0, timer.ends_at - correctedTick.unix()); | ||
const m = Math.floor(remaining / 60); | ||
const s = remaining % 60; | ||
|
||
return { | ||
tick: correctedTick, | ||
remaining, | ||
m: `${m < 10 ? "0" : ""}${m}`, | ||
s: `${s < 10 ? "0" : ""}${s}`, | ||
}; | ||
} |
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,23 @@ | ||
import React from "react"; | ||
|
||
import { Track } from "./Api"; | ||
|
||
import { Box, Flex, Skeleton } from "@chakra-ui/react"; | ||
import { useLightningTimer } from "./LightningTimer"; | ||
import { Fonts } from "./theme"; | ||
|
||
export const SubScreenLightningTimerView: React.FC<{ track: Track }> = ({ track }) => { | ||
const timer = useLightningTimer(track?.card?.lightning_timer, false); | ||
|
||
if (!timer) return <></>; | ||
|
||
return ( | ||
<React.Suspense fallback={<Skeleton w="100%" h="100%" />}> | ||
<Flex w="100%" h="100%" direction="column" justify="space-around"> | ||
<Box w="100%" fontSize="14vw" lineHeight="14vw" textAlign="center" fontFamily={Fonts.heading} fontWeight={700}> | ||
{timer.m}:{timer.s} | ||
</Box> | ||
</Flex> | ||
</React.Suspense> | ||
); | ||
}; |