Files
famapp/src/components/push-opt-in.tsx
T
ginnoirandClaude Sonnet 4.6 1185f5ab50 Production-ready wiring: VAPID prop fix, port mappings, AUTH_URL
- PushOptIn now accepts vapidKey as a prop from its server-component
  parent (settings page reads VAPID_PUBLIC_KEY at runtime) — eliminates
  the NEXT_PUBLIC_* build-time dependency so pre-built GHCR images work
  without a build arg.
- deploy/compose.yaml: famapp exposes 3010:3000, authentik-server exposes
  9200:9000 so the existing Caddy stack can proxy by IP, matching every
  other service in the homelab.  NEXT_PUBLIC_APP_URL replaced by AUTH_URL
  (correct next-auth v5 var).
- deploy/Caddyfile.snippet: updated to 192.168.1.69:3010 / :9200.
- .env.production.example: AUTH_URL, ntfy pre-wired to ntfy.ginnoir.com,
  VAPID_SUBJECT prefilled with real email.
- typecheck and pnpm build both pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-06 18:32:08 -05:00

103 lines
3.1 KiB
TypeScript

"use client";
import { useState, useTransition } from "react";
import { Button } from "@/components/ui/button";
import {
subscribeToPush,
unsubscribeFromPush,
sendTestNotification,
} from "@/app/settings/push-actions";
function urlBase64ToUint8Array(base64String: string): Uint8Array<ArrayBuffer> {
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
const raw = atob(base64);
const buf = new Uint8Array(raw.length);
for (let i = 0; i < raw.length; i++) buf[i] = raw.charCodeAt(i);
return buf;
}
export function PushOptIn({ vapidKey }: { vapidKey: string }) {
const [status, setStatus] = useState<"idle" | "subscribed" | "denied" | "unsupported">("idle");
const [endpoint, setEndpoint] = useState<string | null>(null);
const [isPending, startTransition] = useTransition();
const [testSent, setTestSent] = useState(false);
if (!vapidKey) return null;
if (!("serviceWorker" in navigator) || !("PushManager" in window)) {
return (
<p className="text-sm text-muted-foreground">
Push notifications not supported in this browser.
</p>
);
}
async function subscribe() {
try {
const registration = await navigator.serviceWorker.ready;
const sub = await registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(vapidKey),
});
const json = sub.toJSON() as { endpoint: string; keys: { p256dh: string; auth: string } };
startTransition(async () => {
await subscribeToPush(json, navigator.userAgent);
setEndpoint(json.endpoint);
setStatus("subscribed");
});
} catch {
setStatus("denied");
}
}
async function unsubscribe() {
if (!endpoint) return;
const registration = await navigator.serviceWorker.ready;
const sub = await registration.pushManager.getSubscription();
if (sub) await sub.unsubscribe();
startTransition(async () => {
await unsubscribeFromPush(endpoint);
setEndpoint(null);
setStatus("idle");
});
}
function sendTest() {
startTransition(async () => {
await sendTestNotification();
setTestSent(true);
setTimeout(() => setTestSent(false), 3000);
});
}
if (status === "denied") {
return (
<p className="text-sm text-destructive">
Notification permission denied. Enable it in browser settings.
</p>
);
}
if (status === "subscribed") {
return (
<div className="flex items-center gap-3">
<span className="text-sm text-green-600 dark:text-green-400">
Push notifications enabled
</span>
<Button size="sm" variant="outline" onClick={sendTest} disabled={isPending}>
{testSent ? "Sent!" : "Send test"}
</Button>
<Button size="sm" variant="destructive" onClick={unsubscribe} disabled={isPending}>
Disable
</Button>
</div>
);
}
return (
<Button size="sm" onClick={subscribe} disabled={isPending}>
Enable push notifications
</Button>
);
}