diff --git a/src/components/layout/right-nav.tsx b/src/components/layout/right-nav.tsx index 36e0694..4ea3894 100644 --- a/src/components/layout/right-nav.tsx +++ b/src/components/layout/right-nav.tsx @@ -1,96 +1,37 @@ -import { For, Show, Suspense, createEffect, createMemo, createSignal } from "solid-js"; +import { Show, createMemo } from "solid-js"; import { useLocation, useNavigate } from "@tanstack/solid-router"; -import { patchMessageById } from "@/api/messages"; -import type { Appointment, Event, Exam, Message } from "@/api/client"; -import { appointmentStatusClass, appointmentStatusDotClass, appointmentStatusLabelKey } from "@/lib/appointment-status"; -import { Badge } from "@/components/ui/badge"; -import { Button } from "@/components/ui/button"; -import { PageSpinner } from "@/components/ui/page-spinner"; -import { PaginationControls } from "@/components/ui/pagination-controls"; -import { - IconCalendarDays, - IconChevronLeft, - IconChevronRight, - IconClock, - IconExternalLink, - IconMessage, - IconX, -} from "@/components/ui/icons"; +import type { Event, Exam } from "@/api/client"; +import { IconCalendarDays, IconMessage } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; -import { appointmentCounterpart, personLabel } from "@/lib/person"; -import { useAuth } from "@/stores/auth-context"; -import { usePreferences, useT } from "@/stores/preferences-context"; +import { useT } from "@/stores/preferences-context"; import { useShellFeed } from "@/stores/shell-feed-context"; -type ActiveTab = "messages" | "calendar" | null; -type CalendarItemRef = - | { type: "event"; data: Event } - | { type: "exam"; data: Exam } - | { type: "appointment"; data: Appointment }; - -const PAGE_SIZE = 5; - export function RightNav() { const t = useT(); const navigate = useNavigate(); const location = useLocation(); - const { locale } = usePreferences(); const feed = useShellFeed(); - const auth = useAuth(); const isMessagesActive = () => location().pathname === "/messages"; const isCalendarActive = () => location().pathname === "/calendar"; - // Active drawer tab - const [activeTab, setActiveTab] = createSignal(null); - - // Messages drawer state - const [unreadOnly, setUnreadOnly] = createSignal(false); - const [selectedMessage, setSelectedMessage] = createSignal(null); - - // Calendar drawer state - const [calPage, setCalPage] = createSignal(0); - const [calFilter, setCalFilter] = createSignal<"all" | "events" | "exams" | "appointments">("all"); - const [selectedCalItem, setSelectedCalItem] = createSignal(null); - // Shell feed data comes from the shared poller (ShellFeedProvider), so - // messages/events/exams/appointments are fetched once for the whole shell, - // not independently here and in NotificationCenter. - const refetchMessages = feed.refetchMessages; - const refreshAll = feed.refreshAll; - - // Computed message data - // `.latest` (via the store accessor), not a bare `resource()`: this feeds the - // always-rendered unread badge outside any . A bare read re-suspends - // on every 60s refetch and — since RightNav sits beside in AppShell — - // blanks the whole page for the fetch duration. `.latest` keeps the last value. - const allMessages = () => feed.messages().items; - // Same source of truth as NotificationCenter: the dedicated `read=false` page, - // whose `total` is the exact server-side unread count. The inbox page above is - // capped at 100 and ordered by id DESC, so filtering it silently drops older - // unread messages and the two shell badges disagree. + // messages/events/exams are fetched once for the whole shell, not + // independently here and in NotificationCenter. + + // Two rules ride on this one line: + // 1. `.latest` (via the store accessor), never a bare `resource()`: this feeds + // the always-rendered badge outside any , and RightNav sits beside + // in AppShell, so a re-suspend blanks the whole page for the fetch. + // 2. The dedicated `read=false` page's `total` — the exact server-side count, + // and the same source NotificationCenter reads. Deriving unread by filtering + // the general inbox page instead silently drops anything older than its + // newest 100 rows, and the two shell badges then disagree. const unreadCount = createMemo(() => feed.unreadMessages().total); - const filteredMessages = createMemo(() => { - let list = allMessages(); - if (unreadOnly()) { - list = list.filter((m: Message) => !m.read); - } - return list; - }); - - // Top 5 recent messages only for side panel - const recentMessages = createMemo(() => { - return filteredMessages().slice(0, 5); - }); - // Reactive clock from the shared poller so passed items clear without a remount. const nowMs = feed.nowMs; - createEffect(() => { - if (activeTab() !== null) refreshAll(); - }); - // An item is still notifiable until it has ended (ends_at, else starts_at). const notEnded = (starts?: number | null, ends?: number | null) => { if (!starts) return false; @@ -106,85 +47,13 @@ export function RightNav() { return feed.exams().items.filter((e: Exam) => !e.draft && notEnded(e.starts_at, e.ends_at)); }); - const activeAppointments = createMemo(() => { - return feed.appointments().items.filter( - (a: Appointment) => (a.status === "pending" || a.status === "approved") && notEnded(a.starts_at, a.ends_at) - ); - }); - const hasTodayEvents = createMemo(() => activeEvents().length > 0); const hasTodayExams = createMemo(() => activeExams().length > 0); - const combinedCalendarItems = createMemo(() => { - const items: CalendarItemRef[] = []; - - if (calFilter() === "all" || calFilter() === "events") { - for (const ev of activeEvents()) { - items.push({ type: "event", data: ev }); - } - } - - if (calFilter() === "all" || calFilter() === "exams") { - for (const ex of activeExams()) { - items.push({ type: "exam", data: ex }); - } - } - - if (calFilter() === "all" || calFilter() === "appointments") { - for (const ap of activeAppointments()) { - items.push({ type: "appointment", data: ap }); - } - } - - // Sort chronologically by start date - return items.sort((a, b) => { - const timeA = a.data.starts_at ? new Date(a.data.starts_at).getTime() : 0; - const timeB = b.data.starts_at ? new Date(b.data.starts_at).getTime() : 0; - return timeA - timeB; - }); - }); - - const calTotalPages = createMemo(() => Math.max(1, Math.ceil(combinedCalendarItems().length / PAGE_SIZE))); - const paginatedCalItems = createMemo(() => { - const start = calPage() * PAGE_SIZE; - return combinedCalendarItems().slice(start, start + PAGE_SIZE); - }); - - const closePanel = () => { - setActiveTab(null); - setSelectedMessage(null); - setSelectedCalItem(null); - }; - const goTo = (path: string) => { - closePanel(); void navigate({ to: path }); }; - // Mark message as read/unread toggle - const toggleReadStatus = async (msg: Message) => { - try { - await patchMessageById(msg.id, { read: !msg.read }); - setSelectedMessage({ ...msg, read: !msg.read }); - void refetchMessages(); - } catch { - // ignore - } - }; - - // Mark as read if unread and navigate directly to /messages page - const handleSelectMessage = async (msg: Message) => { - if (!msg.read) { - try { - await patchMessageById(msg.id, { read: true }); - void refetchMessages(); - } catch { - // ignore - } - } - goTo("/messages"); - }; - return ( <> {/* Sticky Right Icon Bar */} @@ -267,568 +136,6 @@ export function RightNav() {
- - {/* Flyout Side Drawer Panel */} - - {/* Backdrop overlay blurring main page content on the left */} -
- -
- {/* Drawer Header */} -
-
- - - - {t("rightPanel.messagesTitle")} - 0}> - - {unreadCount()} {t("rightPanel.unreadBadge")} - - - - } - > - - - - - - - - {t("rightPanel.calendarTitle")} - - } - > - - - -
- - -
- - {/* Drawer Body */} -
- {/* MESSAGES TAB */} - - {/* Message Details Preview View */} - }> - {/* Filter Bar */} -
-
- - -
-
- - 0} - fallback={ -
- -

{t("rightPanel.noUnread")}

-
- } - > -
- - {(msg) => ( -
handleSelectMessage(msg)} - class={cn( - "group flex cursor-pointer flex-col gap-1 rounded-xl border p-2.5 transition-all hover:border-primary/50 hover:bg-muted/40 shadow-2xs max-w-full overflow-hidden", - !msg.read - ? "border-primary/30 bg-primary/3 dark:bg-primary/6" - : "border-border/60 bg-card" - )} - > -
-
- - {personLabel(msg.sender).charAt(0).toUpperCase()} - - - {personLabel(msg.sender)} - -
- - {msg.sent_at - ? new Date(msg.sent_at).toLocaleTimeString( - locale() === "tr" ? "tr-TR" : "en-US", - { hour: "2-digit", minute: "2-digit" } - ) - : ""} - -
- -

- {msg.subject} -

-
-

- {msg.body} -

-
- - - - {t("rightPanel.unreadBadge")} - - - - Mesaja Git → - -
-
- )} -
-
- - {/* Redirect link for all messages if total > 5 */} - 5}> -
- -
-
-
- - } - > - {(msg) => ( -
-
-
-
- - {personLabel(msg().sender).charAt(0).toUpperCase()} - -
-

- {personLabel(msg().sender)} -

-

- {msg().sent_at - ? new Date(msg().sent_at).toLocaleString( - locale() === "tr" ? "tr-TR" : "en-US", - { dateStyle: "medium", timeStyle: "short" } - ) - : ""} -

-
-
- - -
- - -

- {msg().subject} -

-
- -
- {msg().body} -
- -
- -
-
-
- )} -
-
- - {/* CALENDAR TAB */} - - {/* Calendar Item Preview View */} - }> - {/* Filter Chips */} -
- - - - -
- - 0} - fallback={ -
- -

{t("rightPanel.noUpcoming")}

-
- } - > -
- - {(item) => { - const isEvent = item.type === "event"; - const isAppt = item.type === "appointment"; - const title = item.type === "appointment" - ? appointmentCounterpart(item.data, auth.user()?.id) - : item.data.title; - return ( -
setSelectedCalItem(item)} - class={cn( - "group flex cursor-pointer items-start justify-between gap-2.5 rounded-xl border p-3 shadow-2xs transition-all hover:shadow-md", - isEvent - ? "border-emerald-500/30 bg-emerald-500/2 hover:border-emerald-500/60 hover:bg-emerald-500/5" - : isAppt - ? "border-violet-500/30 bg-violet-500/2 hover:border-violet-500/60 hover:bg-violet-500/5" - : "border-rose-500/30 bg-rose-500/2 hover:border-rose-500/60 hover:bg-rose-500/5" - )} - > -
-

- {title} -

- -

- {item.type === "appointment" ? item.data.reason : ""} -

-
-
- - - {item.data.starts_at - ? new Date(item.data.starts_at).toLocaleDateString( - locale() === "tr" ? "tr-TR" : "en-US", - { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" } - ) - : ""} - -
-
- - - {isEvent ? t("calendar.events") : t("calendar.exams")} - - } - > - - - {t(appointmentStatusLabelKey((item.data as Appointment).status))} - - -
- ); - }} -
-
- - {/* Pagination Controls */} - 1}> - - -
- - } - > - {(item) => { - const isEvent = item().type === "event"; - const isAppt = item().type === "appointment"; - const cur = item(); - const appt = () => (cur.type === "appointment" ? cur.data : null); - const title = () => { - const a = appt(); - if (a) return appointmentCounterpart(a, auth.user()?.id); - return (cur.data as Event | Exam).title; - }; - return ( -
-
-
- - {isEvent ? t("calendar.events") : t("calendar.exams")} - - } - > - {(a) => ( - <> - - {t("calendar.appointments")} - - - - {t(appointmentStatusLabelKey(a().status))} - - - )} - -
- -

- {title()} -

- -
-
- - - {cur.data.starts_at - ? new Date(cur.data.starts_at).toLocaleString( - locale() === "tr" ? "tr-TR" : "en-US", - { dateStyle: "full", timeStyle: "short" } - ) - : "—"} - -
- - - {(a) => ( - -
-

{t("appointments.reason")}

-

{a().reason}

-
-
- )} -
- - -
-

Açıklama / Detay:

-

{String((cur.data as Event).description)}

-
-
-
- -
- -
-
-
- ); - }} -
-
-
- - {/* Drawer Footer Action */} -
- - - - - - - -
-
- ); }