Skip to content

Commit dc54539

Browse files
author
Pascal Wegner
committed
Evaluate arc progress
1 parent 55c99c9 commit dc54539

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

packages/pwa/src/components/Arc/Arc.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import * as React from 'react';
22

3-
export function Arc(props: React.SVGProps<SVGSVGElement>) {
3+
type ArcProps = React.SVGProps<SVGSVGElement> & {
4+
factor: number;
5+
};
6+
7+
export function Arc({ factor, ...restProps }: ArcProps) {
48
return (
59
<svg
610
fill="none"
711
xmlns="http://www.w3.org/2000/svg"
812
viewBox="0 0 282 282"
9-
{...props}
13+
{...restProps}
1014
>
1115
<circle
1216
cx={141}
@@ -20,6 +24,8 @@ export function Arc(props: React.SVGProps<SVGSVGElement>) {
2024
d="M280 141a138.998 138.998 0 01-237.288 98.288A139.01 139.01 0 012 141 138.997 138.997 0 01141 2a138.997 138.997 0 01139 139h0z"
2125
stroke="#0057FF"
2226
strokeWidth={4}
27+
className="origin-center -rotate-90"
28+
style={{ strokeDasharray: 877, strokeDashoffset: factor * 877 }}
2329
/>
2430
</svg>
2531
);

packages/pwa/src/components/Counter/Counter.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ import * as React from 'react';
22

33
import { DurationInput } from '../DurationInput/DurationInput';
44
import { Arc } from '../Arc/Arc';
5+
import { getMinutes, getSeconds } from 'date-fns';
56

67
type CounterProps = {
78
timeLeft: Date;
89
roundsLeft: number;
910
rounds: number;
11+
timeTotal: Date;
1012
};
1113

12-
export function Counter({ timeLeft, roundsLeft, rounds }: CounterProps) {
14+
export function Counter({
15+
timeTotal,
16+
timeLeft,
17+
roundsLeft,
18+
rounds,
19+
}: CounterProps) {
20+
const factor =
21+
1 -
22+
(getSeconds(timeLeft) + getMinutes(timeLeft)) /
23+
(getSeconds(timeTotal) + getMinutes(timeTotal));
24+
1325
return (
1426
<div>
1527
<div className="flex flex-col items-center mb-20 z-[1]">
@@ -19,7 +31,7 @@ export function Counter({ timeLeft, roundsLeft, rounds }: CounterProps) {
1931
}/${rounds}`}</span>
2032
</div>
2133
<div className="flex flex-col justify-center items-center relative w-72 h-72">
22-
<Arc className="absolute" />
34+
<Arc className="absolute origin-center" factor={factor} />
2335
<div className="z-[1]">
2436
<DurationInput value={timeLeft} readOnly dataTestId={'time-left'} />
2537
</div>

packages/pwa/src/pages/index.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ import { useMachine } from '@xstate/react';
44
import * as React from 'react';
55
import { Counter } from '../components/Counter/Counter';
66
import { useBeep } from '../hooks/useBeep';
7+
import { StateValue } from 'xstate';
8+
9+
const getActiveTimeTotal = ({
10+
breakInterval,
11+
prepareTime,
12+
value,
13+
workInterval,
14+
}: {
15+
value: StateValue;
16+
breakInterval: Date;
17+
workInterval: Date;
18+
prepareTime: Date;
19+
}) => {
20+
if (value === timerStates.WORK) {
21+
return workInterval;
22+
}
23+
24+
if (value === timerStates.BREAK) {
25+
return breakInterval;
26+
}
27+
28+
return prepareTime;
29+
};
730

831
export default function Home() {
932
const { beepBreak, beepBreakLong, beepWork, beepWorkLong } = useBeep();
@@ -72,8 +95,14 @@ export default function Home() {
7295
});
7396
};
7497

75-
const { breakInterval, rounds, workInterval, timeLeft, roundsLeft } =
76-
state.context;
98+
const {
99+
breakInterval,
100+
rounds,
101+
workInterval,
102+
timeLeft,
103+
roundsLeft,
104+
prepareTime,
105+
} = state.context;
77106

78107
return (
79108
<>
@@ -92,6 +121,12 @@ export default function Home() {
92121
></FormFields>
93122
) : (
94123
<Counter
124+
timeTotal={getActiveTimeTotal({
125+
breakInterval,
126+
prepareTime,
127+
value: state.value,
128+
workInterval,
129+
})}
95130
timeLeft={timeLeft}
96131
roundsLeft={roundsLeft}
97132
rounds={rounds}

0 commit comments

Comments
 (0)