2021-07-30 15:01:01 +02:00
|
|
|
import { writable, get } from "svelte/store"
|
2021-08-02 16:12:38 +02:00
|
|
|
import { routeStore } from "./routes"
|
2021-01-22 12:21:44 +01:00
|
|
|
|
2021-01-22 13:11:38 +01:00
|
|
|
const NOTIFICATION_TIMEOUT = 3000
|
2021-01-22 11:37:34 +01:00
|
|
|
|
2021-01-22 12:21:44 +01:00
|
|
|
const createNotificationStore = () => {
|
2022-07-20 14:54:45 +02:00
|
|
|
const timeoutIds = new Set()
|
2021-07-30 15:01:01 +02:00
|
|
|
let block = false
|
|
|
|
|
2022-07-20 14:41:18 +02:00
|
|
|
const store = writable([], () => {
|
2021-03-02 14:26:37 +01:00
|
|
|
return () => {
|
2022-07-20 14:54:45 +02:00
|
|
|
timeoutIds.forEach(timeoutId => {
|
|
|
|
clearTimeout(timeoutId)
|
|
|
|
})
|
2022-07-20 14:41:18 +02:00
|
|
|
store.set([])
|
2021-03-02 14:26:37 +01:00
|
|
|
}
|
|
|
|
})
|
2021-03-15 08:50:51 +01:00
|
|
|
|
|
|
|
const blockNotifications = (timeout = 1000) => {
|
|
|
|
block = true
|
|
|
|
setTimeout(() => (block = false), timeout)
|
|
|
|
}
|
2021-01-22 12:44:43 +01:00
|
|
|
|
2022-02-07 16:25:03 +01:00
|
|
|
const send = (message, type = "info", icon, autoDismiss = true) => {
|
2021-03-15 08:50:51 +01:00
|
|
|
if (block) {
|
|
|
|
return
|
|
|
|
}
|
2021-08-02 16:12:38 +02:00
|
|
|
|
|
|
|
// If peeking, pass notifications back to parent window
|
|
|
|
if (get(routeStore).queryParams?.peek) {
|
2021-11-04 17:22:02 +01:00
|
|
|
window.parent.postMessage({
|
|
|
|
type: "notification",
|
|
|
|
detail: {
|
|
|
|
message,
|
|
|
|
type,
|
|
|
|
icon,
|
2022-02-07 16:25:03 +01:00
|
|
|
autoDismiss,
|
2021-11-04 17:22:02 +01:00
|
|
|
},
|
|
|
|
})
|
2021-08-02 16:12:38 +02:00
|
|
|
return
|
|
|
|
}
|
2022-07-20 14:54:45 +02:00
|
|
|
const _id = id()
|
2022-07-20 14:41:18 +02:00
|
|
|
store.update(state => {
|
2022-07-20 14:54:45 +02:00
|
|
|
return [
|
|
|
|
...state,
|
|
|
|
{
|
|
|
|
id: _id,
|
|
|
|
type,
|
|
|
|
message,
|
|
|
|
icon,
|
|
|
|
dismissable: !autoDismiss,
|
|
|
|
delay: get(store) != null,
|
2022-07-20 14:55:12 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
2022-02-07 16:25:03 +01:00
|
|
|
if (autoDismiss) {
|
2022-07-20 14:54:45 +02:00
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
dismiss(_id)
|
2022-02-07 16:25:03 +01:00
|
|
|
}, NOTIFICATION_TIMEOUT)
|
2022-07-20 14:54:45 +02:00
|
|
|
timeoutIds.add(timeoutId)
|
2022-02-07 16:25:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 14:41:18 +02:00
|
|
|
const dismiss = id => {
|
|
|
|
store.update(state => {
|
|
|
|
return state.filter(n => n.id !== id)
|
|
|
|
})
|
2021-03-02 14:26:37 +01:00
|
|
|
}
|
2021-02-11 10:39:35 +01:00
|
|
|
|
2021-01-22 11:37:34 +01:00
|
|
|
return {
|
2021-07-30 15:01:01 +02:00
|
|
|
subscribe: store.subscribe,
|
|
|
|
actions: {
|
2021-08-02 16:12:38 +02:00
|
|
|
send,
|
2021-07-30 15:01:01 +02:00
|
|
|
info: msg => send(msg, "info", "Info"),
|
|
|
|
success: msg => send(msg, "success", "CheckmarkCircle"),
|
|
|
|
warning: msg => send(msg, "warning", "Alert"),
|
2022-02-07 16:25:03 +01:00
|
|
|
error: msg => send(msg, "error", "Alert", false),
|
2021-07-30 15:01:01 +02:00
|
|
|
blockNotifications,
|
2022-02-07 16:25:03 +01:00
|
|
|
dismiss,
|
2021-07-30 15:01:01 +02:00
|
|
|
},
|
2021-01-22 11:37:34 +01:00
|
|
|
}
|
2022-07-20 14:54:45 +02:00
|
|
|
|
|
|
|
function id() {
|
|
|
|
return "_" + Math.random().toString(36).slice(2, 9)
|
|
|
|
}
|
2021-01-22 12:21:44 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 14:26:37 +01:00
|
|
|
export const notificationStore = createNotificationStore()
|