From cb8f0c0aef3cd33730ee27a2e56be64456a64fe2 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Mon, 5 Sep 2022 22:49:03 +0800 Subject: [PATCH] refactor: profile component for better performance --- src/common/component/User/index.tsx | 25 ++++++++++++++++++++++++- src/routes/chat/ChannelChat/index.tsx | 5 +++-- src/routes/chat/index.tsx | 5 +++-- src/routes/home/index.tsx | 5 +++-- src/routes/users/index.tsx | 8 +++++--- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/common/component/User/index.tsx b/src/common/component/User/index.tsx index e7b4a45e..2ccc6897 100644 --- a/src/common/component/User/index.tsx +++ b/src/common/component/User/index.tsx @@ -42,6 +42,30 @@ const User: FC = ({ }; if (!curr) return null; const online = curr.online || curr.uid == loginUid; + if (!popover) + return ( + + +
+ +
+
+ {!compact && {curr?.name}} + {owner && } +
+
+ ); return ( = ({ } diff --git a/src/routes/chat/ChannelChat/index.tsx b/src/routes/chat/ChannelChat/index.tsx index 7c449ddd..9b38d8d3 100644 --- a/src/routes/chat/ChannelChat/index.tsx +++ b/src/routes/chat/ChannelChat/index.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect, memo } from "react"; import { useDebounce } from "rooks"; import { NavLink, useLocation } from "react-router-dom"; import Tippy from "@tippyjs/react"; @@ -32,7 +32,7 @@ type Props = { cid?: number; dropFiles?: File[]; }; -export default function ChannelChat({ cid = 0, dropFiles = [] }: Props) { +function ChannelChat({ cid = 0, dropFiles = [] }: Props) { const { values: agoraConfig } = useConfig("agora"); const { list: msgIds, @@ -216,3 +216,4 @@ export default function ChannelChat({ cid = 0, dropFiles = [] }: Props) { ); } +export default memo(ChannelChat); diff --git a/src/routes/chat/index.tsx b/src/routes/chat/index.tsx index 86c79ce4..ab726a58 100644 --- a/src/routes/chat/index.tsx +++ b/src/routes/chat/index.tsx @@ -1,5 +1,5 @@ // import React from 'react'; -import { useState } from "react"; +import { memo, useState } from "react"; import { useParams } from "react-router-dom"; import StyledWrapper from "./styled"; @@ -14,7 +14,7 @@ import { useAppSelector } from "../../app/store"; import GuestBlankPlaceholder from "./GuestBlankPlaceholder"; import GuestChannelChatInfo from "./GuestChannelChatInfo"; import GuestSessionList from "./GuestSessionList"; -export default function ChatPage() { +function ChatPage() { const [channelModalVisible, setChannelModalVisible] = useState(false); const [usersModalVisible, setUsersModalVisible] = useState(false); const { channel_id = 0, user_id = 0 } = useParams(); @@ -66,3 +66,4 @@ export default function ChatPage() { ); } +export default memo(ChatPage); diff --git a/src/routes/home/index.tsx b/src/routes/home/index.tsx index 2153ff5f..580c72ea 100644 --- a/src/routes/home/index.tsx +++ b/src/routes/home/index.tsx @@ -1,4 +1,4 @@ -// import React from 'react'; +import { memo } from "react"; import { Outlet, NavLink, useLocation, useMatch } from "react-router-dom"; import StyledWrapper from "./styled"; import User from "./User"; @@ -16,7 +16,7 @@ import FavIcon from "../../assets/icons/bookmark.svg"; import FolderIcon from "../../assets/icons/folder.svg"; import { useAppSelector } from "../../app/store"; // const routes = ["/setting", "/setting/channel/:cid"]; -export default function HomePage() { +function HomePage() { const isHomePath = useMatch(`/`); const isChatHomePath = useMatch(`/chat`); const { pathname } = useLocation(); @@ -96,3 +96,4 @@ export default function HomePage() { ); } +export default memo(HomePage); diff --git a/src/routes/users/index.tsx b/src/routes/users/index.tsx index f3e9f565..9a44f360 100644 --- a/src/routes/users/index.tsx +++ b/src/routes/users/index.tsx @@ -1,6 +1,7 @@ -import { useEffect } from "react"; +import { memo, useEffect } from "react"; import { NavLink, useParams, useLocation } from "react-router-dom"; import { useDispatch } from "react-redux"; +import _ from "lodash"; import { updateRememberedNavs } from "../../app/slices/ui"; import Search from "../../common/component/Search"; import User from "../../common/component/User"; @@ -10,12 +11,12 @@ import StyledWrapper from "./styled"; import BlankPlaceholder from "../../common/component/BlankPlaceholder"; import { useAppSelector } from "../../app/store"; -export default function UsersPage() { +function UsersPage() { const dispatch = useDispatch(); const { pathname } = useLocation(); const { user_id } = useParams(); - const userIds = useAppSelector((store) => store.users.ids); + const userIds = useAppSelector((store) => store.users.ids, _.isEqual); useEffect(() => { dispatch(updateRememberedNavs({ key: "user" })); return () => { @@ -46,3 +47,4 @@ export default function UsersPage() { ); } +export default memo(UsersPage);