Files
aigen/utils/generationNotify.ts
T
TowstyandCursor 08caeb51de Eliminate xAIGen and LTX; AIGen is the only studio.
Drop the studio split, backup-to-xAIGen link, and LTX engine option. Show all LoRAs on AIGen and remove the Photos show/hide inputs toggle so slots stay visible.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-27 17:55:59 -05:00

80 lines
2.1 KiB
TypeScript

const PREF_KEY = 'aigen-notify-complete'
const TAG = 'aigen-generation-complete'
function appName() {
try {
const pub = useRuntimeConfig().public as { instanceName?: string }
return String(pub.instanceName || 'AIGen')
} catch {
return 'AIGen'
}
}
function appIcon() {
return '/favicon-aigen.svg'
}
export function generationNotifySupported() {
return typeof window !== 'undefined' && 'Notification' in window
}
export function generationNotifyPref() {
if (typeof window === 'undefined') return true
try {
return localStorage.getItem(PREF_KEY) !== '0'
} catch {
return true
}
}
export function setGenerationNotifyPref(on: boolean) {
try {
localStorage.setItem(PREF_KEY, on ? '1' : '0')
} catch { /* ignore */ }
}
export async function ensureGenerationNotifyPermission() {
if (!generationNotifySupported() || !generationNotifyPref()) return false
if (Notification.permission === 'granted') return true
if (Notification.permission === 'denied') return false
try {
const result = await Notification.requestPermission()
return result === 'granted'
} catch {
return false
}
}
export async function notifyGenerationComplete(kind: 'video' | 'image' | 'music' = 'video') {
if (!generationNotifySupported() || !generationNotifyPref()) return
if (Notification.permission !== 'granted') return
if (typeof document !== 'undefined' && !document.hidden && document.hasFocus()) return
const title = appName()
const body = kind === 'image'
? 'Image generation is complete.'
: kind === 'music'
? 'Music generation is complete.'
: 'Generation is complete.'
const options: NotificationOptions = {
body,
icon: appIcon(),
tag: TAG,
renotify: true,
silent: false
}
try {
const registration = await navigator.serviceWorker?.ready
if (registration?.showNotification) {
await registration.showNotification(title, options)
return
}
} catch { /* fall through */ }
try {
const note = new Notification(title, options)
note.onclick = () => {
window.focus()
note.close()
}
} catch { /* ignore */ }
}