Skip to content

armgit / ente

Public Imported from GitHub
0

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

Handle album sidebar browser back

Anand Baburajanc7903e873239
diff --git a/web/apps/albums/src/public-album/components/TripLayout/index.tsx b/web/apps/albums/src/public-album/components/TripLayout/index.tsx
index 8fbd6adda3..6b770ea988 100644
--- a/web/apps/albums/src/public-album/components/TripLayout/index.tsx
+++ b/web/apps/albums/src/public-album/components/TripLayout/index.tsx
@@ -608,18 +608,15 @@ export const TripLayout: React.FC<TripLayoutProps> = ({
<ActiveDownloadStatusNotifications fullWidthOnMobile />
{/* Public feed sidebar */}
- {publicFeedVisibilityProps.open &&
- collection &&
- credentials?.current &&
- collectionKey && (
- <LazyPublicFeedSidebar
- {...publicFeedVisibilityProps}
- files={files}
- credentials={credentials.current}
- collectionKey={collectionKey}
- onItemClick={handleFeedItemClick}
- />
- )}
+ {collection && credentials?.current && collectionKey && (
+ <LazyPublicFeedSidebar
+ {...publicFeedVisibilityProps}
+ files={files}
+ credentials={credentials.current}
+ collectionKey={collectionKey}
+ onItemClick={handleFeedItemClick}
+ />
+ )}
</TripLayoutContainer>
);
};
diff --git a/web/apps/albums/src/public-album/page/PublicAlbumPage.tsx b/web/apps/albums/src/public-album/page/PublicAlbumPage.tsx
index b43c95f050..ca5329f551 100644
--- a/web/apps/albums/src/public-album/page/PublicAlbumPage.tsx
+++ b/web/apps/albums/src/public-album/page/PublicAlbumPage.tsx
@@ -809,17 +809,15 @@ export default function PublicAlbumPage() {
/>
)}
<ActiveDownloadStatusNotifications fullWidthOnMobile />
- {publicFeedVisibilityProps.open &&
- publicCollection &&
- collectionKey.current && (
- <LazyPublicFeedSidebar
- {...publicFeedVisibilityProps}
- files={publicFiles}
- credentials={credentials.current}
- collectionKey={collectionKey.current}
- onItemClick={handleFeedItemClick}
- />
- )}
+ {publicCollection && collectionKey.current && (
+ <LazyPublicFeedSidebar
+ {...publicFeedVisibilityProps}
+ files={publicFiles}
+ credentials={credentials.current}
+ collectionKey={collectionKey.current}
+ onItemClick={handleFeedItemClick}
+ />
+ )}
</>
);
diff --git a/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
index e509f2517e..98654301c1 100644
--- a/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
@@ -9,6 +9,7 @@ import {
deletePublicReaction,
getStoredAnonIdentity,
} from "@/public-album/social/api/public-reaction";
+import { useBrowserBackClose } from "@/shared/hooks/useBrowserBackClose";
import { getAvatarColor } from "@/shared/utils/avatar-colors";
import CloseIcon from "@mui/icons-material/Close";
import {
@@ -469,6 +470,12 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
onJoinAlbum,
enableJoin = true,
}) => {
+ useBrowserBackClose({
+ open,
+ onClose,
+ stateKey: "__enteCommentsSidebarBackState",
+ });
+
const [commentText, setCommentText] = useState("");
const [replyingTo, setReplyingTo] = useState<Comment | null>(null);
const [contextMenu, setContextMenu] = useState<ContextMenuState | null>(
diff --git a/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx b/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
index 440e96feb2..4480153eb0 100644
--- a/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
@@ -1246,8 +1246,9 @@ export const FileViewer: React.FC<FileViewerProps> = ({
);
window.history.pushState(viewerState, "", window.location.href);
- const handlePopState = () => {
+ const handlePopState = (event: PopStateEvent) => {
if (browserBackStateRef.current != stateMarker) return;
+ if (hasFileViewerBackStateMarker(event.state, stateMarker)) return;
browserBackStateRef.current = undefined;
handleCloseRef.current();
};
diff --git a/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
index 5f10c6247f..5bed513799 100644
--- a/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
@@ -1,5 +1,6 @@
import { downloadManager } from "@/public-album/download/services/download-manager";
import { getStoredAnonIdentity } from "@/public-album/social/api/public-reaction";
+import { useBrowserBackClose } from "@/shared/hooks/useBrowserBackClose";
import { getAvatarColor } from "@/shared/utils/avatar-colors";
import CloseIcon from "@mui/icons-material/Close";
import {
@@ -155,6 +156,12 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
prefetchedUserIDToEmail,
anonUserNames,
}) => {
+ useBrowserBackClose({
+ open,
+ onClose,
+ stateKey: "__enteLikesSidebarBackState",
+ });
+
const [loading, setLoading] = useState(false);
const [collectionDropdownOpen, setCollectionDropdownOpen] = useState(false);
const showLoading = loading || isSocialDataLoading;
diff --git a/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
index 247c9b5484..fe25c69c18 100644
--- a/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
@@ -6,6 +6,7 @@ import {
type PublicFeedComment,
type PublicFeedReaction,
} from "@/public-album/social/api/public-reaction";
+import { useBrowserBackClose } from "@/shared/hooks/useBrowserBackClose";
import { getAvatarColor } from "@/shared/utils/avatar-colors";
import CloseIcon from "@mui/icons-material/Close";
import {
@@ -639,6 +640,12 @@ export const PublicFeedSidebar: React.FC<PublicFeedSidebarProps> = ({
collectionKey,
onItemClick,
}) => {
+ const { clearBrowserBackState } = useBrowserBackClose({
+ open,
+ onClose,
+ stateKey: "__entePublicFeedSidebarBackState",
+ });
+
const [thumbnailCache, setThumbnailCache] = useState<ThumbnailCache>(
new Map(),
);
@@ -842,12 +849,13 @@ export const PublicFeedSidebar: React.FC<PublicFeedSidebarProps> = ({
onClick={
onItemClick
? () => {
- onItemClick(
- getFeedItemClickInfo(
- item,
- anonUserNames,
- ),
+ const info = getFeedItemClickInfo(
+ item,
+ anonUserNames,
);
+ void clearBrowserBackState(
+ "back",
+ ).then(() => onItemClick(info));
}
: undefined
}
diff --git a/web/apps/albums/src/shared/hooks/useBrowserBackClose.ts b/web/apps/albums/src/shared/hooks/useBrowserBackClose.ts
new file mode 100644
index 0000000000..75f9eb75ad
--- /dev/null
+++ b/web/apps/albums/src/shared/hooks/useBrowserBackClose.ts
@@ -0,0 +1,132 @@
+import { useCallback, useEffect, useRef } from "react";
+
+type BrowserBackCleanupAction = "back" | "replace";
+
+const waitForNextPopState = () =>
+ new Promise<void>((resolve) => {
+ const handlePopState = () => resolve();
+
+ window.addEventListener("popstate", handlePopState, { once: true });
+ window.setTimeout(() => {
+ window.removeEventListener("popstate", handlePopState);
+ resolve();
+ }, 1000);
+ });
+
+const browserBackStateWithMarker = (
+ state: unknown,
+ stateKey: string,
+ marker: string,
+) =>
+ state && typeof state == "object"
+ ? { ...(state as Record<string, unknown>), [stateKey]: marker }
+ : { [stateKey]: marker };
+
+const browserBackStateHasMarker = (
+ state: unknown,
+ stateKey: string,
+ marker: string,
+) =>
+ !!state &&
+ typeof state == "object" &&
+ (state as Record<string, unknown>)[stateKey] == marker;
+
+const browserBackStateWithoutMarker = (state: unknown, stateKey: string) => {
+ if (!state || typeof state != "object") return state;
+
+ const next: Record<string, unknown> = {};
+ for (const [key, value] of Object.entries(
+ state as Record<string, unknown>,
+ )) {
+ if (key != stateKey) next[key] = value;
+ }
+ return next;
+};
+
+export interface UseBrowserBackCloseOptions {
+ open: boolean;
+ onClose: () => void;
+ stateKey: string;
+ enabled?: boolean;
+}
+
+/**
+ * Adds a transient history entry while an overlay is open, so mobile browser
+ * back gestures close the overlay before navigating away or closing a parent
+ * viewer.
+ */
+export const useBrowserBackClose = ({
+ open,
+ onClose,
+ stateKey,
+ enabled = true,
+}: UseBrowserBackCloseOptions) => {
+ const markerRef = useRef<string | undefined>(undefined);
+ const onCloseRef = useRef(onClose);
+ onCloseRef.current = onClose;
+
+ const clearBrowserBackState = useCallback(
+ (action: BrowserBackCleanupAction = "replace") => {
+ if (typeof window == "undefined") return Promise.resolve();
+
+ const marker = markerRef.current;
+ if (!marker) return Promise.resolve();
+
+ markerRef.current = undefined;
+
+ const latestHistoryState: unknown = window.history.state;
+ if (
+ !browserBackStateHasMarker(latestHistoryState, stateKey, marker)
+ ) {
+ return Promise.resolve();
+ }
+
+ if (action == "back") {
+ const popStatePromise = waitForNextPopState();
+ window.history.back();
+ return popStatePromise;
+ } else {
+ window.history.replaceState(
+ browserBackStateWithoutMarker(latestHistoryState, stateKey),
+ "",
+ window.location.href,
+ );
+ return Promise.resolve();
+ }
+ },
+ [stateKey],
+ );
+
+ useEffect(() => {
+ if (!open || !enabled || typeof window == "undefined") return;
+
+ const marker = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
+ markerRef.current = marker;
+
+ window.history.pushState(
+ browserBackStateWithMarker(window.history.state, stateKey, marker),
+ "",
+ window.location.href,
+ );
+
+ const handlePopState = (event: PopStateEvent) => {
+ if (markerRef.current != marker) return;
+ if (browserBackStateHasMarker(event.state, stateKey, marker)) {
+ return;
+ }
+
+ markerRef.current = undefined;
+ onCloseRef.current();
+ };
+
+ window.addEventListener("popstate", handlePopState);
+
+ return () => {
+ window.removeEventListener("popstate", handlePopState);
+ if (markerRef.current != marker) return;
+ void clearBrowserBackState("back");
+ };
+ }, [clearBrowserBackState, enabled, open, stateKey]);
+
+ return { clearBrowserBackState };
+};