Files
frigate/web/public/notifications-worker.js
T
Josh HawkinsandGitHub caa6edecac Migrate web to ESLint 10 flat config (#24326)
* migrate web to eslint 10 flat config

ESLint 10 dropped `.eslintrc` support, so `.eslintrc.cjs` is replaced with `eslint.config.js` and the lint scripts no longer pass `--ext` or `--ignore-path`. typescript-eslint moves to 8, react-hooks to 7, and react-refresh to 0.5, and the unused jest and vitest-globals plugins are removed. Lint behaves as it did before: catch variables aren't checked, unused disable directives aren't reported, and rules newly added to the recommended sets are off until the code passes them. typescript-eslint 8 flags constants used only in `typeof`, so those are now exported, or replaced with a union type where the export would trip react-refresh.

* fix lint findings from the eslint 10 recommended rules

Remove the rule overrides from the flat config migration and fix what they were hiding. Unused catch bindings are dropped, 20 disable directives that suppressed nothing are removed (react-hooks 5.2 and 7.1.1 report identical exhaustive-deps findings with inline config ignored), dead initial values are dropped, short-circuit calls become if statements or optional calls, rethrown errors pass `cause`, and the disabled "No recordings" tooltip in `ReviewTimeline` is removed along with its memo and the `getRecordingAvailability` prop. The 3 react-refresh warnings for files that export contexts or classes are left for a later refactor.
2026-09-14 07:53:45 -06:00

97 lines
2.8 KiB
JavaScript

// Notifications Worker
self.addEventListener("push", function (event) {
// @ts-expect-error we know this exists
if (event.data) {
// @ts-expect-error we know this exists
const data = event.data.json();
let actions = [];
switch (data.type ?? "unknown") {
case "alert":
actions = [
{
action: "markReviewed",
title: "Mark as Reviewed",
},
];
break;
}
const notificationOptions = {
body: data.message,
icon: "/images/maskable-icon.png",
image: data.image,
badge: "/images/maskable-badge.png",
tag: data.id,
data: { id: data.id, link: data.direct_url },
actions,
};
// iOS Safari does not auto-coalesce notifications by tag (WebKit bug #258922).
// On iOS 18.3+ close() works, so we manually close duplicates before showing.
// On other platforms, tag-based replacement works natively — skip the extra work.
const isIOS =
/iPad|iPhone|iPod/.test(navigator.userAgent) && !self.MSStream;
const show = () =>
// @ts-expect-error we know this exists
self.registration.showNotification(data.title, notificationOptions);
// event.waitUntil is required on iOS Safari — without it, the browser
// may consider this a "silent push" and revoke the subscription after 3 occurrences.
event.waitUntil(
isIOS
? // @ts-expect-error we know this exists
self.registration
.getNotifications({ tag: data.id })
.then((existing) => {
for (const n of existing) {
n.close();
}
})
.then(show)
: show(),
);
} else {
// pass
// This push event has no data
}
});
self.addEventListener("notificationclick", (event) => {
// @ts-expect-error we know this exists
if (event.notification) {
// @ts-expect-error we know this exists
event.notification.close();
switch (event.action ?? "default") {
case "markReviewed":
if (event.notification.data) {
event.waitUntil(
fetch("/api/reviews/viewed", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": 1,
},
body: JSON.stringify({ ids: [event.notification.data.id] }),
}),
);
}
break;
default:
// @ts-expect-error we know this exists
if (event.notification.data) {
const url = event.notification.data.link;
// eslint-disable-next-line no-undef
if (clients.openWindow) {
// eslint-disable-next-line no-undef
event.waitUntil(clients.openWindow(url));
}
}
}
}
});