frigate/web/src/views/settings/NotificationsSettingsView.tsx

98 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-07-10 20:25:35 +03:00
import { Button } from "@/components/ui/button";
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";
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() {
const { data: config } = useSWR<FrigateConfig>("config");
2024-07-10 20:25:35 +03:00
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 (
<>
<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 18:14:38 +03:00
disabled={notificationsSubscribed == undefined}
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)
.then((registration) => {
registration.pushManager
.subscribe()
.then((pushSubscription) => {
console.log(pushSubscription.endpoint);
axios.post("notifications/register", {
sub: pushSubscription,
});
});
});
}
});
}}
>
2024-07-20 18:14:38 +03:00
{`${notificationsSubscribed ? "Disable" : "Enable"} Notifications`}
</Button>
</div>
</div>
)}
</div>
</div>
</>
2024-07-10 20:25:35 +03:00
);
}