Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

Commit 290b63c

Browse files
committed
split code for easier reusability
1 parent 142ecd4 commit 290b63c

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

src/lib/githubButton.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @function GithubButton
3+
*/
4+
import * as PropTypes from "prop-types";
5+
import * as React from "react";
6+
7+
import classes from "../styles.css";
8+
9+
import StarIcon from "./icons/starIcon";
10+
11+
function getDataForVariant(variant: string, count: number): { title: string; label: string; Icon: () => JSX.Element; } {
12+
let title: string;
13+
let label: string;
14+
if (variant === "star") {
15+
title = "Star";
16+
label = `${count} user starred this repository`;
17+
return { title, label, Icon: StarIcon };
18+
}
19+
// throw new Error("Invalid Variant, supply one of [star, ]");
20+
return { title: "", label: "", Icon: StarIcon };
21+
}
22+
23+
export interface IPropTypes {
24+
owner: string;
25+
repo: string;
26+
variant: string;
27+
count: number;
28+
}
29+
30+
export default function GithubButton(props: IPropTypes) {
31+
const { owner, repo, variant, count } = props;
32+
const { title, label, Icon } = getDataForVariant(variant, count);
33+
34+
return (
35+
<div className={classes.root}>
36+
<button title={`Star ${owner}/${repo}`} className={classes.button}>
37+
<Icon />
38+
{title}
39+
</button>
40+
<a
41+
className={classes.count}
42+
href={`https://github.com/${owner}/${repo}/stargazers`}
43+
aria-label={label}
44+
target="_blank"
45+
rel="noreferrer noopener"
46+
>
47+
{count}
48+
</a>
49+
</div>
50+
);
51+
}
52+
53+
GithubButton.propTypes = {
54+
count: PropTypes.node.isRequired,
55+
owner: PropTypes.string.isRequired,
56+
repo: PropTypes.string.isRequired,
57+
variant: PropTypes.oneOf(["star", "fork"]),
58+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import classes from "../styles.css";
2+
import classes from "../../styles.css";
33

44
export default function StarIcon() {
55
return (

src/star/star.tsx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import * as PropTypes from "prop-types";
55
import * as React from "react";
66

7-
import classes from "../styles.css";
8-
import StarSvg from "./starIcon";
7+
import GithubButton from "../lib/githubButton";
98

109
export interface IPropTypes {
1110
owner: string;
@@ -37,24 +36,8 @@ export default class Star extends React.Component<IPropTypes, IState> {
3736
}
3837

3938
render() {
40-
const { owner, repo } = this.props;
39+
// const { owner, repo } = this.props;
4140
const { stargazers_count } = this.state;
42-
return (
43-
<div className={classes.root}>
44-
<button title={`Star ${owner}/${repo}`} className={classes.button}>
45-
<StarSvg />
46-
Star
47-
</button>
48-
<a
49-
className={classes.count}
50-
href={`https://github.com/${owner}/${repo}/stargazers`}
51-
aria-label={`${stargazers_count} user starred this repository`}
52-
target="_blank"
53-
rel="noreferrer noopener"
54-
>
55-
{stargazers_count}
56-
</a>
57-
</div>
58-
);
41+
return <GithubButton variant="star" count={stargazers_count} { ...this.props } />;
5942
}
6043
}

0 commit comments

Comments
 (0)