-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show the course time and numbers on summary page (#312)
* feat: show the course time on the share page and summary page * refactor: delete * refactor: course time --------- Co-authored-by: cuixiaorui <cui_xiaorui@126.com>
- Loading branch information
1 parent
62989d4
commit 81309ce
Showing
13 changed files
with
248 additions
and
35 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
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,54 @@ | ||
// CourseTimer.js | ||
type Timestamp = { | ||
s: number; | ||
e: number; | ||
time: number; | ||
}; | ||
|
||
type Timestamps = Record<string, Timestamp>; | ||
|
||
let timestamps: Timestamps = {}; | ||
|
||
function time(label: string) { | ||
if (timestamps[label]) return; | ||
|
||
timestamps[label] = { | ||
s: Date.now(), | ||
e: 0, | ||
time: 0, | ||
}; | ||
} | ||
|
||
function timeEnd(label: string) { | ||
const start = timestamps[label].s; | ||
const end = Date.now(); | ||
const time = (Date.now() - start) / 1000; | ||
|
||
timestamps[label].e = end; | ||
timestamps[label].time = time; | ||
} | ||
|
||
function calculateTotalTime() { | ||
const totalTime = Object.keys(timestamps).reduce((totalTime, key) => { | ||
const { time } = timestamps[key]; | ||
return (totalTime += time); | ||
}, 0); | ||
|
||
return Math.ceil(totalTime); | ||
} | ||
|
||
function totalRecordNumber() { | ||
return Object.keys(timestamps).length; | ||
} | ||
|
||
function reset() { | ||
timestamps = {}; | ||
} | ||
|
||
export const courseTimer = { | ||
time, | ||
timeEnd, | ||
calculateTotalTime, | ||
totalRecordNumber, | ||
reset, | ||
}; |
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,63 @@ | ||
// CourseTimer.spec.js | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { courseTimer } from "../courseTimer"; | ||
|
||
describe("course timers", () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
courseTimer.reset(); | ||
}); | ||
|
||
it("CourseTimer calculates time correctly", async () => { | ||
courseTimer.time("1"); | ||
|
||
vi.advanceTimersByTime(1000); | ||
|
||
courseTimer.timeEnd("1"); | ||
expect(courseTimer.calculateTotalTime()).toBe(1); | ||
}); | ||
|
||
it("should be rounded up", async () => { | ||
courseTimer.time("1"); | ||
|
||
vi.advanceTimersByTime(1600); | ||
|
||
courseTimer.timeEnd("1"); | ||
expect(courseTimer.calculateTotalTime()).toBe(2); | ||
}); | ||
|
||
it("CourseTimer calculates total time correctly with multiple timestamps", async () => { | ||
courseTimer.time("1"); | ||
vi.advanceTimersByTime(1000); | ||
courseTimer.timeEnd("1"); | ||
|
||
courseTimer.time("2"); | ||
vi.advanceTimersByTime(1000); | ||
courseTimer.timeEnd("2"); | ||
|
||
expect(courseTimer.calculateTotalTime()).toBe(2); | ||
}); | ||
|
||
it("should only take effect on the first call", () => { | ||
courseTimer.time("1"); | ||
|
||
vi.advanceTimersByTime(1000); | ||
|
||
courseTimer.time("1"); | ||
courseTimer.timeEnd("1"); | ||
|
||
expect(courseTimer.calculateTotalTime()).toBe(1); | ||
}); | ||
|
||
it("should return record numbers", () => { | ||
courseTimer.time("1"); | ||
vi.advanceTimersByTime(1000); | ||
courseTimer.timeEnd("1"); | ||
|
||
courseTimer.time("2"); | ||
vi.advanceTimersByTime(1000); | ||
courseTimer.timeEnd("2"); | ||
|
||
expect(courseTimer.totalRecordNumber()).toBe(2); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -63,7 +63,7 @@ export const tpl_1 = ({ | |
tw: "text-6xl font-bold flex justify-end", | ||
children: '"', | ||
}, | ||
}, | ||
} | ||
], | ||
}, | ||
}, | ||
|
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
Oops, something went wrong.