Skip to content

Commit

Permalink
feat(new-article-path): Revise pathname of article detail page #4196
Browse files Browse the repository at this point in the history
for #4196
  • Loading branch information
tx0c authored and TomasC committed Mar 6, 2024
1 parent d1683e5 commit d942e18
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/pages/p/[shortHash].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ArticleDetail from '~/views/ArticleDetail'

export default ArticleDetail
10 changes: 6 additions & 4 deletions src/views/ArticleDetail/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const articlePublicFragment = gql`
title
slug
mediaHash
shortHash
state
cover
summary
Expand Down Expand Up @@ -94,8 +95,8 @@ const articlePublicFragment = gql`
`

export const ARTICLE_AVAILABLE_TRANSLATIONS = gql`
query ArticleAvailableTranslations($mediaHash: String!) {
article(input: { mediaHash: $mediaHash }) {
query ArticleAvailableTranslations($mediaHash: String, $shortHash: String) {
article(input: { mediaHash: $mediaHash, shortHash: $shortHash }) {
id
availableTranslations
}
Expand All @@ -115,12 +116,13 @@ export const ARTICLE_AVAILABLE_TRANSLATIONS_BY_NODE_ID = gql`

export const ARTICLE_DETAIL_PUBLIC = gql`
query ArticleDetailPublic(
$mediaHash: String!
$mediaHash: String
$shortHash: String
$language: UserLanguage!
$includeTranslation: Boolean = false
$includeCanSuperLike: Boolean = true
) {
article(input: { mediaHash: $mediaHash }) {
article(input: { mediaHash: $mediaHash, shortHash: $shortHash }) {
...ArticlePublicArticle
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/views/ArticleDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,16 @@ const BaseArticleDetail = ({
)
}

const ArticleDetail = ({
export const ArticleDetail = ({
includeTranslation,
}: {
includeTranslation: boolean
}) => {
const { getQuery, router, routerLang } = useRoute()
const [needRefetchData, setNeedRefetchData] = useState(false)
const mediaHash = getQuery('mediaHash')

const shortHash = getQuery('shortHash')
const mediaHash = getQuery('mediaHash') // will delete by mediaHash & by articleId logic in later PR
const articleId =
(router.query.mediaHash as string)?.match(/^(\d+)/)?.[1] || ''
const viewer = useContext(ViewerContext)
Expand All @@ -523,9 +525,9 @@ const ArticleDetail = ({
* fetch public data
*/
const isQueryByHash = !!(
mediaHash &&
isMediaHashPossiblyValid(mediaHash) &&
!articleId
(mediaHash && isMediaHashPossiblyValid(mediaHash))
|| (shortHash) // && looksValid(shortHash))
// && !articleId
)

// backward compatible with:
Expand All @@ -537,6 +539,7 @@ const ArticleDetail = ({
{
variables: {
mediaHash,
shortHash,
language: routerLang || UserLanguage.ZhHant,
includeTranslation,
},
Expand Down Expand Up @@ -764,21 +767,23 @@ const ArticleDetail = ({
return <BaseArticleDetail article={article} privateFetched={privateFetched} />
}

const ArticleDetailOuter = () => {
export const ArticleDetailOuter = () => {
const { getQuery, router, routerLang } = useRoute()
const mediaHash = getQuery('mediaHash')

const shortHash = getQuery('shortHash')
const mediaHash = getQuery('mediaHash') // will delete by mediaHash & by articleId logic in later PR
const articleId =
(router.query.mediaHash as string)?.match(/^(\d+)/)?.[1] || ''

const isQueryByHash = !!(
mediaHash &&
isMediaHashPossiblyValid(mediaHash) &&
!articleId
(mediaHash && isMediaHashPossiblyValid(mediaHash))
|| shortHash
// && !articleId
)

const resultByHash = usePublicQuery<ArticleAvailableTranslationsQuery>(
ARTICLE_AVAILABLE_TRANSLATIONS,
{ variables: { mediaHash }, skip: !isQueryByHash }
{ variables: { mediaHash, shortHash }, skip: !isQueryByHash }
)
const resultByNodeId = usePublicQuery<ArticleAvailableTranslationsQuery>(
ARTICLE_AVAILABLE_TRANSLATIONS_BY_NODE_ID,
Expand Down

0 comments on commit d942e18

Please sign in to comment.