- FAB y aviso PWA flanquean la columna central (720px): right/left max(16px, calc((100vw - 720px)/2 + 16px)) para acompañar al contenido en pantallas anchas sin pegarse al borde de la ventana - FAB de 64px en PC (54 en móvil); PWA abajo a la izquierda en PC y móvil - Nuevo BotonProponerArriba (solo PC): píldora "Proponer local" arriba a la derecha, alineada al cuerpo, que abre la misma pasarela
126 lines
5.5 KiB
React
126 lines
5.5 KiB
React
import { useEffect, useState } from "react";
|
|
import usePwaInstall from "../hooks/usePwaInstall.js";
|
|
import useEsMovil from "../hooks/useEsMovil.js";
|
|
import { ANCHO_CUERPO } from "../styles/shared.js";
|
|
|
|
const CLAVE_VISITAS = "localesp_visitas";
|
|
const CLAVE_CERRADO = "localesp_banner_cerrado_hasta";
|
|
const VISITAS_MINIMAS = 2;
|
|
const DIAS_OCULTO_TRAS_CERRAR = 14;
|
|
|
|
function registrarVisitaYContar() {
|
|
try {
|
|
const actual = parseInt(localStorage.getItem(CLAVE_VISITAS) || "0", 10) + 1;
|
|
localStorage.setItem(CLAVE_VISITAS, String(actual));
|
|
return actual;
|
|
} catch {
|
|
return VISITAS_MINIMAS; // si localStorage falla, no bloqueamos el banner
|
|
}
|
|
}
|
|
|
|
function estaCerradoRecientemente() {
|
|
try {
|
|
const hasta = parseInt(localStorage.getItem(CLAVE_CERRADO) || "0", 10);
|
|
return Date.now() < hasta;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Aviso flotante para instalar/abrir la PWA. Usa el service worker +
|
|
// manifest.webmanifest ya registrados en main.jsx/index.html: si el
|
|
// navegador soporta la instalación nativa (evento beforeinstallprompt),
|
|
// el botón la dispara directamente. Si la app ya se está ejecutando en
|
|
// modo standalone (o el navegador confirma que está instalada), el aviso
|
|
// se oculta porque ya no aporta nada mostrarlo.
|
|
//
|
|
// Colocación: abajo a la IZQUIERDA en PC y móvil, alineada con el borde
|
|
// izquierdo del cuerpo central (columna de ANCHO_CUERPO px), flanqueando el
|
|
// contenido junto al FAB de "proponer local" (abajo a la derecha): en
|
|
// pantallas anchas acompaña a la columna en vez de pegarse al borde de la
|
|
// ventana; en estrechas queda a 16px. Respeta la barra inferior de la PWA
|
|
// (env(safe-area-inset-bottom)) y en PC se muestra algo más grande.
|
|
//
|
|
// Para no ser intrusivo, solo se muestra a partir de la 2ª visita del
|
|
// usuario (contador en localStorage) y, si se cierra con ✕, no vuelve a
|
|
// aparecer durante 14 días.
|
|
export default function BannerMovil() {
|
|
const [cerrado, setCerrado] = useState(estaCerradoRecientemente);
|
|
const [visitasSuficientes, setVisitasSuficientes] = useState(false);
|
|
const { isInstalled, canPromptInstall, promptInstall, isIos } = usePwaInstall();
|
|
const [instalando, setInstalando] = useState(false);
|
|
const [resultado, setResultado] = useState(null); // "accepted" | "dismissed" | null
|
|
const esMovil = useEsMovil();
|
|
|
|
useEffect(() => {
|
|
setVisitasSuficientes(registrarVisitaYContar() >= VISITAS_MINIMAS);
|
|
}, []);
|
|
|
|
if (cerrado || isInstalled || !visitasSuficientes) return null;
|
|
|
|
const handleCerrar = () => {
|
|
try {
|
|
localStorage.setItem(CLAVE_CERRADO, String(Date.now() + DIAS_OCULTO_TRAS_CERRAR * 24 * 60 * 60 * 1000));
|
|
} catch { /* localStorage no disponible: se cierra solo para esta sesión */ }
|
|
setCerrado(true);
|
|
};
|
|
|
|
const handleInstalar = async () => {
|
|
setInstalando(true);
|
|
const outcome = await promptInstall();
|
|
setResultado(outcome);
|
|
setInstalando(false);
|
|
};
|
|
|
|
const tarjeta = (onCerrarTarjeta, estiloExtra = {}) => (
|
|
<div style={{ position: "relative", background: "white", border: "1px solid #E5DDD5", borderRadius: 14, padding: "14px 18px", boxShadow: "0 6px 24px rgba(0,0,0,0.10)", width: 280, boxSizing: "border-box", fontFamily: "var(--font-sans)", ...estiloExtra }}>
|
|
<button onClick={onCerrarTarjeta} aria-label="Cerrar aviso de instalación"
|
|
style={{ position: "absolute", top: 8, right: 10, background: "none", border: "none", cursor: "pointer", color: "#CCC", fontSize: 16, padding: 0 }}>✕</button>
|
|
|
|
<div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 10 }}>
|
|
<img src="/icon-192.png" alt="" width={30} height={30} style={{ borderRadius: 7, flexShrink: 0 }} />
|
|
<div>
|
|
<p style={{ margin: 0, fontWeight: 700, fontSize: 14 }}>App para móvil</p>
|
|
<p style={{ margin: 0, fontSize: 12, color: "#888" }}>Sincronizada en tiempo real</p>
|
|
</div>
|
|
</div>
|
|
|
|
{canPromptInstall && (
|
|
<button onClick={handleInstalar} disabled={instalando}
|
|
style={{ display: "block", width: "100%", textAlign: "center", padding: "9px 0", background: "#8B0000", color: "white", border: "none", borderRadius: 8, fontSize: 13, fontWeight: 700, cursor: instalando ? "default" : "pointer", opacity: instalando ? 0.7 : 1 }}>
|
|
{instalando ? "Abriendo instalación..." : "📲 Instalar app"}
|
|
</button>
|
|
)}
|
|
|
|
{!canPromptInstall && isIos && (
|
|
<p style={{ margin: 0, fontSize: 12, color: "var(--color-text-secondary, #666)", lineHeight: 1.5 }}>
|
|
Pulsa <i className="ti ti-share-2" aria-hidden="true"></i> <strong>Compartir</strong> y luego <strong>"Añadir a pantalla de inicio"</strong> para instalarla.
|
|
</p>
|
|
)}
|
|
|
|
{!canPromptInstall && !isIos && (
|
|
<p style={{ margin: 0, fontSize: 12, color: "var(--color-text-secondary, #666)", lineHeight: 1.5 }}>
|
|
Instálala desde el menú de tu navegador ("Instalar app" o "Añadir a pantalla de inicio").
|
|
</p>
|
|
)}
|
|
|
|
{resultado === "dismissed" && (
|
|
<p style={{ margin: "8px 0 0", fontSize: 11, color: "#888" }}>Puedes instalarla más tarde cuando quieras.</p>
|
|
)}
|
|
</div>
|
|
);
|
|
|
|
// ── Aviso abajo a la izquierda (PC y móvil), alineado con el borde del cuerpo ──
|
|
return (
|
|
<div style={{
|
|
position: "fixed",
|
|
left: `max(16px, calc((100vw - ${ANCHO_CUERPO}px) / 2 + 16px))`,
|
|
bottom: 0,
|
|
zIndex: 999,
|
|
paddingBottom: "calc(16px + env(safe-area-inset-bottom))",
|
|
}}>
|
|
{tarjeta(handleCerrar, { width: esMovil ? 280 : 320 })}
|
|
</div>
|
|
);
|
|
}
|