Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

Show cached Space posts before network requests

Anand Baburajand6791e5b509f
diff --git a/web/apps/space/src/components/OwnPostTile.tsx b/web/apps/space/src/components/OwnPostTile.tsx
index 377ea15ced..f6e9e26c7e 100644
--- a/web/apps/space/src/components/OwnPostTile.tsx
+++ b/web/apps/space/src/components/OwnPostTile.tsx
@@ -136,9 +136,18 @@ export const SpaceOwnPostTile: React.FC<SpaceOwnPostTileProps> = ({
);
const loading =
isLoading || Boolean(post?.imageAsset && !imageUrl && !unavailable);
+ const [showSkeleton, setShowSkeleton] = React.useState(false);
const isEmpty = !post && !loading && !unavailable;
const caption = post?.caption?.trim();
+ React.useEffect(() => {
+ setShowSkeleton(false);
+ if (!loading) return;
+
+ const timeoutID = window.setTimeout(() => setShowSkeleton(true), 200);
+ return () => window.clearTimeout(timeoutID);
+ }, [loading]);
+
React.useEffect(() => {
if (hasPublication)
tileRef.current?.scrollIntoView({ block: "nearest" });
@@ -178,6 +187,9 @@ export const SpaceOwnPostTile: React.FC<SpaceOwnPostTileProps> = ({
height: groupHeight,
minWidth: 0,
p: `${groupInset}px`,
+ "& .MuiSkeleton-root": {
+ visibility: showSkeleton ? "visible" : "hidden",
+ },
}}
>
<Box
@@ -276,6 +288,11 @@ export const SpaceOwnPostTile: React.FC<SpaceOwnPostTileProps> = ({
phase={publishPhase}
expiresAtMs={postPublication?.statusExpiresAtMs}
/>
+ ) : loading ? (
+ <Skeleton
+ width={72}
+ sx={{ fontSize: 12, lineHeight: "18px" }}
+ />
) : (
<Box
component="span"
@@ -288,11 +305,9 @@ export const SpaceOwnPostTile: React.FC<SpaceOwnPostTileProps> = ({
whiteSpace: "nowrap",
}}
>
- {loading
- ? "Loading…"
- : unavailable
- ? "Post unavailable"
- : "Share your first post."}
+ {unavailable
+ ? "Post unavailable"
+ : "Share your first post."}
</Box>
)}
<Box
@@ -310,9 +325,13 @@ export const SpaceOwnPostTile: React.FC<SpaceOwnPostTileProps> = ({
whiteSpace: "nowrap",
}}
>
- {isEmpty
- ? "What are you up to?"
- : caption || "No caption"}
+ {!post && loading ? (
+ <Skeleton width="70%" />
+ ) : isEmpty ? (
+ "What are you up to?"
+ ) : post ? (
+ caption || "No caption"
+ ) : null}
</Box>
</Box>
</Box>
diff --git a/web/apps/space/src/hooks/use-own-latest-post.ts b/web/apps/space/src/hooks/use-own-latest-post.ts
index b66d4b37c7..b857395720 100644
--- a/web/apps/space/src/hooks/use-own-latest-post.ts
+++ b/web/apps/space/src/hooks/use-own-latest-post.ts
@@ -1,13 +1,15 @@
import log from "ente-base/log";
import { useEffect, useState } from "react";
+import { savedSpaceOwnedSpaces } from "services/persistent-session";
import { loadCachedOwnLatestPost } from "services/post-cache";
+import { loadExistingSpaceId } from "services/profile";
import {
loadCurrentSpaceProfilePostsPage,
type SpacePost,
} from "services/space";
import { useSpaceAppState } from "state/app-state";
-export const useOwnLatestPost = (spaceId: string | undefined) => {
+export const useOwnLatestPost = () => {
const { postPublication } = useSpaceAppState();
const isPublishingPost = postPublication?.phase == "posting";
const [ownLatestPost, setOwnLatestPost] = useState<SpacePost>();
@@ -16,26 +18,39 @@ export const useOwnLatestPost = (spaceId: string | undefined) => {
useState(false);
useEffect(() => {
- setOwnLatestPost(undefined);
setIsOwnLatestPostLoading(true);
setIsOwnLatestPostUnavailable(false);
- if (!spaceId || isPublishingPost) return;
+ if (isPublishingPost) return;
let cancelled = false;
+ const isCancelled = () => cancelled;
let cachedPost: SpacePost | undefined;
- void loadCachedOwnLatestPost(spaceId)
- .then((post) => {
- if (cancelled) return;
- cachedPost = post;
- if (post) {
- setOwnLatestPost(post);
+ void (async () => {
+ const cachedSpaceId = savedSpaceOwnedSpaces()?.[0]?.spaceId;
+ if (cachedSpaceId) {
+ cachedPost = await loadCachedOwnLatestPost(cachedSpaceId);
+ if (isCancelled()) return;
+ if (cachedPost) {
+ setOwnLatestPost(cachedPost);
setIsOwnLatestPostLoading(false);
}
- return loadCurrentSpaceProfilePostsPage(spaceId, spaceId);
- })
- .then((page) => {
- if (!cancelled && page) setOwnLatestPost(page.items[0]);
- })
+ }
+
+ const spaceId = await loadExistingSpaceId();
+ if (isCancelled()) return;
+ if (cachedPost?.spaceId != spaceId) {
+ cachedPost = undefined;
+ setOwnLatestPost(undefined);
+ setIsOwnLatestPostLoading(true);
+ }
+ if (!spaceId) return;
+
+ const page = await loadCurrentSpaceProfilePostsPage(
+ spaceId,
+ spaceId,
+ );
+ if (!isCancelled()) setOwnLatestPost(page.items[0]);
+ })()
.catch((error: unknown) => {
log.error("Failed to load own latest Space post", error);
if (!cancelled && !cachedPost)
@@ -47,7 +62,7 @@ export const useOwnLatestPost = (spaceId: string | undefined) => {
return () => {
cancelled = true;
};
- }, [spaceId, isPublishingPost]);
+ }, [isPublishingPost]);
return {
ownLatestPost: postPublication?.post ?? ownLatestPost,
diff --git a/web/apps/space/src/pages/app/index.tsx b/web/apps/space/src/pages/app/index.tsx
index e60f6343c3..e4c41de91e 100644
--- a/web/apps/space/src/pages/app/index.tsx
+++ b/web/apps/space/src/pages/app/index.tsx
@@ -66,7 +66,7 @@ const Page: React.FC = () => {
ownLatestPost,
isOwnLatestPostLoading,
isOwnLatestPostUnavailable,
- } = useOwnLatestPost(spaceId);
+ } = useOwnLatestPost();
const [unreadPosts, setUnreadPosts] = useState<SpacePost[]>([]);
const [hasUnreadMessages, setHasUnreadMessages] = useState<boolean>();
const [isLatestPostsLoading, setIsLatestPostsLoading] = useState(true);
diff --git a/web/apps/space/src/services/space.ts b/web/apps/space/src/services/space.ts
index 2d4f051c27..f55a0b211a 100644
--- a/web/apps/space/src/services/space.ts
+++ b/web/apps/space/src/services/space.ts
@@ -1268,6 +1268,11 @@ export const loadCurrentSpacePost = async (
export const loadCurrentSpacePostAssetURL: SpacePostAssetURLLoader = async (
asset,
) => {
+ const cachedURL = await cachedSpaceMediaBlobURLIfPresent(
+ postAssetCacheKey(asset),
+ );
+ if (cachedURL) return cachedURL;
+
const profile = await loadExistingSpaceProfile();
const ctx = await ensureCurrentSpaceContext();
try {