feat: make the web app installable and rework the app shell
Add a Vite PWA (`vite-plugin-pwa`): manifest, icons and an `autoUpdate` service worker, plus an install prompt so the app can be added to the home screen. Rework the shell for mobile: mount the bottom navigation bar and the install prompt in the authenticated layout, hide the sidebar trigger below `md`, and add a sign-out card to Settings. Drop the placeholder Tasks page together with its unused shadcn table and the `@tanstack/react-table` dependency, and remove the in-app router/query dev tools.
This commit is contained in:
111
apps/web/src/hooks/use-install-prompt.ts
Normal file
111
apps/web/src/hooks/use-install-prompt.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import * as React from 'react'
|
||||
|
||||
/**
|
||||
* `beforeinstallprompt` is still non-standard, so TypeScript has no DOM type
|
||||
* for it. This is the subset of the event we use.
|
||||
*/
|
||||
interface BeforeInstallPromptEvent extends Event {
|
||||
readonly platforms: string[]
|
||||
readonly userChoice: Promise<{ outcome: 'accepted' | 'dismissed'; platform: string }>
|
||||
prompt(): Promise<void>
|
||||
}
|
||||
|
||||
const DISMISSED_KEY = 'superlist-install-dismissed'
|
||||
|
||||
function readDismissed() {
|
||||
try {
|
||||
return localStorage.getItem(DISMISSED_KEY) === '1'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function isStandalone() {
|
||||
if (typeof window === 'undefined') return false
|
||||
const navigatorWithStandalone = navigator as Navigator & { standalone?: boolean }
|
||||
return (
|
||||
window.matchMedia('(display-mode: standalone)').matches ||
|
||||
navigatorWithStandalone.standalone === true
|
||||
)
|
||||
}
|
||||
|
||||
function isIosSafari() {
|
||||
if (typeof navigator === 'undefined') return false
|
||||
const ua = navigator.userAgent
|
||||
const isIos = /iphone|ipad|ipod/i.test(ua)
|
||||
// On iOS only Safari can add apps to the home screen, and it never fires
|
||||
// `beforeinstallprompt`, so we detect it to show manual instructions.
|
||||
const isSafari = /safari/i.test(ua) && !/crios|fxios|edgios/i.test(ua)
|
||||
return isIos && isSafari
|
||||
}
|
||||
|
||||
export type InstallPromptState = {
|
||||
/** We can trigger the native install dialog. */
|
||||
canInstall: boolean
|
||||
/** iOS Safari: no native prompt, show "Compartir → Añadir a pantalla de inicio". */
|
||||
needsManualInstructions: boolean
|
||||
/** The app is already running as an installed PWA. */
|
||||
isInstalled: boolean
|
||||
/** The user dismissed the banner before (persisted). */
|
||||
isDismissed: boolean
|
||||
install: () => Promise<void>
|
||||
dismiss: () => void
|
||||
/** Show the banner: installable or needing instructions, and not dismissed/installed. */
|
||||
visible: boolean
|
||||
}
|
||||
|
||||
export function useInstallPrompt(): InstallPromptState {
|
||||
const [deferredPrompt, setDeferredPrompt] = React.useState<BeforeInstallPromptEvent | null>(null)
|
||||
const [isInstalled, setIsInstalled] = React.useState(isStandalone)
|
||||
const [isDismissed, setIsDismissed] = React.useState(readDismissed)
|
||||
const [isIos] = React.useState(isIosSafari)
|
||||
|
||||
React.useEffect(() => {
|
||||
const onBeforeInstallPrompt = (event: Event) => {
|
||||
// Stop Chrome from showing its own mini-infobar so we control the banner.
|
||||
event.preventDefault()
|
||||
setDeferredPrompt(event as BeforeInstallPromptEvent)
|
||||
}
|
||||
|
||||
const onInstalled = () => {
|
||||
setDeferredPrompt(null)
|
||||
setIsInstalled(true)
|
||||
}
|
||||
|
||||
window.addEventListener('beforeinstallprompt', onBeforeInstallPrompt)
|
||||
window.addEventListener('appinstalled', onInstalled)
|
||||
return () => {
|
||||
window.removeEventListener('beforeinstallprompt', onBeforeInstallPrompt)
|
||||
window.removeEventListener('appinstalled', onInstalled)
|
||||
}
|
||||
}, [])
|
||||
|
||||
const install = React.useCallback(async () => {
|
||||
if (!deferredPrompt) return
|
||||
await deferredPrompt.prompt()
|
||||
const { outcome } = await deferredPrompt.userChoice
|
||||
if (outcome === 'accepted') setDeferredPrompt(null)
|
||||
}, [deferredPrompt])
|
||||
|
||||
const dismiss = React.useCallback(() => {
|
||||
setIsDismissed(true)
|
||||
try {
|
||||
localStorage.setItem(DISMISSED_KEY, '1')
|
||||
} catch {
|
||||
// Storage can be unavailable (private mode); the banner just comes back.
|
||||
}
|
||||
}, [])
|
||||
|
||||
const canInstall = deferredPrompt !== null
|
||||
const needsManualInstructions = isIos && !deferredPrompt && !isInstalled
|
||||
|
||||
return {
|
||||
canInstall,
|
||||
needsManualInstructions,
|
||||
isInstalled,
|
||||
isDismissed,
|
||||
install,
|
||||
dismiss,
|
||||
visible: !isInstalled && !isDismissed && (canInstall || needsManualInstructions),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user