Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add friend message and unfriend actions

Anand Baburajan26e2536efc39
diff --git a/web/apps/space/src/pages/app/friends/[spaceId].tsx b/web/apps/space/src/pages/app/friends/[spaceId].tsx
index 5c94e3d213..6d987bb92d 100644
--- a/web/apps/space/src/pages/app/friends/[spaceId].tsx
+++ b/web/apps/space/src/pages/app/friends/[spaceId].tsx
@@ -14,6 +14,7 @@ import {
loadCurrentSpacePostAssetURL,
loadCurrentSpaceProfile,
loadCurrentSpaceProfilePostsPage,
+ removeCurrentSpaceFriend,
replyToCurrentPost,
setCurrentPostLiked,
type SpaceProfilePost,
@@ -195,6 +196,24 @@ const Page: React.FC = () => {
void router.push(spaceRoutes.friends);
};
+ const unfriend = React.useCallback(async () => {
+ const actorSpaceId = profile?.spaceId;
+ if (!actorSpaceId || !selectedFriendSpaceId) return;
+
+ await removeCurrentSpaceFriend(actorSpaceId, selectedFriendSpaceId);
+ }, [profile?.spaceId, selectedFriendSpaceId]);
+
+ const finishUnfriend = React.useCallback(() => {
+ setFriends((currentFriends) =>
+ currentFriends.filter(
+ (friend) =>
+ friend.spaceId != selectedFriendSpaceId &&
+ friend.id != friendSpaceId,
+ ),
+ );
+ void router.replace(spaceRoutes.friends);
+ }, [friendSpaceId, router, selectedFriendSpaceId, setFriends]);
+
if (
!router.isReady ||
profileLoadStatus != "ready" ||
@@ -264,6 +283,14 @@ const Page: React.FC = () => {
}}
onBack={goBack}
onLoadPostImage={loadCurrentSpacePostAssetURL}
+ onMessageFriend={
+ selectedFriendSpaceId
+ ? () =>
+ void router.push(
+ spaceRoutes.message(selectedFriendSpaceId),
+ )
+ : undefined
+ }
onOpenProfileCover={() => setOpenProfileImage("cover")}
onOpenProfilePhoto={() => setOpenProfileImage("avatar")}
onReplyToPost={(postSpaceId, postId, text) =>
@@ -272,6 +299,10 @@ const Page: React.FC = () => {
onSetPostLiked={(postId, liked) =>
setCurrentPostLiked(actorSpaceId, postId, liked)
}
+ onUnfriend={selectedFriendSpaceId ? unfriend : undefined}
+ onUnfriendComplete={
+ selectedFriendSpaceId ? finishUnfriend : undefined
+ }
showPostLoadingIndicator={false}
/>
{openProfileImage && (
diff --git a/web/apps/space/src/pages/app/friends/index.tsx b/web/apps/space/src/pages/app/friends/index.tsx
index bcde1e3470..2039111fa1 100644
--- a/web/apps/space/src/pages/app/friends/index.tsx
+++ b/web/apps/space/src/pages/app/friends/index.tsx
@@ -54,6 +54,14 @@ const Page: React.FC = () => {
friends={friends}
onLoadFriendAvatar={loadCurrentFriendAvatarURL}
onBack={() => void router.push(spaceRoutes.profile)}
+ onMessage={(friendID) => {
+ const friend = friends.find(
+ (candidate) => candidate.id == friendID,
+ );
+ if (friend?.spaceId) {
+ void router.push(spaceRoutes.message(friend.spaceId));
+ }
+ }}
onOpenFriend={(friendID) =>
void router.push(spaceRoutes.friend(friendID))
}
diff --git a/web/apps/space/src/screens/FriendsScreen.tsx b/web/apps/space/src/screens/FriendsScreen.tsx
index 1224643b80..fede3777d7 100644
--- a/web/apps/space/src/screens/FriendsScreen.tsx
+++ b/web/apps/space/src/screens/FriendsScreen.tsx
@@ -1,5 +1,6 @@
import {
ArrowLeft02Icon,
+ BubbleChatIcon,
MoreVerticalIcon,
UserAdd02Icon,
UserRemove01Icon,
@@ -41,6 +42,7 @@ interface FriendsScreenProps {
friends: FriendProfile[];
onLoadFriendAvatar?: (friend: FriendProfile) => Promise<string | null>;
onBack?: () => void;
+ onMessage?: (friendID: string) => void;
onOpenFriend?: (friendID: string) => void;
profileLink?: string;
onUnfriend?: (friendID: string) => Promise<void> | void;
@@ -50,6 +52,7 @@ interface FriendRowProps {
avatarUrl?: string | null;
friend: FriendProfile;
onLoadAvatar?: () => Promise<string | null | undefined>;
+ onMessage?: (friendID: string) => void;
onOpenFriend?: (friendID: string) => void;
onUnfriend?: (friendID: string) => void;
}
@@ -58,6 +61,7 @@ const FriendRow: React.FC<FriendRowProps> = ({
avatarUrl,
friend,
onLoadAvatar,
+ onMessage,
onOpenFriend,
onUnfriend,
}) => {
@@ -75,6 +79,11 @@ const FriendRow: React.FC<FriendRowProps> = ({
const closeActions = () => setActionsAnchor(null);
+ const message = () => {
+ closeActions();
+ onMessage?.(friend.id);
+ };
+
const unfriend = () => {
closeActions();
onUnfriend?.(friend.id);
@@ -279,6 +288,44 @@ const FriendRow: React.FC<FriendRowProps> = ({
list: { "aria-labelledby": actionsButtonID, sx: { p: 0 } },
}}
>
+ {onMessage && (
+ <MenuItem
+ dense
+ disableRipple
+ onClick={message}
+ sx={{
+ borderRadius: "10px",
+ color: textBase,
+ gap: "8px",
+ minHeight: 36,
+ px: "9px",
+ py: "4px",
+ whiteSpace: "nowrap",
+ "&.Mui-focusVisible": {
+ bgcolor: "rgba(0, 0, 0, 0.04)",
+ },
+ "&:active": { bgcolor: "rgba(0, 0, 0, 0.04)" },
+ "&:hover": { bgcolor: "rgba(0, 0, 0, 0.04)" },
+ }}
+ >
+ <HugeiconsIcon
+ icon={BubbleChatIcon}
+ size={18}
+ strokeWidth={1.8}
+ />
+ <Box
+ sx={{
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 650,
+ lineHeight: "18px",
+ }}
+ >
+ Message
+ </Box>
+ </MenuItem>
+ )}
<MenuItem
dense
disableRipple
@@ -323,6 +370,7 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({
friends,
onLoadFriendAvatar,
onBack,
+ onMessage,
onOpenFriend,
profileLink,
onUnfriend,
@@ -559,6 +607,7 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({
avatarUrl={loadedAvatarURLFor(friend)}
friend={friend}
onLoadAvatar={() => loadFriendAvatar(friend)}
+ onMessage={onMessage}
onOpenFriend={onOpenFriend}
onUnfriend={() => {
setUnfriendErrorMessage(null);
diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsx
index c9251b21cb..45e8f50393 100644
--- a/web/apps/space/src/screens/ProfileScreen.tsx
+++ b/web/apps/space/src/screens/ProfileScreen.tsx
@@ -1,11 +1,19 @@
import {
AddSquareIcon,
ArrowLeft02Icon,
+ BubbleChatIcon,
Menu01Icon,
+ MoreHorizontalIcon,
MultiplicationSignIcon,
+ UserRemove01Icon,
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
-import { Box, Skeleton } from "@mui/material";
+import { Box, Menu, MenuItem, Skeleton } from "@mui/material";
+import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";
+import {
+ spaceActionDoneDurationMs,
+ type SpaceActionPhase,
+} from "components/SpaceActionFeedback";
import { SpaceAvatarImage } from "components/SpaceAvatarImage";
import {
SpaceFileViewer,
@@ -36,6 +44,7 @@ import { thumbHashDataURLFromBase64 } from "utils/thumbhash";
export const profileBackground = "#FFFFFF";
const green = "#08C225";
+const dangerColor = "#F63A3A";
const textBase = "#000";
const textStrong = "#303030";
const textSoft = "#777777";
@@ -534,12 +543,15 @@ interface ProfileScreenProps {
onOpenProfilePhoto?: () => void;
onOpenSettings?: () => void;
onLoadPostImage?: (asset: SpacePostAsset) => Promise<string>;
+ onMessageFriend?: () => void;
onReplyToPost?: (
postSpaceId: string,
postId: number,
text: string,
) => Promise<void>;
onSetPostLiked?: (postId: number, liked: boolean) => Promise<void>;
+ onUnfriend?: () => Promise<void> | void;
+ onUnfriendComplete?: () => void;
onUpdatePostCaption?: (postId: number, caption: string) => Promise<void>;
postGroups?: ProfilePostGroup[];
profile: SetupProfile;
@@ -561,8 +573,11 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
onOpenProfilePhoto,
onOpenSettings,
onLoadPostImage,
+ onMessageFriend,
onReplyToPost,
onSetPostLiked,
+ onUnfriend,
+ onUnfriendComplete,
onUpdatePostCaption,
postGroups = [],
profile,
@@ -577,6 +592,14 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
);
const [profileShareToast, setProfileShareToast] =
useState<ProfileToastState | null>(null);
+ const [friendActionsAnchor, setFriendActionsAnchor] =
+ useState<HTMLElement | null>(null);
+ const [isUnfriendSheetOpen, setIsUnfriendSheetOpen] = useState(false);
+ const [unfriendActionPhase, setUnfriendActionPhase] =
+ useState<SpaceActionPhase | null>(null);
+ const [unfriendErrorMessage, setUnfriendErrorMessage] = useState<
+ string | null
+ >(null);
const [loadedPhotoDimensionsByID, setLoadedPhotoDimensionsByID] = useState<
Record<string, ProfilePhotoDimensions>
>({});
@@ -598,6 +621,12 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
);
const isOwnerProfile = headerVariant == "owner";
const isFriendProfile = headerVariant == "friend";
+ const friendActionsButtonID = React.useId();
+ const friendActionsMenuID = React.useId();
+ const isFriendActionsOpen = Boolean(friendActionsAnchor);
+ const isUnfriendActionRunning = unfriendActionPhase != null;
+ const canManageFriend =
+ isFriendProfile && Boolean(onMessageFriend || onUnfriend);
const displayName = profile.fullName.trim() || profile.username.trim();
const coverUrl = profile.coverUrl ?? null;
const isCoverURLPending = Boolean(profile.coverObjectID && !coverUrl);
@@ -682,6 +711,57 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
);
}, []);
+ const closeFriendActions = () => setFriendActionsAnchor(null);
+
+ const messageFriend = () => {
+ closeFriendActions();
+ onMessageFriend?.();
+ };
+
+ const requestUnfriend = () => {
+ closeFriendActions();
+ setUnfriendErrorMessage(null);
+ setIsUnfriendSheetOpen(true);
+ };
+
+ const cancelUnfriend = () => {
+ if (isUnfriendActionRunning) return;
+ setUnfriendErrorMessage(null);
+ setIsUnfriendSheetOpen(false);
+ };
+
+ const confirmUnfriend = () => {
+ if (!onUnfriend || isUnfriendActionRunning) return;
+ setUnfriendErrorMessage(null);
+ setUnfriendActionPhase("busy");
+ void (async () => {
+ try {
+ await Promise.resolve(onUnfriend());
+ setUnfriendActionPhase("done");
+ } catch (error) {
+ console.error("Failed to unfriend space friend", error);
+ setUnfriendActionPhase(null);
+ setUnfriendErrorMessage("Couldn't unfriend. Please try again.");
+ }
+ })();
+ };
+
+ React.useEffect(() => {
+ if (unfriendActionPhase != "done") return;
+
+ const timeoutID = window.setTimeout(() => {
+ setIsUnfriendSheetOpen(false);
+ onUnfriendComplete?.();
+ }, spaceActionDoneDurationMs);
+
+ return () => window.clearTimeout(timeoutID);
+ }, [onUnfriendComplete, unfriendActionPhase]);
+
+ const handleUnfriendSheetExited = () => {
+ setUnfriendActionPhase(null);
+ setUnfriendErrorMessage(null);
+ };
+
const revokeLocalPostObjectUrls = React.useCallback(() => {
localPostObjectUrlsRef.current.forEach((objectUrl) =>
URL.revokeObjectURL(objectUrl),
@@ -1371,7 +1451,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
>
{displayName}
</Box>
- {isOwnerProfile && (
+ {isOwnerProfile ? (
<Box
component="button"
type="button"
@@ -1405,6 +1485,192 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
>
<SpaceShareIcon strokeWidth={2.2} />
</Box>
+ ) : (
+ canManageFriend && (
+ <Box
+ component="button"
+ id={friendActionsButtonID}
+ type="button"
+ aria-label={`Actions for ${displayName}`}
+ aria-controls={
+ isFriendActionsOpen
+ ? friendActionsMenuID
+ : undefined
+ }
+ aria-expanded={
+ isFriendActionsOpen
+ ? "true"
+ : undefined
+ }
+ aria-haspopup="menu"
+ onClick={(event) =>
+ setFriendActionsAnchor(
+ event.currentTarget,
+ )
+ }
+ sx={{
+ alignItems: "center",
+ bgcolor: "transparent",
+ border: 0,
+ color: textStrong,
+ cursor: "pointer",
+ display: "flex",
+ gridColumn: 3,
+ height: spaceTouchTargetSize,
+ justifyContent: "flex-start",
+ justifySelf: "start",
+ ml: "8px",
+ p: 0,
+ position: "absolute",
+ top: "50%",
+ transform: "translateY(-50%)",
+ width: spaceTouchTargetSize,
+ "&:focus-visible": {
+ borderRadius: "50%",
+ outline: `2px solid ${green}`,
+ outlineOffset: 2,
+ },
+ }}
+ >
+ <HugeiconsIcon
+ icon={MoreHorizontalIcon}
+ size={24}
+ strokeWidth={2}
+ />
+ </Box>
+ )
+ )}
+ {canManageFriend && (
+ <Menu
+ id={friendActionsMenuID}
+ anchorEl={friendActionsAnchor}
+ open={isFriendActionsOpen}
+ onClose={closeFriendActions}
+ anchorOrigin={{
+ horizontal: "right",
+ vertical: "bottom",
+ }}
+ transformOrigin={{
+ horizontal: "right",
+ vertical: "top",
+ }}
+ slotProps={{
+ paper: {
+ sx: {
+ bgcolor: "#FFFFFF",
+ borderRadius: "14px",
+ boxShadow:
+ "0 14px 40px rgba(0, 0, 0, 0.16)",
+ mt: "6px",
+ minWidth: 0,
+ p: "4px",
+ width: "max-content",
+ },
+ },
+ list: {
+ "aria-labelledby":
+ friendActionsButtonID,
+ sx: { p: 0 },
+ },
+ }}
+ >
+ {onMessageFriend && (
+ <MenuItem
+ dense
+ disableRipple
+ onClick={messageFriend}
+ sx={{
+ alignItems: "center",
+ borderRadius: "10px",
+ color: textBase,
+ display: "flex",
+ gap: "8px",
+ minHeight: 36,
+ px: "9px",
+ py: "4px",
+ whiteSpace: "nowrap",
+ "&.Mui-focusVisible": {
+ bgcolor:
+ "rgba(0, 0, 0, 0.04)",
+ },
+ "&:active": {
+ bgcolor:
+ "rgba(0, 0, 0, 0.04)",
+ },
+ "&:hover": {
+ bgcolor:
+ "rgba(0, 0, 0, 0.04)",
+ },
+ }}
+ >
+ <HugeiconsIcon
+ icon={BubbleChatIcon}
+ size={18}
+ strokeWidth={1.8}
+ style={{ flexShrink: 0 }}
+ />
+ <Box
+ sx={{
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 650,
+ lineHeight: "18px",
+ }}
+ >
+ Message
+ </Box>
+ </MenuItem>
+ )}
+ {onUnfriend && (
+ <MenuItem
+ dense
+ disableRipple
+ onClick={requestUnfriend}
+ sx={{
+ alignItems: "center",
+ borderRadius: "10px",
+ color: dangerColor,
+ display: "flex",
+ gap: "8px",
+ minHeight: 36,
+ px: "9px",
+ py: "4px",
+ whiteSpace: "nowrap",
+ "&.Mui-focusVisible": {
+ bgcolor:
+ "rgba(246, 58, 58, 0.06)",
+ },
+ "&:active": {
+ bgcolor:
+ "rgba(246, 58, 58, 0.06)",
+ },
+ "&:hover": {
+ bgcolor:
+ "rgba(246, 58, 58, 0.06)",
+ },
+ }}
+ >
+ <HugeiconsIcon
+ icon={UserRemove01Icon}
+ size={18}
+ strokeWidth={1.8}
+ style={{ flexShrink: 0 }}
+ />
+ <Box
+ sx={{
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 650,
+ lineHeight: "18px",
+ }}
+ >
+ Unfriend
+ </Box>
+ </MenuItem>
+ )}
+ </Menu>
)}
</Box>
{isStatsLoading ? (
@@ -1813,6 +2079,18 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
/>
)}
</Box>
+ <ConfirmationActionSheet
+ open={isUnfriendSheetOpen}
+ title="Are you sure you want to unfriend?"
+ confirmLabel="Yes, unfriend"
+ confirmActionPhase={unfriendActionPhase}
+ confirmDisabled={isUnfriendActionRunning}
+ errorMessage={unfriendErrorMessage}
+ cancelDisabled={isUnfriendActionRunning}
+ onCancel={cancelUnfriend}
+ onConfirm={confirmUnfriend}
+ onExited={handleUnfriendSheetExited}
+ />
</Box>
);
};