diff --git a/app/components/settings/install-app.tsx b/app/components/settings/install-app.tsx index 3143d6c..927b9ba 100644 --- a/app/components/settings/install-app.tsx +++ b/app/components/settings/install-app.tsx @@ -31,7 +31,7 @@ export default function InstallApp() { }; window.addEventListener("beforeinstallprompt", handler); - return () => window.removeEventListener("transitionend", handler); + return () => window.removeEventListener("beforeinstallprompt", handler); }, []); const onClick = (evt: React.MouseEvent) => { @@ -64,11 +64,7 @@ export default function InstallApp() { {/* Install PWA button */} - @@ -118,3 +114,10 @@ export default function InstallApp() { ); } + +export function isInstalled() { + const UA = navigator.userAgent; + const IOS = UA.match(/iPhone|iPad|iPod/); + const standalone = window.matchMedia("(display-mode: standalone)").matches; + return !!(standalone || (IOS && !UA.match(/Safari/))); +} diff --git a/app/components/settings/notifications.tsx b/app/components/settings/notifications.tsx index e7e84b3..2fdc3c4 100644 --- a/app/components/settings/notifications.tsx +++ b/app/components/settings/notifications.tsx @@ -17,13 +17,47 @@ import { Trash2, Send, AlertTriangle, - Info, + Loader, } from "lucide-react"; -import InstallApp from "./install-app"; +import InstallApp, { isInstalled } from "./install-app"; +import { + isNotificationEnabled, + registerNotification, + unregisterNotification, +} from "~/lib/notification"; export default function NotificationSettings() { + const [pushEnabled, setPushEnabled] = useState(isNotificationEnabled()); + const [isRegistering, setIsRegistering] = useState(false); + + function togglePushEnabled() { + setIsRegistering(true); + if (pushEnabled) { + // Unregister notifications + unregisterNotification() + .then(() => { + setPushEnabled(true); + }) + .finally(() => { + setIsRegistering(false); + }); + } else { + // Register notifications + registerNotification() + .then((result) => { + if ("error" in result) { + console.error("Failed to register notifications:", result.error); + } else { + setPushEnabled(false); + } + }) + .finally(() => { + setIsRegistering(false); + }); + } + } + // TODO: Replace with actual user data - const [pushEnabled, setPushEnabled] = useState(false); const [events, setEvents] = useState([ { id: "new-message", label: "New Message", enabled: true }, { id: "comment-reply", label: "Comment Reply", enabled: true }, @@ -87,24 +121,48 @@ export default function NotificationSettings() { - {/*
-
-

- Notifications -

-

- Recevez des notifications même lorsque l’application est fermée. -

-
- -
*/} + {isInstalled() ? ( + pushEnabled ? ( +
+
+

+ Notifications +

+

+ Recevez des notifications même lorsque l’application est + fermée. +

+
+ +
+ ) : ( + + ) + ) : ( - + )}
diff --git a/app/components/settings/profile.tsx b/app/components/settings/profile.tsx index 1689767..5e1f096 100644 --- a/app/components/settings/profile.tsx +++ b/app/components/settings/profile.tsx @@ -76,7 +76,7 @@ export default function Profile({ user }: { user: User }) {