aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Tools/Tracker/Tool.svelte
blob: c8977bc920a4f8058fafc724d6d5f32e943f5af3 (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
162
163
164
165
166
167
168
169
170
<script lang="ts">
import { onMount } from "svelte";
import { get } from "svelte/store";
import { v6 as uuidv6 } from "uuid";
import { database, type TrackerEntry } from "$lib/Database/IDB/tracker";
import Spacer from "$lib/Layout/Spacer.svelte";
import Message from "$lib/Loading/Message.svelte";
import locale from "$stores/locale";

let url = "";
let title = "";
let progress = 0;
let error = "";
let masterList: TrackerEntry[] | null = null;
let confirmDelete = 0;

$: listAccess = masterList || [];

onMount(async () => {
	masterList = await database.entries.toArray();
});

const adjustEntry = (id: string, to: number) => {
	const entry = listAccess.find((entry) => entry.id === id);

	if (!entry) return;

	entry.progress = Math.max(0, to);

	database.entries.update(id, { progress: entry.progress });

	masterList = listAccess.map((entry) =>
		entry.id === id ? { ...entry, progress: entry.progress } : entry,
	);
};

const addEntry = async (url: string, title: string, progress: number) => {
	if (!url || !title) {
		error =
			get(locale)().tools.tracker?.urlTitleRequired ??
			"URL and title are required fields";

		return;
	}

	if (listAccess.some((entry) => entry.url === url)) {
		const existing = listAccess.find((entry) => entry.url === url)?.title;

		error = (
			get(locale)().tools.tracker?.entryExists ??
			"Entry with URL already exists: {url}"
		).replace("{url}", existing ?? "");

		return;
	}

	await database.entries.add({ url, title, progress, id: uuidv6() });

	masterList = await database.entries.toArray();
};

const deleteEntry = async (id: string) => {
	if (confirmDelete !== 1) {
		confirmDelete = 1;
		error =
			get(locale)().tools.tracker?.confirmDelete ??
			"Click again to confirm deletion";

		return;
	}

	await database.entries.delete(id);

	masterList = await database.entries.toArray();
	confirmDelete = 0;
	error = "";
};
</script>

<div class="card">
  {#if error}
    <p><b>Error</b>: {error}</p>
  {/if}

  <input type="url" placeholder={$locale().tools.tracker?.urlPlaceholder} bind:value={url} />
  <input type="text" placeholder={$locale().tools.tracker?.titlePlaceholder} bind:value={title} />
  <input
    type="number"
    placeholder={$locale().tools.tracker?.progressPlaceholder}
    bind:value={progress}
  />
  <button
    class="button-lined"
    onclick={() => addEntry(url, title, progress)}
    data-umami-event="Add Tracker Entry">{$locale().common?.add}</button
  >

  <Spacer />

  {#if masterList === null}
    <Message message="Loading entries ..." />
  {:else}
    <ul>
      {#each listAccess.sort((a, b) => a.title.localeCompare(b.title)) as entry}
        <li>
          <div class="entry" id={entry.id}>
            <a href={entry.url} title={entry.id}>
              {entry.title}
            </a>

            <span class="opaque">|</span>

            <input
              type="number"
              value={entry.progress}
              size={3}
              onchange={(e) =>
                adjustEntry(
                  entry.id,
                  e.target
                    ? Number((e.target as HTMLInputElement).value) || entry.progress
                    : entry.progress
                )}
            />

            <span class="entry-adjust">
              <span class="opaque">|</span>
              <button
                class="button-square button-action"
                onclick={() => adjustEntry(entry.id, entry.progress - 1)}
                data-umami-event="Decrement Tracker Progress"
                >-
              </button>
              <button
                class="button-square button-action"
                onclick={() => adjustEntry(entry.id, entry.progress + 1)}
                data-umami-event="Increment Tracker Progress"
              >
                +
              </button>
              <span class="opaque">|</span>
              <button onclick={() => deleteEntry(entry.id)} data-umami-event="Delete Tracker Entry"
                >{$locale().common?.remove}</button
              >
            </span>
          </div>
        </li>
      {/each}
    </ul>
  {/if}
</div>

<style>
  .entry {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    width: 100%;
  }

  .entry-adjust {
    opacity: 0;
    transition: opacity 0.2s;
  }

  .entry:hover .entry-adjust {
    opacity: 1;
    transition: opacity 0.2s;
  }
</style>