Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.
Go email-relayemail-routinghttp-to-smtpmail-deliverymailchimp-alternativemailgun-alternativemailsendermailservermailtemplatemailtestpostmark-alternativesend-emailsendgrid-alternativesendmailtransactional-emailtransactional-outbox
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { inboxApi, type InboxNotification } from "../api/inbox";
// The user's in-app inbox: the header bell and the dashboard alert banner read
// from here. Distinct from stores/notification, which is transient toasts.
//
// There is no push channel; the counts are polled and the banner refreshes with
// the dashboard. An inbox item is never urgent enough to justify a socket, and a
// poll degrades gracefully when the tab is backgrounded.
const POLL_MS = 60_000;
export const useInboxStore = defineStore("inbox", () => {
const unread = ref(0);
const items = ref<InboxNotification[]>([]);
const banner = ref<InboxNotification[]>([]);
const loading = ref(false);
let timer: ReturnType<typeof setInterval> | null = null;
const badge = computed(() => (unread.value > 99 ? "99+" : String(unread.value)));
async function loadCounts() {
try {
unread.value = (await inboxApi.counts()).data.data?.unread ?? 0;
} catch {
/* transient; the badge is not worth an error toast */
}
}
async function loadRecent(limit = 20) {
loading.value = true;
try {
items.value = (await inboxApi.list({ limit })).data.data ?? [];
} catch {
items.value = [];
} finally {
loading.value = false;
}
}
async function loadBanner() {
try {
banner.value = (await inboxApi.banner()).data.data ?? [];
} catch {
banner.value = [];
}
}
function start() {
if (timer) return;
void loadCounts();
timer = setInterval(() => void loadCounts(), POLL_MS);
}
function stop() {
if (timer) clearInterval(timer);
timer = null;
}
function stamp(list: InboxNotification[], ids: number[], field: "read_at" | "dismissed_at") {
const now = new Date().toISOString();
for (const n of list) {
if (ids.includes(n.id) && !n[field]) n[field] = now;
}
}
async function markRead(ids: number[]) {
if (!ids.length) return;
stamp(items.value, ids, "read_at");
await inboxApi.markRead(ids);
await loadCounts();
}
async function markAllRead() {
stamp(
items.value,
items.value.map((n) => n.id),
"read_at"
);
unread.value = 0;
await inboxApi.markAllRead();
}
// Dismissal removes the item from the banner optimistically. The server binds
// it to the condition as it currently stands, so it comes back on its own if
// the condition materially worsens.
async function dismiss(id: number) {
banner.value = banner.value.filter((n) => n.id !== id);
stamp(items.value, [id], "dismissed_at");
stamp(items.value, [id], "read_at");
await inboxApi.dismiss([id]);
await loadCounts();
}
async function dismissAll() {
banner.value = [];
await inboxApi.dismissAll();
await loadCounts();
}
function reset() {
stop();
unread.value = 0;
items.value = [];
banner.value = [];
}
return {
unread,
badge,
items,
banner,
loading,
start,
stop,
reset,
loadCounts,
loadRecent,
loadBanner,
markRead,
markAllRead,
dismiss,
dismissAll,
};
});