Line data Source code
1 : import { onMounted, onUnmounted } from 'vue'
2 :
3 : import { isDesktop } from '@/platform/distribution/types'
4 : import { useSettingStore } from '@/platform/settings/settingStore'
5 : import { reportError } from '@comfyorg/telemetry/reportError'
6 : import { useCloudNotificationDialog } from '@/platform/cloud/notification/composables/useCloudNotificationDialog'
7 : import { electronAPI } from '@/utils/envUtil'
8 :
9 : /** Shows the one-time macOS desktop cloud notification after the host component mounts. */
10 2355 : export function useDesktopCloudNotification(): void {
11 2355 : const settingStore = useSettingStore()
12 2355 : const { showCloudNotification } = useCloudNotificationDialog()
13 :
14 : let disposed = false
15 : const isDisposed = (): boolean => disposed
16 : let cloudNotificationTimer: ReturnType<typeof setTimeout> | undefined
17 :
18 : function reportNotificationFailure(
19 : errorType:
20 : | 'cloud_notification_state_save_failed'
21 : | 'cloud_notification_show_failed'
22 : | 'cloud_notification_state_reset_failed',
23 : operation: 'save' | 'render',
24 : cause: unknown,
25 : platform: string
26 : ) {
27 : reportError(cause, {
28 : surface: 'platform',
29 : errorType,
30 : tags: {
31 : failure_kind: 'caught_unexpected',
32 : feature_area: 'cloud',
33 : operation,
34 : outcome: 'failed',
35 : assert_mode: 'soft'
36 : },
37 : context: { platform, is_disposed: disposed },
38 : level: 'error'
39 : })
40 : }
41 :
42 : async function resetNotificationState(platform: string) {
43 : try {
44 : await settingStore.set('Comfy.Desktop.CloudNotificationShown', false)
45 : } catch (error) {
46 : reportNotificationFailure(
47 : 'cloud_notification_state_reset_failed',
48 : 'save',
49 : error,
50 : platform
51 : )
52 : }
53 : }
54 :
55 2355 : async function scheduleCloudNotification() {
56 : if (!isDesktop) return
57 : const platform = electronAPI().getPlatform()
58 : if (platform !== 'darwin') return
59 :
60 : await settingStore.load()
61 : if (settingStore.error !== undefined) return
62 : if (isDisposed()) return
63 : if (settingStore.get('Comfy.Desktop.CloudNotificationShown')) return
64 :
65 : cloudNotificationTimer = setTimeout(async () => {
66 : if (isDisposed()) return
67 :
68 : try {
69 : await settingStore.set('Comfy.Desktop.CloudNotificationShown', true)
70 : } catch (error) {
71 : reportNotificationFailure(
72 : 'cloud_notification_state_save_failed',
73 : 'save',
74 : error,
75 : platform
76 : )
77 : await resetNotificationState(platform)
78 : return
79 : }
80 :
81 : if (isDisposed()) {
82 : await resetNotificationState(platform)
83 : return
84 : }
85 :
86 : try {
87 : await showCloudNotification()
88 : } catch (error) {
89 : reportNotificationFailure(
90 : 'cloud_notification_show_failed',
91 : 'render',
92 : error,
93 : platform
94 : )
95 : await resetNotificationState(platform)
96 : }
97 : }, 2000)
98 : }
99 :
100 2355 : onMounted(() => {
101 2355 : void scheduleCloudNotification()
102 : })
103 :
104 2355 : onUnmounted(() => {
105 : disposed = true
106 : if (cloudNotificationTimer) clearTimeout(cloudNotificationTimer)
107 : })
108 : }
|