diff --git a/.fallowrc.json b/.fallowrc.json index ba50fb5..022e0cc 100644 --- a/.fallowrc.json +++ b/.fallowrc.json @@ -3,7 +3,6 @@ "entry": [ "src/index.{ts,tsx,js,jsx}", "src/main.{ts,tsx,js,jsx}", - "apps/pwa/src/serviceWorker.ts", "apps/tauri/render-version.ts", "packages/**/*.test.ts" ], diff --git a/apps/pwa/src/runtime.tsx b/apps/pwa/src/runtime.tsx index 21c2556..d7917cc 100644 --- a/apps/pwa/src/runtime.tsx +++ b/apps/pwa/src/runtime.tsx @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { toast } from "sonner"; import { setDatabaseEntry } from "@tensamin/shared/indexedDb"; @@ -6,6 +6,11 @@ import { isTauri } from "@tauri-apps/api/core"; import "./style.css"; +type BeforeInstallPromptEvent = Event & { + prompt: () => Promise; + userChoice: Promise<{ outcome: "accepted" | "dismissed" }>; +}; + const launchedFiles: File[] = []; const fileListeners = new Set<(file: File) => void>(); @@ -61,6 +66,9 @@ async function enablePush() { } export default function PwaRuntime() { + const [installPrompt, setInstallPrompt] = + useState(null); + useEffect(() => { if ( isTauri() || @@ -121,6 +129,16 @@ export default function PwaRuntime() { }; }, []); + useEffect(() => { + const handleInstallPrompt = (event: Event) => { + event.preventDefault(); + setInstallPrompt(event as BeforeInstallPromptEvent); + }; + window.addEventListener("beforeinstallprompt", handleInstallPrompt); + return () => + window.removeEventListener("beforeinstallprompt", handleInstallPrompt); + }, []); + useEffect(() => { const launchQueue = ( window as Window & { @@ -140,6 +158,39 @@ export default function PwaRuntime() { }); }, []); + useEffect(() => { + if (!installPrompt) return; + toast("Install Tensamin for a native app experience", { + duration: Infinity, + action: { + label: "Install", + onClick: () => { + void installPrompt.prompt().then(() => installPrompt.userChoice); + setInstallPrompt(null); + }, + }, + }); + }, [installPrompt]); + + useEffect(() => { + const isAppleMobile = /iPad|iPhone|iPod/.test(navigator.userAgent); + if ( + isTauri() || + !isAppleMobile || + isStandalone() || + localStorage.getItem("pwa-ios-install-hint") + ) { + return; + } + localStorage.setItem("pwa-ios-install-hint", "shown"); + toast( + "Install Tensamin from Safari's Share menu to enable background notifications.", + { + duration: 12_000, + }, + ); + }, []); + useEffect(() => { if ( !isStandalone() || diff --git a/apps/pwa/src/serviceWorker.ts b/apps/pwa/src/serviceWorker.ts index 554ceeb..de341bb 100644 --- a/apps/pwa/src/serviceWorker.ts +++ b/apps/pwa/src/serviceWorker.ts @@ -34,22 +34,19 @@ function isPushPayload(value: unknown): value is PushPayload { const message = payload.message as Partial | undefined; const secret = payload.secret as Partial | undefined; - const stringValues = [ - payload.sender, - message?.content, - secret?.chatId, - secret?.secretId, - secret?.encryptedSecret, - secret?.kemCiphertext, - secret?.wrappingScheme, - ]; return ( payload.version === 1 && typeof payload.senderId === "number" && Number.isSafeInteger(payload.senderId) && payload.senderId > 0 && - typeof secret?.version === "number" && - stringValues.every((item) => typeof item === "string") + typeof payload.sender === "string" && + typeof message?.content === "string" && + typeof secret?.chatId === "string" && + typeof secret.secretId === "string" && + typeof secret.version === "number" && + typeof secret.encryptedSecret === "string" && + typeof secret.kemCiphertext === "string" && + typeof secret.wrappingScheme === "string" ); } diff --git a/apps/tauri/src-tauri/Cargo.lock b/apps/tauri/src-tauri/Cargo.lock index 2c8b2e5..5be3499 100644 --- a/apps/tauri/src-tauri/Cargo.lock +++ b/apps/tauri/src-tauri/Cargo.lock @@ -2745,7 +2745,7 @@ dependencies = [ [[package]] name = "mtp" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "mtp-client", "mtp-codec 0.3.0", @@ -2760,7 +2760,7 @@ dependencies = [ [[package]] name = "mtp-client" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "mtp-codec 0.3.0", "mtp-common 0.3.0", @@ -2785,7 +2785,7 @@ dependencies = [ [[package]] name = "mtp-codec" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "base64 0.23.1", "byteorder", @@ -2810,7 +2810,7 @@ dependencies = [ [[package]] name = "mtp-common" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "quinn", "rustls", @@ -2841,7 +2841,7 @@ dependencies = [ [[package]] name = "mtp-crypto" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "base64 0.22.1", "chacha20poly1305", @@ -2863,7 +2863,7 @@ dependencies = [ [[package]] name = "mtp-host" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "mtp-codec 0.3.0", "mtp-common 0.3.0", @@ -2897,7 +2897,7 @@ dependencies = [ [[package]] name = "mtp-transport" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "async-trait", "mtp-codec 0.3.0", @@ -2926,7 +2926,7 @@ dependencies = [ [[package]] name = "mtp-type-map" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "serde", "serde_yaml", @@ -2935,7 +2935,7 @@ dependencies = [ [[package]] name = "mtp-webserver" version = "0.3.0" -source = "git+https://git.methanium.net/methanium/mtp.git?rev=b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8#b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8" +source = "git+https://git.methanium.net/methanium/mtp.git?rev=3395b91ad1cd1af1863f28df6d7cb9a7c9f51617#3395b91ad1cd1af1863f28df6d7cb9a7c9f51617" dependencies = [ "async-trait", "bytes", diff --git a/apps/tauri/src-tauri/Cargo.toml b/apps/tauri/src-tauri/Cargo.toml index 905528a..85eb9a3 100644 --- a/apps/tauri/src-tauri/Cargo.toml +++ b/apps/tauri/src-tauri/Cargo.toml @@ -24,7 +24,7 @@ serde_json = "1" base64 = "0.22" reqwest = { version = "0.13", default-features = false, features = ["json", "rustls"] } tokio = { version = "1", features = ["rt-multi-thread", "sync", "time"] } -mtp = { git = "https://git.methanium.net/methanium/mtp.git", rev = "b331b9f6a3943d0331d8fdcb7c696d6c2bc5e8b8", features = ["client", "crypto"] } +mtp = { git = "https://git.methanium.net/methanium/mtp.git", rev = "3395b91ad1cd1af1863f28df6d7cb9a7c9f51617", features = ["client", "crypto"] } mtp-transport = { git = "https://git.methanium.net/methanium/mtp.git", rev = "b067614a684eb1856bc5db7b3fd82148c036ce6b" } webpki-root-certs = "1" tauri-plugin-deep-link = "2" diff --git a/flake.nix b/flake.nix index d91a817..2b8781f 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ systems = [ "x86_64-linux" ]; forAllSystems = nixpkgs.lib.genAttrs systems; version = "0.0.11"; - x86_64DebHash = "sha256-p/6boNv9uWTHsPTVa3EhGzr41EwRzCY/fJWlj854DVA="; + x86_64DebHash = "sha256-E4FGMpCt2ByaN6d+YzglqeA6LbhMOQmPf3UzAj7uZXE="; forgejoBaseUrl = "https://git.methanium.net/tensamin/client/releases/download/${version}"; in {