diff --git a/src/views/User/Writings/UserWritings.tsx b/src/views/User/Writings/UserWritings.tsx index cd4732b6d8..fbb7ff1987 100644 --- a/src/views/User/Writings/UserWritings.tsx +++ b/src/views/User/Writings/UserWritings.tsx @@ -23,7 +23,11 @@ import { import { UserWritingsPublicQuery } from '~/gql/graphql' import MomentForm from '../MomentForm' -import { USER_WRITINGS_PRIVATE, USER_WRITINGS_PUBLIC } from './gql' +import { + USER_MOMENTS_REACTIVE_DATA, + USER_WRITINGS_PRIVATE, + USER_WRITINGS_PUBLIC, +} from './gql' import PinBoard from './PinBoard' import Placeholder from './Placeholder' @@ -64,9 +68,29 @@ const UserWritings = () => { }) } - // fetch private data for first page + // fetch the latest moment data + const loadMomentReactiveData = (publicData?: UserWritingsPublicQuery) => { + if (!publicData || !user) { + return + } + + const publiceEdges = publicData.user?.writings?.edges || [] + const momentEdges = publiceEdges.filter( + ({ node }) => node.__typename === 'Moment' + ) + const publicIds = momentEdges.map(({ node }) => node.id) + + client.query({ + query: USER_MOMENTS_REACTIVE_DATA, + fetchPolicy: 'network-only', + variables: { ids: publicIds }, + }) + } + + // fetch private and moment data for first page useEffect(() => { loadPrivate(data) + loadMomentReactiveData(data) }, [user?.id, viewer.id]) // load next page diff --git a/src/views/User/Writings/gql.ts b/src/views/User/Writings/gql.ts index f5c5993857..24c841a41a 100644 --- a/src/views/User/Writings/gql.ts +++ b/src/views/User/Writings/gql.ts @@ -90,3 +90,15 @@ export const USER_WRITINGS_PRIVATE = gql` ${ArticleDigestFeed.fragments.article.private} ${MomentDigestFeed.fragments.moment.private} ` + +export const USER_MOMENTS_REACTIVE_DATA = gql` + query UserMomentsReactiveData($ids: [ID!]!) { + nodes(input: { ids: $ids }) { + id + ... on Moment { + likeCount + commentCount + } + } + } +`