Skip to content

Commit f9f202c

Browse files
committed
refactor the card header dropdown component
1 parent 60abe18 commit f9f202c

2 files changed

Lines changed: 59 additions & 50 deletions

File tree

src/cards/ReposCard.js

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,59 @@
11
import React, { useState, useEffect, useContext } from 'react'
22
import CardComponent from "../components/CardComponent";
3-
import ListComponent from "../components/ListComponent";
4-
import { SiGithub } from 'react-icons/si';
5-
import { VscRepo, VscRepoForked, VscStarFull } from 'react-icons/vsc';
3+
import ListComponent from '../components/ListComponent'
4+
import { VscRepo, VscRepoForked, VscStarFull } from 'react-icons/vsc'
65
import githubApi from '../services/github'
7-
import { RiArrowDownSFill } from 'react-icons/ri';
8-
import {
9-
Menu,
10-
Item,
11-
animation,
12-
useContextMenu
13-
} from "react-contexify";
14-
import "react-contexify/dist/ReactContexify.css";
6+
import { Menu, Item, animation, useContextMenu } from 'react-contexify'
7+
import 'react-contexify/dist/ReactContexify.css'
158
import PreferencesContext from '../preferences/PreferencesContext'
16-
import CardLink from "../components/CardLink";
9+
import CardLink from '../components/CardLink'
1710
import CardItemWithActions from '../components/CardItemWithActions'
18-
import { trackReposLanguageChange, trackReposDateRangeChange } from "../utils/Analytics"
19-
import ColoredLanguagesBadge from "../components/ColoredLanguagesBadge"
20-
11+
import { trackReposLanguageChange, trackReposDateRangeChange } from '../utils/Analytics'
12+
import ColoredLanguagesBadge from '../components/ColoredLanguagesBadge'
13+
import DropDownMenu from '../components/DropDownMenu'
2114

2215
const RepoItem = ({ item, index }) => {
23-
2416
const { listingMode } = useContext(PreferencesContext)
2517

2618
return (
2719
<CardItemWithActions
2820
source={'github'}
2921
key={index}
3022
index={index}
31-
item={{...item, title: `${item.owner ? item.owner + "/" : ""}${item.name}`}}
32-
cardItem={(
23+
item={{ ...item, title: `${item.owner ? item.owner + '/' : ''}${item.name}` }}
24+
cardItem={
3325
<>
3426
<CardLink className="githubTitle" link={item.url} analyticsSource="repos">
35-
<VscRepo className={"rowTitleIcon"} />
36-
{
37-
item.owner && `${item?.owner}/`
38-
}
27+
<VscRepo className={'rowTitleIcon'} />
28+
{item.owner && `${item?.owner}/`}
3929
<b>{item.name}</b>
4030
</CardLink>
4131
<p className="rowDescription">{item.description}</p>
42-
{ listingMode === "normal" &&
32+
{listingMode === 'normal' && (
4333
<div className="rowDetails">
4434
<ColoredLanguagesBadge languages={[item.programmingLanguage]} />
45-
{
46-
item.stars &&
47-
<span className="rowItem"><VscStarFull className="rowItemIcon" /> {item.stars} stars</span>
48-
}
49-
{
50-
item.forks &&
51-
<span className="rowItem"><VscRepoForked className="rowItemIcon" /> {item.forks} forks</span>
52-
}
35+
{item.stars && (
36+
<span className="rowItem">
37+
<VscStarFull className="rowItemIcon" /> {item.stars} stars
38+
</span>
39+
)}
40+
{item.forks && (
41+
<span className="rowItem">
42+
<VscRepoForked className="rowItemIcon" /> {item.forks} forks
43+
</span>
44+
)}
5345
</div>
54-
}
46+
)}
5547
</>
56-
)}
48+
}
5749
/>
5850
)
5951
}
6052

61-
62-
const TAGS_MENU_ID = "tags-menu";
63-
const DATE_RANGE_MENU_ID = "date-range-id"
53+
const TAGS_MENU_ID = 'tags-menu'
54+
const DATE_RANGE_MENU_ID = 'date-range-id'
6455

6556
function ReposCard({ analyticsTag, icon, withAds }) {
66-
6757
const getInitialSelectedTag = () => {
6858
const githubCardSettings = cardsSettings && cardsSettings.repos ? cardsSettings.repos : null
6959
if (githubCardSettings && githubCardSettings.language) {
@@ -139,20 +129,20 @@ function ReposCard({ analyticsTag, icon, withAds }) {
139129
}
140130

141131
const tagValue = selectedTag.githubValues[0]
142-
const key = `${tagValue}-${since}`
132+
const key = `${tagValue}-${since}`
143133

144134
if (repos[key]) {
145135
return repos[key]
146136
}
147137

148138
if (tagValue == myLangsTag.githubValues[0]) {
149-
const promises = userSelectedTags.map(
150-
t => !t.githubValues ? false : githubApi.getTrending(t.githubValues[0], since)
139+
const promises = userSelectedTags.map((t) =>
140+
!t.githubValues ? false : githubApi.getTrending(t.githubValues[0], since)
151141
)
152142
let values = await Promise.all(promises)
153143
values = values.filter(Boolean)
154144
const nbrTags = values.length
155-
let minLength = Math.min(...values.map(v => v.length))
145+
let minLength = Math.min(...values.map((v) => v.length))
156146
const data = []
157147
for (let index = 0; index < minLength; index++) {
158148
for (let i = 0; i < nbrTags; i++) {
@@ -162,7 +152,6 @@ function ReposCard({ analyticsTag, icon, withAds }) {
162152

163153
setRepos({ ...repos, [key]: data })
164154
return data
165-
166155
}
167156

168157
const data = await githubApi.getTrending(tagValue, since)
@@ -176,15 +165,9 @@ function ReposCard({ analyticsTag, icon, withAds }) {
176165
}
177166
return (
178167
<div style={{ display: 'inline-block', margin: 0, padding: 0 }}>
179-
<span onClick={displayMenu} className="headerSelect" data-target-id={TAGS_MENU_ID}>
180-
{selectedTag.label}
181-
<RiArrowDownSFill className="headerSelectIcon" />
182-
</span>
168+
<DropDownMenu tagId={TAGS_MENU_ID} label={selectedTag.label} />
183169
<span> Repos of </span>
184-
<span onClick={displayMenu} className="headerSelect" data-target-id={DATE_RANGE_MENU_ID}>
185-
{dateRangeMapper[since]}
186-
<RiArrowDownSFill className="headerSelectIcon" />
187-
</span>
170+
<DropDownMenu tagId={DATE_RANGE_MENU_ID} label={dateRangeMapper[since]} />
188171
</div>
189172
)
190173
}

src/components/DropDownMenu.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { RiArrowDownSFill } from 'react-icons/ri'
2+
import { useContextMenu } from 'react-contexify'
3+
4+
const DropDownMenu = ({ tagId, label }) => {
5+
const { show: showMenu } = useContextMenu()
6+
7+
const displayMenu = (e) => {
8+
const {
9+
target: {
10+
dataset: { targetId },
11+
},
12+
} = e
13+
if (targetId) {
14+
showMenu(e, { id: targetId })
15+
}
16+
}
17+
18+
return (
19+
<span onClick={displayMenu} className="headerSelect" data-target-id={tagId}>
20+
{label}
21+
<RiArrowDownSFill className="headerSelectIcon" />
22+
</span>
23+
)
24+
}
25+
26+
export default DropDownMenu

0 commit comments

Comments
 (0)