2024-07-10 20:25:35 +03:00
|
|
|
import { Button } from "@/components/ui/button";
|
2024-07-20 00:41:17 +03:00
|
|
|
import Heading from "@/components/ui/heading";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { Toaster } from "@/components/ui/sonner";
|
|
|
|
|
import { Switch } from "@/components/ui/switch";
|
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
|
import axios from "axios";
|
2024-07-20 18:14:38 +03:00
|
|
|
import { useEffect, useState } from "react";
|
2024-07-20 00:41:17 +03:00
|
|
|
import useSWR from "swr";
|
2024-07-10 20:25:35 +03:00
|
|
|
|
2024-07-20 18:14:38 +03:00
|
|
|
const NOTIFICATION_SERVICE_WORKER = "notifications-worker.ts";
|
|
|
|
|
|
2024-07-10 20:25:35 +03:00
|
|
|
export default function NotificationView() {
|
2024-07-20 00:41:17 +03:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
2024-07-10 20:25:35 +03:00
|
|
|
|
2024-07-20 20:25:44 +03:00
|
|
|
// notification key handling
|
|
|
|
|
|
|
|
|
|
const { data: publicKey } = useSWR(
|
|
|
|
|
config?.notifications?.enabled ? "notifications/pubkey" : null,
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-20 18:14:38 +03:00
|
|
|
// notification state
|
|
|
|
|
|
|
|
|
|
const [notificationsSubscribed, setNotificationsSubscribed] =
|
|
|
|
|
useState<boolean>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
navigator.serviceWorker
|
|
|
|
|
.getRegistration(NOTIFICATION_SERVICE_WORKER)
|
|
|
|
|
.then((worker) => {
|
|
|
|
|
setNotificationsSubscribed(worker != null);
|
|
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-07-10 20:25:35 +03:00
|
|
|
return (
|
2024-07-20 00:41:17 +03:00
|
|
|
<>
|
|
|
|
|
<div className="flex size-full flex-col md:flex-row">
|
|
|
|
|
<Toaster position="top-center" closeButton={true} />
|
|
|
|
|
<div className="scrollbar-container order-last mb-10 mt-2 flex h-full w-full flex-col overflow-y-auto rounded-lg border-[1px] border-secondary-foreground bg-background_alt p-2 md:order-none md:mb-0 md:mr-2 md:mt-0">
|
|
|
|
|
<Heading as="h3" className="my-2">
|
|
|
|
|
Notification Settings
|
|
|
|
|
</Heading>
|
|
|
|
|
|
|
|
|
|
<div className="mt-2 space-y-6">
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
<div className="flex flex-row items-center justify-start gap-2">
|
|
|
|
|
<Switch
|
|
|
|
|
id="auto-live"
|
|
|
|
|
checked={config?.notifications?.enabled}
|
|
|
|
|
onCheckedChange={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
<Label className="cursor-pointer" htmlFor="auto-live">
|
|
|
|
|
Notifications
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="my-2 text-sm text-muted-foreground">
|
|
|
|
|
<p>
|
|
|
|
|
Enable notifications for Frigate alerts. This requires Frigate
|
|
|
|
|
to be externally accessible.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{config?.notifications.enabled && (
|
|
|
|
|
<div className="mt-2 space-y-6">
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
{
|
|
|
|
|
// TODO need to register the worker before enabling the notifications button
|
|
|
|
|
// TODO make the notifications button show enable / disable depending on current state
|
|
|
|
|
}
|
|
|
|
|
<Button
|
2024-07-20 21:34:22 +03:00
|
|
|
disabled={
|
|
|
|
|
notificationsSubscribed == undefined ||
|
|
|
|
|
publicKey == undefined
|
|
|
|
|
}
|
2024-07-20 00:41:17 +03:00
|
|
|
onClick={() => {
|
|
|
|
|
Notification.requestPermission().then((permission) => {
|
|
|
|
|
console.log("notification permissions are ", permission);
|
|
|
|
|
if (permission === "granted") {
|
|
|
|
|
navigator.serviceWorker
|
2024-07-20 18:14:38 +03:00
|
|
|
.register(NOTIFICATION_SERVICE_WORKER)
|
2024-07-20 00:41:17 +03:00
|
|
|
.then((registration) => {
|
|
|
|
|
registration.pushManager
|
2024-07-20 21:34:22 +03:00
|
|
|
.subscribe({
|
|
|
|
|
userVisibleOnly: true,
|
|
|
|
|
applicationServerKey: publicKey,
|
|
|
|
|
})
|
2024-07-20 00:41:17 +03:00
|
|
|
.then((pushSubscription) => {
|
|
|
|
|
console.log(pushSubscription.endpoint);
|
|
|
|
|
axios.post("notifications/register", {
|
|
|
|
|
sub: pushSubscription,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-07-20 18:14:38 +03:00
|
|
|
{`${notificationsSubscribed ? "Disable" : "Enable"} Notifications`}
|
2024-07-20 00:41:17 +03:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2024-07-10 20:25:35 +03:00
|
|
|
);
|
|
|
|
|
}
|