@@ -4,9 +4,12 @@ import type { EmitterEvents } from '@shared/types/renderer/composable'
44import { API_PORT } from '../../main/config'
55import { useFolderStore } from '@/store/folders'
66import { useSnippetStore } from '@/store/snippets'
7- import { ipc , track } from '@/electron'
7+ import { i18n , ipc , store , track } from '@/electron'
88import type { NotificationRequest } from '@shared/types/main'
99import type { Snippet , SnippetsSort } from '@shared/types/main/db'
10+ import axios from 'axios'
11+ import { createToast } from 'vercel-toast'
12+ import { differenceInDays } from 'date-fns'
1013
1114export const useApi = createFetch ( {
1215 baseUrl : `http://localhost:${ API_PORT } `
@@ -128,3 +131,58 @@ export const useHljsTheme = async (theme: 'dark' | 'light') => {
128131
129132 document . head . appendChild ( style )
130133}
134+
135+ export const checkForRemoteNotification = async ( ) => {
136+ const showMessage = ( message : string , date : string | number ) => {
137+ const el = document . createElement ( 'div' )
138+ el . innerHTML = message
139+
140+ const links = el . querySelectorAll ( 'a' )
141+
142+ links . forEach ( i => {
143+ i . addEventListener ( 'click' , e => {
144+ e . preventDefault ( )
145+ ipc . invoke ( 'main:open-url' , i . href )
146+ track ( 'app/notify' , i . innerHTML )
147+ } )
148+ } )
149+
150+ createToast ( el , {
151+ action : {
152+ text : i18n . t ( 'close' ) ,
153+ callback ( toast ) {
154+ toast . destroy ( )
155+ store . app . set ( 'prevRemoteNotice' , date )
156+ track ( 'app/notify' , `remoteNotification-${ date } ` )
157+ }
158+ }
159+ } )
160+ }
161+
162+ const checkAndShow = async ( ) => {
163+ try {
164+ const { data } = await axios . get < { message : string ; date : string } > (
165+ 'https://masscode.io/notification.json'
166+ )
167+
168+ if ( ! data ) return
169+ const { message, date } = data
170+ const prevDate = store . app . get ( 'prevRemoteNotice' )
171+
172+ if ( prevDate ) {
173+ const diff = differenceInDays ( prevDate , new Date ( date ) )
174+ if ( diff < 0 ) showMessage ( message , date )
175+ } else {
176+ showMessage ( message , date )
177+ }
178+ } catch ( err ) {
179+ console . error ( err )
180+ }
181+ }
182+
183+ checkAndShow ( )
184+
185+ setInterval ( ( ) => {
186+ checkAndShow ( )
187+ } , 1000 * 60 * 360 ) // 6 часов
188+ }
0 commit comments