Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix photo viewer reopening and polish conversations

Anand Baburajand330046b1679
diff --git a/web/apps/space/src/components/ConversationPokeButton.tsx b/web/apps/space/src/components/ConversationPokeButton.tsx
index 43e3bb782c..c381e41008 100644
--- a/web/apps/space/src/components/ConversationPokeButton.tsx
+++ b/web/apps/space/src/components/ConversationPokeButton.tsx
@@ -73,7 +73,6 @@ export const SpaceConversationPokeButton = ({
fontWeight: 600,
height: spaceTouchTargetSize,
justifyContent: "flex-end",
- opacity: disabled ? 0.5 : 1,
p: 0,
pr: "4px",
width: "100%",
diff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsx
index cd1ba29441..31e46d582c 100644
--- a/web/apps/space/src/components/FileViewer.tsx
+++ b/web/apps/space/src/components/FileViewer.tsx
@@ -243,7 +243,6 @@ interface ViewerMotion {
landed: boolean;
flight?: ReturnType<typeof playViewerOpen>;
reveal?: () => void;
- stopClose?: () => void;
}
const endOpenFlight = (motion: ViewerMotion, root: HTMLElement) => {
@@ -778,36 +777,24 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
delete root.dataset.spaceViewerFlying;
delete root.dataset.spaceViewerWaiting;
}
- const stop =
- root && item && origin && from && src
- ? playViewerClose(
- root,
- {
- origin,
- src,
- photo: {
- width: item.width ?? defaultPhotoWidth,
- height: item.height ?? defaultPhotoHeight,
- },
- },
- from,
- throwVelocity,
- (t) =>
- setViewerBackground(
- root,
- startBackground * viewerBackgroundOpacity(t),
- ),
- () => onCloseRef.current(),
- )
- : undefined;
- if (!stop || !root) {
- onCloseRef.current();
- return;
+ if (root && item && origin && from && src) {
+ playViewerClose(
+ root,
+ {
+ origin,
+ src,
+ photo: {
+ width: item.width ?? defaultPhotoWidth,
+ height: item.height ?? defaultPhotoHeight,
+ },
+ },
+ from,
+ throwVelocity,
+ startBackground,
+ );
}
motion.closing = true;
- motion.stopClose = stop;
- root.dataset.spaceViewerFlying = "true";
- root.dataset.spaceViewerLeaving = "true";
+ onCloseRef.current();
};
React.useImperativeHandle(
@@ -1368,8 +1355,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
const stopMotion = () => {
motion.flight?.stop();
motion.flight = undefined;
- motion.stopClose?.();
- motion.stopClose = undefined;
motion.reveal?.();
motion.reveal = undefined;
if (root) {
@@ -1822,8 +1807,6 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
opacity:
"calc((var(--space-viewer-bg-opacity, 1) - 0.5) * 2)",
},
- "&[data-space-viewer-leaving='true'] :is([data-space-viewer-chrome='true'], [data-space-viewer-bottom='true'], [data-space-viewer-close])":
- { opacity: 0, transition: "opacity 140ms ease" },
// PhotoSwipe sets an inline opacity, so hide it with visibility.
"&[data-space-viewer-flying='true'] .pswp": {
visibility: "hidden !important",
diff --git a/web/apps/space/src/components/FriendProfileSheet.tsx b/web/apps/space/src/components/FriendProfileSheet.tsx
index 20296d2bdd..8392c90caf 100644
--- a/web/apps/space/src/components/FriendProfileSheet.tsx
+++ b/web/apps/space/src/components/FriendProfileSheet.tsx
@@ -191,7 +191,7 @@ export const FriendProfileSheet: React.FC<FriendProfileSheetProps> = ({
<Box
ref={rootRef}
sx={{
- "--space-profile-post-loading-top": "70%",
+ "--space-profile-post-loading-top": "50%",
...(expanded
? { position: "relative" }
: { inset: 0, position: "fixed", zIndex: 1300 }),
diff --git a/web/apps/space/src/components/MessagesPage.tsx b/web/apps/space/src/components/MessagesPage.tsx
index 9be2afa564..b0439172cf 100644
--- a/web/apps/space/src/components/MessagesPage.tsx
+++ b/web/apps/space/src/components/MessagesPage.tsx
@@ -103,6 +103,7 @@ export const SpaceMessagesPage: React.FC<SpaceMessagesPageProps> = ({
}) => {
const router = useSpaceRouter();
const {
+ friends,
postPublication,
profile,
profileLoadError,
@@ -151,11 +152,11 @@ export const SpaceMessagesPage: React.FC<SpaceMessagesPageProps> = ({
const selectedFriendFromFriends = React.useMemo(
() =>
selectedSpaceId
- ? conversationFriends.find(
+ ? friends.find(
(friend) => friendSpaceId(friend) == selectedSpaceId,
)
: undefined,
- [conversationFriends, selectedSpaceId],
+ [friends, selectedSpaceId],
);
const selectedLoadedFriendProfile =
selectedFriendProfile &&
diff --git a/web/apps/space/src/components/viewer-transition.ts b/web/apps/space/src/components/viewer-transition.ts
index b0ef361535..6e3861e25c 100644
--- a/web/apps/space/src/components/viewer-transition.ts
+++ b/web/apps/space/src/components/viewer-transition.ts
@@ -14,6 +14,7 @@ const openDuration = 0.6;
const openBounce = 0.26;
const closeDuration = 0.46;
const swapFadeMs = 220;
+const closingFlights = new WeakMap<HTMLElement, () => void>();
export const viewerOriginKey = (spaceId?: string, postId?: number) =>
spaceId && postId != undefined ? `${spaceId}:${postId}` : undefined;
@@ -273,6 +274,7 @@ export const playViewerOpen = (
setProgress: (t: number) => void,
onLanded: () => void,
) => {
+ closingFlights.get(origin)?.();
if (prefersReducedMotion() || !isOnScreen(origin)) return undefined;
const from = tilePhotoState(
@@ -363,15 +365,24 @@ export const playViewerOpen = (
// throwVelocity is in px per ms. A photo other than the tile's fades into it.
export const playViewerClose = (
- root: HTMLElement,
+ viewerRoot: HTMLElement,
{ origin, src, photo }: ViewerFlightTarget,
from: ViewerPhotoState,
throwVelocity: { x: number; y: number },
- setProgress: (t: number) => void,
- onDone: () => void,
+ startBackground: number,
) => {
if (prefersReducedMotion() || !isOnScreen(origin)) return undefined;
+ const root = document.createElement("div");
+ root.setAttribute("aria-hidden", "true");
+ root.inert = true;
+ Object.assign(root.style, {
+ inset: "0",
+ pointerEvents: "none",
+ position: "fixed",
+ zIndex: "1299",
+ });
+ viewerRoot.after(root);
const end = tilePhotoState(
rectOf(origin),
photo,
@@ -383,6 +394,7 @@ export const playViewerClose = (
const state = mixPhotoState(end, from, t);
draw(state);
rider?.draw(state, t);
+ root.style.backgroundColor = `rgb(0 0 0 / ${startBackground * viewerBackgroundOpacity(t)})`;
};
origin.style.opacity = "0";
drawAt(1);
@@ -402,11 +414,15 @@ export const playViewerClose = (
"img:not([aria-hidden])",
);
const isTilePhoto = tileImage?.currentSrc == src || tileImage?.src == src;
+ let fade: Animation | undefined;
const cleanUp = () => {
+ fade?.cancel();
origin.style.opacity = "";
rider?.remove();
rider?.reveal();
+ root.remove();
+ closingFlights.delete(origin);
};
const stopSpring = runSpring({
from: 1,
@@ -415,49 +431,24 @@ export const playViewerClose = (
duration: closeDuration,
bounce: 0,
restDelta: 0.5 / Math.max(1, photoStateTravel(from, end)),
- onUpdate: (t) => {
- drawAt(t);
- setProgress(t);
- },
+ onUpdate: drawAt,
onDone: () => {
- cleanUp();
+ origin.style.opacity = "";
+ rider?.reveal();
if (isTilePhoto) {
- flyer.remove();
+ cleanUp();
} else {
- // The viewer unmounts next, so fade a copy.
- const ghost = flyer.cloneNode(true) as HTMLElement;
- Object.assign(ghost.style, {
- position: "fixed",
- zIndex: "1301",
+ fade = root.animate([{ opacity: 1 }, { opacity: 0 }], {
+ duration: swapFadeMs,
+ easing: "ease",
+ fill: "forwards",
});
- document.body.append(ghost);
- const overlay = rider?.element.cloneNode(true) as
- HTMLElement | undefined;
- if (overlay) {
- Object.assign(overlay.style, {
- position: "fixed",
- zIndex: "1302",
- });
- document.body.append(overlay);
- }
- flyer.remove();
- ghost
- .animate([{ opacity: 1 }, { opacity: 0 }], {
- duration: swapFadeMs,
- easing: "ease",
- fill: "forwards",
- })
- .addEventListener("finish", () => {
- ghost.remove();
- overlay?.remove();
- });
+ fade.addEventListener("finish", cleanUp, { once: true });
}
- onDone();
},
});
- return () => {
+ closingFlights.set(origin, () => {
stopSpring();
cleanUp();
- flyer.remove();
- };
+ });
};
diff --git a/web/apps/space/src/screens/MessagesScreen.tsx b/web/apps/space/src/screens/MessagesScreen.tsx
index dc2e352307..2b5ec84c13 100644
--- a/web/apps/space/src/screens/MessagesScreen.tsx
+++ b/web/apps/space/src/screens/MessagesScreen.tsx
@@ -2269,7 +2269,10 @@ export const MessagesScreen: React.FC<MessagesScreenProps> = ({
>
<Avatar
avatarUrl={selectedFriend.avatarUrl}
- loading={isThreadRecipientLoading}
+ loading={
+ isThreadRecipientLoading &&
+ !selectedFriend.avatarUrl
+ }
size={36}
/>
<Box
diff --git a/web/apps/space/tests/viewer-transition.test.ts b/web/apps/space/tests/viewer-transition.test.ts
index 5be5628a1e..3930e85bd5 100644
--- a/web/apps/space/tests/viewer-transition.test.ts
+++ b/web/apps/space/tests/viewer-transition.test.ts
@@ -1,8 +1,10 @@
import { afterEach, expect, test, vi } from "vitest";
import {
+ playViewerClose,
playViewerOpen,
viewerOriginKey,
} from "../src/components/viewer-transition";
+import { fullPhotoState } from "../src/utils/viewer-geometry";
afterEach(() => vi.unstubAllGlobals());
@@ -55,7 +57,9 @@ const pendingFlight = () => {
borderBottomRightRadius: "0px",
borderBottomLeftRadius: "0px",
}));
- const requestFrame = vi.fn(() => 1);
+ const requestFrame = vi.fn<(callback: FrameRequestCallback) => number>(
+ () => 1,
+ );
vi.stubGlobal("requestAnimationFrame", requestFrame);
vi.stubGlobal("cancelAnimationFrame", vi.fn());
const onLanded = vi.fn();
@@ -122,3 +126,103 @@ test("a failed flyer leaves the tile visible and allows viewer handover", async
expect(onLanded).toHaveBeenCalledOnce();
flight.stop();
});
+
+const closingFlight = (src = "full-photo") => {
+ const { flight, origin, requestFrame } = pendingFlight();
+ flight.stop();
+ vi.mocked(cancelAnimationFrame).mockClear();
+ origin.querySelector = (selector) =>
+ selector.startsWith("img") ? { currentSrc: "full-photo" } : null;
+ const fade = { cancel: vi.fn(), addEventListener: vi.fn() };
+ const elements: {
+ style: Record<string, string>;
+ inert: boolean;
+ remove: ReturnType<typeof vi.fn>;
+ animate: ReturnType<typeof vi.fn>;
+ }[] = [];
+ const after = vi.fn();
+ vi.stubGlobal("document", {
+ createElement: () => {
+ const element = {
+ style: {} as Record<string, string>,
+ inert: false,
+ setAttribute: vi.fn(),
+ append: vi.fn(),
+ prepend: vi.fn(),
+ remove: vi.fn(),
+ decode: () => Promise.resolve(),
+ animate: vi.fn(() => fade),
+ };
+ elements.push(element);
+ return element;
+ },
+ });
+ let nextFrame: FrameRequestCallback | undefined;
+ let now = performance.now();
+ requestFrame.mockImplementation((callback: FrameRequestCallback) => {
+ nextFrame = callback;
+ return 1;
+ });
+ const target = {
+ origin: origin as unknown as HTMLElement,
+ src,
+ photo: { width: 200, height: 100 },
+ };
+ const rect = { x: 0, y: 100, w: 390, h: 195 };
+ playViewerClose(
+ { after } as unknown as HTMLElement,
+ target,
+ fullPhotoState(rect),
+ { x: 0, y: 0 },
+ 1,
+ );
+ const finishSpring = () => {
+ for (let i = 0; nextFrame && i < 100; i++) {
+ const frame = nextFrame;
+ nextFrame = undefined;
+ now += 16;
+ frame(now);
+ }
+ expect(nextFrame).toBeUndefined();
+ };
+ const reopen = () =>
+ playViewerOpen(
+ { dataset: {}, prepend: vi.fn() } as unknown as HTMLElement,
+ target,
+ rect,
+ vi.fn(),
+ vi.fn(),
+ )!;
+ return { root: elements[0]!, after, origin, fade, finishSpring, reopen };
+};
+
+test("closing animates outside the viewer without intercepting feed taps", () => {
+ const { root, after, origin, finishSpring } = closingFlight();
+ expect(after).toHaveBeenCalledWith(root);
+ expect(root.style.pointerEvents).toBe("none");
+ expect(root.inert).toBe(true);
+ expect(origin.style.opacity).toBe("0");
+ finishSpring();
+ expect(root.remove).toHaveBeenCalledOnce();
+ expect(origin.style.opacity).toBe("");
+});
+
+test("reopening a photo cancels its unfinished close animation", () => {
+ const { root, origin, reopen } = closingFlight();
+ const flight = reopen();
+ expect(root.remove).toHaveBeenCalledOnce();
+ expect(cancelAnimationFrame).toHaveBeenCalledExactlyOnceWith(1);
+ expect(origin.style.opacity).toBe("");
+ flight.stop();
+});
+
+test("reopening during the thumbnail fade cancels the old overlay", () => {
+ const { root, fade, finishSpring, reopen } = closingFlight("other-photo");
+ finishSpring();
+ expect(root.animate).toHaveBeenCalledOnce();
+ expect(root.remove).not.toHaveBeenCalled();
+ const flight = reopen();
+ expect(fade.cancel).toHaveBeenCalledOnce();
+ expect(root.remove).toHaveBeenCalledOnce();
+ flight.stop();
+});