π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix Space viewer handover and latest post motion
Anand Baburajan5d07ecb7324a
diff --git a/web/apps/space/package.json b/web/apps/space/package.jsonindex 6451f521ca..c823156904 100644--- a/web/apps/space/package.json+++ b/web/apps/space/package.json@@ -12,7 +12,6 @@"@fontsource/nunito": "5.3.0","@hugeicons/core-free-icons": "4.3.5","@hugeicons/react": "1.1.10",- "@rive-app/canvas-lite": "2.43.1","ente-accounts": "*","ente-base": "*","ente-media": "*",diff --git a/web/apps/space/public/images/like.riv b/web/apps/space/public/images/like.rivdeleted file mode 100644index 1f2cbec1e0..0000000000Binary files a/web/apps/space/public/images/like.riv and /dev/null differdiff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsxindex 866e00b750..2758877549 100644--- a/web/apps/space/src/components/FileViewer.tsx+++ b/web/apps/space/src/components/FileViewer.tsx@@ -23,7 +23,8 @@ import { SpaceBottomSheetTransition } from "components/BottomSheetTransition";import { SpaceCaptionText } from "components/CaptionText";import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";import { spacePostLikePopDurationMs } from "components/post-like-animation";-import { photoDotStep, SpacePostPhotosDots } from "components/PostPhotosDots";+import { paintStretchingDot } from "components/post-photos-dots";+import { SpacePostPhotosDots } from "components/PostPhotosDots";import { SpacePostReplyControls } from "components/PostReplyControls";import { createSpaceVideoContent } from "components/PostVideoContent";import { SpaceVideoError } from "components/VideoError";@@ -212,31 +213,18 @@ const viewerPadding = (hasDraftPhotoControls: boolean) => {};// index is the photo whose dots are shown, position the gallery index as a float.-const paintStretchingDot = (+const paintViewerPhotoDot = (root: HTMLElement,photos: SpaceViewerPhoto[],index: number,position: number,) => {- const dot = root.querySelector<HTMLElement>(- "[data-space-photo-dots-indicator]",- );const photo = photos[index];- if (!dot || !photo) return;+ if (!photo) return;const count = photo.postPhotoCount ?? 1;const neighbour = photos[position > index ? index + 1 : index - 1];const offset = neighbour?.postId == photo.postId ? position - index : 0;- const p = Math.min(- count - 1,- Math.max(0, (photo.postPhotoIndex ?? 0) + offset),- );- const from = Math.min(Math.floor(p), count - 2);- const head =- (from + Math.min(1, Math.max(0, (p - from) * 2))) * photoDotStep;- const tail =- (from + Math.min(1, Math.max(0, (p - from) * 2 - 1))) * photoDotStep;- dot.style.transform = `translate(${tail - 0.5}px, -50%)`;- dot.style.width = `${head - tail + 6}px`;+ paintStretchingDot(root, (photo.postPhotoIndex ?? 0) + offset, count);};interface ViewerMotion {@@ -251,6 +239,7 @@ const endOpenFlight = (motion: ViewerMotion, root: HTMLElement) => {motion.flight?.release();motion.flight = undefined;delete root.dataset.spaceViewerFlying;+ delete root.dataset.spaceViewerWaiting;setViewerBackground(root, 1);};@@ -260,21 +249,18 @@ const handOverOpenFlight = (motion: ViewerMotion,root: HTMLElement,pswp: PhotoSwipe | undefined,- attempt = 0,) => {if (!motion.flight || !motion.landed || !pswp) return;const content = pswp.currSlide?.content;+ const image = content?.element;const isShown =- !content?.element ||- content.isError() ||- (content.element.isConnected && !content.isDecoding);- if (!isShown && attempt < 120) {- requestAnimationFrame(() =>- handOverOpenFlight(motion, root, pswp, attempt + 1),- );- return;- }- endOpenFlight(motion, root);+ content?.isError() ||+ (image instanceof HTMLImageElement &&+ image.complete &&+ image.naturalWidth > 0 &&+ image.isConnected &&+ !content?.isDecoding);+ if (isShown) endOpenFlight(motion, root);};const viewerTouchPoint = (event: Event) => {@@ -581,7 +567,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({activePhoto.viewerLiked ?? false,);const [photoLikePopID, setPhotoLikePopID] = React.useState(0);- const [photoLikeToggleID, setPhotoLikeToggleID] = React.useState(0);const [caption, setCaption] = React.useState(activePhoto.caption ?? "");const [captionEditValue, setCaptionEditValue] = React.useState(activePhoto.caption ?? "",@@ -773,7 +758,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({motion.flight = undefined;motion.reveal?.();motion.reveal = undefined;- if (root) delete root.dataset.spaceViewerFlying;+ if (root) {+ delete root.dataset.spaceViewerFlying;+ delete root.dataset.spaceViewerWaiting;+ }const stop =root && item && origin && from && src? playViewerClose(@@ -819,7 +807,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({}const nextLiked = !isPhotoLiked;- setPhotoLikeToggleID((id) => id + 1);if (!activePhoto.postId || !onSetPostLiked) {setIsPhotoLiked(nextLiked);if (nextLiked) setPhotoLikePopID((id) => id + 1);@@ -1337,7 +1324,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({motion.stopClose = undefined;motion.reveal?.();motion.reveal = undefined;- if (root) delete root.dataset.spaceViewerFlying;+ if (root) {+ delete root.dataset.spaceViewerFlying;+ delete root.dataset.spaceViewerWaiting;+ }};const item = fallbackPhotoRef.current;if (!root || isDraftPost || item.video || !item.imageUrl)@@ -1356,6 +1346,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({(t) => setViewerBackground(root, viewerBackgroundOpacity(t)),() => {motion.landed = true;+ root.dataset.spaceViewerWaiting = "true";handOverOpenFlight(motion, root, pswpRef.current);},);@@ -1441,6 +1432,14 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({videos.get(content)?.destroy();videos.delete(content);});+ const handOver = () => {+ queueMicrotask(() => {+ if (!disposed)+ handOverOpenFlight(motionRef.current, root, pswp);+ });+ };+ pswp.on("contentAppendImage", handOver);+ pswp.on("loadComplete", handOver);pswp.on("destroy", () => {videos.forEach((video) => video.destroy());videos.clear();@@ -1555,7 +1554,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({? `${-offset * viewportSize.x * 0.4}px 0`: "";}- paintStretchingDot(+ paintViewerPhotoDot(root,viewerPhotosRef.current,currIndex,@@ -1751,6 +1750,8 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({},"&[data-space-viewer-flying='true'] :is([data-space-viewer-chrome='true'], [data-space-viewer-bottom='true'], [data-space-viewer-chrome='true'] *, [data-space-viewer-bottom='true'] *)":{ pointerEvents: "none !important" },+ "&[data-space-viewer-waiting='true'] [data-space-viewer-close], &[data-space-viewer-waiting='true'] [data-space-viewer-close] *":+ { pointerEvents: "auto !important" },"@media (prefers-reduced-motion: reduce)": {transition: "none",},@@ -2009,6 +2010,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({? "Cancel caption edit": "Close viewer"}+ data-space-viewer-closedisabled={isDraftPostActionRunning}onClick={closeViewer}sx={{ ...draftHeaderButtonSx, color: "#C0C0C0" }}@@ -2654,7 +2656,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({isReplyMode={isReplyMode}liked={isPhotoLiked}likePopID={photoLikePopID}- likeToggleID={photoLikeToggleID}onLike={handlePhotoLikeClick}onSendReply={sendReply}replyActionPhase={replyActionPhase}diff --git a/web/apps/space/src/components/PostLikeHeart.tsx b/web/apps/space/src/components/PostLikeHeart.tsxdeleted file mode 100644index d11c1ee39c..0000000000--- a/web/apps/space/src/components/PostLikeHeart.tsx+++ /dev/null@@ -1,168 +0,0 @@-import { Box } from "@mui/material";-import type { Rive, ViewModelInstance } from "@rive-app/canvas-lite";-import {- likeAnimationVariants,- pickLikeVariant,-} from "components/post-like-animation";-import log from "ente-base/log";-import React from "react";--// Shows the liked or unliked state without animating.-const jumpVariant = -1;-const likeAnimationMs = 1600;-// The 160px artboard draws a 22px heart; scale it to the 26px icon it replaces.-const canvasSize = Math.round((160 * 26) / 22);--let likeFile: Promise<ArrayBuffer> | undefined;-const loadLikeFile = () => {- if (!likeFile) {- likeFile = fetch("/images/like.riv").then((response) => {- if (!response.ok) throw new Error(`HTTP ${response.status}`);- return response.arrayBuffer();- });- void likeFile.catch(() => {- likeFile = undefined;- });- }- return likeFile;-};--interface SpacePostLikeHeartProps {- liked: boolean;- // Changes on each like or unlike. Other changes of liked do not animate.- toggleID: number;- fallback: React.ReactNode;-}--export const SpacePostLikeHeart: React.FC<SpacePostLikeHeartProps> = ({- liked,- toggleID,- fallback,-}) => {- const canvasRef = React.useRef<HTMLCanvasElement | null>(null);- const riveRef = React.useRef<Rive | undefined>(undefined);- const pauseTimerRef = React.useRef(0);- const shownRef = React.useRef({ liked, toggleID, variant: jumpVariant });- const [viewModel, setViewModel] = React.useState<ViewModelInstance>();-- // Rive draws every frame while playing, so pause it when idle.- const playFor = React.useCallback((ms: number) => {- const rive = riveRef.current;- if (!rive) return;- window.clearTimeout(pauseTimerRef.current);- rive.play();- pauseTimerRef.current = window.setTimeout(() => rive.pause(), ms);- }, []);-- React.useEffect(() => {- const canvas = canvasRef.current;- if (- !canvas ||- window.matchMedia("(prefers-reduced-motion: reduce)").matches- )- return;-- let disposed = false;- void Promise.all([import("@rive-app/canvas-lite"), loadLikeFile()])- .then(([{ Fit, Layout, Rive, RuntimeLoader }, file]) => {- if (disposed) return;- // The default is a public CDN, which the CSP blocks.- RuntimeLoader.setWasmUrl(- new URL("@rive-app/canvas-lite/rive.wasm", import.meta.url)- .href,- );- RuntimeLoader.setWasmFallbackUrl(null);- const rive = new Rive({- autoBind: true,- autoplay: true,- buffer: file.slice(0),- canvas,- enableRiveAssetCDN: false,- layout: new Layout({ fit: Fit.Contain }),- onLoad: () => {- if (disposed) return;- rive.resizeDrawingSurfaceToCanvas();- const instance = rive.viewModelInstance;- if (!instance) {- rive.cleanup();- riveRef.current = undefined;- return;- }- const variant = instance.number("variant");- const isLiked = instance.boolean("liked");- if (variant) variant.value = jumpVariant;- if (isLiked) isLiked.value = shownRef.current.liked;- playFor(150);- setViewModel(instance);- },- onLoadError: () =>- log.warn("Failed to load the like animation"),- stateMachine: "Like",- });- riveRef.current = rive;- })- .catch((error: unknown) => {- log.warn("Failed to load the like animation", error);- });- return () => {- disposed = true;- window.clearTimeout(pauseTimerRef.current);- riveRef.current?.cleanup();- riveRef.current = undefined;- };- }, [playFor]);-- React.useEffect(() => {- const shown = shownRef.current;- if (viewModel) {- const variant = viewModel.number("variant");- const isLiked = viewModel.boolean("liked");- const isToggle = toggleID != shown.toggleID;- if (!isToggle) {- if (variant) variant.value = jumpVariant;- } else if (liked) {- shown.variant = pickLikeVariant(shown.variant);- if (variant) variant.value = shown.variant;- } else if (variant && variant.value < 0) {- // A jump would skip the unlike animation.- variant.value = likeAnimationVariants[0]!;- }- if (isLiked) isLiked.value = liked;- playFor(isToggle ? likeAnimationMs : 150);- }- shown.liked = liked;- shown.toggleID = toggleID;- }, [liked, toggleID, viewModel, playFor]);-- return (- <Box- component="span"- sx={{ display: "flex", lineHeight: 0, position: "relative" }}- >- <Box- component="span"- sx={{- display: "flex",- visibility: viewModel ? "hidden" : "visible",- }}- >- {fallback}- </Box>- <Box- ref={canvasRef}- component="canvas"- aria-hidden- sx={{- height: canvasSize,- left: "50%",- pointerEvents: "none",- position: "absolute",- top: "50%",- transform: "translate(-50%, -50%)",- visibility: viewModel ? "visible" : "hidden",- width: canvasSize,- }}- />- </Box>- );-};diff --git a/web/apps/space/src/components/PostPhotosDots.tsx b/web/apps/space/src/components/PostPhotosDots.tsxindex 7173981335..e4f6037e24 100644--- a/web/apps/space/src/components/PostPhotosDots.tsx+++ b/web/apps/space/src/components/PostPhotosDots.tsx@@ -1,7 +1,5 @@import { Box } from "@mui/material";--// A 5px dot and the 6px gap after it.-export const photoDotStep = 11;+import { photoDotStep } from "components/post-photos-dots";export const SpacePostPhotosDots = ({index,diff --git a/web/apps/space/src/components/PostReplyControls.tsx b/web/apps/space/src/components/PostReplyControls.tsxindex 6175b5ad92..817cef2187 100644--- a/web/apps/space/src/components/PostReplyControls.tsx+++ b/web/apps/space/src/components/PostReplyControls.tsx@@ -14,7 +14,6 @@ import {spacePostLikePopDurationMs,spacePostLikePopTiming,} from "components/post-like-animation";-import { SpacePostLikeHeart } from "components/PostLikeHeart";import React from "react";import {spaceControlBackground,@@ -61,7 +60,6 @@ interface SpacePostReplyControlsProps {isReplyMode: boolean;liked: boolean;likePopID: number;- likeToggleID: number;onLike: () => void;onSendReply: () => void;replyActionPhase: SpaceActionPhase | null;@@ -76,7 +74,6 @@ export const SpacePostReplyControls: React.FC<SpacePostReplyControlsProps> = ({isReplyMode,liked,likePopID,- likeToggleID,onLike,onSendReply,replyActionPhase,@@ -239,38 +236,29 @@ export const SpacePostReplyControls: React.FC<SpacePostReplyControlsProps> = ({/>)) : (- <SpacePostLikeHeart- liked={liked}- toggleID={likeToggleID}- fallback={- <Box- key={- isLikePopping- ? `heart-${likePopID}`- : "heart"- }- component="span"- sx={{- animation: isLikePopping- ? `${spacePostLikeHeartPop} ${spacePostLikePopDurationMs}ms ${spacePostLikePopTiming} both`- : undefined,- display: "flex",- lineHeight: 0,- transformOrigin: "50% 58%",- "@media (prefers-reduced-motion: reduce)":- { animation: "none" },- }}- >- <HugeiconsIcon- fill={liked ? green : "none"}- icon={FavouriteIcon}- primaryColor={liked ? green : undefined}- size={26}- strokeWidth={1.8}- />- </Box>- }- />+ <Box+ key={isLikePopping ? `heart-${likePopID}` : "heart"}+ component="span"+ sx={{+ animation: isLikePopping+ ? `${spacePostLikeHeartPop} ${spacePostLikePopDurationMs}ms ${spacePostLikePopTiming} both`+ : undefined,+ display: "flex",+ lineHeight: 0,+ transformOrigin: "50% 58%",+ "@media (prefers-reduced-motion: reduce)": {+ animation: "none",+ },+ }}+ >+ <HugeiconsIcon+ fill={liked ? green : "none"}+ icon={FavouriteIcon}+ primaryColor={liked ? green : undefined}+ size={26}+ strokeWidth={1.8}+ />+ </Box>)}</Box></Box>diff --git a/web/apps/space/src/components/post-like-animation.ts b/web/apps/space/src/components/post-like-animation.tsindex 60052ffcdd..d064cd104d 100644--- a/web/apps/space/src/components/post-like-animation.ts+++ b/web/apps/space/src/components/post-like-animation.ts@@ -1,15 +1,5 @@import { keyframes } from "@mui/material/styles";-// The burst, draw and flip animations in like.riv.-export const likeAnimationVariants = [0, 2, 4];--export const pickLikeVariant = (previous: number, random = Math.random) => {- const choices = likeAnimationVariants.filter(- (variant) => variant != previous,- );- return choices[Math.floor(random() * choices.length)]!;-};-export const spacePostLikePopDurationMs = 520;export const spacePostLikePopTiming = "cubic-bezier(0.2, 0.82, 0.22, 1)";diff --git a/web/apps/space/src/components/post-photos-dots.ts b/web/apps/space/src/components/post-photos-dots.tsnew file mode 100644index 0000000000..51d29ba628--- /dev/null+++ b/web/apps/space/src/components/post-photos-dots.ts@@ -0,0 +1,20 @@+export const photoDotStep = 11;++export const paintStretchingDot = (+ root: HTMLElement,+ position: number,+ count: number,+) => {+ const dot = root.querySelector<HTMLElement>(+ "[data-space-photo-dots-indicator]",+ );+ if (!dot) return;+ const p = Math.min(count - 1, Math.max(0, position));+ const from = Math.min(Math.floor(p), count - 2);+ const head =+ (from + Math.min(1, Math.max(0, (p - from) * 2))) * photoDotStep;+ const tail =+ (from + Math.min(1, Math.max(0, (p - from) * 2 - 1))) * photoDotStep;+ dot.style.transform = `translate(${tail - 0.5}px, -50%)`;+ dot.style.width = `${head - tail + 6}px`;+};diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsxindex 6a6a4d9a76..78fb1c6e23 100644--- a/web/apps/space/src/screens/HomeScreen.tsx+++ b/web/apps/space/src/screens/HomeScreen.tsx@@ -27,6 +27,7 @@ import {spacePostLikePopDurationMs,spacePostLikePopTiming,} from "components/post-like-animation";+import { paintStretchingDot } from "components/post-photos-dots";import { SpacePostAvatar } from "components/PostAvatar";import { SpacePostGrid } from "components/PostGrid";import { SpacePostPhotosCounter } from "components/PostPhotosCounter";@@ -36,6 +37,7 @@ import { SpacePWAInstallPrompt } from "components/PWAInstallPrompt";import { SpaceLoadingSpinner } from "components/RouteFallback";import { SpaceShareInviteButton } from "components/ShareInviteButton";import { SpaceSkipLink } from "components/SkipLink";+import { viewerOriginKey } from "components/viewer-transition";import log from "ente-base/log";import { useBrowserBackClose } from "hooks/use-browser-back-close";import { useDecodedImage } from "hooks/use-decoded-image";@@ -617,6 +619,7 @@ const FeedPhotoOverlay: React.FC<{index={photoIndex}count={photoCount}activeColor={green}+ stretches/>{caption && (<Box title={caption} sx={{ minWidth: 0, width: "100%" }}>@@ -640,6 +643,7 @@ const FeedPhoto: React.FC<{shouldLoad: boolean;shouldPreloadVideo: boolean;thumbHash?: string;+ viewerOrigin?: string;}> = ({imageUrl,isActive,@@ -653,6 +657,7 @@ const FeedPhoto: React.FC<{shouldPreloadVideo,thumbHash,video,+ viewerOrigin,}) => {const decodedPhoto = useDecodedImage(imageUrl, true);const isPostUnavailable = isUnavailable || Boolean(decodedPhoto.failed);@@ -695,6 +700,7 @@ const FeedPhoto: React.FC<{}disabled={video ? undefined : !canOpenPhoto}tabIndex={!video && isActive ? 0 : -1}+ data-space-viewer-origin={viewerOrigin}onClick={video ? undefined : onOpenPhoto}onFocus={(event: React.FocusEvent<HTMLElement>) => {if (video && event.target == event.currentTarget)@@ -706,6 +712,7 @@ const FeedPhoto: React.FC<{appearance: "none",bgcolor: "transparent",border: 0,+ borderRadius: "inherit",cursor: canOpenPhoto ? "pointer" : "default",display: "block",height: "100%",@@ -1347,6 +1354,11 @@ const FeedItem: React.FC<FeedItemProps> = ({}}onScroll={(event) => {const carousel = event.currentTarget;+ paintStretchingDot(+ rootRef.current!,+ carousel.scrollLeft / carousel.clientWidth,+ photoCount,+ );const index = Math.round(carousel.scrollLeft / carousel.clientWidth,);@@ -1374,6 +1386,7 @@ const FeedItem: React.FC<FeedItemProps> = ({scrollToPhoto(index);}}sx={{+ borderRadius: "inherit",display: "flex",height: "100%",overflowX: "hidden",@@ -1414,6 +1427,11 @@ const FeedItem: React.FC<FeedItemProps> = ({shouldPreloadVideo={shouldPreloadVideo}thumbHash={photo.thumbHash}video={photo.video}+ viewerOrigin={+ index == photoIndex+ ? viewerOriginKey(spaceId, postId)+ : undefined+ }/>))}</Box>diff --git a/web/apps/space/tests/post-like-animation.test.ts b/web/apps/space/tests/post-like-animation.test.tsdeleted file mode 100644index 7b5856ce53..0000000000--- a/web/apps/space/tests/post-like-animation.test.ts+++ /dev/null@@ -1,18 +0,0 @@-import { expect, test } from "vitest";-import { pickLikeVariant } from "../src/components/post-like-animation";--test("a like never plays the same animation twice in a row", () => {- for (const previous of [0, 2, 4]) {- for (const random of [0, 0.4, 0.99]) {- expect(pickLikeVariant(previous, () => random)).not.toBe(previous);- }- }-});--test("the first like can play any of the three animations", () => {- const picked = new Set(- [0, 0.4, 0.99].map((random) => pickLikeVariant(-1, () => random)),- );-- expect(picked).toEqual(new Set([0, 2, 4]));-});diff --git a/web/package-lock.json b/web/package-lock.jsonindex f35eb339d2..76be026854 100644--- a/web/package-lock.json+++ b/web/package-lock.json@@ -352,7 +352,6 @@"@fontsource/nunito": "5.3.0","@hugeicons/core-free-icons": "4.3.5","@hugeicons/react": "1.1.10",- "@rive-app/canvas-lite": "2.43.1","ente-accounts": "*","ente-base": "*","ente-media": "*",@@ -3263,12 +3262,6 @@"react-dom": "^19.0.0"}},- "node_modules/@rive-app/canvas-lite": {- "version": "2.43.1",- "resolved": "https://registry.npmjs.org/@rive-app/canvas-lite/-/canvas-lite-2.43.1.tgz",- "integrity": "sha512-6Hb9J3Yfz3JKgLYv1pU/K14kEpj6RUAFKJV/lI1LmogFsLJibA69TQLIk6NoNUUTDmrbqGc4/8ZrEmhpwf0Z9Q==",- "license": "MIT"- },"node_modules/@rolldown/binding-android-arm-eabi": {"version": "1.2.12","resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.12.tgz",