teqo/src/components/campaign/shell/InstallPwaToast.tsx
Francisco Ignácio Fontoura Solla 6a20e98047 fix(campanha): share one Drawer across municipalities mobile cards
Root cause of the prod-build React #130 on /campanha/municipios: the
md:hidden mobile-cards tree still hydrated ~25 rows x ~5 sheet controls
(~125 eager Drawer/DrawerCloseButton instances) even on wide viewports.
Filtered ?q=... routes failed more often only because they exercise the
list with filters active — the search param itself was not the bug.

CampaignListSheetProvider hosts a single bottom Drawer for the whole
mobile section; CampaignCellEditOverlay sheet cells portal their
body/footer into it while keeping per-cell open state and autosave
scaffolds. Footers with DrawerCloseButton are only created once a sheet
actually opens. Desktop popover path is unchanged; per-cell Drawer
remains the fallback for surfaces without the provider.

Also: DrawerCloseButton (function render prop, avoids base-ui element
render in prod bundles) replaces the DrawerClose render={<Button/>}
pattern across call sites; stale e2e selectors refreshed; e2e affected
script + playwright github/list reporters for CI; use-mobile reverted
(no consumers of the gate remain).

Guardrail filed as issue #52.
2026-07-30 17:37:20 -03:00

144 lines
4.4 KiB
TypeScript

'use client'
import { useEffect, useRef, useState } from 'react'
import { toast } from 'sonner'
import {
Drawer,
DrawerCloseButton,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
} from '@/components/ui/Drawer'
import { CAMPAIGN_PWA_INSTALL_TOAST_KEY } from '@/utilities/campaignPwa'
type BeforeInstallPromptEvent = Event & {
prompt: () => Promise<void>
userChoice: Promise<{ outcome: 'accepted' | 'dismissed'; platform: string }>
}
const TOAST_ID = 'campaign-pwa-install'
const isMobileViewport = (): boolean =>
window.matchMedia('(pointer: coarse)').matches || window.matchMedia('(max-width: 768px)').matches
const isStandaloneDisplay = (): boolean => {
const standaloneMedia = window.matchMedia('(display-mode: standalone)').matches
const iosStandalone = Boolean((navigator as Navigator & { standalone?: boolean }).standalone)
return standaloneMedia || iosStandalone
}
const wasDismissedThisSession = (): boolean => {
try {
return sessionStorage.getItem(CAMPAIGN_PWA_INSTALL_TOAST_KEY) === '1'
} catch {
// sessionStorage can throw in private mode.
return false
}
}
const markDismissedThisSession = (): void => {
try {
sessionStorage.setItem(CAMPAIGN_PWA_INSTALL_TOAST_KEY, '1')
} catch {
// Ignore quota / private-mode failures.
}
}
const isIosSafari = (): boolean => {
const ua = navigator.userAgent
const isIos =
/iPad|iPhone|iPod/.test(ua) ||
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)
return isIos && /WebKit/.test(ua) && !/CriOS|FxiOS|EdgiOS/.test(ua)
}
const dismissInstallToast = (): void => {
markDismissedThisSession()
toast.dismiss(TOAST_ID)
}
export const InstallPwaToast = () => {
const deferredPromptRef = useRef<BeforeInstallPromptEvent | null>(null)
const [iosDrawerOpen, setIosDrawerOpen] = useState(false)
useEffect(() => {
const onBeforeInstallPrompt = (event: Event) => {
event.preventDefault()
deferredPromptRef.current = event as BeforeInstallPromptEvent
}
window.addEventListener('beforeinstallprompt', onBeforeInstallPrompt)
return () => window.removeEventListener('beforeinstallprompt', onBeforeInstallPrompt)
}, [])
useEffect(() => {
if (!isMobileViewport() || isStandaloneDisplay() || wasDismissedThisSession()) return
const installAndroid = async () => {
const promptEvent = deferredPromptRef.current
if (!promptEvent) return
try {
await promptEvent.prompt()
const choice = await promptEvent.userChoice
if (choice.outcome === 'accepted') dismissInstallToast()
} catch {
// Native install prompt dismissed or rejected.
}
}
// Wait briefly so `beforeinstallprompt` can arrive after SW registration.
const timer = window.setTimeout(() => {
if (isStandaloneDisplay() || wasDismissedThisSession()) return
const hasPrompt = deferredPromptRef.current != null
if (!hasPrompt && !isIosSafari()) return
toast.message('Instale o app da campanha', {
id: TOAST_ID,
description: 'Acesse mais rápido e receba avisos',
duration: Infinity,
closeButton: true,
onDismiss: markDismissedThisSession,
action: {
label: hasPrompt ? 'Instalar' : 'Como instalar',
onClick: () => {
if (hasPrompt) {
void installAndroid()
return
}
setIosDrawerOpen(true)
},
},
})
}, 1200)
return () => {
window.clearTimeout(timer)
toast.dismiss(TOAST_ID)
}
}, [])
return iosDrawerOpen ? (
<Drawer open={iosDrawerOpen} onOpenChange={setIosDrawerOpen}>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Como instalar no iPhone</DrawerTitle>
<DrawerDescription>
No Safari, use o menu de compartilhamento para adicionar o app à Tela de Início.
</DrawerDescription>
</DrawerHeader>
<ol className="list-decimal space-y-2 px-4 pb-2 text-left text-sm text-foreground">
<li>Toque no botão Compartilhar (quadrado com seta para cima).</li>
<li>Role e toque em &quot;Adicionar à Tela de Início&quot;.</li>
<li>Confirme tocando em Adicionar.</li>
</ol>
<DrawerFooter>
<DrawerCloseButton onClick={dismissInstallToast}>Entendi</DrawerCloseButton>
</DrawerFooter>
</DrawerContent>
</Drawer>
) : null
}