const { notifications, setNotifications } = useNotificationStore();
const [localNotifications, setLocalNotifications] = useState<
Notification[]
>([]);
const { warnings, setWarnings } = useWarningStore();
const [loading, setLoading] = useState(true);
const router = useRouter();
useEffect(() => {
const existingUUIDs = new Set(
notifications?.map((notification) => notification.uuid),
);
localNotifications.forEach(
(notification) =>
(notification.shouldNotify = !existingUUIDs.has(
notification.uuid,
)),
);
setNotifications(localNotifications);
}, [localNotifications]);
useEffect(() => {
if (!loading && user) {
const eventSource = new EventSource(
process.env.NEXT_PUBLIC_BACKEND_URI + "/users/@me/events",
{
withCredentials: true,
},
);
eventSource.onerror = (error) => {
console.error("EventSource failed: ", error);
eventSource.close();
};
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (user) {
setLocalNotifications(data.notifications);
setWarnings(data.warnings);
setUser({
...user,
banned: data.banned,
limited: data.limited,
});
} else eventSource.close();
};
return () => eventSource.close();
}
}, [loading]);
const { notifications, setNotifications } = useNotificationStore();
const [localNotifications, setLocalNotifications] = useState<
Notification[]
>([]);
const { warnings, setWarnings } = useWarningStore();
const [loading, setLoading] = useState(true);
const router = useRouter();
useEffect(() => {
const existingUUIDs = new Set(
notifications?.map((notification) => notification.uuid),
);
localNotifications.forEach(
(notification) =>
(notification.shouldNotify = !existingUUIDs.has(
notification.uuid,
)),
);
setNotifications(localNotifications);
}, [localNotifications]);
useEffect(() => {
if (!loading && user) {
const eventSource = new EventSource(
process.env.NEXT_PUBLIC_BACKEND_URI + "/users/@me/events",
{
withCredentials: true,
},
);
eventSource.onerror = (error) => {
console.error("EventSource failed: ", error);
eventSource.close();
};
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (user) {
setLocalNotifications(data.notifications);
setWarnings(data.warnings);
setUser({
...user,
banned: data.banned,
limited: data.limited,
});
} else eventSource.close();
};
return () => eventSource.close();
}
}, [loading]);