diff --git a/frontend/src/components/comment.tsx b/frontend/src/components/comment.tsx index 38c52e8..7b42cfd 100644 --- a/frontend/src/components/comment.tsx +++ b/frontend/src/components/comment.tsx @@ -2,8 +2,8 @@ import {Flex, Space, Avatar, Typography, Divider} from 'antd'; import {Comment} from "../declarations/feed/feed" -export function Comments(props: { content: Comment }) { - const {content} = props +export function Comments(props: { content: Comment, avatar?: string, name?: string }) { + const {content, avatar, name} = props return ( -

NeutronStarDAO

+

{name ? name : "NeutronStarDAO"}

{content.content} diff --git a/frontend/src/routes/content.tsx b/frontend/src/routes/content.tsx index 067c548..313bd2f 100644 --- a/frontend/src/routes/content.tsx +++ b/frontend/src/routes/content.tsx @@ -2,84 +2,75 @@ import React, {useEffect, useState} from "react"; import {Layout, Spin, Flex} from "antd"; import Post from "../components/post"; import {Comments} from "../components/comment"; -import {Like, PostId, PostImmutable, Repost, Time, UserId} from "../declarations/feed/feed"; +import {PostImmutable} from "../declarations/feed/feed"; import {Profile} from '../declarations/user/user'; -import { userApi } from '../actors/user'; -import { Principal } from "@dfinity/principal"; +import {userApi} from '../actors/user'; export const Content = React.memo((props: { contents?: PostImmutable[] }) => { const {contents} = props const [postItem, setPostItem] = useState() const [userProfileArray, setUserProfileArray] = useState(); - const [onLoading, setOnloading] = useState(false); + const [onLoading, setOnloading] = useState(true); + const [commentProfiles, setCommentProfiles] = useState() + const [commentLoading, setCommentLoading] = useState(true) + + const getAllUserProfile = async () => { + if (!contents) return setUserProfileArray([]) + const userPrincipalArray = contents.map(v => v.user) + const result = await userApi.batchGetProfile(userPrincipalArray) + setUserProfileArray(result) + setOnloading(false) + } useEffect(() => { - const initUserProfileArray = async () => { - if(contents !== undefined) { - const userPrincipalArray = (Array.from(contents!.reduce((uniqueUsers, value) => { - uniqueUsers.add(value.user.toString()); - return uniqueUsers; - }, new Set()))).map(value => Principal.fromText(value)); - if(userPrincipalArray !== undefined && userPrincipalArray?.length > 0) { - const result = await userApi.batchGetProfile(userPrincipalArray!); - setUserProfileArray(result) - setOnloading(true) - } - } - }; - initUserProfileArray() + getAllUserProfile() }, [contents]); - if(onLoading) { - return <> - - {contents && contents.map((v, k) => { - const _userProfile = userProfileArray?.find(item => { - return item.id.toString() === v.user.toString() - }); - return - })} - - - {postItem && postItem.comment.map((v, k) => { - return - })} - - - } else { - return <> - - - - - - - - + const getAllCommentProfiles = async () => { + if (!postItem) return setCommentProfiles([]) + const comments = postItem.comment + const allIds = comments.map(v => v.user) + const result = await userApi.batchGetProfile(allIds); + setCommentProfiles(result) + setCommentLoading(false) } + useEffect(() => { + getAllCommentProfiles() + }, [postItem]) + + return <> + + {onLoading && + + } + {!onLoading && contents && contents.map((v, k) => { + return + })} + + + {postItem ? !commentLoading ? postItem.comment.map((v, k) => { + return + }) : + + : <>} + + + }) diff --git a/frontend/src/routes/profile.tsx b/frontend/src/routes/profile.tsx index 9c0ebf4..024a6ab 100644 --- a/frontend/src/routes/profile.tsx +++ b/frontend/src/routes/profile.tsx @@ -1,5 +1,5 @@ import React, {useState, useEffect} from 'react'; -import {Layout, Image, Typography, Avatar, Flex, Space, Button, Modal, message} from 'antd'; +import {Layout, Image, Typography, Avatar, Flex, Space, Button, Modal, message, Spin} from 'antd'; import Post from '../components/post'; import {userApi} from '../actors/user'; import {Profile} from '../declarations/user/user'; @@ -24,7 +24,22 @@ export default function UserProfile() { const [followers, setFollowers] = useState(0) const [allPosts, setAllPosts] = useState() const {userid} = useParams() + const [commentProfiles, setCommentProfiles] = useState() + const [commentLoading, setCommentLoading] = useState(true) const {allPost} = useAllDataStore() + const getAllCommentProfiles = async () => { + if (!postItem) return setCommentProfiles([]) + const comments = postItem.comment + const allIds = comments.map(v => v.user) + const result = await userApi.batchGetProfile(allIds); + setCommentProfiles(result) + setCommentLoading(false) + } + + useEffect(() => { + getAllCommentProfiles() + }, [postItem]) + const principal = React.useMemo(() => { try { return Principal.from(userid) @@ -155,7 +170,8 @@ export default function UserProfile() { {allPosts && allPosts.map((v, k) => { - return + return })} @@ -165,9 +181,13 @@ export default function UserProfile() { scrollbarWidth: 'thin', padding: "40px 20px" }}> - {postItem && postItem.comment.map((v, k) => { - return - })} + {postItem ? !commentLoading ? postItem.comment.map((v, k) => { + return + }) : + + : <>} )