Skip to content

Commit d028d26

Browse files
committed
chore: add remote notification
1 parent c5ddd23 commit d028d26

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

src/renderer/App.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import {
3535
onCopySnippet,
3636
emitter,
3737
onCreateSnippet,
38-
onAddDescription
38+
onAddDescription,
39+
checkForRemoteNotification
3940
} from '@/composable'
4041
import { createToast, destroyAllToasts } from 'vercel-toast'
4142
import { useRoute } from 'vue-router'
@@ -80,6 +81,7 @@ const init = () => {
8081
}
8182
8283
trackAppUpdate()
84+
checkForRemoteNotification()
8385
}
8486
8587
const setTheme = (theme: string) => {

src/renderer/assets/scss/vendor.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@
8484
}
8585
}
8686
}
87+
88+
.toast {
89+
code {
90+
background-color: #eee;
91+
border-radius: 3px;
92+
padding: 0 4px;
93+
}
94+
}

src/renderer/composable/index.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import type { EmitterEvents } from '@shared/types/renderer/composable'
44
import { API_PORT } from '../../main/config'
55
import { useFolderStore } from '@/store/folders'
66
import { useSnippetStore } from '@/store/snippets'
7-
import { ipc, track } from '@/electron'
7+
import { i18n, ipc, store, track } from '@/electron'
88
import type { NotificationRequest } from '@shared/types/main'
99
import 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

1114
export 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+
}

src/shared/types/main/store.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface AppStore {
55
bounds: object
66
dateInstallation?: number
77
nextSupportNotice?: number
8+
prevRemoteNotice?: number
89
selectedFolderAlias?: string
910
selectedFolderId?: string
1011
selectedFolderIds?: string[]

0 commit comments

Comments
 (0)