Skip to content

Commit a48211d

Browse files
committed
add repo type
1 parent ddb67f2 commit a48211d

6 files changed

Lines changed: 72 additions & 51 deletions

File tree

src/components/List/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react'
2-
import { ArticleType, ConferenceType } from 'src/types'
2+
import { ArticleType, ConferenceType, RepoType } from 'src/types'
33

44
export type ListComponentPropsType = {
5-
items: ArticleType[] | ConferenceType[]
5+
items: ArticleType[] | ConferenceType[] | RepoType[],
66
isLoading: boolean,
77
renderItem: (item: any, index: number) => React.ReactNode,
88
withAds: boolean,

src/features/githubCard/api/getArticles.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useQueries, UseQueryOptions } from '@tanstack/react-query'
2+
import { QueryConfig, ExtractFnReturnType } from 'src/lib/react-query'
3+
import { RepoType } from 'src/types'
4+
import { axios } from 'src/lib/axios'
5+
6+
const getRepos = async (tag: string, dateRange: string): Promise<RepoType[]> => {
7+
return axios.get(`/data/v2/github/${tag}/${dateRange}.json?s`)
8+
}
9+
10+
type QueryFnType = typeof getRepos
11+
12+
type UseGetReposOptions = {
13+
config?: QueryConfig<QueryFnType>
14+
tags: string[]
15+
dateRange: "daily" | "monthly" | "weekly"
16+
}
17+
18+
export const useGetRepos = ({ config, tags, dateRange }: UseGetReposOptions) => {
19+
return useQueries({
20+
queries: tags.map<UseQueryOptions<RepoType[]>>((tag) => {
21+
return {
22+
...config,
23+
queryKey: ['githubArticles', tag, dateRange],
24+
queryFn: () => getRepos(tag, dateRange),
25+
}
26+
})
27+
})
28+
}

src/features/githubCard/components/GithubCard.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useState, useEffect } from 'react'
22
import CardComponent from 'src/components/CardComponent'
33
import { ListComponent } from 'src/components/List'
4-
import { useGetArticles } from '../api/getArticles'
5-
import { ArticleType, CardPropsType } from 'src/types'
4+
import { useGetRepos } from '../api/getRepos'
5+
import { RepoType, CardPropsType } from 'src/types'
66
import { useUserPreferences } from 'src/stores/preferences'
77
import { getCardTagsValue } from 'src/utils/DataEnhancement'
8-
import ArticleItem from './ArticleItem'
8+
import RepoItem from './RepoItem'
99
import { Tag } from 'src/features/remoteConfig'
1010
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
1111
import { trackCardLanguageSelect, trackCardDateRangeSelect } from 'src/lib/analytics'
@@ -56,7 +56,7 @@ export function GithubCard(props: CardPropsType) {
5656
return selectedTag.githubValues
5757
}
5858

59-
const results = useGetArticles({
59+
const results = useGetRepos({
6060
tags: getQueryTags(),
6161
dateRange: selectedDateRange.value,
6262
config: {
@@ -68,15 +68,15 @@ export function GithubCard(props: CardPropsType) {
6868

6969
const getData = () => {
7070
return results
71-
.reduce((acc: ArticleType[], curr) => {
71+
.reduce((acc: RepoType[], curr) => {
7272
if (!curr.data) return acc
7373
return [...acc, ...curr.data]
7474
}, [])
75-
.sort((a, b) => b.reactions - a.reactions)
75+
.sort((a, b) => b.stars - a.stars)
7676
}
7777

78-
const renderItem = (item: ArticleType, index: number) => (
79-
<ArticleItem
78+
const renderItem = (item: RepoType, index: number) => (
79+
<RepoItem
8080
item={item}
8181
key={`rp-${index}`}
8282
index={index}

src/features/githubCard/components/ArticleItem.tsx renamed to src/features/githubCard/components/RepoItem.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import CardLink from 'src/components/CardLink'
22
import CardItemWithActions from 'src/components/CardItemWithActions'
33
import { Attributes } from 'src/lib/analytics'
4-
import { ArticleItemPropsType } from 'src/types'
4+
import { RepoItemPropsType } from 'src/types'
55
import ColoredLanguagesBadge from 'src/components/ColoredLanguagesBadge'
66
import { VscRepo, VscRepoForked, VscStarFull } from 'react-icons/vsc'
77

88
const sourceName = 'github'
99

10-
const ArticleItem = (props: ArticleItemPropsType) => {
11-
const { item, index, listingMode, selectedTag } = props
10+
function numberWithCommas(x:number|string) {
11+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
12+
}
1213

14+
const RepoItem = (props: RepoItemPropsType) => {
15+
const { item, index, listingMode, selectedTag } = props
16+
1317
return (
1418
<CardItemWithActions
1519
source={sourceName}
@@ -22,7 +26,7 @@ const ArticleItem = (props: ArticleItemPropsType) => {
2226
className="githubTitle"
2327
link={item.url}
2428
analyticsAttributes={{
25-
[Attributes.POINTS]: item.reactions,
29+
[Attributes.POINTS]: numberWithCommas(item.stars),
2630
[Attributes.TRIGERED_FROM]: 'card',
2731
[Attributes.TITLE]: item.title,
2832
[Attributes.LINK]: item.url,
@@ -35,15 +39,15 @@ const ArticleItem = (props: ArticleItemPropsType) => {
3539
<p className="rowDescription">{item.description}</p>
3640
{listingMode === 'normal' && (
3741
<div className="rowDetails">
38-
<ColoredLanguagesBadge languages={item.tags} />
39-
{item.reactions && (
42+
<ColoredLanguagesBadge languages={[item.programmingLanguage]} />
43+
{numberWithCommas(item.stars) && (
4044
<span className="rowItem">
41-
<VscStarFull className="rowItemIcon" /> {item.reactions} stars
45+
<VscStarFull className="rowItemIcon" /> {numberWithCommas(item.stars)} stars
4246
</span>
4347
)}
4448
{item.forks && (
4549
<span className="rowItem">
46-
<VscRepoForked className="rowItemIcon" /> {item.forks} forks
50+
<VscRepoForked className="rowItemIcon" /> {numberWithCommas(item.forks)} forks
4751
</span>
4852
)}
4953
</div>
@@ -54,4 +58,4 @@ const ArticleItem = (props: ArticleItemPropsType) => {
5458
)
5559
}
5660

57-
export default ArticleItem
61+
export default RepoItem

src/types/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,20 @@ export type ArticleType = {
4949
flair_text?: string
5050
flair_background_color?: string
5151
flair_text_color?: string
52-
owner?: string
53-
forks?: string
54-
starsInDateRange?: string
55-
name?: string
52+
}
53+
54+
export type RepoType = {
55+
id: string
56+
title: string
57+
url: string
58+
programmingLanguage: string
59+
stars: number
60+
source: string
61+
description: string
62+
owner: string
63+
forks: number
64+
starsInDateRange?: number
65+
name: string
5666
}
5767

5868
export type ConferenceType = {
@@ -90,4 +100,11 @@ export type ConferenceItemPropsType = {
90100
index: number
91101
item: ConferenceType
92102
listingMode?: ListingMode
103+
}
104+
105+
export type RepoItemPropsType = {
106+
index: number
107+
item: RepoType
108+
listingMode?: ListingMode,
109+
selectedTag?: SelectedTag
93110
}

0 commit comments

Comments
 (0)