- Extract snake draft order calculation to shared getTeamForPick() helper in lib/draft-order.ts, removing the duplicated logic - Fix notification firing on user's own picks (guard with team id check) - Fix notification firing after draft is complete (early return) - Use a ref for sendNotification to prevent full socket handler re-registration every time the toggle is changed - Add permission drift listener via Permissions API so the UI reacts if the user revokes notification permission in browser settings - Add SSR guard for document.hidden in sendNotification - Remove redundant isSupported prop from NotificationSettings; derive unsupported state from permissionState === "unsupported" - Move NotificationSettings out of QueueSection prop-drilling path; render it via a new settingsSection slot on DraftSidebar instead https://claude.ai/code/session_0149MvVUYDY6pFUAV1fL4K69
109 lines
3 KiB
TypeScript
109 lines
3 KiB
TypeScript
import { useState, useEffect, useCallback } from "react";
|
|
|
|
function getStorageKey(seasonId: string) {
|
|
return `draftNotifications-${seasonId}`;
|
|
}
|
|
|
|
export function useDraftNotifications(seasonId: string) {
|
|
const [isSupported, setIsSupported] = useState(false);
|
|
const [permissionState, setPermissionState] = useState<
|
|
NotificationPermission | "unsupported"
|
|
>("unsupported");
|
|
const [enabled, setEnabledState] = useState(false);
|
|
|
|
// Check browser support, restore persisted preference, and watch for permission changes
|
|
useEffect(() => {
|
|
if (typeof window === "undefined" || !("Notification" in window)) {
|
|
return;
|
|
}
|
|
|
|
setIsSupported(true);
|
|
setPermissionState(Notification.permission);
|
|
|
|
// Restore preference from localStorage only if permission is granted
|
|
if (Notification.permission === "granted") {
|
|
const stored = localStorage.getItem(getStorageKey(seasonId));
|
|
if (stored === "true") {
|
|
setEnabledState(true);
|
|
}
|
|
}
|
|
|
|
// Watch for the user revoking/granting permission in browser settings
|
|
let permissionStatus: PermissionStatus | null = null;
|
|
navigator.permissions
|
|
.query({ name: "notifications" })
|
|
.then((status) => {
|
|
permissionStatus = status;
|
|
status.onchange = () => {
|
|
setPermissionState(status.state as NotificationPermission);
|
|
// If permission was revoked, disable notifications
|
|
if (status.state !== "granted") {
|
|
setEnabledState(false);
|
|
}
|
|
};
|
|
})
|
|
.catch(() => {
|
|
// Permissions API not available in all environments; silently ignore
|
|
});
|
|
|
|
return () => {
|
|
if (permissionStatus) {
|
|
permissionStatus.onchange = null;
|
|
}
|
|
};
|
|
}, [seasonId]);
|
|
|
|
const setEnabled = useCallback(
|
|
async (value: boolean) => {
|
|
if (!isSupported) return;
|
|
|
|
if (value) {
|
|
// Request permission if not yet granted
|
|
if (Notification.permission === "default") {
|
|
const result = await Notification.requestPermission();
|
|
setPermissionState(result);
|
|
if (result !== "granted") {
|
|
return;
|
|
}
|
|
} else if (Notification.permission === "denied") {
|
|
return;
|
|
}
|
|
|
|
setEnabledState(true);
|
|
localStorage.setItem(getStorageKey(seasonId), "true");
|
|
} else {
|
|
setEnabledState(false);
|
|
localStorage.setItem(getStorageKey(seasonId), "false");
|
|
}
|
|
},
|
|
[isSupported, seasonId]
|
|
);
|
|
|
|
const sendNotification = useCallback(
|
|
(title: string, body: string) => {
|
|
if (
|
|
!enabled ||
|
|
!isSupported ||
|
|
Notification.permission !== "granted" ||
|
|
typeof document === "undefined" ||
|
|
!document.hidden
|
|
) {
|
|
return;
|
|
}
|
|
|
|
new Notification(title, {
|
|
body,
|
|
tag: `draft-${seasonId}`,
|
|
});
|
|
},
|
|
[enabled, isSupported, seasonId]
|
|
);
|
|
|
|
return {
|
|
isSupported,
|
|
permissionState,
|
|
enabled,
|
|
setEnabled,
|
|
sendNotification,
|
|
};
|
|
}
|