Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add Space PWA install prompt

Anand Baburajan2f62b723cfaf
diff --git a/web/apps/space/public/manifest.webmanifest b/web/apps/space/public/manifest.webmanifest
index a02c908200..61ed5a265b 100644
--- a/web/apps/space/public/manifest.webmanifest
+++ b/web/apps/space/public/manifest.webmanifest
@@ -1,7 +1,7 @@
{
"name": "Ente Space",
"short_name": "Ente Space",
- "description": "Share everyday moments with friends and family.",
+ "description": "Share everyday moments with friends and family, privately.",
"start_url": "/app",
"scope": "/",
"display": "standalone",
diff --git a/web/apps/space/src/components/SpacePWAInstallPrompt.tsx b/web/apps/space/src/components/SpacePWAInstallPrompt.tsx
new file mode 100644
index 0000000000..76d1c60c02
--- /dev/null
+++ b/web/apps/space/src/components/SpacePWAInstallPrompt.tsx
@@ -0,0 +1,454 @@
+import {
+ AddSquareIcon,
+ ArrowDown01Icon,
+ MoreHorizontalIcon,
+ MoreVerticalIcon,
+ MultiplicationSignIcon,
+ SafariIcon,
+ ScreenAddToHomeIcon,
+ Upload01Icon,
+} from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { Box, Dialog, useMediaQuery } from "@mui/material";
+import { SpaceBottomSheetTransition } from "components/SpaceBottomSheetTransition";
+import { useSpacePWAInstallPrompt } from "hooks/useSpacePWAInstallPrompt";
+import React from "react";
+import { spaceTouchTargetSize } from "styles/touchTargets";
+
+const green = "#08C225";
+const textBase = "#000";
+const textSoft = "#777777";
+const promptHorizontalPadding = "16px";
+
+interface SpacePWAInstallPromptProps {
+ enabled: boolean;
+}
+
+interface InstallStep {
+ icon: React.ReactNode;
+ text: string;
+}
+
+export const SpacePWAInstallPrompt: React.FC<SpacePWAInstallPromptProps> = ({
+ enabled,
+}) => {
+ const { dismiss, mode, shouldShow } = useSpacePWAInstallPrompt();
+ const [instructionsOpen, setInstructionsOpen] = React.useState(false);
+ const showPrompt = enabled && shouldShow;
+
+ React.useEffect(() => {
+ if (!showPrompt) setInstructionsOpen(false);
+ }, [showPrompt]);
+
+ if (!showPrompt) return null;
+
+ const openInstructions = () => setInstructionsOpen(true);
+ const closeInstructions = () => setInstructionsOpen(false);
+ const dismissInstructions = () => {
+ setInstructionsOpen(false);
+ dismiss();
+ };
+
+ return (
+ <>
+ <Box
+ sx={{
+ boxSizing: "border-box",
+ left: "50%",
+ px: promptHorizontalPadding,
+ pointerEvents: "none",
+ position: "fixed",
+ top: "calc(env(safe-area-inset-top) + 10px)",
+ transform: "translateX(-50%)",
+ width: "100%",
+ zIndex: 19,
+ "@media (min-width: 600px)": { maxWidth: 390 },
+ }}
+ >
+ <Box
+ role="status"
+ aria-live="polite"
+ sx={{
+ alignItems: "center",
+ bgcolor: "#FFFFFF",
+ borderRadius: "18px",
+ boxShadow: "0 12px 32px rgba(0, 0, 0, 0.18)",
+ boxSizing: "border-box",
+ color: textBase,
+ display: "flex",
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 650,
+ gap: "10px",
+ lineHeight: "20px",
+ minHeight: 50,
+ pointerEvents: "auto",
+ pl: "10px",
+ pr: "6px",
+ py: "3px",
+ width: "100%",
+ }}
+ >
+ <Box
+ component="span"
+ sx={{
+ alignItems: "center",
+ color: green,
+ display: "flex",
+ flexShrink: 0,
+ height: 24,
+ justifyContent: "center",
+ width: 24,
+ }}
+ >
+ <HugeiconsIcon
+ icon={ScreenAddToHomeIcon}
+ size={24}
+ strokeWidth={1.9}
+ />
+ </Box>
+ <Box
+ sx={{
+ flex: "1 1 auto",
+ minWidth: 0,
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ whiteSpace: "nowrap",
+ }}
+ >
+ Add to Home Screen
+ </Box>
+ <Box
+ sx={{
+ alignItems: "center",
+ display: "flex",
+ flexShrink: 0,
+ gap: 0,
+ }}
+ >
+ <Box
+ component="button"
+ type="button"
+ onClick={openInstructions}
+ sx={{
+ alignItems: "center",
+ bgcolor: green,
+ border: 0,
+ borderRadius: "14px",
+ color: "#FFFFFF",
+ cursor: "pointer",
+ display: "flex",
+ flexShrink: 0,
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 700,
+ height: 34,
+ justifyContent: "center",
+ lineHeight: "18px",
+ minWidth: 48,
+ px: "17px",
+ transition: "filter 120ms ease",
+ "&:active": { filter: "brightness(0.96)" },
+ "&:focus-visible": {
+ outline: "2px solid rgba(0 0 0 / 0.72)",
+ outlineOffset: 2,
+ },
+ "&:hover": { filter: "brightness(0.98)" },
+ }}
+ >
+ Add
+ </Box>
+ <Box
+ component="button"
+ type="button"
+ aria-label="Close install prompt"
+ onClick={dismiss}
+ sx={{
+ alignItems: "center",
+ appearance: "none",
+ bgcolor: "transparent",
+ border: 0,
+ color: textBase,
+ cursor: "pointer",
+ display: "flex",
+ flexShrink: 0,
+ height: spaceTouchTargetSize,
+ justifyContent: "center",
+ opacity: 0.9,
+ p: 0,
+ width: 36,
+ "&:focus-visible": {
+ outline: "2px solid rgba(0 0 0 / 0.72)",
+ outlineOffset: 2,
+ },
+ }}
+ >
+ <HugeiconsIcon
+ icon={MultiplicationSignIcon}
+ size={16}
+ strokeWidth={2}
+ />
+ </Box>
+ </Box>
+ </Box>
+ </Box>
+ <SpacePWAInstallInstructions
+ mode={mode}
+ open={instructionsOpen}
+ onClose={closeInstructions}
+ onDismiss={dismissInstructions}
+ />
+ </>
+ );
+};
+
+interface SpacePWAInstallInstructionsProps {
+ mode: ReturnType<typeof useSpacePWAInstallPrompt>["mode"];
+ open: boolean;
+ onClose: () => void;
+ onDismiss: () => void;
+}
+
+const SpacePWAInstallInstructions: React.FC<
+ SpacePWAInstallInstructionsProps
+> = ({ mode, open, onClose, onDismiss }) => {
+ const titleID = React.useId();
+ const isBottomSheet = useMediaQuery("(max-width: 599px)");
+ const iosSafariSteps: InstallStep[] = [
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={MoreHorizontalIcon}
+ size={18}
+ strokeWidth={3.2}
+ />
+ ),
+ text: "Open your browser menu",
+ },
+ {
+ icon: (
+ <HugeiconsIcon icon={Upload01Icon} size={18} strokeWidth={2} />
+ ),
+ text: 'Tap "Share"',
+ },
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={ArrowDown01Icon}
+ size={18}
+ strokeWidth={2}
+ />
+ ),
+ text: 'Tap "View More"',
+ },
+ {
+ icon: (
+ <HugeiconsIcon icon={AddSquareIcon} size={18} strokeWidth={2} />
+ ),
+ text: 'Tap "Add to Home Screen"',
+ },
+ ];
+ const steps: InstallStep[] =
+ mode == "ios-fallback"
+ ? [
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={SafariIcon}
+ size={18}
+ strokeWidth={2}
+ />
+ ),
+ text: "Open this page in Safari",
+ },
+ ...iosSafariSteps,
+ ]
+ : mode == "ios-safari"
+ ? iosSafariSteps
+ : [
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={MoreVerticalIcon}
+ size={18}
+ strokeWidth={3.2}
+ />
+ ),
+ text: "Open your browser menu",
+ },
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={MoreHorizontalIcon}
+ size={18}
+ strokeWidth={3.2}
+ />
+ ),
+ text: 'Tap "More"',
+ },
+ {
+ icon: (
+ <HugeiconsIcon
+ icon={ScreenAddToHomeIcon}
+ size={18}
+ strokeWidth={2}
+ />
+ ),
+ text: 'Tap "Add to Home screen"',
+ },
+ ];
+
+ return (
+ <Dialog
+ open={open}
+ onClose={onClose}
+ maxWidth={false}
+ aria-labelledby={titleID}
+ slots={
+ isBottomSheet
+ ? { transition: SpaceBottomSheetTransition }
+ : undefined
+ }
+ slotProps={{
+ backdrop: { sx: { backgroundColor: "rgba(0, 0, 0, 0.48)" } },
+ paper: {
+ sx: {
+ bgcolor: "#FAFAFA",
+ borderRadius: "28px 28px 0 0",
+ bottom: 0,
+ boxShadow: "none",
+ boxSizing: "border-box",
+ left: 0,
+ m: 0,
+ maxWidth: "none",
+ p: "26px 20px calc(24px + env(safe-area-inset-bottom))",
+ position: "fixed",
+ width: "100vw",
+ "@media (min-width: 600px)": {
+ borderRadius: "20px",
+ bottom: "auto",
+ boxShadow: "0 18px 48px rgba(0, 0, 0, 0.18)",
+ left: "50%",
+ maxWidth: 342,
+ p: "24px 20px 20px",
+ top: "50%",
+ transform: "translate(-50%, -50%)",
+ width: 342,
+ },
+ },
+ },
+ }}
+ >
+ <Box
+ sx={{
+ maxWidth: 320,
+ mx: "auto",
+ width: "100%",
+ "@media (min-width: 600px)": { maxWidth: "none" },
+ }}
+ >
+ <Box
+ component="h2"
+ id={titleID}
+ sx={{
+ color: textBase,
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 15,
+ fontWeight: 600,
+ lineHeight: "20px",
+ m: 0,
+ px: "20px",
+ textAlign: "center",
+ }}
+ >
+ Add to Home Screen
+ </Box>
+ <Box sx={{ display: "grid", gap: "12px", mt: "22px" }}>
+ {steps.map((step, index) => (
+ <InstallInstructionStep
+ key={index}
+ icon={step.icon}
+ text={step.text}
+ />
+ ))}
+ </Box>
+ <Box
+ component="button"
+ type="button"
+ onClick={onDismiss}
+ sx={{
+ alignItems: "center",
+ bgcolor: green,
+ border: 0,
+ borderRadius: "20px",
+ color: "#FFFFFF",
+ cursor: "pointer",
+ display: "flex",
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 600,
+ height: 48,
+ justifyContent: "center",
+ lineHeight: "20px",
+ mt: "24px",
+ px: "24px",
+ transition: "filter 120ms ease",
+ width: "100%",
+ "&:active": { filter: "brightness(0.96)" },
+ "&:focus-visible": {
+ outline: "2px solid rgba(0 0 0 / 0.72)",
+ outlineOffset: 2,
+ },
+ "&:hover": { filter: "brightness(0.98)" },
+ }}
+ >
+ Got it
+ </Box>
+ </Box>
+ </Dialog>
+ );
+};
+
+const InstallInstructionStep: React.FC<InstallStep> = ({ icon, text }) => (
+ <Box
+ sx={{
+ alignItems: "center",
+ bgcolor: "#FFFFFF",
+ borderRadius: "18px",
+ color: textSoft,
+ display: "flex",
+ gap: "12px",
+ minHeight: 52,
+ px: "14px",
+ py: "10px",
+ }}
+ >
+ <Box
+ sx={{
+ alignItems: "center",
+ bgcolor: "#E7F6E9",
+ borderRadius: "14px",
+ color: green,
+ display: "flex",
+ flexShrink: 0,
+ height: 32,
+ justifyContent: "center",
+ width: 32,
+ }}
+ >
+ {icon}
+ </Box>
+ <Box
+ sx={{
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 600,
+ lineHeight: "20px",
+ minWidth: 0,
+ }}
+ >
+ {text}
+ </Box>
+ </Box>
+);
diff --git a/web/apps/space/src/hooks/useSpacePWAInstallPrompt.ts b/web/apps/space/src/hooks/useSpacePWAInstallPrompt.ts
new file mode 100644
index 0000000000..ee443779c0
--- /dev/null
+++ b/web/apps/space/src/hooks/useSpacePWAInstallPrompt.ts
@@ -0,0 +1,135 @@
+import React from "react";
+
+type SpacePWAInstallMode = "android-fallback" | "ios-fallback" | "ios-safari";
+
+interface SpacePWAInstallPromptState {
+ dismiss: () => void;
+ mode: SpacePWAInstallMode | null;
+ shouldShow: boolean;
+}
+
+const dismissedUntilKey = "space.pwaInstall.dismissedUntil";
+const dismissalDurationMs = 14 * 24 * 60 * 60 * 1000;
+
+const hasWindow = () => typeof window != "undefined";
+
+const isIOS = () => {
+ if (!hasWindow()) return false;
+
+ const { maxTouchPoints, userAgent } = navigator;
+ return (
+ /iPad|iPhone|iPod/.test(userAgent) ||
+ (userAgent.includes("Macintosh") && maxTouchPoints > 1)
+ );
+};
+
+const isAndroid = () => hasWindow() && /Android/i.test(navigator.userAgent);
+
+const isIOSSafari = () => {
+ if (!isIOS()) return false;
+
+ const { userAgent } = navigator;
+ return (
+ /Safari/i.test(userAgent) &&
+ !/CriOS|FxiOS|EdgiOS|OPiOS|DuckDuckGo/i.test(userAgent)
+ );
+};
+
+const isAndroidNativePromptBrowser = () => {
+ if (!isAndroid()) return false;
+
+ const { userAgent } = navigator;
+ if (/Firefox|OPR\/|Opera/i.test(userAgent)) return false;
+ return /Chrome|Chromium|EdgA|SamsungBrowser/i.test(userAgent);
+};
+
+const shouldShowAndroidInstructions = () =>
+ isAndroid() && !isAndroidNativePromptBrowser();
+
+const isStandalone = () =>
+ hasWindow() &&
+ (window.matchMedia("(display-mode: standalone)").matches ||
+ ("standalone" in navigator &&
+ Boolean((navigator as { standalone?: boolean }).standalone)));
+
+const storageGet = (key: string) => {
+ try {
+ return window.localStorage.getItem(key);
+ } catch {
+ return null;
+ }
+};
+
+const storageSet = (key: string, value: string) => {
+ try {
+ window.localStorage.setItem(key, value);
+ } catch {
+ return;
+ }
+};
+
+const storageRemove = (key: string) => {
+ try {
+ window.localStorage.removeItem(key);
+ } catch {
+ return;
+ }
+};
+
+const isDismissed = () => {
+ if (!hasWindow()) return true;
+
+ const dismissedUntil = Number(storageGet(dismissedUntilKey));
+ if (!Number.isFinite(dismissedUntil) || dismissedUntil <= 0) return false;
+ if (dismissedUntil > Date.now()) return true;
+
+ storageRemove(dismissedUntilKey);
+ return false;
+};
+
+export const useSpacePWAInstallPrompt = (): SpacePWAInstallPromptState => {
+ const [mode, setMode] = React.useState<SpacePWAInstallMode | null>(null);
+ const [dismissed, setDismissed] = React.useState(true);
+ const [installed, setInstalled] = React.useState(true);
+
+ React.useEffect(() => {
+ if (!hasWindow()) return;
+
+ setDismissed(isDismissed());
+ setInstalled(isStandalone());
+
+ if (isIOSSafari()) {
+ setMode("ios-safari");
+ } else if (isIOS()) {
+ setMode("ios-fallback");
+ } else if (shouldShowAndroidInstructions()) {
+ setMode("android-fallback");
+ }
+
+ const appInstalled = () => {
+ setInstalled(true);
+ setMode(null);
+ };
+ const standaloneChange = () => setInstalled(isStandalone());
+ const standaloneQuery = window.matchMedia("(display-mode: standalone)");
+
+ window.addEventListener("appinstalled", appInstalled);
+ standaloneQuery.addEventListener("change", standaloneChange);
+
+ return () => {
+ window.removeEventListener("appinstalled", appInstalled);
+ standaloneQuery.removeEventListener("change", standaloneChange);
+ };
+ }, []);
+
+ const dismiss = React.useCallback(() => {
+ storageSet(dismissedUntilKey, String(Date.now() + dismissalDurationMs));
+ setDismissed(true);
+ }, []);
+
+ return {
+ dismiss,
+ mode,
+ shouldShow: Boolean(mode) && !dismissed && !installed,
+ };
+};
diff --git a/web/apps/space/src/pages/app/index.tsx b/web/apps/space/src/pages/app/index.tsx
index 2adb7a54b1..8342926716 100644
--- a/web/apps/space/src/pages/app/index.tsx
+++ b/web/apps/space/src/pages/app/index.tsx
@@ -211,6 +211,11 @@ const Page: React.FC = () => {
isFeedLoadingMore={isFeedLoadingMore}
localFeedPosts={localFeedPosts}
profile={profile}
+ showInstallPrompt={
+ profileLoadStatus == "ready" &&
+ Boolean(profile) &&
+ !isHomeFeedLoading
+ }
onFriendRequestSentToastClose={closeFriendRequestSentToast}
onCreatePost={
profile
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index 048471fe76..8d2d436654 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -21,6 +21,7 @@ import {
spacePostLikePopDurationMs,
spacePostLikePopTiming,
} from "components/SpacePostLikeAnimation";
+import { SpacePWAInstallPrompt } from "components/SpacePWAInstallPrompt";
import { SpaceLoadingSpinner } from "components/SpaceRouteFallback";
import { useBrowserBackClose } from "hooks/useBrowserBackClose";
import React, { useState } from "react";
@@ -110,6 +111,7 @@ interface HomeScreenProps {
isFeedLoading?: boolean;
isFeedLoadingMore?: boolean;
localFeedPosts?: LocalSpaceFeedPost[];
+ showInstallPrompt?: boolean;
onCreatePost?: (
image: DraftSpacePostImage,
caption: string,
@@ -1268,6 +1270,7 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
isFeedLoading = false,
isFeedLoadingMore = false,
localFeedPosts = [],
+ showInstallPrompt = false,
onCreatePost,
onDeletePost,
onLoadMoreFeedItems,
@@ -1324,6 +1327,8 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
localFeedPosts.length > 0 || remoteFeedItems.length > 0;
const isEmptyFeedLoading = !hasFeedItems && isFeedLoading;
const showFeedCards = hasFeedItems;
+ const isInstallPromptEnabled =
+ showInstallPrompt && !friendRequestSentToastName && !selectedViewer;
const showUnreadIndicator = hasUnreadMessages === true;
const emptyFeedMessage =
friendsCount == 0
@@ -2176,11 +2181,13 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
onSetPostLiked={onSetPostLiked}
/>
)}
- {friendRequestSentToastName && (
+ {friendRequestSentToastName ? (
<AddedFriendToast
message={`Friend request sent to @${friendRequestSentToastName}`}
onClose={onFriendRequestSentToastClose}
/>
+ ) : (
+ <SpacePWAInstallPrompt enabled={isInstallPromptEnabled} />
)}
</Box>
</Box>
diff --git a/web/apps/space/src/screens/OnboardingScreen.tsx b/web/apps/space/src/screens/OnboardingScreen.tsx
index fb8e1ed422..6087ddf66c 100644
--- a/web/apps/space/src/screens/OnboardingScreen.tsx
+++ b/web/apps/space/src/screens/OnboardingScreen.tsx
@@ -4,7 +4,7 @@ import React from "react";
export const onboardingGreen = "#08C225";
export const onboardingTitle = "Share your life";
export const onboardingDescription =
- "A private space for sharing everyday moments with friends and family. No ads. No brain rot.";
+ "A private space for sharing everyday moments with friends and family only. No ads. No brain rot.";
export const addFriendOnboardingTitle = "Follow their life";
const softGreen = "#E7F6E9";