Skip to content

Commit

Permalink
feat(article): use shortHash as query input
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed May 2, 2024
1 parent ae3da0e commit c9c4520
Show file tree
Hide file tree
Showing 47 changed files with 179 additions and 459 deletions.
4 changes: 2 additions & 2 deletions src/common/enums/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export const ROUTES: {
},

// Article
{ key: 'ARTICLE_DETAIL', pathname: '/[name]/[mediaHash]' },
{ key: 'ARTICLE_DETAIL_HISTORY', pathname: '/[name]/[mediaHash]/history' },
{ key: 'ARTICLE_DETAIL', pathname: '/a/[shortHash]' },
{ key: 'ARTICLE_DETAIL_HISTORY', pathname: '/a/[shortHash]/history' },

// Circle
{ key: 'CIRCLE_DETAIL', pathname: '/[name]' },
Expand Down
83 changes: 33 additions & 50 deletions src/common/utils/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,27 @@ import { getSearchType, toPath } from './route'
describe('utils/route/toPath', () => {
describe('page: articleDetail', () => {
it('should return the correct path', () => {
const { href } = toPath({ page: 'articleDetail', article: MOCK_ARTILCE })
// /@matty/1-slug-Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${
fromGlobalId(MOCK_ARTILCE.id).id
}-${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}`
)
})

it('should return the correct path when the article id is not a valid global id', () => {
const { href } = toPath({
page: 'articleDetail',
article: {
...MOCK_ARTILCE,
id: 'invalidId',
shortHash: 'r5ade0on7x1g',
},
})
// /@matty/slug-Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}`
)
})

it('should return the correct path when the article id is not provided', () => {
const { href } = toPath({
page: 'articleDetail',
article: {
...MOCK_ARTILCE,
id: undefined,
},
})
// /@matty/slug-Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}`
)
expect(href).toBe(`/a/r5ade0on7x1g`)
})

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

expect(href).toBe(`/a/r5ade0on7x1g`)
expect(href).toBe(`/a/${MOCK_ARTILCE.shortHash}?collection=123`)
})

it('should return the correct path with utm paramaters', () => {
Expand All @@ -73,11 +47,7 @@ describe('utils/route/toPath', () => {
utm_campaign: 'test-campaign',
})
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${
fromGlobalId(MOCK_ARTILCE.id).id
}-${MOCK_ARTILCE.slug}-${
MOCK_ARTILCE.mediaHash
}?utm_source=test-source&utm_campaign=test-campaign`
`/a/${MOCK_ARTILCE.shortHash}?utm_source=test-source&utm_campaign=test-campaign`
)
})

Expand All @@ -91,15 +61,35 @@ describe('utils/route/toPath', () => {
},
})
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${
fromGlobalId(MOCK_ARTILCE.id).id
}-${MOCK_ARTILCE.slug}-${
MOCK_ARTILCE.mediaHash
}?referral=code&utm_source=test-source`
`/a/${MOCK_ARTILCE.shortHash}?referral=code&utm_source=test-source`
)
})
})

describe('page: articleHistory', () => {
it('should return the correct path', () => {
const { href } = toPath({
page: 'articleHistory',
article: {
...MOCK_ARTILCE,
},
})

expect(href).toBe(`/a/${MOCK_ARTILCE.shortHash}/history`)
})
it('should return the correct path with version id', () => {
const { href } = toPath({
page: 'articleHistory',
article: {
...MOCK_ARTILCE,
},
versionId: '123',
})

expect(href).toBe(`/a/${MOCK_ARTILCE.shortHash}/history?v=123`)
})
})

