1import { useAuthStore } from "@/stores/auth";
2import { NotificationLog, useNotifications } from "@framework/ui";
3import { defineStore } from "pinia";
4import { computed, ref, watch } from "vue";
5import { RouteLocationRaw } from "vue-router";
6import { globalStore } from "./globalStore";
7
8export const useNotificationStore = defineStore("notification", () => {
9 const authStore = useAuthStore();
10 const { $socket } = globalStore();
11
12 const visible = ref(false);
13 const controller = useNotifications({
14 appName: "opendesk",
15 currentUser: authStore.userId,
16 socket: $socket,
17 });
18
19 const unread = computed(() => controller.unreadCount);
20
21 const readFilter = ref<"all" | "unread">("all");
22 // a Notification Log `type`; empty for all categories
23 const category = ref("");
24
25 function toggle() {
26 visible.value = !visible.value;
27 if (visible.value) controller.markSeen();
28 else resetFilters();
29 }
30
31 // the filters also scope the bell's unread count, so drop them on leaving
32 function resetFilters() {
33 readFilter.value = "all";
34 category.value = "";
35 }
36
37 // Every opendesk notification references the ticket; the ones about a
38 // comment carry it as the source, which is the deep-link target.
39 function routeFor(n: NotificationLog): RouteLocationRaw | null {
40 if (n.document_type !== "OD Ticket" || !n.document_name) return null;
41 const route = {
42 name: "TicketAgent",
43 params: { ticketId: n.document_name },
44 };
45 if (n.source_doctype !== "Comment" || !n.source_name) return route;
46 return {
47 ...route,
48 // ?highlight is the activity deep-link target (element id); the hash
49 // only selects the tab
50 hash: "#activity",
51 query: { highlight: "comment-" + n.source_name },
52 };
53 }
54
55 watch([readFilter, category], () => {
56 const filters: Record<string, unknown> = {};
57 if (readFilter.value === "unread") filters.read = 0;
58 if (category.value) filters.type = category.value;
59 controller.setFilters(filters);
60 });
61
62 return {
63 category,
64 controller,
65 toggle,
66 routeFor,
67 unread,
68 readFilter,
69 resetFilters,
70 visible,
71 };
72});
73