Skip to content

Commit

Permalink
timer
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbach committed Apr 14, 2024
1 parent 2f12e83 commit 4b033da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/renderer/recorder/recorder-insession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const RecorderInsession = forwardRef<HTMLDivElement>((_, ref) => {
addHighlight,
setMeta,
reset,
meta,
} = useRecorderState();
const promptTimestampedNote = usePromptText(
"Add note at current time",
Expand Down Expand Up @@ -62,9 +63,11 @@ export const RecorderInsession = forwardRef<HTMLDivElement>((_, ref) => {
</EntityTitle>
)}
<Flex justify="center" gap=".5rem" align="center">
<Badge style={{ width: "4.5rem" }}>
<Badge style={{ minWidth: "3.5rem" }}>
<HiOutlineClock />
<Timer isRunning={!isPaused} />
<Timer
start={(Date.now() - new Date(meta.started).getTime()) / 1000}
/>
</Badge>
{isPaused ? (
<RecordingActionButton tooltip="Resume recording" onClick={resume}>
Expand Down
9 changes: 4 additions & 5 deletions src/renderer/recorder/timer.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { FC, useEffect, useState } from "react";
import { timeToDisplayString } from "../../utils";

export const Timer: FC<{ isRunning: boolean }> = ({ isRunning }) => {
const [time, setTime] = useState(0);
export const Timer: FC<{ start: number }> = ({ start }) => {
const [time, setTime] = useState(start);

useEffect(() => {
if (!isRunning) return () => {};
const interval = setInterval(() => {
setTime((prev) => prev + 1);
}, 1000);
return () => {
clearInterval(interval);
};
}, [isRunning]);
}, []);

return <>{timeToDisplayString(time)}</>;
return <>{timeToDisplayString(time, false)}</>;
};
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ export const blobToBuffer = async (blob: Blob) => {
});
};

export const timeToDisplayString = (time: number) => {
export const timeToDisplayString = (time: number, withMs = true) => {
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time % 3600) / 60);
const seconds = Math.floor(time % 60);
const ms = Math.floor((time % 1) * 10);

const hoursString = hours > 0 ? `${hours}:` : "";
const msString = withMs ? `.${String(ms).padStart(1, "0")}` : "";
return `${hoursString}${String(minutes).padStart(2, "0")}:${String(
seconds,
).padStart(2, "0")}.${String(ms).padStart(1, "0")}`;
).padStart(2, "0")}${msString}`;
};

export const isNotNull = <T>(x: T | null): x is T => x !== null;
Expand Down

0 comments on commit 4b033da

Please sign in to comment.