-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
139 lines (128 loc) · 4.97 KB
/
index.tsx
File metadata and controls
139 lines (128 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use client";
import clsx from "clsx";
import { motion, MotionProps } from "framer-motion";
import { forwardRef, useEffect, useRef, useMemo, useState } from "react";
import { FilterTabs } from "./FilterTabs";
import { NotificationFooter } from "./NotificationFooter";
import { NotificationHeader } from "./NotificationHeader";
import { NotificationItem } from "./NotificationItem";
import { useQuery } from "@tanstack/react-query";
import { toast } from "sonner";
import { NotificationsSkeleton } from "./Skeleton";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBellSlash } from "@fortawesome/free-solid-svg-icons";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { useApiClient } from "@/utils/web-api";
import { FilterLabels, FilterType, matchNotificationFilter } from "./Filter";
type NotificationsProps = MotionProps & React.HTMLAttributes<HTMLDivElement>;
const Notifications = forwardRef<HTMLDivElement, NotificationsProps>(
(props, ref) => {
const { className } = props;
const { activeOrganization } = useDashboardContext();
const [activeFilter, setActiveFilter] = useState<FilterType>("all");
const scrollRef = useRef<HTMLDivElement>(null);
const api = useApiClient();
const notifications = useQuery({
queryKey: ["notifications", activeOrganization?.organization.id],
queryFn: async () => {
const resp = await api.notifications.get();
if (resp.status !== 200) {
toast.error("Failed to fetch notifications");
return { notifications: [], count: {} };
}
return resp.body;
},
refetchOnWindowFocus: false,
});
const filteredNotifications = useMemo(
() =>
notifications.data?.notifications.filter((notification) =>
matchNotificationFilter(activeFilter, notification.type)
),
[notifications.data, activeFilter]
);
const isNotificationTabEmpty = useMemo(() => {
return filteredNotifications?.length === 0;
}, [filteredNotifications]);
useEffect(() => {
if (!scrollRef.current) return;
const handleKeyDown = (e: KeyboardEvent) => {
const target = e.target as HTMLElement;
// Ignore typing/navigation inside inputs/editable fields
if (target.closest("input, textarea, [contenteditable='true']")) return;
// Only handle when event originates within the panel's scroll area
if (!scrollRef.current?.contains(target)) return;
if (e.key === "ArrowUp") {
e.preventDefault();
scrollRef.current?.scrollBy({ top: -100, behavior: "smooth" });
} else if (e.key === "ArrowDown") {
e.preventDefault();
scrollRef.current?.scrollBy({ top: 100, behavior: "smooth" });
}
};
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, []);
return (
<motion.div
ref={ref}
initial={{ opacity: 0, y: 4, scale: 0.98, display: "none" }}
animate={{ opacity: 1, y: 0, scale: 1, display: "flex" }}
exit={{ opacity: 0, y: 4, scale: 0.98, display: "none" }}
transition={{ ease: "easeOut", duration: 0.2 }}
onClick={(e) => e.stopPropagation()}
className={clsx(
"flex absolute right-0 top-12 flex-col rounded-xl origin-top-right cursor-default w-[400px] h-[450px] bg-gray-1",
className
)}
{...props}
>
<NotificationHeader />
<FilterTabs
loading={notifications.isPending}
count={notifications.data?.count}
activeFilter={activeFilter}
setActiveFilter={setActiveFilter}
/>
<div
ref={scrollRef}
className="flex isolate flex-col flex-1 h-full divide-y custom-scroll border-x border-gray-3 divide-gray-3"
>
{notifications.isPending ? (
<NotificationsSkeleton />
) : isNotificationTabEmpty ? (
<div className="flex flex-col gap-3 justify-center items-center h-full">
<FontAwesomeIcon
icon={faBellSlash}
className="text-gray-10 size-10"
/>
<p className="text-gray-10 text-[13px]">
No notifications{" "}
{activeFilter !== "all" && (
<>
for{" "}
<span className="font-medium text-gray-11">
{FilterLabels[activeFilter]}
</span>
</>
)}
</p>
</div>
) : (
filteredNotifications?.map((notification) => (
<NotificationItem
key={notification.id}
notification={notification}
/>
))
)}
<div className="flex-1 border-t border-gray-3" />
</div>
<NotificationFooter />
</motion.div>
);
}
);
export default Notifications;