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 29, 2024
1 parent b32bc27 commit 6e06a71
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 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
6 changes: 5 additions & 1 deletion src/common/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ArticleArgs {
id?: string
slug: string
mediaHash?: string | null
shortHash?: string | null
author: {
userName?: string | null
}
Expand Down Expand Up @@ -103,10 +104,13 @@ export const toPath = (
id,
slug,
mediaHash,
shortHash,
author: { userName },
} = args.article

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

try {
const { id: articleId } = fromGlobalId(id as string)
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
19 changes: 13 additions & 6 deletions src/views/ArticleDetail/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const articlePublicFragment = gql`
title
slug
mediaHash
dataHash
shortHash
state
cover
summary
Expand Down Expand Up @@ -87,8 +89,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 @@ -108,12 +110,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 @@ -165,8 +168,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
30 changes: 14 additions & 16 deletions src/views/ArticleDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ const ArticleDetail = ({
const { getQuery, router, routerLang } = useRoute()
const [needRefetchData, setNeedRefetchData] = useState(false)
const mediaHash = getQuery('mediaHash')
const shortHash = getQuery('shortHash')
const articleId =
(router.query.mediaHash as string)?.match(/^(\d+)/)?.[1] || ''
const viewer = useContext(ViewerContext)
Expand All @@ -377,11 +378,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 @@ -391,6 +392,7 @@ const ArticleDetail = ({
{
variables: {
mediaHash,
shortHash,
language: routerLang || UserLanguage.ZhHant,
includeTranslation,
},
Expand Down Expand Up @@ -455,7 +457,7 @@ const ArticleDetail = ({
await loadPrivate()
setNeedRefetchData(false)
})()
}, [mediaHash])
}, [mediaHash, shortHash])

// fetch private data when mediaHash of public data is changed
useEffect(() => {
Expand All @@ -467,13 +469,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 All @@ -484,12 +486,8 @@ const ArticleDetail = ({
`https://${process.env.NEXT_PUBLIC_SITE_DOMAIN}${newPath.href}`
)

// TODO: can remove this after 2024/2
const isNomadTags = article.tags?.some(
(tag) => tag.content === 'nomadmatters' || tag.content === '遊牧者計畫'
)
const hasReferral = u.searchParams.has(REFERRAL_QUERY_REFERRAL_KEY)
if (!hasReferral && isNomadTags && viewer.userName) {
if (!hasReferral && viewer.userName) {
u.searchParams.append(REFERRAL_QUERY_REFERRAL_KEY, viewer.userName)
}

Expand Down Expand Up @@ -621,18 +619,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 6e06a71

Please sign in to comment.