feat: build production-ready wtr weather PWA
This commit is contained in:
37
components/ui/install-pwa-button.tsx
Normal file
37
components/ui/install-pwa-button.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Download } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface BeforeInstallPromptEvent extends Event {
|
||||
prompt: () => Promise<void>;
|
||||
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
|
||||
}
|
||||
|
||||
export function InstallPWAButton() {
|
||||
const [event, setEvent] = useState<BeforeInstallPromptEvent | null>(null);
|
||||
useEffect(() => {
|
||||
const handlePrompt = (promptEvent: Event) => {
|
||||
promptEvent.preventDefault();
|
||||
setEvent(promptEvent as BeforeInstallPromptEvent);
|
||||
};
|
||||
window.addEventListener("beforeinstallprompt", handlePrompt);
|
||||
return () => window.removeEventListener("beforeinstallprompt", handlePrompt);
|
||||
}, []);
|
||||
|
||||
if (!event) return null;
|
||||
return (
|
||||
<Button
|
||||
variant="glass"
|
||||
onClick={async () => {
|
||||
await event.prompt();
|
||||
await event.userChoice;
|
||||
setEvent(null);
|
||||
}}
|
||||
>
|
||||
<Download className="size-4" />
|
||||
Zainstaluj
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user