summaryrefslogtreecommitdiffstats
path: root/apps/web-shared/src/components/stopwatch.svelte
blob: 8287e31061a6cf0df0640146ef7fdc6a3676def8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script lang="ts">
    import {writable_persistent} from "$shared/lib/persistent-store";
    import Button from "$shared/components/button.svelte";
    import {Textarea} from "$shared/components/form";
    import {StorageKeys} from "$shared/lib/configuration";
    import {Temporal} from "@js-temporal/polyfill";
    import {createEventDispatcher, onMount} from "svelte";

    const state = writable_persistent({
        initialState: {
            hours: 0,
            minutes: 0,
            seconds: 0,
            startTime: null as Temporal.PlainTime,
            isRunning: false,
            intervalId: 0,
            note: "",
        },
        name: StorageKeys.stopwatch,
    });

    let timeString;

    $: if ($state.hours || $state.minutes || $state.seconds) {
        timeString = $state.hours.toLocaleString(undefined, {minimumIntegerDigits: 2})
            + ":" + $state.minutes.toLocaleString(undefined, {minimumIntegerDigits: 2})
            + ":" + $state.seconds.toLocaleString(undefined, {minimumIntegerDigits: 2});
    } else {
        timeString = "--:--:--";
    }

    onMount(() => {
        if ($state.isRunning) {
            clearInterval($state.intervalId);
            $state.intervalId = setInterval(step, 1000);
        }
    });

    const dispatch = createEventDispatcher();

    function step() {
        $state.seconds = $state.seconds + 1;

        if ($state.seconds == 60) {
            $state.minutes = $state.minutes + 1;
            $state.seconds = 0;
        }

        if ($state.minutes == 60) {
            $state.hours = $state.hours + 1;
            $state.minutes = 0;
            $state.seconds = 0;
        }

        if (!$state.startTime) $state.startTime = Temporal.Now.plainTimeISO();
    }

    function reset() {
        clearInterval($state.intervalId);
        $state.isRunning = false;
        $state.hours = 0;
        $state.minutes = 0;
        $state.seconds = 0;
        $state.startTime = null;
        $state.intervalId = 0;
        $state.note = "";
    }

    let roundUpToNearest = 30;
    let roundDownToNearest = 30;

    function on_round_up() {
        const newTime = Temporal.PlainTime
            .from({hour: $state.hours, minute: $state.minutes, second: $state.seconds})
            .round({
                roundingIncrement: roundUpToNearest,
                smallestUnit: "minute",
                roundingMode: "ceil"
            });
        $state.hours = newTime.hour;
        $state.minutes = newTime.minute;
        $state.seconds = newTime.second;
    }

    function on_round_down() {
        const newTime = Temporal.PlainTime
            .from({hour: $state.hours, minute: $state.minutes, second: $state.seconds,})
            .round({
                roundingIncrement: roundDownToNearest,
                smallestUnit: "minute",
                roundingMode: "trunc"
            });
        $state.hours = newTime.hour;
        $state.minutes = newTime.minute;
        $state.seconds = newTime.second;
    }

    function on_start_stop() {
        if ($state.isRunning) {
            clearInterval($state.intervalId);
            $state.isRunning = false;
            return;
        }
        step();
        $state.intervalId = setInterval(step, 1000);
        $state.isRunning = true;
    }

    function on_create_entry() {
        if (!$state.startTime) return;
        const plainStartTime = Temporal.PlainTime.from($state.startTime);
        dispatch("create", {
            from: plainStartTime,
            to: plainStartTime.add({hours: $state.hours, minutes: $state.minutes, seconds: $state.seconds}),
            description: $state.note
        });
        reset();
    }
</script>

<div class="grid">
    <div class="col-6">
        <slot name="header"></slot>
        <pre class="text-xxl padding-y-sm">{timeString}</pre>
    </div>
    <div class="col-6 flex align-bottom flex-column text-xs">
        <Button title="{$state.isRunning ? 'Stop' : 'Start'}"
                text="{$state.isRunning ? 'Stop' : 'Start'}"
                variant="link"
                on:click={on_start_stop}/>

        {#if $state.startTime}
            <Button title="Reset"
                    text="Reset"
                    variant="link"
                    class="bg-error-lighter@hover color-white@hover"
                    on:click={reset}/>
            {#if !$state.isRunning}
                <Button title="Round up"
                        text="Round up"
                        variant="link"
                        on:click={on_round_up}/>
                <Button title="Round down"
                        text="Round down"
                        variant="link"
                        on:click={on_round_down}/>
                {#if $state.minutes > 0 || $state.hours > 0}
                    <Button title="Create entry"
                            text="Create entry"
                            variant="link"
                            on:click={on_create_entry}/>
                {/if}
            {/if}
        {/if}
    </div>
</div>
<Textarea class="width-100% margin-top-xs"
          placeholder="What's your focus?"
          rows="1"
          bind:value={$state.note}
/>