Skip to content

Commit cdaab98

Browse files
committed
refactor(composable): notification
1 parent 9737535 commit cdaab98

File tree

3 files changed

+139
-119
lines changed

3 files changed

+139
-119
lines changed

src/renderer/App.vue

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ import {
3737
onCopySnippet,
3838
emitter,
3939
onCreateSnippet,
40-
onAddDescription,
41-
checkForRemoteNotification
40+
onAddDescription
4241
} from '@/composable'
43-
import { createToast, destroyAllToasts } from 'vercel-toast'
4442
import { useRoute } from 'vue-router'
4543
import type { Snippet } from '@shared/types/main/db'
46-
import { addDays, isSameDay, isYesterday } from 'date-fns'
4744
import { loadWASM } from 'onigasm'
4845
import onigasmFile from 'onigasm/lib/onigasm.wasm?url'
4946
import { loadGrammars } from '@/components/editor/grammars'
47+
import {
48+
useSupportNotification,
49+
checkForRemoteNotification
50+
} from '@/composable/notification'
5051
5152
// По какой то причине необходимо явно установить роут в '/'
5253
// для корректного поведения в продакшен сборке
@@ -57,8 +58,9 @@ const appStore = useAppStore()
5758
const snippetStore = useSnippetStore()
5859
const route = useRoute()
5960
61+
const { showSupportToast } = useSupportNotification()
62+
6063
const isUpdateAvailable = ref(false)
61-
const isSupportToastShow = ref(false)
6264
const isDev = import.meta.env.DEV
6365
6466
const init = async () => {
@@ -118,55 +120,6 @@ const trackAppUpdate = () => {
118120
store.app.set('version', appStore.version)
119121
}
120122
121-
// Yes, this is that annoying piece of crap code.
122-
// You can delete it, but know that you hurt me.
123-
const showSupportToast = () => {
124-
if (appStore.isSponsored) return
125-
126-
const addNextNotice = () => {
127-
store.app.set('nextSupportNotice', addDays(new Date(), 7).valueOf())
128-
}
129-
130-
const date = store.app.get('nextSupportNotice')
131-
132-
if (!date) addNextNotice()
133-
134-
const isShow = isSameDay(new Date(), date) || isYesterday(date)
135-
136-
if (!isShow) return
137-
if (isSupportToastShow.value) return
138-
139-
const message = document.createElement('div')
140-
message.innerHTML = i18n.t('special:supportMessage', {
141-
tagStart: '<a id="donate" href="#">',
142-
tagEnd: '</a>'
143-
})
144-
145-
createToast(message, {
146-
action: {
147-
text: i18n.t('close'),
148-
callback (toast) {
149-
toast.destroy()
150-
addNextNotice()
151-
isSupportToastShow.value = false
152-
track('app/notify', 'support-close')
153-
}
154-
}
155-
})
156-
157-
const d = document.querySelector('#donate')
158-
159-
d?.addEventListener('click', () => {
160-
ipc.invoke('main:open-url', 'https://masscode.io/donate')
161-
destroyAllToasts()
162-
addNextNotice()
163-
isSupportToastShow.value = false
164-
track('app/notify', 'support-go-to-masscode')
165-
})
166-
167-
isSupportToastShow.value = true
168-
}
169-
170123
init()
171124
172125
watch(
@@ -198,6 +151,8 @@ ipc.on('main:update-available', () => {
198151
})
199152
200153
ipc.on('main:focus', () => {
154+
// Yes, this is that annoying piece of crap code.
155+
// You can delete it, but know that you hurt me.
201156
showSupportToast()
202157
})
203158
@@ -226,7 +181,6 @@ ipc.on('main-menu:preview-markdown', async () => {
226181
ipc.on('main-menu:presentation-mode', async () => {
227182
if (snippetStore.currentLanguage === 'markdown') {
228183
router.push('/presentation')
229-
track('snippets/presentation-mode')
230184
}
231185
})
232186

src/renderer/composable/index.ts

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ 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 { i18n, ipc, store, track } from '@/electron'
7+
import { ipc, 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'
1210

1311
export const useApi = createFetch({
1412
baseUrl: `http://localhost:${API_PORT}`
@@ -131,67 +129,6 @@ export const useHljsTheme = async (theme: 'dark' | 'light') => {
131129
document.head.appendChild(style)
132130
}
133131

134-
export const checkForRemoteNotification = async () => {
135-
const showMessage = (message: string, date: string | number) => {
136-
const el = document.createElement('div')
137-
el.innerHTML = message
138-
139-
const links = el.querySelectorAll('a')
140-
141-
links.forEach(i => {
142-
i.addEventListener('click', e => {
143-
e.preventDefault()
144-
ipc.invoke('main:open-url', i.href)
145-
track('app/notify', i.innerHTML)
146-
})
147-
})
148-
149-
createToast(el, {
150-
action: {
151-
text: i18n.t('close'),
152-
callback (toast) {
153-
toast.destroy()
154-
store.app.set('prevRemoteNotice', date)
155-
track('app/notify', `remoteNotification-${date}`)
156-
}
157-
}
158-
})
159-
}
160-
161-
const checkAndShow = async () => {
162-
try {
163-
const headers = {
164-
'Cache-Control': 'no-cache',
165-
Expires: 0
166-
}
167-
168-
const { data } = await axios.get<{ message: string; date: number }>(
169-
'https://masscode.io/notification.json',
170-
{ headers }
171-
)
172-
173-
if (!data) return
174-
175-
const { message, date } = data
176-
const prevDate = store.app.get('prevRemoteNotice')
177-
178-
if (prevDate) {
179-
if (prevDate < date) showMessage(message, date)
180-
} else {
181-
showMessage(message, date)
182-
}
183-
} catch (err) {
184-
console.error(err)
185-
}
186-
}
187-
188-
checkAndShow()
189-
190-
setInterval(() => {
191-
checkAndShow()
192-
}, 1000 * 60 * 180) // 3 часа
193-
}
194-
195132
export const onClickUrl = (url: string) => {
196133
ipc.invoke('main:open-url', url)
197134
track('app/open-url', url)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { createToast, destroyAllToasts } from 'vercel-toast'
2+
import { i18n, ipc, store, track } from '@/electron'
3+
import { useAppStore } from '@/store/app'
4+
import { addDays } from 'date-fns'
5+
import { ref } from 'vue'
6+
import axios from 'axios'
7+
8+
export const checkForRemoteNotification = async () => {
9+
const showMessage = (message: string, date: string | number) => {
10+
const el = document.createElement('div')
11+
el.innerHTML = message
12+
13+
const links = el.querySelectorAll('a')
14+
15+
links.forEach(i => {
16+
i.addEventListener('click', e => {
17+
e.preventDefault()
18+
ipc.invoke('main:open-url', i.href)
19+
track('app/notify', i.innerHTML)
20+
})
21+
})
22+
23+
createToast(el, {
24+
action: {
25+
text: i18n.t('close'),
26+
callback (toast) {
27+
toast.destroy()
28+
store.app.set('prevRemoteNotice', date)
29+
track('app/notify', `remoteNotification-close-${date}`)
30+
}
31+
}
32+
})
33+
}
34+
35+
const checkAndShow = async () => {
36+
try {
37+
const headers = {
38+
'Cache-Control': 'no-cache',
39+
Expires: 0
40+
}
41+
42+
const { data } = await axios.get<{ message: string; date: number }>(
43+
'https://masscode.io/notification.json',
44+
{ headers }
45+
)
46+
47+
if (!data) return
48+
49+
const { message, date } = data
50+
const prevDate = store.app.get('prevRemoteNotice')
51+
52+
if (prevDate) {
53+
if (prevDate < date) showMessage(message, date)
54+
} else {
55+
showMessage(message, date)
56+
}
57+
} catch (err) {
58+
console.error(err)
59+
}
60+
}
61+
62+
checkAndShow()
63+
64+
setInterval(() => {
65+
checkAndShow()
66+
}, 1000 * 60 * 180) // 3 часа
67+
}
68+
69+
export const useSupportNotification = () => {
70+
const appStore = useAppStore()
71+
const isShow = ref(false)
72+
73+
const setNextNoticeDate = () => {
74+
store.app.set('nextSupportNotice', addDays(new Date(), 7).valueOf())
75+
}
76+
77+
const showMessage = () => {
78+
const message = document.createElement('div')
79+
message.innerHTML = i18n.t('special:supportMessage', {
80+
tagStart: '<a id="donate" href="#">',
81+
tagEnd: '</a>'
82+
})
83+
84+
createToast(message, {
85+
action: {
86+
text: i18n.t('close'),
87+
callback (toast) {
88+
toast.destroy()
89+
setNextNoticeDate()
90+
isShow.value = false
91+
track('app/notify', 'support-close')
92+
}
93+
}
94+
})
95+
96+
const d = document.querySelector('#donate')
97+
98+
d?.addEventListener('click', () => {
99+
ipc.invoke('main:open-url', 'https://masscode.io/donate')
100+
destroyAllToasts()
101+
setNextNoticeDate()
102+
isShow.value = false
103+
track('app/notify', 'support-go-to-masscode')
104+
})
105+
106+
isShow.value = true
107+
}
108+
109+
const showSupportToast = () => {
110+
if (appStore.isSponsored) return
111+
112+
const date = store.app.get('nextSupportNotice')
113+
114+
if (!date && !isShow.value) {
115+
showMessage()
116+
} else {
117+
const isDateToShow = new Date().valueOf() > date
118+
119+
if (!isDateToShow) return
120+
if (isShow.value) return
121+
122+
showMessage()
123+
}
124+
}
125+
126+
return {
127+
showSupportToast
128+
}
129+
}

0 commit comments

Comments
 (0)