π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Add username friend requests in Space
Anand Baburajan62da083e73f5
diff --git a/web/apps/space/src/pages/app/friends/index.tsx b/web/apps/space/src/pages/app/friends/index.tsxindex 6fbe14ecb7..2941d268ad 100644--- a/web/apps/space/src/pages/app/friends/index.tsx+++ b/web/apps/space/src/pages/app/friends/index.tsx@@ -13,6 +13,7 @@ import {loadCurrentFriendRequests,loadCurrentSpaceFriends,removeCurrentSpaceFriend,+ requestFriendByUsername,type SpaceFriendRequest,} from "services/space";import {@@ -93,6 +94,35 @@ const Page: React.FC = () => {isLoading={isFriendsLoading}onLoadFriendAvatar={loadCurrentFriendAvatarURL}onBack={() => void router.push(spaceRoutes.profile)}+ onAddFriend={async (username) => {+ const actorSpaceId = profile.spaceId;+ if (!actorSpaceId) throw new Error("Missing space.");+ const status = await requestFriendByUsername({+ spaceUsername: username,+ });+ try {+ if (status == "friend") {+ clearSpaceFriendsCache();+ void invalidateCachedSpaceFeed(actorSpaceId);+ const [requests, friends] = await Promise.all([+ loadCurrentFriendRequests(actorSpaceId),+ loadCurrentSpaceFriends(actorSpaceId),+ ]);+ setFriendRequests(requests);+ setFriends(friends);+ } else {+ setFriendRequests(+ await loadCurrentFriendRequests(actorSpaceId),+ );+ }+ } catch (error) {+ log.error(+ "Failed to refresh friends after sending request",+ error,+ );+ }+ return status;+ }}onMessage={(friendID) => {const friend = friends.find((candidate) => candidate.id == friendID,@@ -116,6 +146,7 @@ const Page: React.FC = () => {profileLink={spaceInviteURL({spaceUsername: profile.username,})}+ username={profile.username}onAcceptFriendRequest={async (requestID) => {const actorSpaceId = profile.spaceId;if (!actorSpaceId) return;diff --git a/web/apps/space/src/screens/FriendsScreen.tsx b/web/apps/space/src/screens/FriendsScreen.tsxindex f3309e801a..630586fb18 100644--- a/web/apps/space/src/screens/FriendsScreen.tsx+++ b/web/apps/space/src/screens/FriendsScreen.tsx@@ -7,7 +7,14 @@ import {UserRemove01Icon,} from "@hugeicons/core-free-icons";import { HugeiconsIcon } from "@hugeicons/react";-import { Box, Menu, MenuItem, Skeleton } from "@mui/material";+import {+ Box,+ Dialog,+ Menu,+ MenuItem,+ Skeleton,+ useMediaQuery,+} from "@mui/material";import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";import {SpaceActionFeedbackIcon,@@ -15,13 +22,17 @@ import {type SpaceActionPhase,} from "components/SpaceActionFeedback";import { SpaceAvatarImage } from "components/SpaceAvatarImage";+import { SpaceBottomSheetTransition } from "components/SpaceBottomSheetTransition";import { SpaceLoadingSpinner } from "components/SpaceRouteFallback";import { SpaceShareInviteButton } from "components/SpaceShareInviteButton";import type { FriendProfile } from "data/friends";import log from "ente-base/log";import React, { useState } from "react";import type { SpaceFriendRequest } from "services/space";-import { openSpaceShareLinkDialog } from "services/spaceShareLink";+import {+ normalizeSpaceUsername,+ spaceUsernameValidationError,+} from "services/spaceProfile";import { spaceTouchTargetSize } from "styles/touchTargets";export const friendsBackground = "#FFFFFF";@@ -51,9 +62,11 @@ interface FriendsScreenProps {onDeleteFriendRequest: (requestID: number) => Promise<void>;onLoadFriendAvatar?: (friend: FriendProfile) => Promise<string | null>;onBack?: () => void;+ onAddFriend: (username: string) => Promise<"friend" | "requested">;onMessage?: (friendID: string) => void;onOpenFriend?: (friendID: string) => void;profileLink?: string;+ username: string;onUnfriend?: (friendID: string) => Promise<void> | void;}@@ -593,19 +606,349 @@ const FriendRequestRow: React.FC<FriendRequestRowProps> = ({);};+interface AddFriendSheetProps {+ friendRequests: SpaceFriendRequest[];+ friends: FriendProfile[];+ onAddFriend: FriendsScreenProps["onAddFriend"];+ onClose: () => void;+ open: boolean;+ username: string;+}++const friendRequestErrorMessage = (error: unknown, username: string) => {+ if (!error || typeof error != "object") {+ return "Couldn't send the friend request. Please try again.";+ }++ const { message, status } = error as {+ message?: unknown;+ status?: unknown;+ };+ if (status == 404) return `No Space profile found for @${username}.`;+ if (+ status == 400 &&+ typeof message == "string" &&+ message.includes("cannot add yourself")+ ) {+ return "You can't add yourself as a friend.";+ }+ if (status == 409) {+ return `@${username} can't receive more friend requests right now.`;+ }+ return "Couldn't send the friend request. Please try again.";+};++const AddFriendSheet: React.FC<AddFriendSheetProps> = ({+ friendRequests,+ friends,+ onAddFriend,+ onClose,+ open,+ username: currentUsername,+}) => {+ const titleID = React.useId();+ const isBottomSheet = useMediaQuery("(max-width: 599px)");+ const [username, setUsername] = React.useState("");+ const [isSubmitting, setIsSubmitting] = React.useState(false);+ const [isSent, setIsSent] = React.useState(false);+ const [errorMessage, setErrorMessage] = React.useState<string>();++ const submit = () => {+ if (isSubmitting || isSent) return;++ const normalizedUsername = normalizeSpaceUsername(username);+ const validationError = normalizedUsername+ ? spaceUsernameValidationError(normalizedUsername)+ : "Enter a username.";+ if (validationError) {+ setErrorMessage(validationError);+ return;+ }+ if (normalizedUsername == normalizeSpaceUsername(currentUsername)) {+ setErrorMessage("You can't add yourself as a friend.");+ return;+ }+ if (+ friends.some(+ (friend) =>+ normalizeSpaceUsername(friend.username) ==+ normalizedUsername,+ )+ ) {+ setErrorMessage(+ `You're already friends with @${normalizedUsername}.`,+ );+ return;+ }+ if (+ friendRequests.some(+ (request) =>+ request.direction == "sent" &&+ normalizeSpaceUsername(request.friend.username) ==+ normalizedUsername,+ )+ ) {+ setErrorMessage(+ `Friend request already sent to @${normalizedUsername}.`,+ );+ return;+ }++ setErrorMessage(undefined);+ setIsSubmitting(true);+ void onAddFriend(normalizedUsername)+ .then(() => setIsSent(true))+ .catch((error: unknown) => {+ log.error("Failed to send space friend request", error);+ setErrorMessage(+ friendRequestErrorMessage(error, normalizedUsername),+ );+ })+ .finally(() => setIsSubmitting(false));+ };++ React.useEffect(() => {+ if (!open || !isSent) return;++ const timeoutID = window.setTimeout(onClose, spaceActionDoneDurationMs);+ return () => window.clearTimeout(timeoutID);+ }, [isSent, onClose, open]);++ return (+ <Dialog+ open={open}+ onClose={isSubmitting ? undefined : onClose}+ maxWidth={false}+ aria-labelledby={titleID}+ slots={+ isBottomSheet+ ? { transition: SpaceBottomSheetTransition }+ : undefined+ }+ slotProps={{+ paper: {+ sx: {+ bgcolor: "#FAFAFA",+ borderRadius: "28px 28px 0 0",+ bottom: 0,+ boxShadow: "none",+ boxSizing: "border-box",+ left: 0,+ m: 0,+ maxWidth: "none",+ p: "26px 20px 24px",+ position: "fixed",+ width: "100vw",+ "@media (min-width: 600px)": {+ borderRadius: "20px",+ bottom: "auto",+ left: "50%",+ maxWidth: 363,+ top: "50%",+ transform: "translate(-50%, -50%)",+ width: 363,+ },+ },+ },+ transition: {+ onExited: () => {+ setUsername("");+ setIsSent(false);+ setErrorMessage(undefined);+ },+ },+ }}+ >+ <Box+ component="form"+ noValidate+ onSubmit={(event) => {+ event.preventDefault();+ submit();+ }}+ sx={{+ maxWidth: 320,+ mx: "auto",+ width: "100%",+ "@media (min-width: 600px)": { maxWidth: "none" },+ }}+ >+ <Box+ component="h2"+ id={titleID}+ sx={{+ color: textBase,+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 15,+ fontWeight: 600,+ lineHeight: "20px",+ m: 0,+ px: "20px",+ textAlign: "center",+ }}+ >+ Add a friend+ </Box>+ <Box+ sx={{+ color: "#666666",+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 13,+ lineHeight: "18px",+ mt: "8px",+ textAlign: "center",+ }}+ >+ Enter your friend's username to add them on Space+ </Box>+ <Box+ component="label"+ sx={{ display: "block", mt: "20px", width: "100%" }}+ >+ <Box+ sx={{+ alignItems: "center",+ bgcolor: "#F2F2F2",+ border: `1px solid ${errorMessage ? dangerColor : "transparent"}`,+ borderRadius: "14px",+ display: "flex",+ height: 48,+ px: "14px",+ width: "100%",+ "&:focus-within": {+ borderColor: green,+ boxShadow: `0 0 0 1px ${green}`,+ },+ }}+ >+ <Box+ component="span"+ aria-hidden+ sx={{+ color: textSoft,+ flexShrink: 0,+ fontFamily:+ '"Inter Variable", Inter, sans-serif',+ fontSize: 14,+ fontWeight: 600,+ }}+ >+ @+ </Box>+ <Box+ component="input"+ autoCapitalize="none"+ autoComplete="off"+ autoCorrect="off"+ autoFocus+ aria-invalid={Boolean(errorMessage) || undefined}+ disabled={isSubmitting || isSent}+ onChange={(+ event: React.ChangeEvent<HTMLInputElement>,+ ) => {+ const value = event.target.value;+ setUsername(+ value.startsWith("@")+ ? value.slice(1)+ : value,+ );+ setErrorMessage(undefined);+ }}+ placeholder="username"+ spellCheck={false}+ value={username}+ sx={{+ bgcolor: "transparent",+ border: 0,+ color: textBase,+ flex: 1,+ fontFamily:+ '"Inter Variable", Inter, sans-serif',+ fontSize: 14,+ fontWeight: 500,+ height: "100%",+ minWidth: 0,+ outline: 0,+ p: 0,+ "&::placeholder": { color: "#888", opacity: 1 },+ }}+ />+ </Box>+ </Box>+ {errorMessage && (+ <Box+ role="alert"+ sx={{+ color: dangerColor,+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 13,+ fontWeight: 600,+ lineHeight: "18px",+ mt: "8px",+ textAlign: "center",+ }}+ >+ {errorMessage}+ </Box>+ )}+ <Box+ className="green-bg"+ component="button"+ type="submit"+ disabled={isSubmitting || isSent}+ sx={{+ alignItems: "center",+ bgcolor: green,+ border: 0,+ borderRadius: "20px",+ color: "#FFFFFF",+ cursor: isSubmitting || isSent ? "default" : "pointer",+ display: "flex",+ fontFamily: '"Inter Variable", Inter, sans-serif',+ fontSize: 14,+ fontWeight: 600,+ height: 48,+ justifyContent: "center",+ lineHeight: "20px",+ mt: "20px",+ px: "24px",+ width: "100%",+ "&:disabled": { opacity: isSent ? 1 : 0.6 },+ "&:focus-visible": {+ outline: `2px solid ${green}`,+ outlineOffset: 2,+ },+ }}+ >+ {isSubmitting ? (+ <SpaceActionFeedbackIcon phase="busy" />+ ) : isSent ? (+ "Request sent"+ ) : (+ "Send request"+ )}+ </Box>+ </Box>+ </Dialog>+ );+};+export const FriendsScreen: React.FC<FriendsScreenProps> = ({friendRequests,friends,isLoading,onAcceptFriendRequest,+ onAddFriend,onDeleteFriendRequest,onLoadFriendAvatar,onBack,onMessage,onOpenFriend,profileLink,+ username,onUnfriend,}) => {+ const [isAddFriendOpen, setIsAddFriendOpen] = React.useState(false);const [friendToUnfriend, setFriendToUnfriend] =React.useState<FriendProfile | null>(null);const [unfriendActionPhase, setUnfriendActionPhase] =@@ -784,18 +1127,16 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({<Boxcomponent="button"type="button"- aria-label="Invite friends"- disabled={!profileLink}- onClick={() =>- profileLink &&- openSpaceShareLinkDialog(profileLink, "invite")- }+ aria-expanded={isAddFriendOpen}+ aria-label="Add friend"+ disabled={isLoading}+ onClick={() => setIsAddFriendOpen(true)}sx={{alignItems: "center",bgcolor: "transparent",border: 0,color: textBase,- cursor: profileLink ? "pointer" : "default",+ cursor: isLoading ? "default" : "pointer",display: "flex",height: spaceTouchTargetSize,justifyContent: "flex-end",@@ -817,6 +1158,15 @@ export const FriendsScreen: React.FC<FriendsScreenProps> = ({</Box></Box>+ <AddFriendSheet+ friendRequests={friendRequests}+ friends={friends}+ onAddFriend={onAddFriend}+ onClose={() => setIsAddFriendOpen(false)}+ open={isAddFriendOpen}+ username={username}+ />+{isLoading ? (<Boxsx={{