Header redesign, sound/toast notifications, and agent scoping
Four requested improvements: 1. Logo + wordmark now link to /dashboard. 2. Header reordered: nav links (Заявки/Статистика) centered, right side now reads notifications -> theme -> settings -> account name -> logout, matching the requested order. 3. New TicketToasts component: in-page toast + a short Web Audio chime for new tickets and customer messages, shown regardless of tab focus. Complements the existing DesktopNotifications, which only fires the native OS notification while the tab is backgrounded. Also added a 30s polling refresh on the dashboard as a plain safety net under the existing SSE push, in case an event is dropped by whatever's in front of this app on the network. 4. Agents now see only tickets assigned to them plus unassigned ones (not the whole board) — listTickets() takes an optional visibility filter, applied in both the dashboard page and the /api/tickets search route, and mirrored client-side so a live SSE update can't leak a just-reassigned-away ticket onto their board. Settings gear and the Статистика nav link are now hidden entirely for agents rather than just showing fewer items inside. Also closed a real gap this surfaced: /stats had no server-side role check at all (only the nav link was hidden), so any agent could load it directly — added the same session.user.role !== "admin" guard the other admin-only settings pages already have. Verified end-to-end with a temporary Playwright pass (installed and fully removed afterward): logo click navigation, header icon order, agent view missing the gear/Статистика, direct-URL access to /stats and /settings/accounts still redirecting non-admins, and ticket visibility correctly disappearing from an agent's board the moment it's reassigned to someone else. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012o9j9RezxbZVKQMrB7oRLY
This commit is contained in:
1 parent
c32888981c
commit
69467239ed
11 files changed
+201
-21
No files matched your search
@@ -0,0 +1,108 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { AnimatePresence, motion } from "framer-motion";
|
||||
import { Bell, X } from "lucide-react";
|
||||
import { useTicketEvents } from "@/lib/events/use-ticket-events";
|
||||
import { isTicketVisibleTo } from "@/lib/tickets/visibility";
|
||||
|
||||
interface Toast {
|
||||
id: string;
|
||||
title: string;
|
||||
body: string;
|
||||
href: string;
|
||||
}
|
||||
|
||||
/** A short two-tone chime via the Web Audio API — no audio asset needed. */
|
||||
function playChime() {
|
||||
try {
|
||||
const AudioCtor = window.AudioContext ?? (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;
|
||||
if (!AudioCtor) return;
|
||||
const ctx = new AudioCtor();
|
||||
const now = ctx.currentTime;
|
||||
for (const [freq, start] of [[880, 0], [1175, 0.12]] as const) {
|
||||
const osc = ctx.createOscillator();
|
||||
const gain = ctx.createGain();
|
||||
osc.type = "sine";
|
||||
osc.frequency.value = freq;
|
||||
gain.gain.setValueAtTime(0.0001, now + start);
|
||||
gain.gain.exponentialRampToValueAtTime(0.25, now + start + 0.01);
|
||||
gain.gain.exponentialRampToValueAtTime(0.0001, now + start + 0.25);
|
||||
osc.connect(gain);
|
||||
gain.connect(ctx.destination);
|
||||
osc.start(now + start);
|
||||
osc.stop(now + start + 0.25);
|
||||
}
|
||||
setTimeout(() => ctx.close(), 500);
|
||||
} catch {
|
||||
// audio isn't critical to the notification — a silent toast still works
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In-page toast + sound for new tickets and customer messages, shown
|
||||
* regardless of tab focus. Complements DesktopNotifications, which only
|
||||
* fires the native OS notification while the tab is in the background —
|
||||
* this is the "still on the page" case.
|
||||
*/
|
||||
export function TicketToasts({ currentUser }: { currentUser: { id: string; role: "admin" | "agent" } }) {
|
||||
const router = useRouter();
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
const idCounter = useRef(0);
|
||||
|
||||
const pushToast = useCallback((title: string, body: string, href: string) => {
|
||||
const id = `${Date.now()}-${idCounter.current++}`;
|
||||
setToasts((prev) => [...prev, { id, title, body, href }]);
|
||||
playChime();
|
||||
setTimeout(() => setToasts((prev) => prev.filter((t) => t.id !== id)), 6_000);
|
||||
}, []);
|
||||
|
||||
useTicketEvents("/api/events", (event) => {
|
||||
if (event.type === "ticket.created") {
|
||||
if (!isTicketVisibleTo(event.ticket, currentUser)) return;
|
||||
pushToast("Новая заявка", event.ticket.subject, `/tickets/${event.ticket.id}`);
|
||||
} else if (event.type === "message.created" && event.message.authorType === "customer") {
|
||||
if (!isTicketVisibleTo({ assigneeId: event.assigneeId }, currentUser)) return;
|
||||
pushToast(`Сообщение от ${event.message.authorName}`, event.message.body.slice(0, 120), `/tickets/${event.ticketId}`);
|
||||
}
|
||||
});
|
||||
|
||||
function dismiss(id: string) {
|
||||
setToasts((prev) => prev.filter((t) => t.id !== id));
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="pointer-events-none fixed bottom-4 right-4 z-50 flex w-full max-w-sm flex-col gap-2">
|
||||
<AnimatePresence initial={false}>
|
||||
{toasts.map((toast) => (
|
||||
<motion.div
|
||||
key={toast.id}
|
||||
initial={{ opacity: 0, y: 12, scale: 0.96 }}
|
||||
animate={{ opacity: 1, y: 0, scale: 1 }}
|
||||
exit={{ opacity: 0, scale: 0.96 }}
|
||||
transition={{ duration: 0.15 }}
|
||||
className="card pointer-events-auto flex items-start gap-2 p-3 shadow-md"
|
||||
>
|
||||
<span className="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-accent-soft text-accent-soft-text">
|
||||
<Bell size={13} />
|
||||
</span>
|
||||
<button
|
||||
onClick={() => {
|
||||
dismiss(toast.id);
|
||||
router.push(toast.href);
|
||||
}}
|
||||
className="min-w-0 flex-1 text-left"
|
||||
>
|
||||
<p className="truncate text-sm font-medium">{toast.title}</p>
|
||||
<p className="line-clamp-2 text-xs text-text-muted">{toast.body}</p>
|
||||
</button>
|
||||
<button onClick={() => dismiss(toast.id)} className="text-text-faint hover:text-text" aria-label="Закрыть">
|
||||
<X size={13} />
|
||||
</button>
|
||||
</motion.div>
|
||||
))}
|
||||
</AnimatePresence>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user