π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Simplify Space sharing and fix home post races
Anand Baburajan4f702e418588
diff --git a/web/apps/space/src/components/ShareInviteButton.tsx b/web/apps/space/src/components/ShareInviteButton.tsxindex 97bd9df71b..02dab60d8b 100644--- a/web/apps/space/src/components/ShareInviteButton.tsx+++ b/web/apps/space/src/components/ShareInviteButton.tsx@@ -1,51 +1,68 @@import { Share08Icon } from "@hugeicons/core-free-icons";import { HugeiconsIcon } from "@hugeicons/react";-import { Box, type SxProps, type Theme } from "@mui/material";+import { Box } from "@mui/material";+import { spaceToastAutoDismissDurationMs } from "components/ActionToast";import React from "react";-import { openSpaceShareLinkDialog } from "services/share-link";+import { spaceEmptyStateButtonSx } from "styles/buttons";-export const SpaceShareIcon: React.FC<{ strokeWidth?: number }> = ({- strokeWidth = 1.8,-}) => <HugeiconsIcon icon={Share08Icon} size={18} strokeWidth={strokeWidth} />;+export const SpaceShareIcon: React.FC<{+ size?: number;+ strokeWidth?: number;+}> = ({ size = 18, strokeWidth = 1.8 }) => (+ <HugeiconsIcon icon={Share08Icon} size={size} strokeWidth={strokeWidth} />+);interface SpaceShareInviteButtonProps {- className?: string;- iconStrokeWidth?: number;- label?: string;profileLink?: string;- showIcon?: boolean;sharing?: boolean;- sx?: SxProps<Theme>;- onShareComplete?: () => void;onShareError?: (error: unknown) => void;- onShareStart?: () => void;onSharingChange?: (sharing: boolean) => void;}export const SpaceShareInviteButton: React.FC<SpaceShareInviteButtonProps> = ({- className,- iconStrokeWidth,- label,profileLink,- showIcon = true,sharing,- sx,- onShareComplete,onShareError,- onShareStart,onSharingChange,}) => {+ const [copied, setCopied] = React.useState(false);+ const [canShare, setCanShare] = React.useState(false);const isSharing = sharing ?? false;const isDisabled = isSharing || !profileLink;- const buttonLabel = label ?? "Share invite";- const shareInvite = () => {+ React.useEffect(() => {+ setCanShare(typeof navigator.share == "function");+ }, []);++ React.useEffect(() => {+ if (!copied) return;+ const timeoutID = window.setTimeout(+ () => setCopied(false),+ spaceToastAutoDismissDurationMs,+ );+ return () => window.clearTimeout(timeoutID);+ }, [copied]);++ const shareInvite = async () => {if (isDisabled || !profileLink) return;- onShareStart?.();+ onSharingChange?.(true);try {- onSharingChange?.(true);- openSpaceShareLinkDialog(profileLink, "invite");- onShareComplete?.();+ if (typeof navigator.share == "function") {+ try {+ await navigator.share({ url: profileLink });+ } catch (error) {+ if (+ error instanceof DOMException &&+ error.name == "AbortError"+ ) {+ return;+ }+ throw error;+ }+ } else {+ await navigator.clipboard.writeText(profileLink);+ setCopied(true);+ }} catch (error) {onShareError?.(error);} finally {@@ -55,15 +72,16 @@ export const SpaceShareInviteButton: React.FC<SpaceShareInviteButtonProps> = ({return (<Box- className={className}+ className="green-bg"component="button"type="button"+ aria-live="polite"disabled={isDisabled}- onClick={shareInvite}- sx={sx}+ onClick={() => void shareInvite()}+ sx={spaceEmptyStateButtonSx}>- {showIcon && <SpaceShareIcon strokeWidth={iconStrokeWidth} />}- {buttonLabel}+ <SpaceShareIcon />+ {copied ? "Copied" : canShare ? "Share invite" : "Copy invite"}</Box>);};diff --git a/web/apps/space/src/components/ShareLinkDialog.tsx b/web/apps/space/src/components/ShareLinkDialog.tsxindex 6a7d813a13..2408b45830 100644--- a/web/apps/space/src/components/ShareLinkDialog.tsx+++ b/web/apps/space/src/components/ShareLinkDialog.tsx@@ -2,10 +2,7 @@ import { Box, Dialog, useMediaQuery } from "@mui/material";import { SpaceBottomSheetTransition } from "components/BottomSheetTransition";import log from "ente-base/log";import React from "react";-import {- onOpenSpaceShareLinkDialog,- type SpaceShareLinkDialogMode,-} from "services/share-link";+import { onOpenSpaceShareLinkDialog } from "services/share-link";import { spaceText, spaceTextMuted } from "styles/colors";const green = "#08C225";@@ -26,21 +23,16 @@ export const SpaceShareLinkDialogHost: React.FC = () => {const [error, setError] = React.useState<string>();const [copied, setCopied] = React.useState(false);const [useNativeShare, setUseNativeShare] = React.useState(false);- const [mode, setMode] = React.useState<SpaceShareLinkDialogMode>("profile");- const showLink = React.useCallback(- (nextMode: SpaceShareLinkDialogMode, nextProfileLink: string) => {- setOpen(true);- setMode(nextMode);- setProfileLink(nextProfileLink);- setError(undefined);- setCopied(false);- setUseNativeShare(- isMobileBrowser() && typeof navigator.share == "function",- );- },- [],- );+ const showLink = React.useCallback((nextProfileLink: string) => {+ setOpen(true);+ setProfileLink(nextProfileLink);+ setError(undefined);+ setCopied(false);+ setUseNativeShare(+ isMobileBrowser() && typeof navigator.share == "function",+ );+ }, []);React.useEffect(() => {return onOpenSpaceShareLinkDialog(showLink);@@ -137,7 +129,7 @@ export const SpaceShareLinkDialogHost: React.FC = () => {textAlign: "center",}}>- {mode == "invite" ? "Invite friends" : "Share your profile"}+ Invite friends</Box><Boxsx={{diff --git a/web/apps/space/src/pages/app/post.tsx b/web/apps/space/src/pages/app/post.tsxindex 3ee1bfd242..9d7e5ab5b3 100644--- a/web/apps/space/src/pages/app/post.tsx+++ b/web/apps/space/src/pages/app/post.tsx@@ -6,6 +6,7 @@ import { SpacePageMeta } from "components/PageMeta";import { SpaceRouteFallback } from "components/RouteFallback";import React from "react";import { useSpaceAppState } from "state/app-state";+import { spaceEmptyStateButtonSx } from "styles/buttons";import {spaceAppBackground,spaceAppBackgroundColor,@@ -173,36 +174,11 @@ const Page: React.FC = () => {type="button"disabled={isOpeningPost}onClick={() => inputRef.current?.click()}- sx={{- alignItems: "center",- bgcolor: green,- border: 0,- borderRadius: "22px",- color: "#FFFFFF",- cursor: isOpeningPost ? "default" : "pointer",- display: "inline-flex",- fontFamily:- '"Inter Variable", Inter, sans-serif',- fontSize: 15,- fontWeight: 700,- gap: "8px",- height: 48,- justifyContent: "center",- mt: "28px",- opacity: isOpeningPost ? 0.65 : 1,- px: "22px",- "&:focus-visible": {- outline: `2px solid ${green}`,- outlineOffset: 3,- },- "&:hover": isOpeningPost- ? undefined- : { bgcolor: "#07AE22" },- }}+ sx={{ ...spaceEmptyStateButtonSx, mt: "28px" }}><HugeiconsIconicon={AddSquareIcon}- size={20}+ size={18}strokeWidth={1.8}/>Postdiff --git a/web/apps/space/src/screens/FriendsScreen.tsx b/web/apps/space/src/screens/FriendsScreen.tsxindex a24b31c264..3fc180fd41 100644--- a/web/apps/space/src/screens/FriendsScreen.tsx+++ b/web/apps/space/src/screens/FriendsScreen.tsx@@ -20,12 +20,11 @@ import {spaceActionDoneDurationMs,type SpaceActionPhase,} from "components/ActionFeedback";-import { spaceToastAutoDismissDurationMs } from "components/ActionToast";import { SpaceAvatarImage } from "components/AvatarImage";import { SpaceBottomSheetTransition } from "components/BottomSheetTransition";import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";import { SpaceLoadingSpinner } from "components/RouteFallback";-import { SpaceShareIcon } from "components/ShareInviteButton";+import { SpaceShareInviteButton } from "components/ShareInviteButton";import type { FriendProfile } from "data/friends";import log from "ente-base/log";import React, { useState } from "react";@@ -969,54 +968,12 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({const [loadedAvatarURLsByKey, setLoadedAvatarURLsByKey] = React.useState<Record<string, string>>({});- const [inviteShareStatus, setInviteShareStatus] = React.useState<- "idle" | "sharing" | "copied"- >("idle");+ const [isInviteSharing, setIsInviteSharing] = React.useState(false);const avatarLoadsInFlightRef = React.useRef<Map<string, Promise<string | null | undefined>>>(new Map());const isUnfriendActionRunning = unfriendActionPhase != null;- React.useEffect(() => {- if (inviteShareStatus != "copied") return;- const timeoutID = window.setTimeout(- () => setInviteShareStatus("idle"),- spaceToastAutoDismissDurationMs,- );- return () => window.clearTimeout(timeoutID);- }, [inviteShareStatus]);-- const shareInvite = async () => {- if (!profileLink || inviteShareStatus == "sharing") return;-- setInviteShareStatus("sharing");- try {- if (typeof navigator.share == "function") {- try {- await navigator.share({ url: profileLink });- return;- } catch (error) {- if (- error instanceof DOMException &&- error.name == "AbortError"- ) {- return;- }- log.warn("Failed to share Space invite link", error);- }- }-- await navigator.clipboard.writeText(profileLink);- setInviteShareStatus("copied");- } catch (error) {- log.error("Failed to copy Space invite link", error);- } finally {- setInviteShareStatus((status) =>- status == "sharing" ? "idle" : status,- );- }- };-const loadedAvatarURLFor = React.useCallback((friend: FriendProfile) =>friend.avatarUrl ??@@ -1274,6 +1231,7 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({color: textSoft,display: "flex",flexDirection: "column",+ gap: "22px",inset: 0,justifyContent: "center",fontFamily: '"Inter Variable", Inter, sans-serif',@@ -1290,55 +1248,17 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({Invite your close friends and family. Share everydayphotos and keep up with each other.</Box>- <Box- component="button"- type="button"- aria-live="polite"- disabled={- !profileLink || inviteShareStatus == "sharing"+ <SpaceShareInviteButton+ profileLink={profileLink}+ sharing={isInviteSharing}+ onShareError={(error) =>+ log.error(+ "Failed to share Space invite link",+ error,+ )}- onClick={() => void shareInvite()}- sx={{- alignItems: "center",- bgcolor: spaceSurface,- border: 0,- borderRadius: "18px",- color: textBase,- cursor:- profileLink &&- inviteShareStatus != "sharing"- ? "pointer"- : "default",- display: "inline-flex",- fontFamily:- '"Inter Variable", Inter, sans-serif',- fontSize: 13,- fontWeight: 600,- gap: "6px",- height: spaceTouchTargetSize,- justifyContent: "center",- lineHeight: "18px",- mt: "22px",- pointerEvents: "auto",- px: "14px",- whiteSpace: "nowrap",- "&:disabled": { opacity: 0.45 },- "&:focus-visible": {- outline: `2px solid ${green}`,- outlineOffset: 2,- },- "&:hover":- profileLink &&- inviteShareStatus != "sharing"- ? { bgcolor: spaceSurfaceHover }- : undefined,- }}- >- <SpaceShareIcon />- {inviteShareStatus == "copied"- ? "Copied"- : "Share invite"}- </Box>+ onSharingChange={setIsInviteSharing}+ /></Box>)}</Box>diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsxindex 91e7c3dc0c..75af407bf8 100644--- a/web/apps/space/src/screens/HomeScreen.tsx+++ b/web/apps/space/src/screens/HomeScreen.tsx@@ -1137,11 +1137,17 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({if (!post || !friend) return;markPostRead(post);- const updateSelectedViewer = (imageUrl: string) => {+ const updateSelectedViewer = (+ imageUrl: string,+ requireActivePost = false,+ ) => {setSelectedViewer((viewer) => {if (!viewer?.posts ||- viewer.posts !== currentViewer.posts+ viewer.posts !== currentViewer.posts ||+ (requireActivePost &&+ (viewer.postIndex != postIndex ||+ viewer.photo.postId != post.postId))) {return viewer;}@@ -1166,7 +1172,9 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({updateSelectedViewer("");void loadPostImage(post).then((loadedImageUrl) => {- if (loadedImageUrl) updateSelectedViewer(loadedImageUrl);+ if (loadedImageUrl) {+ updateSelectedViewer(loadedImageUrl, true);+ }});},[loadPostImage, loadedPostImageURLFor, markPostRead, selectedViewer],diff --git a/web/apps/space/src/screens/MessagesScreen.tsx b/web/apps/space/src/screens/MessagesScreen.tsxindex fc31aa5493..74ec72f39d 100644--- a/web/apps/space/src/screens/MessagesScreen.tsx+++ b/web/apps/space/src/screens/MessagesScreen.tsx@@ -2817,7 +2817,6 @@ export const MessagesScreen: React.FC<MessagesScreenProps> = ({</Box>{showInviteEmptyState && (<SpaceShareInviteButton- label="Invite friends"profileLink={profileLink}sharing={isInviteSharing}onShareError={(error) =>@@ -2827,43 +2826,6 @@ export const MessagesScreen: React.FC<MessagesScreenProps> = ({)}onSharingChange={setIsInviteSharing}- sx={{- alignItems: "center",- bgcolor: spaceSurface,- border: 0,- borderRadius: "18px",- color: textBase,- cursor:- profileLink &&- !isInviteSharing- ? "pointer"- : "default",- display: "inline-flex",- fontFamily:- '"Inter Variable", Inter, sans-serif',- fontSize: 13,- fontWeight: 600,- gap: "6px",- height: spaceTouchTargetSize,- justifyContent: "center",- lineHeight: "18px",- pointerEvents: "auto",- px: "14px",- whiteSpace: "nowrap",- "&:disabled": { opacity: 0.45 },- "&:focus-visible": {- outline: `2px solid ${green}`,- outlineOffset: 2,- },- "&:hover":- profileLink &&- !isInviteSharing- ? {- bgcolor:- spaceSurfaceHover,- }- : undefined,- }}/>)}</Box>diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsxindex 7339af657f..a024589e3d 100644--- a/web/apps/space/src/screens/ProfileScreen.tsx+++ b/web/apps/space/src/screens/ProfileScreen.tsx@@ -29,8 +29,8 @@ import { useBrowserBackClose } from "hooks/use-browser-back-close";import React, { useState } from "react";import type { SetupProfile } from "screens/SetupProfileScreen";import type { SpaceInviteIntent } from "services/invite";-import { openSpaceShareLinkDialog } from "services/share-link";import { isSpaceContentError, type SpacePostAsset } from "services/space";+import { spaceEmptyStateButtonSx } from "styles/buttons";import {spaceAppBackground,spaceAppBackgroundColor,@@ -1011,9 +1011,18 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({});};- const shareProfile = () => {- if (!profileLink) return;- openSpaceShareLinkDialog(profileLink);+ const shareInvite = async () => {+ if (!profileLink || typeof navigator.share != "function") return;++ try {+ await navigator.share({ url: profileLink });+ } catch (error) {+ if (+ !(error instanceof DOMException && error.name == "AbortError")+ ) {+ log.warn("Failed to share Space invite link", error);+ }+ }};const deleteSelectedPost = async () => {@@ -1530,7 +1539,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({component="button"type="button"aria-label="Share invite"- onClick={shareProfile}+ onClick={() => void shareInvite()}sx={{alignItems: "center",bgcolor: "transparent",@@ -1907,46 +1916,11 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({type="button"disabled={isPostPhotoOpening}onClick={openPostPhotoPicker}- sx={{- appearance: "none",- alignItems: "center",- bgcolor: green,- border: 0,- borderRadius: "22px",- boxSizing: "border-box",- color: "#FFFFFF",- cursor: isPostPhotoOpening- ? "default"- : "pointer",- display: "inline-flex",- fontFamily:- '"Inter Variable", Inter, sans-serif',- fontSize: 15,- fontWeight: 700,- gap: "8px",- height: 48,- justifyContent: "center",- lineHeight: "20px",- px: "22px",- py: 0,- pointerEvents: "auto",- whiteSpace: "nowrap",- "& svg": {- display: "block",- flexShrink: 0,- },- "&:focus-visible": {- outline: `2px solid ${green}`,- outlineOffset: 3,- },- "&:hover": isPostPhotoOpening- ? undefined- : { bgcolor: "#07AE22" },- }}+ sx={spaceEmptyStateButtonSx}><HugeiconsIconicon={AddSquareIcon}- size={20}+ size={18}strokeWidth={1.8}/>Postdiff --git a/web/apps/space/src/services/home-posts.ts b/web/apps/space/src/services/home-posts.tsindex cd17340dff..6d36a9d964 100644--- a/web/apps/space/src/services/home-posts.ts+++ b/web/apps/space/src/services/home-posts.ts@@ -389,11 +389,21 @@ export const refreshSpaceHomePosts = async (refreshAfterCursor(savedState),);const latestPosts = latestPostsFor(result.items, currentFriendSpaceIdSet);+ const savedLatestPostsBySpace = new Map(+ savedState.latestPosts.map((post) => [post.spaceId, post]),+ );const returnedPostsByID = new Map(result.items.map((post) => [post.postId, post]),);const discoveredPosts = savedMarker- ? result.items.filter((post) => isAfter(post, savedMarker))+ ? result.items.filter((post) => {+ if (isAfter(post, savedMarker)) return true;+ if (latestPosts.get(post.spaceId)?.postId != post.postId) {+ return false;+ }+ const savedLatestPost = savedLatestPostsBySpace.get(post.spaceId);+ return !savedLatestPost || isAfter(post, savedLatestPost);+ }): [];const nextMarker = markerFromCursor(result.syncCursor);return updateState(viewerSpaceId, (state) => {diff --git a/web/apps/space/src/services/share-link.ts b/web/apps/space/src/services/share-link.tsindex 47f10ed0a3..1c20fed375 100644--- a/web/apps/space/src/services/share-link.ts+++ b/web/apps/space/src/services/share-link.ts@@ -1,27 +1,14 @@const openEvent = "ente-space-open-share-link";-export type SpaceShareLinkDialogMode = "invite" | "profile";--export const openSpaceShareLinkDialog = (- profileLink: string,- mode: SpaceShareLinkDialogMode = "profile",-) => {- window.dispatchEvent(- new CustomEvent(openEvent, { detail: { mode, profileLink } }),- );+export const openSpaceShareLinkDialog = (profileLink: string) => {+ window.dispatchEvent(new CustomEvent(openEvent, { detail: profileLink }));};export const onOpenSpaceShareLinkDialog = (- listener: (mode: SpaceShareLinkDialogMode, profileLink: string) => void,+ listener: (profileLink: string) => void,) => {const handleOpen = (event: Event) => {- const { mode, profileLink } = (- event as CustomEvent<{- mode: SpaceShareLinkDialogMode;- profileLink: string;- }>- ).detail;- listener(mode, profileLink);+ listener((event as CustomEvent<string>).detail);};window.addEventListener(openEvent, handleOpen);diff --git a/web/apps/space/src/styles/buttons.ts b/web/apps/space/src/styles/buttons.tsnew file mode 100644index 0000000000..558dbe5f2e--- /dev/null+++ b/web/apps/space/src/styles/buttons.ts@@ -0,0 +1,30 @@+import { spaceTouchTargetSize } from "styles/touch-targets";++const green = "#08C225";++export const spaceEmptyStateButtonSx = {+ alignItems: "center",+ appearance: "none",+ bgcolor: green,+ border: 0,+ borderRadius: "18px",+ boxSizing: "border-box",+ color: "#FFF",+ cursor: "pointer",+ display: "inline-flex",+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 14,+ fontWeight: 600,+ gap: "6px",+ height: spaceTouchTargetSize,+ justifyContent: "center",+ lineHeight: "18px",+ pointerEvents: "auto",+ px: "14px",+ py: 0,+ whiteSpace: "nowrap",+ "& svg": { display: "block", flexShrink: 0 },+ "&:disabled": { cursor: "default", opacity: 0.45 },+ "&:focus-visible": { outline: `2px solid ${green}`, outlineOffset: 2 },+ "&:hover:not(:disabled)": { bgcolor: "#07AE22" },+} as const;