1
- import { DateUtils , dayOfTheWeek } from '@dvcol/common-utils/common/date' ;
1
+ import { DateUtils , dayOfTheWeek , getTodayISOLocal } from '@dvcol/common-utils/common/date' ;
2
2
import { defineStore , storeToRefs } from 'pinia' ;
3
3
import { computed , ref , watch } from 'vue' ;
4
4
5
5
import type { CalendarItem } from '~/utils/calendar.utils' ;
6
6
7
7
import { MessageType } from '~/models/message/message-type.model' ;
8
8
import { Logger } from '~/services/logger.service' ;
9
+ import { NotificationService } from '~/services/notification.service' ;
9
10
import { fetchCalendarData } from '~/stores/data/calendar.store' ;
10
11
import { Colors } from '~/styles/colors.style' ;
11
12
import { sendMessage } from '~/utils/browser/browser-message.utils' ;
@@ -25,7 +26,7 @@ const sendCalendarBadge = async () => {
25
26
{ } as Record < string , CalendarItem [ ] > ,
26
27
) ;
27
28
28
- const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
29
+ const today = getTodayISOLocal ( ) . split ( 'T' ) [ 0 ] ;
29
30
const text = week [ today ] ?. length . toString ( ) ;
30
31
if ( ! text ?. length ) return ;
31
32
const title = Object . entries ( week )
@@ -64,6 +65,8 @@ const BadgeStoreConstants = {
64
65
export const useBadgeStore = defineStore ( BadgeStoreConstants . Store , ( ) => {
65
66
const enableBadge = ref ( false ) ;
66
67
68
+ const loading = ref ( false ) ;
69
+
67
70
const saveState = debounce (
68
71
( ) =>
69
72
storage . sync . set < BadgeStoreState > ( BadgeStoreConstants . Store , {
@@ -78,14 +81,35 @@ export const useBadgeStore = defineStore(BadgeStoreConstants.Store, () => {
78
81
if ( restored ?. enableBadge !== undefined ) enableBadge . value = restored . enableBadge ;
79
82
} ;
80
83
84
+ const getBadgeData = async ( ) => {
85
+ if ( loading . value ) {
86
+ Logger . warn ( 'Already loading badge data' ) ;
87
+ return ;
88
+ }
89
+
90
+ loading . value = true ;
91
+
92
+ Logger . debug ( 'Getting badge data' ) ;
93
+
94
+ try {
95
+ await sendCalendarBadge ( ) ;
96
+ } catch ( e ) {
97
+ Logger . error ( 'Failed to get badge data' , e ) ;
98
+ NotificationService . error ( 'Failed to get badge data' , e ) ;
99
+ throw e ;
100
+ } finally {
101
+ loading . value = false ;
102
+ }
103
+ } ;
104
+
81
105
const initBadgeStore = async ( ) => {
82
106
await restoreState ( ) ;
83
107
84
108
watch (
85
109
enableBadge ,
86
110
async ( ) => {
87
111
if ( ! enableBadge . value ) return calendarBadge ( ) ;
88
- await sendCalendarBadge ( ) ;
112
+ await getBadgeData ( ) ;
89
113
} ,
90
114
{
91
115
immediate : true ,
0 commit comments