Skip to content

Commit

Permalink
Merge pull request #4573 from thematters/fix/lazy-load-article-side-prod
Browse files Browse the repository at this point in the history
Release: v5.1.2
  • Loading branch information
robertu7 authored Jun 14, 2024
2 parents 6929ca6 + 6a15117 commit 84c084c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "matters-web",
"version": "5.1.1",
"version": "5.1.2",
"description": "codebase of Matters' website",
"author": "Matters <[email protected]>",
"engines": {
Expand Down
4 changes: 3 additions & 1 deletion src/views/ArticleDetail/AuthorSidebar/Collection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type CollectionProps = {
article: ArticleDetailPublicQuery['article']
}

export const Collection = ({ article, collectionId }: CollectionProps) => {
const Collection = ({ article, collectionId }: CollectionProps) => {
/**
* Data Fetching
*/
Expand Down Expand Up @@ -132,3 +132,5 @@ export const Collection = ({ article, collectionId }: CollectionProps) => {
</section>
)
}

export default Collection
31 changes: 29 additions & 2 deletions src/views/ArticleDetail/AuthorSidebar/RelatedArticles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import {
ArticleDigestAuthorSidebar,
ArticleDigestSidebar,
List,
usePublicQuery,
} from '~/components'
import { ArticleDetailPublicQuery } from '~/gql/graphql'
import {
ArticleDetailPublicQuery,
AuthorSidebarRelatedArticlesQuery,
} from '~/gql/graphql'

import { FeedPlaceholder } from '../Placeholder'

type RelatedArticlesProps = {
article: NonNullable<ArticleDetailPublicQuery['article']>
Expand All @@ -30,8 +36,29 @@ const fragments = {
`,
}

const RELATED_ARTICLES = gql`
query AuthorSidebarRelatedArticles($shortHash: String) {
article(input: { shortHash: $shortHash }) {
id
...AuthorSidebarRelatedArticles
}
}
${fragments.article}
`

export const RelatedArticles = ({ article }: RelatedArticlesProps) => {
const edges = article.relatedArticles.edges
const { data, loading } = usePublicQuery<AuthorSidebarRelatedArticlesQuery>(
RELATED_ARTICLES,
{
variables: { shortHash: article.shortHash },
}
)

const edges = data?.article?.relatedArticles.edges

if (loading) {
return <FeedPlaceholder />
}

if (!edges || edges.length <= 0) {
return null
Expand Down
6 changes: 6 additions & 0 deletions src/views/ArticleDetail/AuthorSidebar/gql.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import gql from 'graphql-tag'

import Author from './Author'
import { FromAuthor } from './FromAuthor'

export const fragments = {
article: gql`
fragment AuthorSidebarArticle on Article {
id
relatedArticles(input: { first: 0 }) {
totalCount
}
...AuthorSidebarAuthorArticle
...AuthorSidebarFromAuthor
}
${Author.fragments.article}
${FromAuthor.fragments.article}
`,
}
20 changes: 14 additions & 6 deletions src/views/ArticleDetail/AuthorSidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import dynamic from 'next/dynamic'
import { useState } from 'react'

import { useRoute } from '~/components'
import { SpinnerBlock, useRoute } from '~/components'
import { ArticleDetailPublicQuery } from '~/gql/graphql'

import Author from './Author'
import { Collection } from './Collection'
import { FromAuthor } from './FromAuthor'
import { fragments } from './gql'
import { Placeholder } from './Placeholder'
Expand All @@ -16,16 +16,21 @@ type AuthorSidebarProps = {
article: NonNullable<ArticleDetailPublicQuery['article']>
}

const DynamicCollection = dynamic(() => import('./Collection'), {
ssr: false,
loading: () => <SpinnerBlock />,
})

export const AuthorSidebar = ({ article }: AuthorSidebarProps) => {
const { getQuery } = useRoute()
const cid = getQuery('collection')
const collectionId = getQuery('collection')
const latestWorks = article.author?.latestWorks.filter((work) => {
return work.id !== article.id
})
const hasFromAuthor = latestWorks && latestWorks.length > 0
const hasRecommendation = article.relatedArticles?.totalCount > 0
const [tab, setTab] = useState<TABS>(
!!cid
!!collectionId
? 'Collection'
: hasFromAuthor
? 'Author'
Expand All @@ -41,8 +46,11 @@ export const AuthorSidebar = ({ article }: AuthorSidebarProps) => {
<>
<Tabs article={article} tab={tab} setTab={setTab} />
<section className={styles.list}>
{!!cid && tab === 'Collection' && (
<Collection article={article} collectionId={cid} />
{!!collectionId && tab === 'Collection' && (
<DynamicCollection
article={article}
collectionId={collectionId}
/>
)}
{tab === 'Author' && <FromAuthor article={article} />}
{tab === 'Recommendation' && <RelatedArticles article={article} />}
Expand Down
6 changes: 0 additions & 6 deletions src/views/ArticleDetail/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import gql from 'graphql-tag'
import { UserDigest } from '~/components/UserDigest'

import { AuthorSidebar } from './AuthorSidebar'
import { FromAuthor } from './AuthorSidebar/FromAuthor'
import { RelatedArticles } from './AuthorSidebar/RelatedArticles'
import MetaInfo from './MetaInfo'
import StickyTopBanner from './StickyTopBanner'
import { fragments as supportWidgetFragments } from './Support/SupportWidget/gql'
Expand Down Expand Up @@ -68,8 +66,6 @@ const articlePublicFragment = gql`
...AuthorSidebarArticle
...MetaInfoArticle
...TagListArticle
...AuthorSidebarRelatedArticles
...AuthorSidebarFromAuthor
...StateArticle
...ToolbarArticlePublic
...ToolbarArticlePrivate
Expand All @@ -79,8 +75,6 @@ const articlePublicFragment = gql`
${AuthorSidebar.fragments.article}
${MetaInfo.fragments.article}
${TagList.fragments.article}
${RelatedArticles.fragments.article}
${FromAuthor.fragments.article}
${StickyTopBanner.fragments.article}
${UserDigest.Rich.fragments.user.public}
${UserDigest.Rich.fragments.user.private}
Expand Down

0 comments on commit 84c084c

Please sign in to comment.