Skip to content

armgit / ente

Public Imported from GitHub
0

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

Allow Space profile photo edits

Anand Baburajan199b88de7117
diff --git a/web/apps/space/src/components/SpaceAvatarCropPage.tsx b/web/apps/space/src/components/SpaceAvatarCropPage.tsx
new file mode 100644
index 0000000000..4c8e68734c
--- /dev/null
+++ b/web/apps/space/src/components/SpaceAvatarCropPage.tsx
@@ -0,0 +1,407 @@
+import { ArrowLeft02Icon } from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { Box } from "@mui/material";
+import React from "react";
+import Cropper, { type Area, type Point } from "react-easy-crop";
+
+const green = "#08C225";
+const textBase = "#000";
+const textLight = "#969696";
+const warning = "#F63A3A";
+
+interface SpaceAvatarCropPageProps {
+ background: string;
+ crop: Point;
+ errorMessage?: string;
+ headerVariant?: "app" | "setup";
+ imageURL: string;
+ isDoneDisabled?: boolean;
+ isSaving?: boolean;
+ onBack: () => void;
+ onChooseAnother: () => void;
+ onCropChange: (crop: Point) => void;
+ onCropComplete: (croppedArea: Area, croppedAreaPixels: Area) => void;
+ onDone: () => void;
+ onZoomChange: (zoom: number) => void;
+ title?: string;
+ zoom: number;
+}
+
+const SetupBackIcon: React.FC = () => (
+ <Box
+ component="svg"
+ viewBox="0 0 24 24"
+ aria-hidden
+ sx={{ display: "block", height: 24, width: 24 }}
+ >
+ <path
+ d="M15 6L9 12L15 18"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ />
+ </Box>
+);
+
+const SpaceAvatarCropPageButton: React.FC<{
+ children: React.ReactNode;
+ disabled?: boolean;
+ onClick?: () => void;
+}> = ({ children, disabled = false, onClick }) => (
+ <Box
+ className={!disabled ? "green-bg" : undefined}
+ component="button"
+ type="button"
+ disabled={disabled}
+ onClick={onClick}
+ sx={{
+ alignItems: "center",
+ bgcolor: disabled ? "#F5F5F5" : green,
+ border: 0,
+ borderRadius: "20px",
+ color: disabled ? textLight : "white",
+ cursor: disabled ? "default" : "pointer",
+ display: "flex",
+ flex: "0 0 auto",
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 500,
+ height: 44,
+ justifyContent: "center",
+ lineHeight: "20px",
+ minWidth: 0,
+ px: 2,
+ width: "100%",
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: 3,
+ },
+ "&:hover": !disabled ? { bgcolor: "#07AE22" } : undefined,
+ }}
+ >
+ {children}
+ </Box>
+);
+
+const SpaceAvatarCropPageLinkButton: React.FC<{
+ children: React.ReactNode;
+ disabled?: boolean;
+ onClick?: () => void;
+}> = ({ children, disabled = false, onClick }) => (
+ <Box
+ component="button"
+ type="button"
+ disabled={disabled}
+ onClick={onClick}
+ sx={{
+ alignItems: "center",
+ bgcolor: "transparent",
+ border: 0,
+ color: "#666",
+ cursor: disabled ? "default" : "pointer",
+ display: "flex",
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 500,
+ justifyContent: "center",
+ lineHeight: "20px",
+ minWidth: 0,
+ opacity: 0.8,
+ p: 0,
+ textAlign: "center",
+ textDecoration: "underline",
+ textDecorationSkipInk: "none",
+ textUnderlineOffset: "2px",
+ textUnderlinePosition: "from-font",
+ width: "fit-content",
+ alignSelf: "center",
+ "&:focus-visible": {
+ borderRadius: "4px",
+ outline: `2px solid ${green}`,
+ outlineOffset: 4,
+ },
+ }}
+ >
+ {children}
+ </Box>
+);
+
+export const SpaceAvatarCropPage: React.FC<SpaceAvatarCropPageProps> = ({
+ background,
+ crop,
+ errorMessage,
+ headerVariant = "setup",
+ imageURL,
+ isDoneDisabled = false,
+ isSaving = false,
+ onBack,
+ onChooseAnother,
+ onCropChange,
+ onCropComplete,
+ onDone,
+ onZoomChange,
+ title = "Edit profile picture",
+ zoom,
+}) => {
+ const isAppHeader = headerVariant == "app";
+
+ return (
+ <Box
+ component="main"
+ sx={{
+ "--avatar-crop-size": isAppHeader
+ ? "min(calc(100vw - 48px), calc(100dvh - 308px), 342px)"
+ : "min(calc(100vw - 48px), calc(100dvh - 294px), 342px)",
+ bgcolor: background,
+ color: textBase,
+ display: "grid",
+ height: "100dvh",
+ inset: 0,
+ overflow: "hidden",
+ placeItems: { xs: "stretch", sm: "start center" },
+ position: "fixed",
+ width: "100%",
+ }}
+ >
+ <Box
+ sx={{
+ bgcolor: background,
+ boxSizing: "border-box",
+ display: "grid",
+ gridTemplateRows: isAppHeader
+ ? "56px auto auto minmax(0, 1fr) auto"
+ : "42px auto auto minmax(0, 1fr) auto",
+ height: "100%",
+ minHeight: 0,
+ mx: "auto",
+ overflowX: "hidden",
+ overflowY: "hidden",
+ pb: "calc(24px + env(safe-area-inset-bottom))",
+ pt: isAppHeader ? 0 : "32px",
+ px: isAppHeader ? 0 : 3,
+ width: "100%",
+ "@media (min-width: 600px)": { maxWidth: 390 },
+ }}
+ >
+ <Box
+ component="header"
+ sx={
+ isAppHeader
+ ? {
+ alignItems: "center",
+ display: "grid",
+ gridTemplateColumns: "24px 1fr 24px",
+ height: 56,
+ px: 2,
+ width: "100%",
+ }
+ : {
+ display: "grid",
+ gridTemplateColumns: "42px 1fr 42px",
+ height: 42,
+ width: "100%",
+ }
+ }
+ >
+ <Box
+ component="button"
+ type="button"
+ aria-label="Back"
+ disabled={isSaving}
+ onClick={onBack}
+ sx={
+ isAppHeader
+ ? {
+ alignItems: "center",
+ bgcolor: "transparent",
+ border: 0,
+ color: textBase,
+ cursor: isSaving ? "default" : "pointer",
+ display: "flex",
+ height: 24,
+ justifyContent: "flex-start",
+ ml: "-2px",
+ p: 0,
+ width: 24,
+ "&:focus-visible": {
+ borderRadius: "50%",
+ outline: `2px solid ${green}`,
+ outlineOffset: 2,
+ },
+ }
+ : {
+ alignItems: "center",
+ bgcolor: "transparent",
+ border: 0,
+ color: textBase,
+ cursor: isSaving ? "default" : "pointer",
+ display: "flex",
+ height: 42,
+ justifyContent: "flex-start",
+ p: 0,
+ width: 42,
+ "&:focus-visible": {
+ borderRadius: "50%",
+ outline: `2px solid ${green}`,
+ outlineOffset: 2,
+ },
+ }
+ }
+ >
+ {isAppHeader ? (
+ <HugeiconsIcon
+ icon={ArrowLeft02Icon}
+ size={24}
+ strokeWidth={1.8}
+ />
+ ) : (
+ <SetupBackIcon />
+ )}
+ </Box>
+ <Box
+ component="h1"
+ sx={{
+ alignSelf: "center",
+ color: textBase,
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: isAppHeader ? 18 : 20,
+ fontWeight: isAppHeader ? 700 : 600,
+ justifySelf: "center",
+ lineHeight: isAppHeader ? "24px" : "28px",
+ m: 0,
+ minWidth: 0,
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ whiteSpace: "nowrap",
+ }}
+ >
+ {title}
+ </Box>
+ <Box />
+ </Box>
+
+ <Box
+ sx={{
+ alignSelf: "center",
+ bgcolor: "#111",
+ borderRadius: "8px",
+ height: "var(--avatar-crop-size)",
+ aspectRatio: "1 / 1",
+ justifySelf: "center",
+ mt: { xs: "24px", sm: "32px" },
+ overflow: "hidden",
+ position: "relative",
+ width: "var(--avatar-crop-size)",
+ }}
+ >
+ <Cropper
+ aspect={1}
+ crop={crop}
+ cropShape="round"
+ disableAutomaticStylesInjection
+ image={imageURL}
+ maxZoom={3}
+ minZoom={1}
+ objectFit="cover"
+ onCropChange={onCropChange}
+ onCropComplete={onCropComplete}
+ onZoomChange={onZoomChange}
+ showGrid={false}
+ zoom={zoom}
+ />
+ </Box>
+
+ <Box
+ sx={{
+ boxSizing: "border-box",
+ mt: "28px",
+ px: isAppHeader ? 3 : 0,
+ width: "100%",
+ }}
+ >
+ <Box
+ component="label"
+ htmlFor="space-avatar-zoom"
+ sx={{
+ border: 0,
+ clip: "rect(0 0 0 0)",
+ height: 1,
+ m: -1,
+ overflow: "hidden",
+ p: 0,
+ position: "absolute",
+ width: 1,
+ }}
+ >
+ Zoom
+ </Box>
+ <Box
+ component="input"
+ id="space-avatar-zoom"
+ type="range"
+ min={1}
+ max={3}
+ step={0.01}
+ value={zoom}
+ onChange={(event) =>
+ onZoomChange(Number(event.target.value))
+ }
+ sx={{
+ accentColor: green,
+ display: "block",
+ m: 0,
+ maxWidth: "100%",
+ width: "100%",
+ }}
+ />
+ </Box>
+ {errorMessage && (
+ <Box
+ role="alert"
+ sx={{
+ color: warning,
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 500,
+ lineHeight: "18px",
+ mt: 2,
+ px: isAppHeader ? 3 : 0,
+ textAlign: "center",
+ }}
+ >
+ {errorMessage}
+ </Box>
+ )}
+
+ <Box
+ sx={{
+ bgcolor: background,
+ boxSizing: "border-box",
+ display: "flex",
+ flexDirection: "column",
+ gap: "16px",
+ gridRow: 5,
+ pt: 3,
+ px: isAppHeader ? 3 : 0,
+ width: "100%",
+ }}
+ >
+ <SpaceAvatarCropPageButton
+ disabled={isSaving || isDoneDisabled}
+ onClick={onDone}
+ >
+ {isSaving ? "Saving..." : "Done"}
+ </SpaceAvatarCropPageButton>
+ <SpaceAvatarCropPageLinkButton
+ disabled={isSaving}
+ onClick={onChooseAnother}
+ >
+ Change picture
+ </SpaceAvatarCropPageLinkButton>
+ </Box>
+ </Box>
+ </Box>
+ );
+};
diff --git a/web/apps/space/src/components/SpaceAvatarEditButton.tsx b/web/apps/space/src/components/SpaceAvatarEditButton.tsx
new file mode 100644
index 0000000000..93e6fd7c87
--- /dev/null
+++ b/web/apps/space/src/components/SpaceAvatarEditButton.tsx
@@ -0,0 +1,65 @@
+import { Box } from "@mui/material";
+import React from "react";
+
+const green = "#08C225";
+const textBase = "#000";
+
+const PencilIcon: React.FC = () => (
+ <Box
+ component="svg"
+ viewBox="0 0 24 24"
+ aria-hidden
+ sx={{ display: "block", height: 16, width: 16 }}
+ >
+ <path
+ d="M14.25 5.25L18.75 9.75M4.75 19.25L8.9 18.45L19.35 8C20.25 7.1 20.25 5.65 19.35 4.75C18.45 3.85 17 3.85 16.1 4.75L5.65 15.2L4.75 19.25Z"
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.8"
+ />
+ </Box>
+);
+
+interface SpaceAvatarEditButtonProps {
+ ariaLabel?: string;
+ disabled?: boolean;
+ onClick?: () => void;
+}
+
+export const SpaceAvatarEditButton: React.FC<SpaceAvatarEditButtonProps> = ({
+ ariaLabel = "Edit profile picture",
+ disabled = false,
+ onClick,
+}) => (
+ <Box
+ component="button"
+ type="button"
+ aria-label={ariaLabel}
+ disabled={disabled}
+ onClick={onClick}
+ sx={{
+ alignItems: "center",
+ bgcolor: "white",
+ border: 0,
+ borderRadius: "50%",
+ bottom: 10,
+ boxShadow: "0 1px 3px rgba(0, 0, 0, 0.08)",
+ color: textBase,
+ cursor: disabled ? "default" : "pointer",
+ display: "flex",
+ height: 26,
+ justifyContent: "center",
+ position: "absolute",
+ right: 2,
+ width: 26,
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: 3,
+ },
+ }}
+ >
+ <PencilIcon />
+ </Box>
+);
diff --git a/web/apps/space/src/pages/app/profile.tsx b/web/apps/space/src/pages/app/profile.tsx
index d03dd96148..774316b1ce 100644
--- a/web/apps/space/src/pages/app/profile.tsx
+++ b/web/apps/space/src/pages/app/profile.tsx
@@ -25,6 +25,7 @@ const Page: React.FC = () => {
profileLoadError,
profileLoadStatus,
setFriends,
+ setPendingProfileAvatarFile,
} = useSpaceAppState();
const [posts, setPosts] = useState<SpacePost[]>([]);
const [isPostsLoading, setIsPostsLoading] = useState(true);
@@ -102,6 +103,10 @@ const Page: React.FC = () => {
setPosts((currentPosts) => [post, ...currentPosts]);
}}
onDraftPostPublished={() => void router.push(spaceRoutes.home)}
+ onEditProfilePhoto={(file) => {
+ setPendingProfileAvatarFile(file);
+ void router.push(spaceRoutes.editProfilePhoto);
+ }}
onDeletePost={async (postId) => {
await deleteCurrentPost(postId);
setPosts((currentPosts) =>
diff --git a/web/apps/space/src/pages/app/profile/photo.tsx b/web/apps/space/src/pages/app/profile/photo.tsx
new file mode 100644
index 0000000000..3f57d25fb4
--- /dev/null
+++ b/web/apps/space/src/pages/app/profile/photo.tsx
@@ -0,0 +1,78 @@
+import { SpacePageMeta } from "components/SpacePageMeta";
+import { SpaceRouteFallback } from "components/SpaceRouteFallback";
+import { useRouter } from "next/router";
+import React, { useEffect } from "react";
+import { EditProfilePhotoScreen } from "screens/EditProfilePhotoScreen";
+import { profileBackground } from "screens/ProfileScreen";
+import {
+ saveSpaceProfile,
+ spaceProfileErrorMessage,
+} from "services/spaceProfile";
+import { useSpaceAppState } from "state/spaceAppState";
+import { spaceRoutes } from "utils/spaceRoutes";
+
+const Page: React.FC = () => {
+ const router = useRouter();
+ const {
+ pendingProfileAvatarFile,
+ profile,
+ profileLoadError,
+ profileLoadStatus,
+ setPendingProfileAvatarFile,
+ setProfile,
+ } = useSpaceAppState();
+
+ useEffect(() => {
+ if (profileLoadStatus == "ready" && !profile) {
+ void router.replace(spaceRoutes.onboarding);
+ }
+ }, [profile, profileLoadStatus, router]);
+
+ useEffect(() => {
+ if (
+ profileLoadStatus == "ready" &&
+ profile &&
+ !pendingProfileAvatarFile
+ ) {
+ void router.replace(spaceRoutes.profile);
+ }
+ }, [pendingProfileAvatarFile, profile, profileLoadStatus, router]);
+
+ if (profileLoadStatus != "ready" || !profile || !pendingProfileAvatarFile) {
+ return (
+ <SpaceRouteFallback
+ background={profileBackground}
+ message={profileLoadError}
+ />
+ );
+ }
+
+ return (
+ <>
+ <SpacePageMeta themeColor={profileBackground} />
+ <EditProfilePhotoScreen
+ avatarFile={pendingProfileAvatarFile}
+ onBack={() => {
+ setPendingProfileAvatarFile(null);
+ void router.push(spaceRoutes.profile);
+ }}
+ onSave={async (avatarFile) => {
+ try {
+ const savedProfile = await saveSpaceProfile({
+ ...profile,
+ avatarFile,
+ });
+ setProfile(savedProfile);
+ setPendingProfileAvatarFile(null);
+ await router.push(spaceRoutes.profile);
+ } catch (error) {
+ console.error("Space avatar update failed", error);
+ throw new Error(spaceProfileErrorMessage(error));
+ }
+ }}
+ />
+ </>
+ );
+};
+
+export default Page;
diff --git a/web/apps/space/src/screens/EditProfilePhotoScreen.tsx b/web/apps/space/src/screens/EditProfilePhotoScreen.tsx
new file mode 100644
index 0000000000..1946e994a1
--- /dev/null
+++ b/web/apps/space/src/screens/EditProfilePhotoScreen.tsx
@@ -0,0 +1,345 @@
+import { ArrowLeft02Icon } from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { Box } from "@mui/material";
+import { SpaceAvatarCropPage } from "components/SpaceAvatarCropPage";
+import { SpaceLoadingSpinner } from "components/SpaceRouteFallback";
+import React, { useEffect, useRef, useState } from "react";
+import type { Area, Point } from "react-easy-crop";
+import { profileBackground } from "screens/ProfileScreen";
+import {
+ prepareSpaceAvatarImageFromCrop,
+ spaceAvatarCropImageForFile,
+ spaceAvatarImageErrorMessage,
+ spaceAvatarImageInputAccept,
+} from "utils/spacePostImage";
+
+const green = "#08C225";
+const textBase = "#000";
+const warning = "#F63A3A";
+
+interface AvatarCropImage {
+ file: File;
+ url: string;
+}
+
+interface EditProfilePhotoScreenProps {
+ avatarFile: File;
+ onBack: () => void;
+ onSave: (avatarFile: File) => Promise<void>;
+}
+
+const EditProfilePhotoShell: React.FC<
+ React.PropsWithChildren<{ onBack: () => void }>
+> = ({ children, onBack }) => (
+ <Box
+ component="main"
+ sx={{
+ bgcolor: profileBackground,
+ color: textBase,
+ display: "grid",
+ minHeight: "100svh",
+ overflowX: "hidden",
+ placeItems: { xs: "stretch", sm: "start center" },
+ }}
+ >
+ <Box
+ sx={{
+ bgcolor: profileBackground,
+ boxSizing: "border-box",
+ display: "flex",
+ flexDirection: "column",
+ minHeight: "100svh",
+ mx: "auto",
+ width: "100%",
+ "@media (min-width: 600px)": { maxWidth: 390 },
+ }}
+ >
+ <Box
+ component="header"
+ sx={{
+ alignItems: "center",
+ display: "grid",
+ gridTemplateColumns: "24px 1fr 24px",
+ height: 56,
+ px: 2,
+ width: "100%",
+ }}
+ >
+ <Box
+ component="button"
+ type="button"
+ aria-label="Back to profile"
+ onClick={onBack}
+ sx={{
+ alignItems: "center",
+ bgcolor: "transparent",
+ border: 0,
+ color: textBase,
+ cursor: "pointer",
+ display: "flex",
+ height: 24,
+ justifyContent: "flex-start",
+ ml: "-2px",
+ p: 0,
+ width: 24,
+ "&:focus-visible": {
+ borderRadius: "50%",
+ outline: `2px solid ${green}`,
+ outlineOffset: 2,
+ },
+ }}
+ >
+ <HugeiconsIcon
+ icon={ArrowLeft02Icon}
+ size={24}
+ strokeWidth={1.8}
+ />
+ </Box>
+ <Box
+ component="h1"
+ sx={{
+ color: textBase,
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 18,
+ fontWeight: 700,
+ justifySelf: "center",
+ lineHeight: "24px",
+ m: 0,
+ }}
+ >
+ Edit profile photo
+ </Box>
+ <Box />
+ </Box>
+ {children}
+ </Box>
+ </Box>
+);
+
+export const EditProfilePhotoScreen: React.FC<EditProfilePhotoScreenProps> = ({
+ avatarFile,
+ onBack,
+ onSave,
+}) => {
+ const [avatarCropImage, setAvatarCropImage] =
+ useState<AvatarCropImage | null>(null);
+ const [avatarCrop, setAvatarCrop] = useState<Point>({ x: 0, y: 0 });
+ const [avatarCropPixels, setAvatarCropPixels] = useState<Area | null>(null);
+ const [avatarError, setAvatarError] = useState<string>();
+ const [avatarZoom, setAvatarZoom] = useState(1);
+ const [isApplyingAvatarCrop, setIsApplyingAvatarCrop] = useState(false);
+ const [isPreparingAvatar, setIsPreparingAvatar] = useState(true);
+ const avatarCropUrlRef = useRef<string | null>(null);
+ const avatarSelectionIDRef = useRef(0);
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
+
+ useEffect(
+ () => () => {
+ if (avatarCropUrlRef.current) {
+ URL.revokeObjectURL(avatarCropUrlRef.current);
+ }
+ },
+ [],
+ );
+
+ const prepareSelectedAvatar = React.useCallback(
+ async (file: File, selectionID: number) => {
+ setIsPreparingAvatar(true);
+ if (avatarCropUrlRef.current) {
+ URL.revokeObjectURL(avatarCropUrlRef.current);
+ avatarCropUrlRef.current = null;
+ }
+ setAvatarCropImage(null);
+ setAvatarCropPixels(null);
+ try {
+ const cropImage = await spaceAvatarCropImageForFile(file);
+ if (avatarSelectionIDRef.current != selectionID) {
+ URL.revokeObjectURL(cropImage.url);
+ return;
+ }
+
+ avatarCropUrlRef.current = cropImage.url;
+ setAvatarCrop({ x: 0, y: 0 });
+ setAvatarCropImage({ file, url: cropImage.url });
+ setAvatarCropPixels(null);
+ setAvatarError(undefined);
+ setAvatarZoom(1);
+ } catch (error) {
+ if (avatarSelectionIDRef.current != selectionID) return;
+ console.error("Failed to prepare space avatar", error);
+ setAvatarError(spaceAvatarImageErrorMessage(error));
+ } finally {
+ if (avatarSelectionIDRef.current == selectionID) {
+ setIsPreparingAvatar(false);
+ }
+ }
+ },
+ [],
+ );
+
+ useEffect(() => {
+ const selectionID = avatarSelectionIDRef.current + 1;
+ avatarSelectionIDRef.current = selectionID;
+ setAvatarError(undefined);
+ void prepareSelectedAvatar(avatarFile, selectionID);
+ }, [avatarFile, prepareSelectedAvatar]);
+
+ const handleAvatarChange: React.ChangeEventHandler<HTMLInputElement> = (
+ event,
+ ) => {
+ const file = event.target.files?.[0];
+ event.target.value = "";
+ if (!file) return;
+
+ const selectionID = avatarSelectionIDRef.current + 1;
+ avatarSelectionIDRef.current = selectionID;
+ setAvatarError(undefined);
+ void prepareSelectedAvatar(file, selectionID);
+ };
+
+ const applyAvatarCrop = async () => {
+ if (!avatarCropImage || !avatarCropPixels) return;
+
+ setIsApplyingAvatarCrop(true);
+ let avatarFile: File;
+ try {
+ const avatar = await prepareSpaceAvatarImageFromCrop(
+ avatarCropImage.file,
+ avatarCropImage.url,
+ avatarCropPixels,
+ );
+ avatarFile = avatar.file;
+ } catch (error) {
+ console.error("Failed to crop space avatar", error);
+ setAvatarError(spaceAvatarImageErrorMessage(error));
+ setIsApplyingAvatarCrop(false);
+ return;
+ }
+
+ try {
+ await onSave(avatarFile);
+ } catch (error) {
+ console.error("Failed to save space avatar", error);
+ setAvatarError(
+ error instanceof Error
+ ? error.message
+ : "Couldn't save your profile. Please try again.",
+ );
+ setIsApplyingAvatarCrop(false);
+ }
+ };
+
+ const avatarFileInput = (
+ <Box
+ component="input"
+ ref={fileInputRef}
+ accept={spaceAvatarImageInputAccept}
+ onChange={handleAvatarChange}
+ type="file"
+ sx={{ display: "none" }}
+ />
+ );
+
+ if (!avatarCropImage) {
+ return (
+ <>
+ {avatarFileInput}
+ <EditProfilePhotoShell onBack={onBack}>
+ <Box
+ sx={{
+ alignItems: "center",
+ display: "flex",
+ flex: 1,
+ flexDirection: "column",
+ justifyContent: "center",
+ px: 3,
+ textAlign: "center",
+ }}
+ >
+ {isPreparingAvatar ? (
+ <SpaceLoadingSpinner ariaLabel="Preparing profile photo" />
+ ) : (
+ <>
+ {avatarError && (
+ <Box
+ role="alert"
+ sx={{
+ color: warning,
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 500,
+ lineHeight: "18px",
+ maxWidth: 300,
+ }}
+ >
+ {avatarError}
+ </Box>
+ )}
+ <Box
+ className="green-bg"
+ component="button"
+ type="button"
+ onClick={() =>
+ fileInputRef.current?.click()
+ }
+ sx={{
+ alignItems: "center",
+ bgcolor: green,
+ border: 0,
+ borderRadius: "20px",
+ color: "white",
+ cursor: "pointer",
+ display: "flex",
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 14,
+ fontWeight: 500,
+ height: 44,
+ justifyContent: "center",
+ lineHeight: "20px",
+ mt: avatarError ? 3 : 0,
+ px: 2,
+ width: "100%",
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: 3,
+ },
+ "&:hover": { bgcolor: "#07AE22" },
+ }}
+ >
+ Change picture
+ </Box>
+ </>
+ )}
+ </Box>
+ </EditProfilePhotoShell>
+ </>
+ );
+ }
+
+ return (
+ <>
+ {avatarFileInput}
+ <SpaceAvatarCropPage
+ background={profileBackground}
+ crop={avatarCrop}
+ errorMessage={avatarError}
+ headerVariant="app"
+ imageURL={avatarCropImage.url}
+ isDoneDisabled={!avatarCropPixels}
+ isSaving={isApplyingAvatarCrop}
+ onBack={onBack}
+ onChooseAnother={() => fileInputRef.current?.click()}
+ onCropChange={setAvatarCrop}
+ onCropComplete={(_croppedArea, croppedAreaPixels) =>
+ setAvatarCropPixels(croppedAreaPixels)
+ }
+ onDone={() => void applyAvatarCrop()}
+ onZoomChange={setAvatarZoom}
+ title="Edit profile photo"
+ zoom={avatarZoom}
+ />
+ </>
+ );
+};
diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsx
index 187c482d28..02f4bbeebe 100644
--- a/web/apps/space/src/screens/ProfileScreen.tsx
+++ b/web/apps/space/src/screens/ProfileScreen.tsx
@@ -6,6 +6,7 @@ import {
import { HugeiconsIcon } from "@hugeicons/react";
import { Box } from "@mui/material";
import { SpaceActionFeedbackIcon } from "components/SpaceActionFeedback";
+import { SpaceAvatarEditButton } from "components/SpaceAvatarEditButton";
import {
SpaceFileViewer,
type SpaceLiker,
@@ -21,6 +22,7 @@ import { createLocalPostPhoto } from "utils/localPostPhoto";
import { firstNameFrom, initialsFor } from "utils/spaceDisplay";
import {
prepareSpacePostImage,
+ spaceAvatarImageInputAccept,
spacePostImageErrorMessage,
spacePostImageInputAccept,
type PreparedSpacePostImage,
@@ -146,6 +148,7 @@ interface ProfileScreenProps {
) => Promise<void>;
onDeletePost?: (postId: number) => Promise<void> | void;
onDraftPostPublished?: () => void;
+ onEditProfilePhoto?: (file: File) => void;
onLoadPostLikers?: (postId: number) => Promise<SpaceLiker[]>;
onOpenFriend?: (friendID: string) => void;
onOpenFriends?: () => void;
@@ -165,6 +168,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
onCreatePost,
onDeletePost,
onDraftPostPublished,
+ onEditProfilePhoto,
onLoadPostLikers,
onOpenFriend,
onOpenFriends,
@@ -183,6 +187,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
);
const [loadedPhotoDimensionsByURL, setLoadedPhotoDimensionsByURL] =
useState<Record<string, ProfilePhotoDimensions>>({});
+ const avatarInputRef = React.useRef<HTMLInputElement | null>(null);
const postInputRef = React.useRef<HTMLInputElement | null>(null);
const localPostObjectUrlsRef = React.useRef<Set<string>>(new Set());
const isPublicProfile = headerVariant == "public";
@@ -218,6 +223,14 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
URL.revokeObjectURL(objectUrl);
localPostObjectUrlsRef.current.delete(objectUrl);
}, []);
+ const openAvatarPhotoPicker = () => avatarInputRef.current?.click();
+ const handleAvatarPhotoSelect: React.ChangeEventHandler<
+ HTMLInputElement
+ > = (event) => {
+ const file = event.target.files?.[0];
+ event.target.value = "";
+ if (file) onEditProfilePhoto?.(file);
+ };
const openPostPhotoPicker = () => {
if (isPostPhotoPreparing) return;
@@ -364,6 +377,16 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
sx={{ display: "none" }}
/>
)}
+ {isOwnerProfile && onEditProfilePhoto && (
+ <Box
+ ref={avatarInputRef}
+ component="input"
+ type="file"
+ accept={spaceAvatarImageInputAccept}
+ onChange={handleAvatarPhotoSelect}
+ sx={{ display: "none" }}
+ />
+ )}
<Box
className="green-bg"
sx={{
@@ -564,48 +587,58 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
}}
>
<Box
- sx={{
- alignItems: "center",
- aspectRatio: "1 / 1",
- bgcolor: profile.avatarUrl
- ? "transparent"
- : paleGreen,
- border: "4px solid #FFFFFF",
- borderRadius: "50%",
- boxSizing: "border-box",
- color: green,
- display: "flex",
- justifyContent: "center",
- overflow: "hidden",
- width: profileAvatarSize,
- }}
+ sx={{ position: "relative", width: profileAvatarSize }}
>
- {profile.avatarUrl ? (
- <Box
- component="img"
- alt=""
- src={profile.avatarUrl}
- sx={{
- display: "block",
- height: "100%",
- objectFit: "cover",
- objectPosition: "center",
- width: "100%",
- }}
+ <Box
+ sx={{
+ alignItems: "center",
+ aspectRatio: "1 / 1",
+ bgcolor: profile.avatarUrl
+ ? "transparent"
+ : paleGreen,
+ border: "4px solid #FFFFFF",
+ borderRadius: "50%",
+ boxSizing: "border-box",
+ color: green,
+ display: "flex",
+ justifyContent: "center",
+ overflow: "hidden",
+ width: "100%",
+ }}
+ >
+ {profile.avatarUrl ? (
+ <Box
+ component="img"
+ alt=""
+ src={profile.avatarUrl}
+ sx={{
+ display: "block",
+ height: "100%",
+ objectFit: "cover",
+ objectPosition: "center",
+ width: "100%",
+ }}
+ />
+ ) : (
+ <Box
+ sx={{
+ color: green,
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 38,
+ fontWeight: 800,
+ lineHeight: 1,
+ }}
+ >
+ {initials}
+ </Box>
+ )}
+ </Box>
+ {isOwnerProfile && onEditProfilePhoto && (
+ <SpaceAvatarEditButton
+ ariaLabel="Edit profile photo"
+ onClick={openAvatarPhotoPicker}
/>
- ) : (
- <Box
- sx={{
- color: green,
- fontFamily:
- '"Inter Variable", Inter, sans-serif',
- fontSize: 38,
- fontWeight: 800,
- lineHeight: 1,
- }}
- >
- {initials}
- </Box>
)}
</Box>
<Box
diff --git a/web/apps/space/src/screens/SetupProfileScreen.tsx b/web/apps/space/src/screens/SetupProfileScreen.tsx
index 59de59025d..6a85512404 100644
--- a/web/apps/space/src/screens/SetupProfileScreen.tsx
+++ b/web/apps/space/src/screens/SetupProfileScreen.tsx
@@ -1,6 +1,8 @@
import { Box } from "@mui/material";
+import { SpaceAvatarCropPage } from "components/SpaceAvatarCropPage";
+import { SpaceAvatarEditButton } from "components/SpaceAvatarEditButton";
import React, { useEffect, useRef, useState } from "react";
-import Cropper, { type Area, type Point } from "react-easy-crop";
+import type { Area, Point } from "react-easy-crop";
import {
prepareSpaceAvatarImageFromCrop,
spaceAvatarCropImageForFile,
@@ -55,21 +57,6 @@ interface TextInputProps {
value?: string;
}
-interface AvatarCropPageProps {
- crop: Point;
- errorMessage?: string;
- imageURL: string;
- isDoneDisabled?: boolean;
- isSaving?: boolean;
- onBack: () => void;
- onChooseAnother: () => void;
- onCropChange: (crop: Point) => void;
- onCropComplete: (croppedArea: Area, croppedAreaPixels: Area) => void;
- onDone: () => void;
- onZoomChange: (zoom: number) => void;
- zoom: number;
-}
-
const BackIcon: React.FC = () => (
<Box
component="svg"
@@ -113,24 +100,6 @@ const UploadIcon: React.FC = () => (
</Box>
);
-const PencilIcon: React.FC = () => (
- <Box
- component="svg"
- viewBox="0 0 24 24"
- aria-hidden
- sx={{ display: "block", height: 14, width: 14 }}
- >
- <path
- d="M14.25 5.25L18.75 9.75M4.75 19.25L8.9 18.45L19.35 8C20.25 7.1 20.25 5.65 19.35 4.75C18.45 3.85 17 3.85 16.1 4.75L5.65 15.2L4.75 19.25Z"
- fill="none"
- stroke="currentColor"
- strokeLinecap="round"
- strokeLinejoin="round"
- strokeWidth="1.8"
- />
- </Box>
-);
-
const CheckIcon: React.FC = () => (
<Box
component="svg"
@@ -261,303 +230,6 @@ const TextInput: React.FC<TextInputProps> = ({
</Box>
);
-const AvatarCropPageButton: React.FC<{
- children: React.ReactNode;
- disabled?: boolean;
- onClick?: () => void;
-}> = ({ children, disabled = false, onClick }) => (
- <Box
- className={!disabled ? "green-bg" : undefined}
- component="button"
- type="button"
- disabled={disabled}
- onClick={onClick}
- sx={{
- alignItems: "center",
- bgcolor: disabled ? "#F5F5F5" : green,
- border: 0,
- borderRadius: "20px",
- color: disabled ? textLight : "white",
- cursor: disabled ? "default" : "pointer",
- display: "flex",
- flex: "0 0 auto",
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 14,
- fontWeight: 500,
- height: 44,
- justifyContent: "center",
- lineHeight: "20px",
- minWidth: 0,
- px: 2,
- width: "100%",
- "&:focus-visible": {
- outline: `2px solid ${green}`,
- outlineOffset: 3,
- },
- "&:hover": !disabled ? { bgcolor: "#07AE22" } : undefined,
- }}
- >
- {children}
- </Box>
-);
-
-const AvatarCropPageLinkButton: React.FC<{
- children: React.ReactNode;
- disabled?: boolean;
- onClick?: () => void;
-}> = ({ children, disabled = false, onClick }) => (
- <Box
- component="button"
- type="button"
- disabled={disabled}
- onClick={onClick}
- sx={{
- alignItems: "center",
- bgcolor: "transparent",
- border: 0,
- color: "#666",
- cursor: disabled ? "default" : "pointer",
- display: "flex",
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 14,
- fontWeight: 500,
- justifyContent: "center",
- lineHeight: "20px",
- minWidth: 0,
- opacity: 0.8,
- p: 0,
- textAlign: "center",
- textDecoration: "underline",
- textDecorationSkipInk: "none",
- textUnderlineOffset: "2px",
- textUnderlinePosition: "from-font",
- width: "fit-content",
- alignSelf: "center",
- "&:focus-visible": {
- borderRadius: "4px",
- outline: `2px solid ${green}`,
- outlineOffset: 4,
- },
- }}
- >
- {children}
- </Box>
-);
-
-const AvatarCropPage: React.FC<AvatarCropPageProps> = ({
- crop,
- errorMessage,
- imageURL,
- isDoneDisabled = false,
- isSaving = false,
- onBack,
- onChooseAnother,
- onCropChange,
- onCropComplete,
- onDone,
- onZoomChange,
- zoom,
-}) => (
- <Box
- component="main"
- sx={{
- "--avatar-crop-size":
- "min(calc(100vw - 48px), calc(100dvh - 294px), 342px)",
- bgcolor: setupProfileBackground,
- color: textBase,
- display: "grid",
- height: "100dvh",
- inset: 0,
- overflow: "hidden",
- placeItems: { xs: "stretch", sm: "start center" },
- position: "fixed",
- width: "100%",
- }}
- >
- <Box
- sx={{
- bgcolor: setupProfileBackground,
- boxSizing: "border-box",
- display: "grid",
- gridTemplateRows: "42px auto auto minmax(0, 1fr) auto",
- height: "100%",
- minHeight: 0,
- mx: "auto",
- overflowX: "hidden",
- overflowY: "hidden",
- pb: "calc(24px + env(safe-area-inset-bottom))",
- pt: "32px",
- px: 3,
- width: "100%",
- "@media (min-width: 600px)": { maxWidth: 390 },
- }}
- >
- <Box
- component="header"
- sx={{
- display: "grid",
- gridTemplateColumns: "42px 1fr 42px",
- height: 42,
- width: "100%",
- }}
- >
- <Box
- component="button"
- type="button"
- aria-label="Back"
- disabled={isSaving}
- onClick={onBack}
- sx={{
- alignItems: "center",
- bgcolor: "transparent",
- border: 0,
- color: textBase,
- cursor: isSaving ? "default" : "pointer",
- display: "flex",
- height: 42,
- justifyContent: "flex-start",
- p: 0,
- width: 42,
- "&:focus-visible": {
- borderRadius: "50%",
- outline: `2px solid ${green}`,
- outlineOffset: 2,
- },
- }}
- >
- <BackIcon />
- </Box>
- <Box
- component="h1"
- sx={{
- alignSelf: "center",
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 20,
- fontWeight: 600,
- justifySelf: "center",
- lineHeight: "28px",
- m: 0,
- whiteSpace: "nowrap",
- }}
- >
- Edit profile picture
- </Box>
- <Box />
- </Box>
-
- <Box
- sx={{
- alignSelf: "center",
- bgcolor: "#111",
- borderRadius: "8px",
- height: "var(--avatar-crop-size)",
- aspectRatio: "1 / 1",
- mt: { xs: "24px", sm: "32px" },
- overflow: "hidden",
- position: "relative",
- width: "var(--avatar-crop-size)",
- }}
- >
- <Cropper
- aspect={1}
- crop={crop}
- cropShape="round"
- disableAutomaticStylesInjection
- image={imageURL}
- maxZoom={3}
- minZoom={1}
- objectFit="cover"
- onCropChange={onCropChange}
- onCropComplete={onCropComplete}
- onZoomChange={onZoomChange}
- showGrid={false}
- zoom={zoom}
- />
- </Box>
-
- <Box sx={{ mt: "28px", width: "100%" }}>
- <Box
- component="label"
- htmlFor="space-avatar-zoom"
- sx={{
- border: 0,
- clip: "rect(0 0 0 0)",
- height: 1,
- m: -1,
- overflow: "hidden",
- p: 0,
- position: "absolute",
- width: 1,
- }}
- >
- Zoom
- </Box>
- <Box
- component="input"
- id="space-avatar-zoom"
- type="range"
- min={1}
- max={3}
- step={0.01}
- value={zoom}
- onChange={(event) =>
- onZoomChange(Number(event.target.value))
- }
- sx={{
- accentColor: green,
- display: "block",
- m: 0,
- maxWidth: "100%",
- width: "100%",
- }}
- />
- </Box>
- {errorMessage && (
- <Box
- role="alert"
- sx={{
- color: warning,
- fontFamily: '"Inter Variable", Inter, sans-serif',
- fontSize: 13,
- fontWeight: 500,
- lineHeight: "18px",
- mt: 2,
- textAlign: "center",
- }}
- >
- {errorMessage}
- </Box>
- )}
-
- <Box
- sx={{
- bgcolor: setupProfileBackground,
- boxSizing: "border-box",
- display: "flex",
- flexDirection: "column",
- gap: "16px",
- gridRow: 5,
- pt: 3,
- width: "100%",
- }}
- >
- <AvatarCropPageButton
- disabled={isSaving || isDoneDisabled}
- onClick={onDone}
- >
- {isSaving ? "Saving..." : "Done"}
- </AvatarCropPageButton>
- <AvatarCropPageLinkButton
- disabled={isSaving}
- onClick={onChooseAnother}
- >
- Change picture
- </AvatarCropPageLinkButton>
- </Box>
- </Box>
- </Box>
-);
-
export const SetupProfileScreen: React.FC<SetupProfileScreenProps> = ({
ctaLabel = "Next",
errorMessage,
@@ -724,7 +396,8 @@ export const SetupProfileScreen: React.FC<SetupProfileScreenProps> = ({
return (
<>
{avatarFileInput}
- <AvatarCropPage
+ <SpaceAvatarCropPage
+ background={setupProfileBackground}
crop={avatarCrop}
errorMessage={avatarError}
imageURL={avatarCropImage.url}
@@ -903,37 +576,10 @@ export const SetupProfileScreen: React.FC<SetupProfileScreenProps> = ({
<UploadIcon />
)}
</Box>
- <Box
- component="button"
- type="button"
- aria-label="Edit profile picture"
+ <SpaceAvatarEditButton
disabled={isPreparingAvatar}
onClick={() => fileInputRef.current?.click()}
- sx={{
- alignItems: "center",
- bgcolor: "white",
- border: 0,
- borderRadius: "50%",
- bottom: 6,
- boxShadow: "0 1px 3px rgba(0, 0, 0, 0.08)",
- color: textBase,
- cursor: isPreparingAvatar
- ? "default"
- : "pointer",
- display: "flex",
- height: 30,
- justifyContent: "center",
- position: "absolute",
- right: -2,
- width: 30,
- "&:focus-visible": {
- outline: `2px solid ${green}`,
- outlineOffset: 3,
- },
- }}
- >
- <PencilIcon />
- </Box>
+ />
</Box>
<Box
diff --git a/web/apps/space/src/state/SpaceAppStateProvider.tsx b/web/apps/space/src/state/SpaceAppStateProvider.tsx
index 2270670705..c10132ccad 100644
--- a/web/apps/space/src/state/SpaceAppStateProvider.tsx
+++ b/web/apps/space/src/state/SpaceAppStateProvider.tsx
@@ -37,6 +37,8 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
useState<SpaceLoginCredentials | null>(null);
const [pendingPasskeyVerification, setPendingPasskeyVerification] =
useState<PendingSpacePasskeyVerification | null>(null);
+ const [pendingProfileAvatarFile, setPendingProfileAvatarFile] =
+ useState<File | null>(null);
const [profile, setProfile] = useState<SetupProfile | null>(null);
const [profileLoadError, setProfileLoadError] = useState<string>();
const [profileLoadStatus, setProfileLoadStatus] =
@@ -133,6 +135,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
setProfileLoadStatus("ready");
setPendingLoginCredentials(null);
setPendingPasskeyVerification(null);
+ setPendingProfileAvatarFile(null);
setOnboardingEntrySource("direct");
setFriends(initialFriends());
}, [applyProfile]);
@@ -148,6 +151,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
onboardingEntrySource,
pendingLoginCredentials,
pendingPasskeyVerification,
+ pendingProfileAvatarFile,
profile,
profileLoadError,
profileLoadStatus,
@@ -158,6 +162,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
setOnboardingEntrySource,
setPendingLoginCredentials,
setPendingPasskeyVerification,
+ setPendingProfileAvatarFile,
setProfile: applyProfile,
setSignupEmail,
signupEmail,
@@ -168,6 +173,7 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
onboardingEntrySource,
pendingLoginCredentials,
pendingPasskeyVerification,
+ pendingProfileAvatarFile,
profile,
profileLoadError,
profileLoadStatus,
diff --git a/web/apps/space/src/state/spaceAppState.tsx b/web/apps/space/src/state/spaceAppState.tsx
index a2312b54d2..b56515bdfd 100644
--- a/web/apps/space/src/state/spaceAppState.tsx
+++ b/web/apps/space/src/state/spaceAppState.tsx
@@ -13,6 +13,7 @@ export interface SpaceAppState {
onboardingEntrySource: OnboardingEntrySource;
pendingLoginCredentials: SpaceLoginCredentials | null;
pendingPasskeyVerification: PendingSpacePasskeyVerification | null;
+ pendingProfileAvatarFile: File | null;
profile: SetupProfile | null;
profileLoadError?: string;
profileLoadStatus: SpaceProfileLoadStatus;
@@ -30,6 +31,9 @@ export interface SpaceAppState {
setPendingPasskeyVerification: React.Dispatch<
React.SetStateAction<PendingSpacePasskeyVerification | null>
>;
+ setPendingProfileAvatarFile: React.Dispatch<
+ React.SetStateAction<File | null>
+ >;
setProfile: (profile: SetupProfile | null) => void;
setSignupEmail: React.Dispatch<React.SetStateAction<string>>;
}
diff --git a/web/apps/space/src/utils/spaceRoutes.ts b/web/apps/space/src/utils/spaceRoutes.ts
index 41329c6189..9d40c01c74 100644
--- a/web/apps/space/src/utils/spaceRoutes.ts
+++ b/web/apps/space/src/utils/spaceRoutes.ts
@@ -14,6 +14,7 @@ export const spaceRoutes = {
onboarding: "/",
passkeysFinish: "/passkeys/finish",
passkeysVerify: "/passkeys/verify",
+ editProfilePhoto: "/app/profile/photo",
profile: "/app/profile",
settings: "/app/settings",
setupProfile: (from?: SetupProfileSource) =>