|
| 1 | +import type { RealtimeChannel } from '@supabase/supabase-js' |
| 2 | +import { onUnmounted, ref, watch } from 'vue' |
| 3 | +import { useI18n } from 'vue-i18n' |
| 4 | +import { useRouter } from 'vue-router' |
| 5 | +import { toast } from 'vue-sonner' |
| 6 | +import { useSupabase } from '~/services/supabase' |
| 7 | +import { useMainStore } from '~/stores/main' |
| 8 | +import { useOrganizationStore } from '~/stores/organization' |
| 9 | + |
| 10 | +interface CLIActivityPayload { |
| 11 | + event: string |
| 12 | + channel: string |
| 13 | + description?: string |
| 14 | + icon?: string |
| 15 | + app_id?: string |
| 16 | + org_id: string |
| 17 | + channel_name?: string |
| 18 | + bundle_name?: string |
| 19 | + timestamp: string |
| 20 | +} |
| 21 | + |
| 22 | +function getRouteForEvent(payload: CLIActivityPayload): string | null { |
| 23 | + const appId = payload.app_id |
| 24 | + if (!appId) |
| 25 | + return null |
| 26 | + |
| 27 | + const evt = payload.event.toLowerCase() |
| 28 | + |
| 29 | + if (evt.includes('deleted') && (evt.includes('app') && !evt.includes('bundle'))) |
| 30 | + return '/app' |
| 31 | + if (evt.includes('upload') || evt.includes('bundle') || evt.includes('external') || evt.includes('unlink')) |
| 32 | + return `/app/${appId}/bundles` |
| 33 | + if (evt.includes('channel')) |
| 34 | + return `/app/${appId}/channels` |
| 35 | + if (evt.includes('app')) |
| 36 | + return `/app/${appId}` |
| 37 | + |
| 38 | + return `/app/${appId}` |
| 39 | +} |
| 40 | + |
| 41 | +export function useRealtimeCLIFeed() { |
| 42 | + const supabase = useSupabase() |
| 43 | + const main = useMainStore() |
| 44 | + const orgStore = useOrganizationStore() |
| 45 | + const router = useRouter() |
| 46 | + const { t } = useI18n() |
| 47 | + |
| 48 | + let currentChannel: RealtimeChannel | null = null |
| 49 | + const isConnected = ref(false) |
| 50 | + |
| 51 | + function isEnabled(): boolean { |
| 52 | + const prefs = (main.user as any)?.email_preferences as Record<string, boolean> | undefined |
| 53 | + return prefs?.cli_realtime_feed ?? true |
| 54 | + } |
| 55 | + |
| 56 | + function subscribe(orgId: string) { |
| 57 | + unsubscribe() |
| 58 | + if (!isEnabled()) |
| 59 | + return |
| 60 | + |
| 61 | + const channelName = `cli-events:org:${orgId}` |
| 62 | + currentChannel = supabase.channel(channelName) |
| 63 | + |
| 64 | + currentChannel |
| 65 | + .on('broadcast', { event: 'cli-activity' }, (message) => { |
| 66 | + const payload = message.payload as CLIActivityPayload |
| 67 | + showToast(payload) |
| 68 | + }) |
| 69 | + .subscribe(async (status) => { |
| 70 | + if (status === 'SUBSCRIBED') { |
| 71 | + isConnected.value = true |
| 72 | + await currentChannel?.track({ |
| 73 | + user_id: main.user?.id, |
| 74 | + online_at: new Date().toISOString(), |
| 75 | + }) |
| 76 | + } |
| 77 | + else { |
| 78 | + isConnected.value = false |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + function unsubscribe() { |
| 84 | + if (currentChannel) { |
| 85 | + supabase.removeChannel(currentChannel) |
| 86 | + currentChannel = null |
| 87 | + isConnected.value = false |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + function showToast(payload: CLIActivityPayload) { |
| 92 | + const route = getRouteForEvent(payload) |
| 93 | + const icon = payload.icon ?? '📡' |
| 94 | + const title = `${icon} ${payload.event}` |
| 95 | + const description = payload.description |
| 96 | + ?? (payload.app_id ? `App: ${payload.app_id}` : undefined) |
| 97 | + |
| 98 | + if (route) { |
| 99 | + toast(title, { |
| 100 | + description, |
| 101 | + duration: 5000, |
| 102 | + action: { |
| 103 | + label: t('view'), |
| 104 | + onClick: () => router.push(route), |
| 105 | + }, |
| 106 | + }) |
| 107 | + } |
| 108 | + else { |
| 109 | + toast(title, { |
| 110 | + description, |
| 111 | + duration: 4000, |
| 112 | + }) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Re-subscribe when org changes |
| 117 | + watch( |
| 118 | + () => orgStore.currentOrganization?.gid, |
| 119 | + (orgId) => { |
| 120 | + if (orgId) |
| 121 | + subscribe(orgId) |
| 122 | + else |
| 123 | + unsubscribe() |
| 124 | + }, |
| 125 | + { immediate: true }, |
| 126 | + ) |
| 127 | + |
| 128 | + // React to user toggling the setting |
| 129 | + watch( |
| 130 | + () => (main.user as any)?.email_preferences?.cli_realtime_feed, |
| 131 | + (enabled) => { |
| 132 | + const orgId = orgStore.currentOrganization?.gid |
| 133 | + if (enabled === false) { |
| 134 | + unsubscribe() |
| 135 | + } |
| 136 | + else if (orgId && !currentChannel) { |
| 137 | + subscribe(orgId) |
| 138 | + } |
| 139 | + }, |
| 140 | + ) |
| 141 | + |
| 142 | + onUnmounted(() => unsubscribe()) |
| 143 | + |
| 144 | + return { isConnected } |
| 145 | +} |
0 commit comments