2
2
import { Controls , Checkbox } from " @/components"
3
3
import { useSafeModeStore } from " ./store"
4
4
import { storeToRefs } from " pinia"
5
- import { onBeforeMount } from " vue"
5
+ import { onBeforeMount , watchEffect } from " vue"
6
6
import { Duration , add } from " date-fns"
7
+ import { sendSafeModeNotificationMessage } from " ~/notifications/safeMode/client"
7
8
8
- const { shouldRemindSafeMode, shouldDisplayRemainingTimeInHeader, expirationTimestamp } = storeToRefs (
9
- useSafeModeStore ()
10
- )
9
+ const { shouldRemindSafeMode, shouldDisplayRemainingTimeInHeader, shouldUseChromeNotifications, expirationTimestamp } =
10
+ storeToRefs (useSafeModeStore ())
11
11
12
- onBeforeMount (() => {
12
+ function getRemainingDuration() {
13
13
const timeDiv = document .querySelector <HTMLDivElement >(" main .bg-white.mt-4 div.text-sm.text-gray-500" )
14
- if (! timeDiv ) {
15
- expirationTimestamp .value = new Date ().toISOString ()
16
- return
17
- }
14
+ if (! timeDiv ) return {}
18
15
19
16
const remainingTimeString = timeDiv .innerText .replace (" Expires in " , " " )
20
- const remainingDuration = remainingTimeString
17
+ return remainingTimeString
21
18
.split (/ (,| and)/ )
22
19
.map ((e ) => e .trim ().split (" " ))
23
20
.map ((e ): [number , keyof Duration ] => [Number (e [0 ]), e [1 ] as keyof Duration ])
24
21
.reduce <Duration >((acc , [val , key ]) => ({ ... acc , [key ]: val }), {})
22
+ }
23
+
24
+ const remainingDuration = getRemainingDuration ()
25
25
26
+ onBeforeMount (() => {
26
27
expirationTimestamp .value = add (new Date (), remainingDuration ).toISOString ()
27
28
})
29
+
30
+ watchEffect (() => {
31
+ if (! Object .keys (remainingDuration ).length ) return
32
+
33
+ sendSafeModeNotificationMessage (add (new Date (), remainingDuration ).toISOString (), shouldUseChromeNotifications .value )
34
+ })
28
35
</script >
29
36
30
37
<template >
@@ -33,6 +40,11 @@ onBeforeMount(() => {
33
40
<template #default > Remind to enable safe mode </template >
34
41
<template #subtitle > A safe mode a day keeps the doctor away </template >
35
42
</Checkbox >
43
+ <Checkbox v-model =" shouldUseChromeNotifications" :parent =" shouldRemindSafeMode" >
44
+ <template #default > Show chrome notification </template >
45
+ <template #subtitle > Be on point even outside simple mmo page </template >
46
+ <template #requires > Requires reminder enabled </template >
47
+ </Checkbox >
36
48
<Checkbox v-model =" shouldDisplayRemainingTimeInHeader" :parent =" shouldRemindSafeMode" >
37
49
<template #default > Display remaining time in header </template >
38
50
<template #subtitle > Know when the time to reenable is coming </template >
0 commit comments