describe('page: circleDetail', () => {
it('should return the correct path', () => {
const { href } = toPath({ page: 'circleDetail', circle: MOCK_CIRCLE })
Expand Down Expand Up @@ -175,11 +165,7 @@ describe('utils/route/toPath', () => {
comment: { ...MOCK_COMMENT, parentComment: null },
article: MOCK_ARTILCE,
})
expect(href).toBe(
`/@${MOCK_ARTILCE.author.userName}/${
fromGlobalId(MOCK_ARTILCE.id).id
}-${MOCK_ARTILCE.slug}-${MOCK_ARTILCE.mediaHash}#${MOCK_COMMENT.id}`
)
expect(href).toBe(`/a/${MOCK_ARTILCE.shortHash}#${MOCK_COMMENT.id}`)

// with parent comment
const { href: href2 } = toPath({
Expand All @@ -188,10 +174,7 @@ describe('utils/route/toPath', () => {
article: MOCK_ARTILCE,
})
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}`
`/a/${MOCK_ARTILCE.shortHash}#${MOCK_COMMENT.parentComment?.id}-${MOCK_COMMENT.id}`
)
})

Expand Down
66 changes: 12 additions & 54 deletions src/common/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ import { slugifyTag } from './text'
import { parseURL } from './url'

interface ArticleArgs {
id?: string
slug: string
mediaHash?: string | null
shortHash?: string | null
collectionId?: string | null
author: {
userName?: string | null
}
shortHash: string
}

interface CircleArgs {
Expand Down Expand Up @@ -107,58 +100,23 @@ export const toPath = (

switch (args.page) {
case 'articleDetail': {
const {
id,
slug,
mediaHash,
shortHash,
collectionId,
author: { userName },
} = args.article

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

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

href = `/a/${shortHash}`

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

break
}
case 'articleHistory': {
const {
id,
slug,
mediaHash,
author: { userName },
} = args.article

href = `/@${userName}/${slug}-${mediaHash}`

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

href = `/a/${shortHash}/history`

if (args.versionId) {
href = `${href}?v=${args.versionId}`
}

break
Expand Down
13 changes: 0 additions & 13 deletions src/common/utils/text/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,3 @@ export const optimizeEmbed = (content: string) => {
*/
export const measureDiffs = (source: string, target: string) =>
distance(source, target)

export const isMediaHashPossiblyValid = (mediaHash?: string | null) => {
// is there a better way to detect valid?
// a valid mediaHash, should have length 49 or 59 chars
// 'zdpuAsCXC87Tm1fFvAbysV7HVt7J8aV6chaTKeJZ5ryLALK3Z'
// 'bafyreief6bryqsa4byabnmx222jvo4khlodvpypw27af43frecbumn6ocq'

return (
mediaHash &&
((mediaHash?.length === 49 && mediaHash.startsWith('zdpu')) ||
(mediaHash?.length === 59 && mediaHash.startsWith('bafy')))
)
}
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/AuthorSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const fragments = {
articleState: state
title
slug
mediaHash
shortHash
cover
...ArticleDigestTitleArticle
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Card/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const fragments = {
state
title
slug
mediaHash
shortHash
cover
summary
author {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Curated/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const fragments = {
articleState: state
title
slug
mediaHash
shortHash
cover
author {
id
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const fragments = {
title
articleState: state
slug
mediaHash
shortHash
author {
id
userName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const fragments = {
article: gql`
fragment EditArticleButtonArticle on Article {
id
mediaHash
shortHash
slug
author {
id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fragments = {
id
slug
articleState: state
mediaHash
shortHash
responseCount
author {
userName
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Feed/FooterActions/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const fragments = {
id
title
slug
mediaHash
shortHash
createdAt
author {
id
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Feed/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const fragments = {
id
title
slug
mediaHash
shortHash
articleState: state
cover
summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const fragments = {
id
title
slug
mediaHash
shortHash
createdAt
readerCount
appreciationsReceivedTotal
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Published/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const fragments = {
id
title
slug
mediaHash
shortHash
articleState: state
...ArticleDigestTitleArticle
...FooterActionsPublishedArticlePublic
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const fragments = {
articleState: state
title
slug
mediaHash
shortHash
cover
author {
id
Expand Down
2 changes: 1 addition & 1 deletion src/components/ArticleDigest/Title/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const fragments = {
title
articleState: state
slug
mediaHash
shortHash
author {
id
userName
Expand Down
2 changes: 1 addition & 1 deletion src/components/Comment/CreatedAt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const fragments = {
... on Article {
id
slug
mediaHash
shortHash
author {
userName
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Comment/DropdownActions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const fragments = {
}
... on Article {
id
mediaHash
shortHash
author {
id
}
Expand Down
Loading

0 comments on commit c9c4520

Please sign in to comment.