From 1e924092e2f6f96508d2997df5ad61174d83ba22 Mon Sep 17 00:00:00 2001 From: Aminejv Date: Tue, 3 Aug 2021 18:04:16 +0100 Subject: [PATCH] feat(ProfilePhoto): update size prop to be responsive --- components/core/Profile.js | 2 +- components/core/ProfilePhoto.js | 84 ++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/components/core/Profile.js b/components/core/Profile.js index a39bb2ad..ad4df94a 100644 --- a/components/core/Profile.js +++ b/components/core/Profile.js @@ -612,7 +612,7 @@ export default class Profile extends React.Component {
- + {showStatusIndicator && this.checkStatus({ id: user.id }) && (
)} diff --git a/components/core/ProfilePhoto.js b/components/core/ProfilePhoto.js index e4b9508c..805dbab1 100644 --- a/components/core/ProfilePhoto.js +++ b/components/core/ProfilePhoto.js @@ -1,7 +1,10 @@ import * as React from "react"; import * as Constants from "~/common/constants"; +import * as Utilities from "~/common/utilities"; import { css } from "@emotion/react"; +import { useMemoCompare } from "~/common/hooks"; +import isEqual from "lodash/isEqual"; import Dismissible from "~/components/core/Dismissible"; @@ -26,56 +29,59 @@ const STYLES_AVATAR_ONLINE = css` border-radius: 16px; `; -function BoringAvatar(props) { - let colors = ['A9B9C1', '5B6B74', '3C444A', 'D4DBDF', '293137']; +function BoringAvatar({ avatarCss, ...props }) { + let colors = ["A9B9C1", "5B6B74", "3C444A", "D4DBDF", "293137"]; let avatarUrl = `https://source.boringavatars.com/marble/${props.size}/${props.userId}?square&colors=${colors}`; return ( - - + profile preview ); } -function UploadedAvatar(props) { +function UploadedAvatar({ avatarCss, ...props }) { return ( - {props.visible ? props.popover : null} - {props.online ? : null} - - ) + css={[avatarCss, STYLES_AVATAR]} + captureResize={false} + captureScroll={true} + style={{ + ...props.style, + backgroundImage: `url('${props.url}')`, + cursor: "pointer", + }} + > + {props.visible ? props.popover : null} + {props.online ? : null} + + ); } -export default class ProfilePhoto extends React.Component { - render() { - return ( - <> - {this.props.user.data.photo - ? - : - } - - ); - } +export default function ProfilePhoto({ size, ...props }) { + // NOTE(amine): will calculate only when the size prop changes + const memoizedSizeProp = useMemoCompare(size, isEqual); + const STYLES_SIZE = React.useMemo(() => { + const responsiveStyles = Utilities.mapResponsiveProp(size, (size) => ({ + width: size, + height: size, + })); + return css(responsiveStyles); + }, [memoizedSizeProp]); + + return ( + <> + {props.user.data.photo ? ( + + ) : ( + + )} + + ); }