After playing around with it for a while, I found a solution I believe to be elegant, though it uses Pinia. With this method, you can call toasts in helper functions as well, and reusing the functions are quite simplistic.
main.ts
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
import PrimeVue from "primevue/config";
import ToastService from "primevue/toastservice";
import Toast from "primevue/toast";
createApp(App)
.use(router)
.use(PrimeVue)
.use(createPinia())
.use(ToastService)
.component("Toast", Toast)
.mount("#app");
interfaces.ts
export interface Message {
severity: string;
summary: string;
detail: string;
}
useNotifications.ts
import { defineStore } from "pinia";
import { Message } from "@interfaces";
interface State {
info: Message;
notify: Message;
confirm: Message;
}
const useNotifications = defineStore({
id: "notificationStore",
// Might be better to only have one piece of state, but this is the solution I went with
// info for basic notifs, notify for sticky notifs, confirm for notifs that need confirmation
state: (): State => ({
info: {
severity: "",
summary: "",
detail: "",
},
notify: {
severity: "",
summary: "",
detail: "",
},
confirm: {
severity: "",
summary: "",
detail: "",
},
}),
});
export default useNotifications;
App.vue
<script setup lang="ts">
import { useToast } from "primevue/usetoast";
import { useNotifications } from "@store";
import { Message } from "@interfaces";
const notificationStore = useNotifications();
const toast = useToast();
// Watches changes on notificationStore throughout the app
notificationStore.$subscribe((mutation, state) => {
// Checks which part of the state has been mutated, and updates that state based on those conditions
// mutation.events.key will throw a TypeScript error, but it will still work (until build time where another solution should be found)
const key = mutation.events.key;
if (key === "info") {
const { severity, summary, detail } = state.info;
toast.add({ severity, summary, detail, life: 3000, group: "br" });
} else if (key === "notify") {
const { severity, summary, detail } = state.notify;
toast.add({ severity, summary, detail, group: "br" });
} else if (key === "confirm") {
const { severity, summary, detail } = state.confirm;
toast.add({ severity, summary, detail, group: "bc" });
}
});
// Use provide to make Toast functionality easily injectable
provide("toastConfirm", (args: Message) => {
const { severity, summary, detail } = args;
notificationStore.confirm = { severity, summary, detail };
});
provide("toastNotify", (args: Message) => {
const { severity, summary, detail } = args;
notificationStore.notify = { severity, summary, detail };
});
provide("toastInfo", (args: Message) => {
const { severity, summary, detail } = args;
notificationStore.info = { severity, summary, detail };
});
const denyToast = () => {
toast.removeGroup("bc");
};
// Have not figured out how to make this function useable
const acceptToast = () => {
toast.removeGroup("bc");
};
</script>
<template>
<!-- This group will represent the toasts I do not wish to have to confirm -->
<Toast position="bottom-right" group="br" />
<!-- The 'confirmable' toast template is basically copy pasted from PrimeVue docs -->
<!-- This Toast will appear in the bottom center -->
<Toast position="bottom-center" group="bc">
<template #message="slotProps">
<div>
<div>
<i class="pi pi-exclamation-triangle" />
<h4>{{ slotProps.message.summary }}</h4>
<p>{{ slotProps.message.detail }}</p>
</div>
<div>
<div>
<Button class="p-button-danger" label="No" @click="denyToast" />
<Button class="p-button-success" label="Yes" @click="acceptToast" />
</div>
</div>
</div>
</template>
</Toast>
</template>
AnyChildComponent.vue
<script setup lang="ts">
import { inject } from "vue";
import { Message } from "@interface";
const notifyMe = inject("toastNotify", (args: Message) => {});
const handleClick = () => {
notifyMe({
severity: "success",
summary: "success!",
detail: "this is from child component",
});
};
</script>
<template>
<Button @click="handleClick" />
</template>
exampleOfUsageInHelperFunction.ts
import { useNotifications } from "@store";
// Database helper function
const send = async (channel: string, data?: Object) => {
const notificationStore = useNotifications();
if (data) data = JSON.parse(JSON.stringify(data));
// response will return something like: { message: "Query failed", error: error } or { message: "Query succeeded", success?: returnData }
const response = await window.electron.ipcRenderer.invoke(channel, data);
if (response.error) {
console.log(response.error);
notificationStore.notify = {
severity: "danger",
summary: "Error",
detail: response.message,
};
} else {
notificationStore.info = {
severity: "success",
summary: "Success",
detail: response.message,
};
if (response.success) return response.success;
}
};
export default send;