-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add ClassRoom Timer (#1288)
* feat(ui): add Countdown * fix(eslint): format * fix(component): use date-fns in Countdown * fix(component): rename Countdown to Timer * fix(component): useClockTick support duration rename countdown to timer * fix(component): adjust negated conditions * fix(component): rename timer * fix(component): remove timer in AppStoreButton * fix(component): format code * fix(component): rename timer * fix(component): timer types * Update packages/flat-components/src/components/ClassroomPage/Timer/index.tsx Co-authored-by: Black-Hole <158blackhole@gmail.com> * Update packages/flat-components/src/components/ClassroomPage/Timer/index.tsx * fix(component): use NaN as initialize value to <Timer /> * rename * fix(component): revert countdown code * fix(component): revert countdown code * refactor(component): use react schedule timing update * refactor(component): fix timing * refactor(component): format code * refactor(component): format code * fix(assets): style * refactor(component): format code Co-authored-by: Black-Hole <158blackhole@gmail.com>
- Loading branch information
1 parent
e4d91b0
commit dbd49e9
Showing
12 changed files
with
177 additions
and
16 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
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
73 changes: 73 additions & 0 deletions
73
packages/flat-components/src/components/ClassroomPage/Timer/index.tsx
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,73 @@ | ||
import "./style.less"; | ||
|
||
import React, { useState, useEffect, useMemo } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { useIsUnMounted } from "../../../utils/hooks"; | ||
import { RoomStatus } from "../../../types/room"; | ||
import { intervalToDuration } from "date-fns/fp"; | ||
|
||
export type TimerProps = { | ||
roomStatus: RoomStatus; | ||
beginTime: number; | ||
}; | ||
|
||
const paddingHexCode = (number: number): string => { | ||
return String(number).padStart(2, "0"); | ||
}; | ||
|
||
const useClockTick = (beginTime: number, roomStatus: RoomStatus): string => { | ||
const [timestamp, updateTimestamp] = useState<number>(Date.now()); | ||
const unmounted = useIsUnMounted(); | ||
|
||
useEffect(() => { | ||
let timer = NaN; | ||
|
||
if (unmounted.current) { | ||
return; | ||
} | ||
|
||
if (roomStatus === RoomStatus.Started) { | ||
const startTimer = (): void => { | ||
updateTimestamp(Math.floor(Date.now() / 1000) * 1000); | ||
timer = window.requestAnimationFrame(startTimer); | ||
}; | ||
startTimer(); | ||
} | ||
|
||
return () => { | ||
window.cancelAnimationFrame(timer); | ||
}; | ||
}, [roomStatus, unmounted]); | ||
|
||
return useMemo(() => { | ||
const { | ||
days = 0, | ||
hours = 0, | ||
minutes = 0, | ||
seconds = 0, | ||
} = intervalToDuration({ | ||
start: beginTime, | ||
end: timestamp, | ||
}); | ||
|
||
const minutesAndSeconds = `${paddingHexCode(minutes)}:${paddingHexCode(seconds)}`; | ||
const dayHours = hours + days * 24; | ||
|
||
return dayHours > 0 | ||
? `${paddingHexCode(dayHours)}:${minutesAndSeconds}` | ||
: minutesAndSeconds; | ||
}, [beginTime, timestamp]); | ||
}; | ||
|
||
export const Timer: React.FC<TimerProps> = ({ roomStatus = RoomStatus.Paused, beginTime }) => { | ||
const timing = useClockTick(beginTime, roomStatus); | ||
|
||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<span className="timer-bar"> | ||
<span className={`timer-${roomStatus}`}>{t("room-started")}</span> | ||
<span>{timing}</span> | ||
</span> | ||
); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/flat-components/src/components/ClassroomPage/Timer/style.less
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,5 @@ | ||
.timer-bar { | ||
color: #7a7b7c; | ||
font-size: 12px; | ||
margin: 0 8px; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/flat-components/src/components/ClassroomPage/Timer/timer.stories.tsx
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,20 @@ | ||
import { Meta, Story } from "@storybook/react"; | ||
import React from "react"; | ||
import { RoomStatus } from "../../../types/room"; | ||
import { Timer, TimerProps } from "."; | ||
|
||
const storyMeta: Meta = { | ||
title: "ClassroomPage/Timer", | ||
component: Timer, | ||
}; | ||
|
||
export default storyMeta; | ||
|
||
export const Overview: Story<TimerProps> = args => { | ||
return <Timer {...args} />; | ||
}; | ||
|
||
Overview.args = { | ||
roomStatus: RoomStatus.Paused, | ||
beginTime: Date.now(), | ||
}; |
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
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