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
49659410+tx0c committed Apr 30, 2024
1 parent a69af9c commit 484c2b9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
17 changes: 15 additions & 2 deletions src/common/utils/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ describe('utils/route/toPath', () => {
)
})

it('should return the short hash path when the shortHash is provided', () => {
const { href } = toPath({
page: 'articleDetail',
article: {
...MOCK_ARTILCE,
shortHash: 'r5ade0on7x1g',
},
})

expect(href).toBe(`/a/r5ade0on7x1g`)
})

it('should return the correct path with utm paramaters', () => {
const { href } = toPath({
page: 'articleDetail',
Expand Down Expand Up @@ -178,8 +190,9 @@ describe('utils/route/toPath', () => {
expect(href2).toBe(
`/@${MOCK_ARTILCE.author.userName}/${
fromGlobalId(MOCK_ARTILCE.id).id
}-${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}#${MOCK_COMMENT
.parentComment?.id}-${MOCK_COMMENT.id}`
}-${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}#${
MOCK_COMMENT.parentComment?.id
}-${MOCK_COMMENT.id}`
)
})

Expand Down
17 changes: 12 additions & 5 deletions src/common/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface ArticleArgs {
id?: string
slug: string
mediaHash?: string | null
shortHash?: string | null
collectionId?: string | null
author: {
userName?: string | null
}
Expand Down Expand Up @@ -109,25 +111,30 @@ export const toPath = (
id,
slug,
mediaHash,
shortHash,
collectionId,
author: { userName },
} = args.article

href = `/@${userName}/${slug}-${mediaHash}`
href = shortHash
? `/a/${shortHash}`
: `/@${userName}/${slug}-${mediaHash}`

try {
const { id: articleId } = fromGlobalId(id as string)
if (id && articleId) {
if (id && articleId && !shortHash) {
href = `/@${userName}/${articleId}-${slug}${
mediaHash ? '-' + mediaHash : ''
}`
}
if (!!args.collectionId) {
href = `${href}?collection=${args.collectionId}`
}
} catch (err) {
// do nothing
}

if (collectionId) {
href = `${href}?collection=${collectionId}`
}

break
}
case 'articleHistory': {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/a/[shortHash].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ArticleDetail from '~/views/ArticleDetail'

export default ArticleDetail
18 changes: 12 additions & 6 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`
slug
mediaHash
dataHash
shortHash
state
cover
summary
Expand Down Expand Up @@ -97,8 +98,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 @@ -118,12 +119,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 Expand Up @@ -176,8 +178,12 @@ export const ARTICLE_DETAIL_PRIVATE = gql`
`

export const ARTICLE_TRANSLATION = gql`
query ArticleTranslation($mediaHash: String!, $language: UserLanguage!) {
article(input: { mediaHash: $mediaHash }) {
query ArticleTranslation(
$mediaHash: String
$shortHash: String
$language: UserLanguage!
) {
article(input: { mediaHash: $mediaHash, shortHash: $shortHash }) {
id
translation(input: { language: $language }) {
content
Expand Down
24 changes: 13 additions & 11 deletions src/views/ArticleDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ const ArticleDetail = ({
}) => {
const { getQuery, router, routerLang } = useRoute()
const mediaHash = getQuery('mediaHash')
const shortHash = getQuery('shortHash')
const articleId =
(router.query.mediaHash as string)?.match(/^(\d+)/)?.[1] || ''
const viewer = useContext(ViewerContext)
Expand All @@ -483,11 +484,11 @@ const ArticleDetail = ({
* fetch public data
*/
const isQueryByHash = !!(
mediaHash &&
isMediaHashPossiblyValid(mediaHash) &&
!articleId
(shortHash || (mediaHash && isMediaHashPossiblyValid(mediaHash)))
// && !articleId
)

// - `/a/:shortHash`
// backward compatible with:
// - `/:username:/:articleId:-:slug:-:mediaHash`
// - `/:username:/:articleId:`
Expand All @@ -497,6 +498,7 @@ const ArticleDetail = ({
{
variables: {
mediaHash,
shortHash,
language: routerLang || UserLanguage.ZhHant,
includeTranslation,
},
Expand Down Expand Up @@ -557,7 +559,7 @@ const ArticleDetail = ({
await refetchPublic()
await loadPrivate()
})()
}, [mediaHash])
}, [mediaHash, shortHash])

// fetch private data when mediaHash of public data is changed
useEffect(() => {
Expand All @@ -569,13 +571,13 @@ const ArticleDetail = ({
(d) => d.publishState === 'published'
)[0]?.mediaHash
useEffect(() => {
if (!article || !latestHash) {
return
if (shortHash || !article || !latestHash) {
return // don't rewrite URL for shortHash
}

const newPath = toPath({
page: 'articleDetail',
article: { ...article, mediaHash: latestHash },
article: { ...article, mediaHash: latestHash, shortHash },
})

// parse current URL: router.asPath
Expand Down Expand Up @@ -675,18 +677,18 @@ const ArticleDetail = ({
const ArticleDetailOuter = () => {
const { getQuery, router, routerLang } = useRoute()
const mediaHash = getQuery('mediaHash')
const shortHash = getQuery('shortHash')
const articleId =
(router.query.mediaHash as string)?.match(/^(\d+)/)?.[1] || ''

const isQueryByHash = !!(
mediaHash &&
isMediaHashPossiblyValid(mediaHash) &&
!articleId
(mediaHash && isMediaHashPossiblyValid(mediaHash))
// && !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 484c2b9

Please sign in to comment.