Skip to content

armgit / ente

Public Imported from GitHub
0

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

Improve space post publish feedback

Anand Baburajanec89d0bba797
diff --git a/web/apps/space/src/pages/app/index.tsx b/web/apps/space/src/pages/app/index.tsx
index 2cfae1dbf4..da86690d44 100644
--- a/web/apps/space/src/pages/app/index.tsx
+++ b/web/apps/space/src/pages/app/index.tsx
@@ -18,7 +18,10 @@ import {
} from "services/space";
import { consumeAcceptedSpaceInviteFriend } from "services/spaceInvite";
import { useSpaceAppState } from "state/spaceAppState";
-import { createLocalFeedPostID } from "utils/localFeedPost";
+import {
+ confirmLocalFeedPost,
+ createLocalFeedPostID,
+} from "utils/localFeedPost";
import { firstNameFrom } from "utils/spaceDisplay";
import { prepareSpacePostImageFromEdit } from "utils/spacePostImage";
import { spaceRoutes } from "utils/spaceRoutes";
@@ -174,6 +177,9 @@ const Page: React.FC = () => {
friendID: spaceId,
height: image.height,
id: localPostId,
+ imageUrl:
+ image.previewUrl ||
+ URL.createObjectURL(image.file),
name: displayName || "You",
spaceId,
status: "pending",
@@ -197,12 +203,10 @@ const Page: React.FC = () => {
width: preparedImage.width,
});
if (!post) throw new Error("Couldn't create post.");
- setLocalFeedPosts((currentPosts) =>
- currentPosts.map((item) =>
- item.id == localPostId
- ? { id: localPostId, post, status: "ready" }
- : item,
- ),
+ confirmLocalFeedPost(
+ setLocalFeedPosts,
+ localPostId,
+ post,
);
void markCurrentFeedRead(post.postId).catch(
(error: unknown) =>
@@ -222,7 +226,7 @@ const Page: React.FC = () => {
setLocalFeedPosts((currentPosts) =>
currentPosts.filter(
(item) =>
- item.status != "ready" ||
+ item.status == "pending" ||
item.post.postId != postId,
),
);
diff --git a/web/apps/space/src/pages/app/profile.tsx b/web/apps/space/src/pages/app/profile.tsx
index 823bc35f4b..68dad1a081 100644
--- a/web/apps/space/src/pages/app/profile.tsx
+++ b/web/apps/space/src/pages/app/profile.tsx
@@ -15,7 +15,10 @@ import {
type SpacePost,
} from "services/space";
import { useSpaceAppState } from "state/spaceAppState";
-import { createLocalFeedPostID } from "utils/localFeedPost";
+import {
+ confirmLocalFeedPost,
+ createLocalFeedPostID,
+} from "utils/localFeedPost";
import { profilePostGroupsFromPosts } from "utils/spacePostDisplay";
import { prepareSpacePostImageFromEdit } from "utils/spacePostImage";
import { spaceRoutes } from "utils/spaceRoutes";
@@ -107,6 +110,9 @@ const Page: React.FC = () => {
friendID: spaceId,
height: image.height,
id: localPostId,
+ imageUrl:
+ image.previewUrl ||
+ URL.createObjectURL(image.file),
name: displayName || "You",
spaceId,
status: "pending",
@@ -130,12 +136,10 @@ const Page: React.FC = () => {
width: preparedImage.width,
});
if (!post) throw new Error("Couldn't create post.");
- setLocalFeedPosts((currentPosts) =>
- currentPosts.map((item) =>
- item.id == localPostId
- ? { id: localPostId, post, status: "ready" }
- : item,
- ),
+ confirmLocalFeedPost(
+ setLocalFeedPosts,
+ localPostId,
+ post,
);
void markCurrentFeedRead(post.postId).catch(
(error: unknown) =>
@@ -156,7 +160,7 @@ const Page: React.FC = () => {
setLocalFeedPosts((currentPosts) =>
currentPosts.filter(
(item) =>
- item.status != "ready" ||
+ item.status == "pending" ||
item.post.postId != postId,
),
);
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index 1b5977c6d2..f6d98dc753 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -3,14 +3,15 @@ import {
Chat01Icon,
FavouriteIcon,
MultiplicationSignIcon,
+ Tick02Icon,
UserCheck01Icon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { Box, Skeleton } from "@mui/material";
import {
SpaceFileViewer,
- type SpaceViewerDraftPostEdit,
type SpaceLiker,
+ type SpaceViewerDraftPostEdit,
type SpaceViewerInitialScreen,
type SpaceViewerPhoto,
type SpaceViewerPostActionMode,
@@ -128,17 +129,19 @@ interface DraftSpacePostImage {
cropArea?: SpaceViewerDraftPostEdit["cropArea"];
file: File;
height?: number;
+ previewUrl?: string;
rotationDegrees?: number;
width?: number;
}
+type FeedTimestampStatus = "posted" | "posting";
+
interface FeedItemProps {
aspectRatio: number;
avatarUrl?: string | null;
caption?: string;
friendID: string;
imageUrl: string;
- isPhotoPending?: boolean;
isOwnPost: boolean;
likeCount: number;
name: string;
@@ -151,6 +154,7 @@ interface FeedItemProps {
onOpenProfile?: () => void;
onSetPostLiked?: (postId: number, liked: boolean) => Promise<void>;
postId: number;
+ timestampStatus?: FeedTimestampStatus;
timestampMs: number;
viewerLiked: boolean;
viewerUnread: boolean;
@@ -324,13 +328,31 @@ const FeedLoadingSkeletons: React.FC = () => (
</Box>
);
+const usePostingDotCount = (isPosting: boolean) => {
+ const [dotCount, setDotCount] = useState(1);
+
+ React.useEffect(() => {
+ if (!isPosting) {
+ setDotCount(1);
+ return;
+ }
+
+ const intervalID = window.setInterval(() => {
+ setDotCount((count) => (count % 3) + 1);
+ }, 500);
+
+ return () => window.clearInterval(intervalID);
+ }, [isPosting]);
+
+ return dotCount;
+};
+
const FeedItem: React.FC<FeedItemProps> = ({
aspectRatio,
avatarUrl,
caption,
friendID,
imageUrl,
- isPhotoPending = false,
isOwnPost,
likeCount,
name,
@@ -339,6 +361,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
onOpenProfile,
onSetPostLiked,
postId,
+ timestampStatus,
timestampMs,
viewerLiked,
viewerUnread,
@@ -347,6 +370,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
const [localLikeCount, setLocalLikeCount] = useState(likeCount);
const firstName = firstNameFrom(name);
const dateLabel = formatSpaceDate(timestampMs);
+ const postingDotCount = usePostingDotCount(timestampStatus == "posting");
const displayCaption = caption?.trim();
const canOpenAuthor = isOwnPost
? Boolean(onOpenProfile)
@@ -388,8 +412,6 @@ const FeedItem: React.FC<FeedItemProps> = ({
initialScreen?: SpaceViewerInitialScreen,
focusReplyOnOpen = false,
) => {
- if (isPhotoPending) return;
-
onOpenPhoto?.(
{
alt: `${name} post`,
@@ -546,20 +568,68 @@ const FeedItem: React.FC<FeedItemProps> = ({
>
{firstName}
</Box>
- <Box
- component="time"
- dateTime={new Date(timestampMs).toISOString()}
- sx={{
- color: textSecondary,
- fontSize: 12,
- fontWeight: 500,
- justifySelf: "end",
- textAlign: "right",
- whiteSpace: "nowrap",
- }}
- >
- {dateLabel}
- </Box>
+ {timestampStatus ? (
+ <Box
+ component="span"
+ aria-label={
+ timestampStatus == "posting" ? "Posting" : "Posted"
+ }
+ sx={{
+ alignItems: "center",
+ color:
+ timestampStatus == "posted"
+ ? green
+ : textSecondary,
+ display: "inline-flex",
+ fontSize: 12,
+ fontWeight: 500,
+ justifyContent: "flex-end",
+ justifySelf: "end",
+ lineHeight: "16px",
+ textAlign: "right",
+ whiteSpace: "nowrap",
+ width: 68,
+ }}
+ >
+ {timestampStatus == "posted" ? (
+ <HugeiconsIcon
+ icon={Tick02Icon}
+ size={16}
+ strokeWidth={2.2}
+ />
+ ) : (
+ <>
+ <Box component="span">Posting</Box>
+ <Box
+ component="span"
+ aria-hidden
+ sx={{
+ display: "inline-block",
+ textAlign: "left",
+ width: 12,
+ }}
+ >
+ {".".repeat(postingDotCount)}
+ </Box>
+ </>
+ )}
+ </Box>
+ ) : (
+ <Box
+ component="time"
+ dateTime={new Date(timestampMs).toISOString()}
+ sx={{
+ color: textSecondary,
+ fontSize: 12,
+ fontWeight: 500,
+ justifySelf: "end",
+ textAlign: "right",
+ whiteSpace: "nowrap",
+ }}
+ >
+ {dateLabel}
+ </Box>
+ )}
{viewerUnread && (
<Box
aria-hidden
@@ -583,55 +653,40 @@ const FeedItem: React.FC<FeedItemProps> = ({
width: "100%",
}}
>
- {isPhotoPending ? (
- <Skeleton
- variant="rectangular"
- animation="wave"
- aria-label="Posting photo"
- sx={{
- bgcolor: feedSkeletonElementBackground,
- display: "block",
- height: "100%",
- transform: "none",
- width: "100%",
- }}
- />
- ) : (
+ <Box
+ component="button"
+ type="button"
+ aria-label={`Open ${name} photo`}
+ onClick={() => openPhoto()}
+ sx={{
+ appearance: "none",
+ bgcolor: "transparent",
+ border: 0,
+ cursor: onOpenPhoto ? "pointer" : "default",
+ display: "block",
+ height: "100%",
+ p: 0,
+ width: "100%",
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: -2,
+ },
+ }}
+ >
<Box
- component="button"
- type="button"
- aria-label={`Open ${name} photo`}
- onClick={() => openPhoto()}
+ component="img"
+ alt={`${name} post`}
+ src={imageUrl}
+ onLoad={rememberLoadedPhotoDimensions}
sx={{
- appearance: "none",
- bgcolor: "transparent",
- border: 0,
- cursor: onOpenPhoto ? "pointer" : "default",
display: "block",
height: "100%",
- p: 0,
+ objectFit: "cover",
+ objectPosition: "center",
width: "100%",
- "&:focus-visible": {
- outline: `2px solid ${green}`,
- outlineOffset: -2,
- },
}}
- >
- <Box
- component="img"
- alt={`${name} post`}
- src={imageUrl}
- onLoad={rememberLoadedPhotoDimensions}
- sx={{
- display: "block",
- height: "100%",
- objectFit: "cover",
- objectPosition: "center",
- width: "100%",
- }}
- />
- </Box>
- )}
+ />
+ </Box>
</Box>
{(!isOwnPost || displayCaption) && (
<Box
@@ -886,13 +941,13 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
const selectedPhotoFriendID = selectedViewer?.photo.friendID;
const selectedPhotoIsOwn =
Boolean(profile.spaceId) && selectedPhotoFriendID == profile.spaceId;
- const localReadyPostIds = new Set(
+ const localResolvedPostIds = new Set(
localFeedPosts
- .filter((item) => item.status == "ready")
+ .filter((item) => item.status != "pending")
.map((item) => item.post.postId),
);
const remoteFeedItems = feedItems.filter(
- (item) => !localReadyPostIds.has(item.postId),
+ (item) => !localResolvedPostIds.has(item.postId),
);
const hasFeedItems =
localFeedPosts.length > 0 || remoteFeedItems.length > 0;
@@ -911,6 +966,9 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
);
localPostObjectUrlsRef.current.clear();
}, []);
+ const releaseLocalPostObjectUrl = React.useCallback((objectUrl: string) => {
+ localPostObjectUrlsRef.current.delete(objectUrl);
+ }, []);
const openPostPhotoPicker = () => {
if (isPostPhotoOpening) return;
@@ -941,7 +999,11 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
await onDeletePost(postId);
};
- const feedItemFor = (item: SpacePost, key: React.Key) => (
+ const feedItemFor = (
+ item: SpacePost,
+ key: React.Key,
+ timestampStatus?: FeedTimestampStatus,
+ ) => (
<FeedItem
key={key}
aspectRatio={
@@ -961,14 +1023,19 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
onOpenProfile={onOpenProfile}
onSetPostLiked={onSetPostLiked}
postId={item.postId}
+ timestampStatus={timestampStatus}
timestampMs={item.timestampMs}
viewerLiked={item.viewerLiked}
viewerUnread={item.viewerUnread}
/>
);
const localFeedItemFor = (item: LocalSpaceFeedPost) =>
- item.status == "ready" ? (
- feedItemFor(item.post, item.id)
+ item.status != "pending" ? (
+ feedItemFor(
+ item.post,
+ item.id,
+ item.status == "posted" ? "posted" : undefined,
+ )
) : (
<FeedItem
key={item.id}
@@ -978,13 +1045,13 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
avatarUrl={item.avatarUrl ?? ""}
caption={item.caption}
friendID={item.friendID}
- imageUrl=""
+ imageUrl={item.imageUrl}
isOwnPost
- isPhotoPending
likeCount={0}
name={item.name}
onOpenProfile={onOpenProfile}
postId={0}
+ timestampStatus="posting"
timestampMs={item.timestampMs}
viewerLiked={false}
viewerUnread={false}
@@ -1593,18 +1660,24 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
}
onPublishDraftPost={
selectedViewer.draftFile && onCreatePost
- ? (caption, edit) =>
- onCreatePost(
+ ? (caption, edit) => {
+ const previewUrl =
+ selectedViewer.photo.imageUrl;
+ const publishPromise = onCreatePost(
{
cropArea: edit.cropArea,
file: selectedViewer.draftFile!,
height: edit.height,
+ previewUrl,
rotationDegrees:
edit.rotationDegrees,
width: edit.width,
},
caption,
- )
+ );
+ releaseLocalPostObjectUrl(previewUrl);
+ return publishPromise;
+ }
: undefined
}
onLoadPostLikers={onLoadPostLikers}
diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsx
index ccab27f858..9d4851b5be 100644
--- a/web/apps/space/src/screens/ProfileScreen.tsx
+++ b/web/apps/space/src/screens/ProfileScreen.tsx
@@ -89,6 +89,7 @@ interface DraftSpacePostImage {
cropArea?: SpaceViewerDraftPostEdit["cropArea"];
file: File;
height?: number;
+ previewUrl?: string;
rotationDegrees?: number;
width?: number;
}
@@ -242,6 +243,9 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
);
localPostObjectUrlsRef.current.clear();
}, []);
+ const releaseLocalPostObjectUrl = React.useCallback((objectUrl: string) => {
+ localPostObjectUrlsRef.current.delete(objectUrl);
+ }, []);
const openPostPhotoPicker = () => {
if (isPostPhotoOpening) return;
@@ -1245,18 +1249,24 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
onOpenProfile={closeSelectedPost}
onPublishDraftPost={
selectedPost.draftFile && onCreatePost
- ? (caption, edit) =>
- onCreatePost(
+ ? (caption, edit) => {
+ const previewUrl =
+ selectedPost.photo.imageUrl;
+ const publishPromise = onCreatePost(
{
cropArea: edit.cropArea,
file: selectedPost.draftFile!,
height: edit.height,
+ previewUrl,
rotationDegrees:
edit.rotationDegrees,
width: edit.width,
},
caption,
- )
+ );
+ releaseLocalPostObjectUrl(previewUrl);
+ return publishPromise;
+ }
: undefined
}
onDraftPostPublished={onDraftPostPublished}
diff --git a/web/apps/space/src/state/SpaceAppStateProvider.tsx b/web/apps/space/src/state/SpaceAppStateProvider.tsx
index 5e5957bfb5..883a76ae76 100644
--- a/web/apps/space/src/state/SpaceAppStateProvider.tsx
+++ b/web/apps/space/src/state/SpaceAppStateProvider.tsx
@@ -53,6 +53,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
const [signupEmail, setSignupEmail] = useState("");
const avatarURLRef = useRef<string | null>(null);
const coverURLRef = useRef<string | null>(null);
+ const localFeedPostURLRef = useRef<Set<string>>(new Set());
const profileRef = useRef<SetupProfile | null>(null);
const profileLoadGenerationRef = useRef(0);
@@ -198,6 +199,37 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
void refreshProfile();
}, [refreshProfile]);
+ useEffect(() => {
+ const nextURLs = new Set(
+ localFeedPosts
+ .filter(
+ (
+ post,
+ ): post is Extract<
+ LocalSpaceFeedPost,
+ { status: "pending" }
+ > => post.status == "pending",
+ )
+ .map((post) => post.imageUrl)
+ .filter((url) => url.startsWith("blob:")),
+ );
+
+ for (const url of localFeedPostURLRef.current) {
+ if (!nextURLs.has(url)) URL.revokeObjectURL(url);
+ }
+ localFeedPostURLRef.current = nextURLs;
+ }, [localFeedPosts]);
+
+ useEffect(
+ () => () => {
+ for (const url of localFeedPostURLRef.current) {
+ URL.revokeObjectURL(url);
+ }
+ localFeedPostURLRef.current.clear();
+ },
+ [],
+ );
+
const value = useMemo<SpaceAppState>(
() => ({
friends,
diff --git a/web/apps/space/src/state/spaceAppState.tsx b/web/apps/space/src/state/spaceAppState.tsx
index 858ccb4c59..765794c3e1 100644
--- a/web/apps/space/src/state/spaceAppState.tsx
+++ b/web/apps/space/src/state/spaceAppState.tsx
@@ -14,6 +14,7 @@ export interface PendingSpaceFeedPost {
friendID: string;
height?: number;
id: string;
+ imageUrl: string;
name: string;
spaceId: string;
status: "pending";
@@ -21,13 +22,22 @@ export interface PendingSpaceFeedPost {
width?: number;
}
+export interface PostedSpaceFeedPost {
+ id: string;
+ post: SpacePost;
+ status: "posted";
+}
+
export interface ReadySpaceFeedPost {
id: string;
post: SpacePost;
status: "ready";
}
-export type LocalSpaceFeedPost = PendingSpaceFeedPost | ReadySpaceFeedPost;
+export type LocalSpaceFeedPost =
+ | PendingSpaceFeedPost
+ | PostedSpaceFeedPost
+ | ReadySpaceFeedPost;
export interface SpaceAppState {
friends: FriendProfile[];
diff --git a/web/apps/space/src/utils/localFeedPost.ts b/web/apps/space/src/utils/localFeedPost.ts
index ca0d570572..d5e58aa3eb 100644
--- a/web/apps/space/src/utils/localFeedPost.ts
+++ b/web/apps/space/src/utils/localFeedPost.ts
@@ -1,4 +1,36 @@
+import type React from "react";
+import type { SpacePost } from "services/space";
+import type { LocalSpaceFeedPost } from "state/spaceAppState";
+
let nextLocalFeedPostID = 0;
+const postedConfirmationDurationMs = 1500;
+
export const createLocalFeedPostID = () =>
`space-local-post-${Date.now()}-${nextLocalFeedPostID++}`;
+
+export const confirmLocalFeedPost = (
+ setLocalFeedPosts: React.Dispatch<
+ React.SetStateAction<LocalSpaceFeedPost[]>
+ >,
+ localPostId: string,
+ post: SpacePost,
+) => {
+ setLocalFeedPosts((currentPosts) =>
+ currentPosts.map((item) =>
+ item.id == localPostId
+ ? { id: localPostId, post, status: "posted" }
+ : item,
+ ),
+ );
+
+ window.setTimeout(() => {
+ setLocalFeedPosts((currentPosts) =>
+ currentPosts.map((item) =>
+ item.id == localPostId && item.status == "posted"
+ ? { id: localPostId, post: item.post, status: "ready" }
+ : item,
+ ),
+ );
+ }, postedConfirmationDurationMs);
+};