fix: address final PWA review findings

This commit is contained in:
AgendaPro Dev
2026-07-27 17:19:45 -06:00
parent 43f5d1374c
commit 0b466f33f3
5 changed files with 89 additions and 11 deletions
+33 -3
View File
@@ -7,6 +7,32 @@ interface BeforeInstallPromptEvent extends Event {
userChoice: Promise<{ outcome: "accepted" | "dismissed"; platform: string }>;
}
const DISMISSED_STORAGE_KEY = "agendamax-install-dismissed";
function hasDismissedPrompt() {
try {
return window.sessionStorage.getItem(DISMISSED_STORAGE_KEY) === "1";
} catch {
return false;
}
}
function clearDismissedPrompt() {
try {
window.sessionStorage.removeItem(DISMISSED_STORAGE_KEY);
} catch {
// Storage can be unavailable in privacy-restricted browsers.
}
}
function persistDismissedPrompt() {
try {
window.sessionStorage.setItem(DISMISSED_STORAGE_KEY, "1");
} catch {
// Storage can be unavailable in privacy-restricted browsers.
}
}
function isStandalone() {
return window.matchMedia("(display-mode: standalone)").matches ||
("standalone" in navigator && Boolean((navigator as Navigator & { standalone?: boolean }).standalone));
@@ -25,7 +51,7 @@ export function useInstallPrompt() {
return isIOSSafari() ? "ios-instructions" : "unsupported";
});
const [deferred, setDeferred] = useState<BeforeInstallPromptEvent | null>(null);
const [dismissed, setDismissed] = useState(false);
const [dismissed, setDismissed] = useState(() => typeof window !== "undefined" && hasDismissedPrompt());
const installInFlight = useRef(false);
useEffect(() => {
@@ -37,10 +63,11 @@ export function useInstallPrompt() {
const onBeforeInstallPrompt = (event: Event) => {
event.preventDefault();
setDeferred(event as BeforeInstallPromptEvent);
setState("available");
if (!hasDismissedPrompt()) setState("available");
};
const onInstalled = () => {
setDeferred(null);
clearDismissedPrompt();
setDismissed(false);
setState("installed");
};
@@ -62,7 +89,10 @@ export function useInstallPrompt() {
await event.prompt();
const choice = await event.userChoice;
if (choice.outcome === "accepted") setState("installed");
else setDismissed(true);
else {
persistDismissedPrompt();
setDismissed(true);
}
} catch {
setDeferred(event);
setState("available");