Skip to content

Commit 989642b

Browse files
committed
filter unique entries when My languages is selected
1 parent c65c8df commit 989642b

8 files changed

Lines changed: 71 additions & 42 deletions

File tree

src/features/cards/components/conferencesCard/ConferencesCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Conference, CardPropsType } from 'src/types'
55
import { useUserPreferences } from 'src/stores/preferences'
66
import { getCardTagsValue } from 'src/utils/DataEnhancement'
77
import ConferenceItem from './ConferenceItem'
8+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
89

910
export function ConferencesCard({ meta, withAds }: CardPropsType) {
1011
const { userSelectedTags } = useUserPreferences()
@@ -14,12 +15,14 @@ export function ConferencesCard({ meta, withAds }: CardPropsType) {
1415
const isLoading = results.some((result) => result.isLoading)
1516

1617
const getData = () => {
17-
return results
18-
.reduce((acc: Conference[], curr) => {
19-
if (!curr.data) return acc
20-
return [...acc, ...curr.data]
21-
}, [])
22-
.sort((a, b) => a.start_date - b.start_date)
18+
return filterUniqueEntries(
19+
results
20+
.reduce((acc: Conference[], curr) => {
21+
if (!curr.data) return acc
22+
return [...acc, ...curr.data]
23+
}, [])
24+
.sort((a, b) => a.start_date - b.start_date)
25+
)
2326
}
2427

2528
const renderItem = (item: Conference, index: number) => (

src/features/cards/components/devtoCard/DevtoCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ArticleItem from './ArticleItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
99
import { trackCardLanguageSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function DevtoCard({ withAds, meta }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -33,12 +34,14 @@ export function DevtoCard({ withAds, meta }: CardPropsType) {
3334
const getIsLoading = () => results.some((result) => result.isLoading)
3435

3536
const getData = () => {
36-
return results
37-
.reduce((acc: Article[], curr) => {
38-
if (!curr.data) return acc
39-
return [...acc, ...curr.data]
40-
}, [])
41-
.sort((a, b) => b.published_at - a.published_at)
37+
return filterUniqueEntries(
38+
results
39+
.reduce((acc: Article[], curr) => {
40+
if (!curr.data) return acc
41+
return [...acc, ...curr.data]
42+
}, [])
43+
.sort((a, b) => b.published_at - a.published_at)
44+
)
4245
}
4346

4447
const renderItem = (item: Article, index: number) => (

src/features/cards/components/freecodecampCard/FreecodecampCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ArticleItem from './ArticleItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
99
import { trackCardLanguageSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function FreecodecampCard({ meta, withAds }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -32,12 +33,14 @@ export function FreecodecampCard({ meta, withAds }: CardPropsType) {
3233
const getIsLoading = () => results.some((result) => result.isLoading)
3334

3435
const getData = () => {
35-
return results
36-
.reduce((acc: Article[], curr) => {
37-
if (!curr.data) return acc
38-
return [...acc, ...curr.data]
39-
}, [])
40-
.sort((a, b) => b.published_at - a.published_at)
36+
return filterUniqueEntries(
37+
results
38+
.reduce((acc: Article[], curr) => {
39+
if (!curr.data) return acc
40+
return [...acc, ...curr.data]
41+
}, [])
42+
.sort((a, b) => b.published_at - a.published_at)
43+
)
4144
}
4245

4346
const renderItem = (item: Article, index: number) => (

src/features/cards/components/githubCard/GithubCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import RepoItem from './RepoItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG, dateRanges } from 'src/config'
99
import { trackCardLanguageSelect, trackCardDateRangeSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function GithubCard({ meta, withAds }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -43,12 +44,14 @@ export function GithubCard({ meta, withAds }: CardPropsType) {
4344
const getIsLoading = () => results.some((result) => result.isLoading)
4445

4546
const getData = () => {
46-
return results
47-
.reduce((acc: Repository[], curr) => {
48-
if (!curr.data) return acc
49-
return [...acc, ...curr.data]
50-
}, [])
51-
.sort((a, b) => b.stars - a.stars)
47+
return filterUniqueEntries(
48+
results
49+
.reduce((acc: Repository[], curr) => {
50+
if (!curr.data) return acc
51+
return [...acc, ...curr.data]
52+
}, [])
53+
.sort((a, b) => b.stars - a.stars)
54+
)
5255
}
5356

5457
const renderItem = (item: Repository, index: number) => (

src/features/cards/components/hashnodeCard/HashnodeCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ArticleItem from './ArticleItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
99
import { trackCardLanguageSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function HashnodeCard({ withAds, meta }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -32,12 +33,14 @@ export function HashnodeCard({ withAds, meta }: CardPropsType) {
3233
const getIsLoading = () => results.some((result) => result.isLoading)
3334

3435
const getData = () => {
35-
return results
36-
.reduce((acc: Article[], curr) => {
37-
if (!curr.data) return acc
38-
return [...acc, ...curr.data]
39-
}, [])
40-
.sort((a, b) => b.published_at - a.published_at)
36+
return filterUniqueEntries(
37+
results
38+
.reduce((acc: Article[], curr) => {
39+
if (!curr.data) return acc
40+
return [...acc, ...curr.data]
41+
}, [])
42+
.sort((a, b) => b.published_at - a.published_at)
43+
)
4144
}
4245

4346
const renderItem = (item: Article, index: number) => (

src/features/cards/components/mediumCard/MediumCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ArticleItem from './ArticleItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
99
import { trackCardLanguageSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function MediumCard({ meta, withAds }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -32,12 +33,14 @@ export function MediumCard({ meta, withAds }: CardPropsType) {
3233
const getIsLoading = () => results.some((result) => result.isLoading)
3334

3435
const getData = () => {
35-
return results
36-
.reduce((acc: Article[], curr) => {
37-
if (!curr.data) return acc
38-
return [...acc, ...curr.data]
39-
}, [])
40-
.sort((a, b) => b.published_at - a.published_at)
36+
return filterUniqueEntries(
37+
results
38+
.reduce((acc: Article[], curr) => {
39+
if (!curr.data) return acc
40+
return [...acc, ...curr.data]
41+
}, [])
42+
.sort((a, b) => b.published_at - a.published_at)
43+
)
4144
}
4245

4346
const renderItem = (item: Article, index: number) => (

src/features/cards/components/redditCard/RedditCard.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ArticleItem from './ArticleItem'
88
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
99
import { trackCardLanguageSelect } from 'src/lib/analytics'
1010
import { FloatingFilter, InlineTextFilter } from 'src/components/Elements'
11+
import { filterUniqueEntries } from 'src/utils/DataEnhancement'
1112

1213
export function RedditCard({ withAds, meta }: CardPropsType) {
1314
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
@@ -32,12 +33,14 @@ export function RedditCard({ withAds, meta }: CardPropsType) {
3233
const getIsLoading = () => results.some((result) => result.isLoading)
3334

3435
const getData = () => {
35-
return results
36-
.reduce((acc: Article[], curr) => {
37-
if (!curr.data) return acc
38-
return [...acc, ...curr.data]
39-
}, [])
40-
.sort((a, b) => b.reactions - a.reactions)
36+
return filterUniqueEntries(
37+
results
38+
.reduce((acc: Article[], curr) => {
39+
if (!curr.data) return acc
40+
return [...acc, ...curr.data]
41+
}, [])
42+
.sort((a, b) => b.reactions - a.reactions)
43+
)
4144
}
4245

4346
const renderItem = (item: Article, index: number) => (

src/utils/DataEnhancement.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Tag, useRemoteConfigStore, TagValuesFieldType } from 'src/features/remoteConfig'
2+
import { BaseEntry } from 'src/types'
23

34
export const enhanceTags = (tags: string[]): Tag[] => {
45
const savedTags = useRemoteConfigStore.getState().supportedTags
@@ -16,3 +17,10 @@ export const getCardTagsValue = (tags: Tag[], valuesField: TagValuesFieldType):
1617
return acc
1718
}, [])
1819
}
20+
21+
22+
export const filterUniqueEntries = (entries: BaseEntry[]) => {
23+
const uniqueResults = new Map()
24+
entries.forEach((item) => uniqueResults.set(item.id, item))
25+
return Array.from(uniqueResults.values())
26+
}

0 commit comments

Comments
 (0)