Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | 'use client' import {useEffect, useState} from 'react' import { canOfferInstall, currentSubscription, install, isInstalled, onInstallOffer, platform, pushSupported, turnOff, turnOn, type Platform, } from './pwa' type Kind = | 'notifyCharts' | 'notifySetlists' | 'notifyRehearsals' | 'notifyProposals' const KINDS: {key: Kind; title: string; hint: string}[] = [ { key: 'notifyCharts', title: 'Songs and charts', hint: 'A chart edited, a song added', }, { key: 'notifySetlists', title: 'Setlists', hint: 'A gig’s set changed or added', }, { key: 'notifyRehearsals', title: 'Rehearsals', hint: 'One scheduled or cancelled', }, { key: 'notifyProposals', title: 'Proposals', hint: 'A song proposed, or voted in', }, ] /** * The app on this device: installing it to the home screen, and * notifications. On an iPhone or iPad, notifications only work in the * installed app, so outside it this explains how to install instead of * offering a switch that can't work. */ export default function AppSettings({ initial, devices, ready, }: { initial: Record<Kind, boolean> /** The devices this person has turned notifications on for */ devices: string[] /** The server can send notifications (its keys are set) */ ready: boolean }) { const [mounted, setMounted] = useState(false) const [plat, setPlat] = useState<Platform>('desktop') const [installed, setInstalled] = useState(false) const [offer, setOffer] = useState(false) const [supported, setSupported] = useState(false) const [on, setOn] = useState(false) const [busy, setBusy] = useState(false) const [note, setNote] = useState<string | null>(null) const [kinds, setKinds] = useState(initial) useEffect(() => { setPlat(platform()) setInstalled(isInstalled()) setSupported(pushSupported()) setOffer(canOfferInstall()) currentSubscription().then((s) => setOn(Boolean(s))) setMounted(true) return onInstallOffer(() => { setOffer(canOfferInstall()) setInstalled(isInstalled()) }) }, []) async function toggle() { setBusy(true) setNote(null) if (on) { await turnOff() setOn(false) } else { const r = await turnOn() if (r === 'on') setOn(true) else if (r === 'denied') setNote( plat === 'ios' ? 'Notifications are blocked. Turn them on in Settings → Notifications → this app, then try again.' : 'Notifications are blocked for this site. Allow them in the browser’s site settings (the icon by the address), then try again.', ) else setNote('This device can’t do notifications.') } setBusy(false) } async function test() { setNote(null) const r = await fetch('/api/push/test', {method: 'POST'}) const {sent} = await r.json() setNote( sent ? 'Sent — it should arrive in a moment.' : 'Nothing was sent: no device has notifications on.', ) } async function setKind(key: Kind) { const next = {...kinds, [key]: !kinds[key]} setKinds(next) const r = await fetch('/api/account/settings', { method: 'PATCH', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({[key]: next[key]}), }) if (!r.ok) setKinds(kinds) } if (!mounted) return null const needsInstall = plat === 'ios' && !installed return ( <section id="notifications" aria-labelledby="app-h" className="mt-8 scroll-mt-24" > <h2 id="app-h" className="text-xs font-bold uppercase tracking-widest text-muted" > The app on this device </h2> {/* Install */} {!installed && ( <div className="mt-2 border-t border-line py-3"> <p className="font-semibold">Add it to your home screen</p> <p className="text-sm text-muted"> It opens full screen like an app, works without signal for anything you’ve opened, and can send notifications. </p> {offer && plat !== 'ios' ? ( <button type="button" onClick={async () => setInstalled(await install())} className="mt-2 min-h-11 rounded-lg bg-accent px-4 font-bold text-on-accent" > Install app </button> ) : ( <InstallSteps plat={plat} /> )} </div> )} {/* Notifications */} <div className="border-t border-line py-3"> {needsInstall ? ( <> <p className="font-semibold">Notifications</p> <p className="text-sm text-muted"> On an iPhone or iPad, notifications come through the app on your home screen. Add it there (above), open it from the home screen, and turn them on here. </p> </> ) : !supported || !ready ? ( <> <p className="font-semibold">Notifications</p> <p className="text-sm text-muted"> {!ready ? 'Notifications aren’t set up on this server yet.' : 'This browser can’t show notifications. Try Chrome, Edge, Firefox or Safari.'} </p> </> ) : ( <> <label className="flex items-start gap-3"> <input type="checkbox" checked={on} disabled={busy} onChange={toggle} className="mt-1 h-5 w-5 shrink-0" /> <span> <span className="block font-semibold"> Notifications on this device </span> <span className="block text-sm text-muted"> {on ? 'You’ll hear when someone else changes something — never about your own changes. A burst of edits comes as one.' : 'Get told when someone else changes a chart, a setlist, a rehearsal or a proposal.'} </span> </span> </label> {on && ( <button type="button" onClick={test} className="ml-8 mt-2 min-h-9 rounded-lg border border-line-2 px-3 text-sm" > Send a test </button> )} </> )} {note && ( <p role="status" className="mt-2 text-sm text-amber"> {note} </p> )} </div> {/* What to hear about: per person, every device */} {(on || (devices.length > 0 && !needsInstall)) && ( <div className="border-t border-line py-3"> <p className="font-semibold">Tell me about</p> <ul className="mt-1"> {KINDS.map((k) => ( <li key={k.key}> <label className="flex min-h-11 items-center gap-3"> <input type="checkbox" checked={kinds[k.key]} onChange={() => setKind(k.key)} className="h-5 w-5 shrink-0" /> <span> {k.title}{' '} <span className="text-sm text-muted">· {k.hint}</span> </span> </label> </li> ))} </ul> {devices.length > 0 && ( <p className="mt-1 text-sm text-muted"> On for: {devices.join(', ')} </p> )} </div> )} </section> ) } /** How to add it to the home screen, for the browser in hand. */ export function InstallSteps({plat}: {plat: Platform}) { if (plat === 'ios') return ( <ol className="mt-2 list-decimal pl-5 text-sm"> <li> In Safari, tap <b>Share</b>{' '} <span aria-hidden>(the square with an arrow ↑)</span>. </li> <li> Choose <b>Add to Home Screen</b>, then <b>Add</b>. </li> <li>Open it from your home screen from now on.</li> </ol> ) if (plat === 'android') return ( <ol className="mt-2 list-decimal pl-5 text-sm"> <li> In Chrome, tap the <b>⋮</b> menu. </li> <li> Choose <b>Install app</b> (or <b>Add to home screen</b>). </li> </ol> ) return ( <p className="mt-2 text-sm"> In Chrome or Edge, use the <b>install</b> icon at the right of the address bar (or the menu → <b>Install</b>). Safari on a Mac: File →{' '} <b>Add to Dock</b>. </p> ) } |