Skip to content

Commit e2e6286

Browse files
authored
Merge pull request #73 from medyo/feature/aggr-repos
Feature/aggr repos
2 parents 64488cd + 79a8c4f commit e2e6286

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

src/cards/ReposCard.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@ function ReposCard({ analyticsTag, icon, withAds }) {
8282
}
8383

8484
const globalTag = { value: 'global', label: 'All trending', githubValues: ['global'] }
85+
const myLangsTag = { value: 'myLangs', label: 'My Languages', githubValues: ['myLangs'] }
8586

8687
const preferences = useContext(PreferencesContext)
8788

8889
const { userSelectedTags = [], dispatcher, cardsSettings } = preferences
8990

90-
const getTags = () => [...userSelectedTags, globalTag]
91+
const getTags = () => [...userSelectedTags, globalTag, myLangsTag]
9192

9293
const { show: showMenu } = useContextMenu()
9394

9495
const [selectedTag, setSelectedTag] = useState(getInitialSelectedTag())
9596
const [since, setSince] = useState(getInitialDateRange())
9697
const [refresh, setRefresh] = useState(true)
98+
const [repos, setRepos] = useState({})
9799
const dateRangeMapper = {
98100
daily: 'the day',
99101
weekly: 'the week',
@@ -102,6 +104,7 @@ function ReposCard({ analyticsTag, icon, withAds }) {
102104

103105
useEffect(() => {
104106
setSelectedTag(getInitialSelectedTag())
107+
setRepos({})
105108
setRefresh(!refresh)
106109
}, [userSelectedTags])
107110

@@ -135,7 +138,34 @@ function ReposCard({ analyticsTag, icon, withAds }) {
135138
throw Error(`Github Trending does not support ${selectedTag.label}.`)
136139
}
137140

138-
const data = await githubApi.getTrending(selectedTag.githubValues[0], since)
141+
const tagValue = selectedTag.githubValues[0]
142+
const key = `${tagValue}-${since}`
143+
144+
if (repos[key]) {
145+
return repos[key]
146+
}
147+
148+
if (tagValue == myLangsTag.githubValues[0]) {
149+
const promises = userSelectedTags.map(
150+
t => !t.githubValues ? false : githubApi.getTrending(t.githubValues[0], since)
151+
)
152+
let values = await Promise.all(promises)
153+
const nbrTags = values.length
154+
let minLength = Math.min(...values.map(v => v.length))
155+
const data = []
156+
for (let index = 0; index < minLength; index++) {
157+
for (let i = 0; i < nbrTags; i++) {
158+
data.push(values[i][index])
159+
}
160+
}
161+
162+
setRepos({ ...repos, [key]: data })
163+
return data
164+
165+
}
166+
167+
const data = await githubApi.getTrending(tagValue, since)
168+
setRepos({ ...repos, [key]: data })
139169
return data
140170
}
141171

0 commit comments

Comments
 (0)