diff --git a/atproto-notifications/package.json b/atproto-notifications/package.json index bc67cf8..653bcb1 100644 --- a/atproto-notifications/package.json +++ b/atproto-notifications/package.json @@ -4,9 +4,9 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite --host 127.0.0.1", + "dev": "VITE_NOTIFICATIONS_HOST=http://127.0.0.1:8000 vite --host 127.0.0.1", "build": "tsc -b && vite build", - "just-build": "VITE_PUSH_PUBKEY=BDZZicf6KaHoH6EI9OETLO3G9M4e6mKQDRJDcj_XUUxvdsnz08ne-URkk_ToqpwWgrRqXIBd0LJ_w8bP-R5xMKA vite build", + "just-build": "vite build", "lint": "eslint .", "preview": "vite preview" }, diff --git a/atproto-notifications/src/App.tsx b/atproto-notifications/src/App.tsx index 73f5fef..24f6daf 100644 --- a/atproto-notifications/src/App.tsx +++ b/atproto-notifications/src/App.tsx @@ -1,6 +1,6 @@ -import { useCallback, useState } from 'react'; +import { useCallback, useState, useEffect } from 'react'; import { useLocalStorage } from "@uidotdev/usehooks"; -import { HostContext } from './context' +import { GetJson } from './components/fetch'; import { WhoAmI } from './components/WhoAmI'; import { Feed } from './components/Feed'; import { urlBase64ToUint8Array } from './utils'; @@ -14,14 +14,14 @@ const Problem = ({ children }) => ( ); -function requestPermission(host, setAsking, setPermissionError) { +function requestPermission(pushServerHost, pushServerPubkey, setAsking, setPermissionError) { return async () => { setAsking(true); let err; try { await Notification.requestPermission(); - const sub = await subscribeToPush(); - const res = await fetch(`${host}/subscribe`, { + const sub = await subscribeToPush(pushServerPubkey); + const res = await fetch(`${pushServerHost}/subscribe`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ sub }), @@ -46,20 +46,17 @@ function requestPermission(host, setAsking, setPermissionError) { } let autoreg; -async function subscribeToPush() { +async function subscribeToPush(pushServerPubkey) { const registration = await navigator.serviceWorker.register('/service-worker.js'); - // auto-update in case they keep it open in a tab for a long time + // auto-update the service worker in case they keep it open in a tab for a long time clearInterval(autoreg); autoreg = setInterval(() => registration.update(), 4 * 60 * 60 * 1000); // every 4h - const subscribeOptions = { + return await registration.pushManager.subscribe({ + pushServerPubkey: urlBase64ToUint8Array(pushServerPubkey), userVisibleOnly: true, - applicationServerKey: urlBase64ToUint8Array(import.meta.env.VITE_PUSH_PUBKEY), - }; - const pushSubscription = await registration.pushManager.subscribe(subscribeOptions); - console.log({ pushSubscription }); - return pushSubscription; + }); } async function verifyUser(host, token) { @@ -73,7 +70,11 @@ async function verifyUser(host, token) { } function App() { - const [host, setHost] = useLocalStorage('spacedust-notif-host', 'http://localhost:8000'); + const host = import.meta.env.VITE_NOTIFICATIONS_HOST; + const [pushPubkey, setPushPubkey] = useState(null); + const [whoamiHost, setWhoamiHost] = useState(null); + + const [role, setRole] = useLocalStorage('spacedust-notif-role', 'anonymous'); const [user, setUser] = useLocalStorage('spacedust-notif-user', null); const [verif, setVerif] = useState(null); const [asking, setAsking] = useState(false); @@ -99,15 +100,26 @@ function App() { let hasPush = 'PushManager' in window; let notifPerm = Notification?.permission ?? 'default'; + function Blah({ info }) { + useEffect(() => { + setPushPubkey(info.webPushPublicKey); + setWhoamiHost(info.whoamiHost); + setRole(info.role); + }); + return <>Got server hello, updating app state…; + } + let content; if (!hasSW) { content = your browser does not support the background task needd to deliver notifications; } else if (!hasPush) { content = your browser does not support registering push notifications. + } else if (!whoamiHost) { + content = } /> } else if (!user) { if (verif === 'verifying') content =

verifying…

; else { - content = ; + content = ; if (verif === 'failed') { content = <>

Sorry, failed to verify that identity. please let us know!

{content}; } @@ -119,7 +131,7 @@ function App() {

To show notifications we need permission: