From ad84630b6d1a6db809dac33f326d3eb6d2298da0 Mon Sep 17 00:00:00 2001 From: gitwoz <177856586+gitwoz@users.noreply.github.com> Date: Tue, 3 Dec 2024 22:34:48 +0700 Subject: [PATCH] feat(tag): use numArticles instead of articles.totalCount for tag article count --- src/components/GQL/fragments/tag.ts | 16 ------- .../GQL/queries/tagArticlesCount.ts | 15 ------ src/components/GQL/updates/index.ts | 1 - .../GQL/updates/tagArticlesCount.ts | 48 ------------------- src/components/Tag/ArticleTag/index.tsx | 1 - src/components/Tag/InlineTag/index.tsx | 1 - src/components/Tag/ListTag/index.tsx | 1 - src/components/Tag/PlainTag/index.tsx | 1 - src/components/TagDigest/Feed/index.tsx | 1 - src/views/Home/Sidebar/Tags/index.tsx | 5 +- src/views/TagDetail/ArticlesCount/index.tsx | 10 +--- src/views/TagDetail/gql.ts | 6 --- src/views/Tags/Feed.tsx | 26 +--------- src/views/Tags/gql.ts | 11 ----- 14 files changed, 6 insertions(+), 137 deletions(-) delete mode 100644 src/components/GQL/fragments/tag.ts delete mode 100644 src/components/GQL/queries/tagArticlesCount.ts delete mode 100644 src/components/GQL/updates/tagArticlesCount.ts diff --git a/src/components/GQL/fragments/tag.ts b/src/components/GQL/fragments/tag.ts deleted file mode 100644 index c01009e6f4..0000000000 --- a/src/components/GQL/fragments/tag.ts +++ /dev/null @@ -1,16 +0,0 @@ -import gql from 'graphql-tag' - -const fragments = { - articleCount: gql` - fragment ArticleCountTag on Tag { - id - numAuthors - numArticles - articles(input: { first: 0 }) { - totalCount - } - } - `, -} - -export default fragments diff --git a/src/components/GQL/queries/tagArticlesCount.ts b/src/components/GQL/queries/tagArticlesCount.ts deleted file mode 100644 index 563061da10..0000000000 --- a/src/components/GQL/queries/tagArticlesCount.ts +++ /dev/null @@ -1,15 +0,0 @@ -import gql from 'graphql-tag' - -import tagFragments from '../fragments/tag' - -export default gql` - query TagArticlesCount($id: ID!) { - node(input: { id: $id }) { - ... on Tag { - id - ...ArticleCountTag - } - } - } - ${tagFragments.articleCount} -` diff --git a/src/components/GQL/updates/index.ts b/src/components/GQL/updates/index.ts index 69d12fde0e..4aa90f493c 100644 --- a/src/components/GQL/updates/index.ts +++ b/src/components/GQL/updates/index.ts @@ -6,7 +6,6 @@ export * from './circleFollowers' export * from './commentDetail' export * from './draftAssets' export * from './momentDetail' -export * from './tagArticlesCount' export * from './userCollectionDetail' export * from './userCollections' export * from './userCollectionsArticles' diff --git a/src/components/GQL/updates/tagArticlesCount.ts b/src/components/GQL/updates/tagArticlesCount.ts deleted file mode 100644 index 46382f9c23..0000000000 --- a/src/components/GQL/updates/tagArticlesCount.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { DataProxy } from 'apollo-cache' -import _cloneDeep from 'lodash/cloneDeep' - -import TAG_ARTICLES_COUNT from '~/components/GQL/queries/tagArticlesCount' -import { TagArticlesCountQuery } from '~/gql/graphql' - -export const updateTagArticlesCount = ({ - cache, - id, - count = 1, - type, -}: { - cache: DataProxy - id: string - count?: number - type: 'increment' | 'decrement' -}) => { - try { - if (!id) { - return - } - - const variables = { id } - const cacheData = cache.readQuery({ - query: TAG_ARTICLES_COUNT, - variables, - }) - - const data = _cloneDeep(cacheData) - if (data?.node?.__typename !== 'Tag') { - return - } - - if (type === 'increment') { - data.node.articles.totalCount += count - } else { - data.node.articles.totalCount -= count - } - - cache.writeQuery({ - query: TAG_ARTICLES_COUNT, - variables, - data, - }) - } catch (e) { - console.error(e) - } -} diff --git a/src/components/Tag/ArticleTag/index.tsx b/src/components/Tag/ArticleTag/index.tsx index 4c4e7d95ce..d41a1342f0 100644 --- a/src/components/Tag/ArticleTag/index.tsx +++ b/src/components/Tag/ArticleTag/index.tsx @@ -59,7 +59,6 @@ ArticleTag.fragments = { id content numArticles - numAuthors } `, } diff --git a/src/components/Tag/InlineTag/index.tsx b/src/components/Tag/InlineTag/index.tsx index 8808fd9556..4137d574e6 100644 --- a/src/components/Tag/InlineTag/index.tsx +++ b/src/components/Tag/InlineTag/index.tsx @@ -75,7 +75,6 @@ InlineTag.fragments = { id content numArticles - numAuthors } `, } diff --git a/src/components/Tag/ListTag/index.tsx b/src/components/Tag/ListTag/index.tsx index 1fcacdfcdb..1f59a29742 100644 --- a/src/components/Tag/ListTag/index.tsx +++ b/src/components/Tag/ListTag/index.tsx @@ -81,7 +81,6 @@ ListTag.fragments = { id content numArticles - numAuthors } `, } diff --git a/src/components/Tag/PlainTag/index.tsx b/src/components/Tag/PlainTag/index.tsx index 0dc778eb1a..aa1ad9763b 100644 --- a/src/components/Tag/PlainTag/index.tsx +++ b/src/components/Tag/PlainTag/index.tsx @@ -52,7 +52,6 @@ PlainTag.fragments = { id content numArticles - numAuthors } `, } diff --git a/src/components/TagDigest/Feed/index.tsx b/src/components/TagDigest/Feed/index.tsx index ddfc6af9c3..104320423f 100644 --- a/src/components/TagDigest/Feed/index.tsx +++ b/src/components/TagDigest/Feed/index.tsx @@ -19,7 +19,6 @@ const fragments = { id content numArticles - numAuthors } `, } diff --git a/src/views/Home/Sidebar/Tags/index.tsx b/src/views/Home/Sidebar/Tags/index.tsx index 9656d0b804..645478cecf 100644 --- a/src/views/Home/Sidebar/Tags/index.tsx +++ b/src/views/Home/Sidebar/Tags/index.tsx @@ -5,7 +5,6 @@ import { useContext } from 'react' import { analytics } from '~/common/utils' import { List, - ListTag, QueryError, ShuffleButton, SpinnerBlock, @@ -33,14 +32,14 @@ const SIDEBAR_TAGS = gql` cursor node { id - ...DigestTag + ...TagDigestSidebarTag } } } } } } - ${ListTag.fragments.tag} + ${TagDigest.Sidebar.fragments.tag} ` const Tags = () => { diff --git a/src/views/TagDetail/ArticlesCount/index.tsx b/src/views/TagDetail/ArticlesCount/index.tsx index 1f5f45a83e..5b09252b06 100644 --- a/src/views/TagDetail/ArticlesCount/index.tsx +++ b/src/views/TagDetail/ArticlesCount/index.tsx @@ -1,17 +1,15 @@ import { FormattedMessage } from 'react-intl' import { numAbbr } from '~/common/utils' -import tagFragments from '~/components/GQL/fragments/tag' -import { ArticleCountTagFragment } from '~/gql/graphql' import styles from './styles.module.css' interface ArticlesCountProps { - tag: ArticleCountTagFragment + tag: { numArticles: number } } const ArticlesCount = ({ tag }: ArticlesCountProps) => { - const { totalCount } = tag.articles || { totalCount: 0 } + const totalCount = tag.numArticles return (
@@ -30,8 +28,4 @@ const ArticlesCount = ({ tag }: ArticlesCountProps) => { ) } -ArticlesCount.fragments = { - tag: tagFragments.articleCount, -} - export default ArticlesCount diff --git a/src/views/TagDetail/gql.ts b/src/views/TagDetail/gql.ts index 5898869933..c58ccc9930 100644 --- a/src/views/TagDetail/gql.ts +++ b/src/views/TagDetail/gql.ts @@ -2,18 +2,14 @@ import gql from 'graphql-tag' import { TagBookmarkButton } from '~/components' -import ArticlesCount from './ArticlesCount' - const tagFragment = gql` fragment TagFragment on Tag { id content numArticles - numAuthors selectedArticles: articles(input: { first: 0 }) { totalCount } - ...ArticleCountTag ...TagBookmarkButtonTagPrivate hottestArticles: articles(input: { first: 0, sortBy: byHottestDesc }) { totalCount @@ -25,12 +21,10 @@ const tagFragment = gql` id content numArticles - numAuthors } } } } - ${ArticlesCount.fragments.tag} ${TagBookmarkButton.fragments.tag.private} ` diff --git a/src/views/Tags/Feed.tsx b/src/views/Tags/Feed.tsx index 82455aa2a0..7d286e01b2 100644 --- a/src/views/Tags/Feed.tsx +++ b/src/views/Tags/Feed.tsx @@ -1,5 +1,4 @@ import _get from 'lodash/get' -import { useEffect } from 'react' import { analytics, mergeConnections } from '~/common/utils' import { @@ -12,7 +11,7 @@ import { } from '~/components' import { AllTagsHottestQuery } from '~/gql/graphql' -import { ALL_TAGS_HOTTEST, TAG_REACTIVE_DATA } from './gql' +import { ALL_TAGS_HOTTEST } from './gql' export type FeedType = 'recommended' | 'hottest' @@ -27,28 +26,7 @@ const Feed = ({ type }: Props) => { const query = ALL_TAGS_HOTTEST - const { data, loading, error, fetchMore, client } = - usePublicQuery(query) - - // fetch the latest tag data - const loadTagReactiveData = (publicData?: FeedQuery) => { - const publicEdges = publicData?.viewer?.recommendation.tags.edges || [] - const publicIds = publicEdges.map(({ node }) => node.id) - - if (publicIds.length <= 0) { - return - } - - client.query({ - query: TAG_REACTIVE_DATA, - fetchPolicy: 'network-only', - variables: { ids: publicIds }, - }) - } - - useEffect(() => { - loadTagReactiveData(data) - }, [data]) + const { data, loading, error, fetchMore } = usePublicQuery(query) if (loading) { return diff --git a/src/views/Tags/gql.ts b/src/views/Tags/gql.ts index 94dbb2448a..23261429a9 100644 --- a/src/views/Tags/gql.ts +++ b/src/views/Tags/gql.ts @@ -27,14 +27,3 @@ export const ALL_TAGS_HOTTEST = gql` } ${TagDigest.Feed.fragments.tag} ` - -export const TAG_REACTIVE_DATA = gql` - query TagReactiveData($ids: [ID!]!) { - nodes(input: { ids: $ids }) { - id - ... on Tag { - numArticles - } - } - } -`