forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralSettings.vue
More file actions
298 lines (276 loc) · 9.13 KB
/
GeneralSettings.vue
File metadata and controls
298 lines (276 loc) · 9.13 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<template>
<div class="settings-item">
<label class="setting-header">
<input
type="checkbox"
class="filled-in"
id="viewTimeInBadge"
v-model="viewTimeInBadge"
@change="onChange(StorageParams.VIEW_TIME_IN_BADGE, $event.target)"
/>
<span>{{ t('viewTimeInBadge.message') }}</span>
<p class="description">
{{ t('viewTimeInBadge.description') }}
</p>
</label>
</div>
<div class="settings-item">
<label class="setting-header">
<input
type="checkbox"
class="filled-in"
id="blockDeferral"
v-model="allowDeferringBlock"
@change="onChange(StorageParams.BLOCK_DEFERRAL, $event.target)"
/>
<span>{{ t('allowDeferringBlock.message') }}</span>
<p class="description">
{{ t('allowDeferringBlock.description') }}
</p>
</label>
</div>
<div class="settings-item">
<label class="setting-header">
<input
type="checkbox"
class="filled-in"
id="darkMode"
v-model="darkMode"
@change="onChange(StorageParams.DARK_MODE, $event.target)"
/>
<span>{{ t('darkTheme.message') }}</span>
</label>
</div>
<div class="settings-item">
<label class="setting-header d-inline-block">{{ t('intervalInactivity.message') }} </label>
<div class="d-inline-block ml-10">
<select
class="option"
v-model="intervalInactivity"
@change="onChange(StorageParams.INTERVAL_INACTIVITY, $event.target)"
>
<option :value="InactivityInterval.Seconds_30">30 {{ t('sec.message') }}</option>
<option :value="InactivityInterval.Seconds_45">45 {{ t('sec.message') }}</option>
<option :value="InactivityInterval.Min_1">1 {{ t('min.message') }}</option>
<option :value="InactivityInterval.Min_2">2 {{ t('2min.message') }}</option>
<option :value="InactivityInterval.Min_5">5 {{ t('mins.message') }}</option>
<option :value="InactivityInterval.Min_10">10 {{ t('mins.message') }}</option>
<option :value="InactivityInterval.Min_20">20 {{ t('mins.message') }}</option>
<option :value="InactivityInterval.Min_30">30 {{ t('mins.message') }}</option>
</select>
</div>
<p class="description">{{ t('intervalInactivity.description') }}</p>
</div>
<div class="settings-item">
<label class="setting-header d-inline-block">{{ t('exportToCsvSetting.message') }}</label>
<p class="description">{{ t('exportToCsvSetting.description') }}</p>
<div class="export-block">
<VueDatePicker
range
:enable-time-picker="false"
class="date-picker"
v-model="selectedDate"
:preset-ranges="presetRanges"
@update:model-value="handleDate"
>
<template #yearly="{ label, range, presetDateRange }">
<span @click="presetDateRange(range)">{{ label }}</span>
</template>
</VueDatePicker>
<input type="button" :value="t('exportToCsv.message')" @click="exportToCsv()" />
</div>
</div>
<div class="settings-item">
<label class="setting-header d-inline-block">{{ t('removeAllData.message') }}</label>
<p class="description">{{ t('removeAllData.description') }}</p>
<input type="button" :value="t('remove.message')" @click="removeAll()" />
</div>
<div class="settings-item">
<label class="setting-header d-inline-block">{{ t('backupAndRestore.message') }}</label>
<p class="description">{{ t('backupAndRestore.description') }}</p>
<input type="button" :value="t('backup.message')" @click="backup()" />
<input
type="file"
ref="restoreFile"
style="display: none"
@change="restoreFileUpload()"
accept="application/json"
/>
<input type="button" class="ml-10" :value="t('restore.message')" @click="restore()" />
</div>
<div class="settings-item">
<label class="setting-header">
<input
type="checkbox"
class="filled-in"
id="showChangelog"
v-model="showChangelog"
@change="onChange(StorageParams.SHOW_CHANGELOG, $event.target)"
/>
<span>{{ t('showChangelog.message') }}</span>
<p class="description">
{{ t('showChangelog.description') }}
</p>
</label>
</div>
<div id="removeAllConfirmModal" class="modal" v-if="needToConfirmDeleteAllData">
<div class="modal-content">
<p class="text-center">{{ t('removeAllDataConfirm.message') }}</p>
<div class="text-center">
<input
type="button"
class="alert"
:value="t('remove.message')"
@click="removeAllConfirm()"
/>
<input type="button" class="info ml-10" :value="t('cancel.message')" @click="cancel()" />
</div>
</div>
</div>
</template>
<script lang="ts">
export default {
name: 'GeneralSettings',
};
</script>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { injecStorage } from '../storage/inject-storage';
import { useNotification } from '@kyvg/vue3-notification';
import {
BLOCK_DEFERRAL_DEFAULT,
DARK_MODE_DEFAULT,
INTERVAL_INACTIVITY_DEFAULT,
StorageParams,
VIEW_TIME_IN_BADGE_DEFAULT,
InactivityInterval,
SHOW_CHANGELOG_DEFAULT,
} from '../storage/storage-params';
import { ranges, ThisWeekRange, todayLocalDate } from '../utils/date';
import { useImportToCsv } from '../functions/useImportToCsv';
import { FileType, useFile } from '../functions/useFile';
import { useRemoveAllData } from '../functions/useRemoveAllData';
import { injectTabsRepository } from '../repository/inject-tabs-repository';
import { useRestoreData } from '../functions/useRestoreData';
import { applyDarkMode } from '../utils/dark-mode';
const { t } = useI18n();
const settingsStorage = injecStorage();
const notification = useNotification();
const viewTimeInBadge = ref<boolean>();
const intervalInactivity = ref<InactivityInterval>();
const allowDeferringBlock = ref<boolean>();
const darkMode = ref<boolean>();
const selectedDate = ref<Date[]>();
const presetRanges = ranges();
const needToConfirmDeleteAllData = ref<boolean>();
const showChangelog = ref<boolean>();
const restoreFile = ref<any>();
onMounted(async () => {
viewTimeInBadge.value = await settingsStorage.getValue(
StorageParams.VIEW_TIME_IN_BADGE,
VIEW_TIME_IN_BADGE_DEFAULT,
);
intervalInactivity.value = await settingsStorage.getValue(
StorageParams.INTERVAL_INACTIVITY,
INTERVAL_INACTIVITY_DEFAULT,
);
darkMode.value = await settingsStorage.getValue(StorageParams.DARK_MODE, DARK_MODE_DEFAULT);
allowDeferringBlock.value = await settingsStorage.getValue(
StorageParams.BLOCK_DEFERRAL,
BLOCK_DEFERRAL_DEFAULT,
);
selectedDate.value = ThisWeekRange;
showChangelog.value = await settingsStorage.getValue(
StorageParams.SHOW_CHANGELOG,
SHOW_CHANGELOG_DEFAULT,
);
});
async function onChange(storageParam: StorageParams, target: any) {
if (target != null)
await save(
storageParam,
storageParam == StorageParams.INTERVAL_INACTIVITY ? Number(target.value) : target.checked,
);
if (storageParam == StorageParams.DARK_MODE) applyDarkMode(target.checked);
}
async function save(storageParam: StorageParams, value: any) {
if (value != undefined) await settingsStorage.saveValue(storageParam, value);
}
async function handleDate(modelData: Date[]) {
selectedDate.value = modelData;
}
async function exportToCsv() {
const dateFrom = selectedDate.value?.[0] as Date;
const dateTo = selectedDate.value?.[1] as Date;
if (dateFrom == undefined || dateTo == undefined) {
notification.notify({
title: 'No time period selected',
type: 'warn',
});
} else {
const csv = await useImportToCsv(dateFrom, dateTo);
useFile(
csv,
FileType.CSV,
`websites_${dateFrom.toLocaleDateString()}-${dateTo.toLocaleDateString()}.csv`,
);
}
}
async function removeAll() {
needToConfirmDeleteAllData.value = true;
}
async function removeAllConfirm() {
await useRemoveAllData();
needToConfirmDeleteAllData.value = false;
}
function cancel() {
needToConfirmDeleteAllData.value = false;
}
async function backup() {
const repo = await injectTabsRepository();
const tabs = repo.getTabs();
const json = JSON.stringify(tabs);
useFile(json, FileType.JSON, `backup-${todayLocalDate()}.json`);
}
function restore() {
restoreFile.value.click();
}
function restoreFileUpload() {
try {
const file = restoreFile.value.files[0];
if (file != null && file.type === FileType.JSON) {
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = async readerEvent => {
if (readerEvent != null) {
let content = readerEvent.target?.result;
if (content != null) {
await useRestoreData(content as string);
}
}
};
} else {
notification.notify({
title: 'Wrong restore file format',
type: 'warn',
});
}
} catch {
notification.notify({
title: 'Wrong restore file format',
type: 'warn',
});
}
}
</script>
<style scoped>
.export-block {
display: flex;
justify-content: start;
}
.export-block .date-picker {
width: 250px;
margin-right: 15px;
}
</style>