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

Commit 2f88148

Browse files
committed
add watch button
1 parent 1eb0e47 commit 2f88148

File tree

9 files changed

+89
-12
lines changed

9 files changed

+89
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to the "react-github-buttons" project will be documented in this file.
44

5+
## [Unreleased]
6+
### Added
7+
8+
- Watch Button
9+
510
## [0.1.0] 2019-6-13
611
### Added
712

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# react-github-buttons
22

3-
> React Components for dynamic buttons for github repo's start, fork etc.
3+
> React Components for dynamic buttons for github repo's start, fork etc.
44
55
[![NPM](https://img.shields.io/npm/v/react-github-buttons.svg)](https://www.npmjs.com/package/react-github-buttons) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
66

77
## Features
88

9-
Fork, GitHub's Star, Fork, Watch etc.(See full list below) buttons with realtime numbers of star, fork etc. as React Components.
9+
Provies GitHub's Star, Fork, Watch etc.(See full list below) buttons with realtime numbers of star, fork etc. as React Components.
1010

1111
## Install
1212

example/src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React, { Component } from 'react';
22

3-
import { Fork, Star } from 'react-github-buttons';
3+
import { Fork, Star, Watch } from 'react-github-buttons';
44

55
export default class App extends Component {
66
render () {
77
return (
88
<div>
99
<Star owner='vaibhavhrt' repo='react-github-buttons' />
1010
<Fork owner='vaibhavhrt' repo='react-github-buttons' />
11+
<Watch owner='vaibhavhrt' repo='react-github-buttons' />
1112
</div>
1213
)
1314
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-github-buttons",
33
"version": "0.1.0",
4-
"description": "React Components for dynamic buttons for github repo&#x27;s star, fork etc.",
4+
"description": "React Components for dynamic buttons for github repo's star, fork etc.",
55
"author": "vaibhavhrt",
66
"license": "MIT",
77
"repository": "vaibhavhrt/react-github-buttons",

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Star from "./star";
22
import Fork from "./fork";
3+
import Watch from "./watch";
34

4-
export { Fork, Star };
5+
export { Fork, Star, Watch };

src/lib/githubButton.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import * as React from "react";
66

77
import classes from "../styles.css";
88

9-
import StarIcon from "./icons/starIcon";
109
import ForkIcon from "./icons/forkIcon";
10+
import StarIcon from "./icons/starIcon";
11+
import WatchIcon from "./icons/watchIcon";
1112

1213
function getDataForVariant(
1314
variant: string,
1415
count: number,
1516
owner: string,
16-
repo: string
17+
repo: string,
1718
): {
1819
title: string;
1920
btnTitle: string;
@@ -35,10 +36,16 @@ function getDataForVariant(
3536
title = "Fork";
3637
btnTitle = `Fork your own copy of ${owner}/${repo} to your account`;
3738
label = `${count} users forked this repository`;
38-
countUrl = `https://github.com/vaibhavhrt/react-github-buttons/network/members`;
39+
countUrl = `https://github.com/${owner}/${repo}/network/members`;
3940
return { title, btnTitle, label, countUrl, Icon: ForkIcon };
41+
} else if (variant === "watch") {
42+
title = "Watch";
43+
btnTitle = "";
44+
label = `${count} users are watching this repository`;
45+
countUrl = `https://github.com/${owner}/${repo}/watchers`;
46+
return { title, btnTitle, label, countUrl, Icon: WatchIcon };
4047
}
41-
throw new Error("Invalid Variant, supply one of [star, fork]");
48+
throw new Error("Invalid Variant, supply one of [star, fork, watch]");
4249
}
4350

4451
export interface IPropTypes {
@@ -56,7 +63,7 @@ export default function GithubButton(props: IPropTypes) {
5663
<div className={classes.root}>
5764
<button title={btnTitle} className={classes.button}>
5865
<Icon />
59-
{title}
66+
{` ${title}`}
6067
</button>
6168
<a
6269
className={classes.count}
@@ -75,5 +82,5 @@ GithubButton.propTypes = {
7582
count: PropTypes.node.isRequired,
7683
owner: PropTypes.string.isRequired,
7784
repo: PropTypes.string.isRequired,
78-
variant: PropTypes.oneOf(["star", "fork"]),
85+
variant: PropTypes.oneOf(["star", "fork", "watch"]),
7986
};

src/lib/icons/forkIcon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* tslint:disable:max-line-length */
21
import * as React from "react";
32
import classes from "../../styles.css";
43

@@ -13,6 +12,7 @@ export default function ForkIcon() {
1312
>
1413
<path
1514
fillRule="evenodd"
15+
// tslint:disable-next-line:max-line-length
1616
d="M8 1a1.993 1.993 0 0 0-1 3.72V6L5 8 3 6V4.72A1.993 1.993 0 0 0 2 1a1.993 1.993 0 0 0-1 3.72V6.5l3 3v1.78A1.993 1.993 0 0 0 5 15a1.993 1.993 0 0 0 1-3.72V9.5l3-3V4.72A1.993 1.993 0 0 0 8 1zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3 10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zm3-10c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"
1717
/>
1818
</svg>

src/lib/icons/watchIcon.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from "react";
2+
import classes from "../../styles.css";
3+
4+
export default function StarIcon() {
5+
return (
6+
<svg
7+
className={classes.svgicon}
8+
viewBox="0 0 16 16"
9+
version="1.1"
10+
width="16"
11+
height="16"
12+
>
13+
<path
14+
fillRule="evenodd"
15+
// tslint:disable-next-line:max-line-length
16+
d="M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z"
17+
/>
18+
</svg>
19+
);
20+
}

src/watch.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @function Watch
3+
*/
4+
import * as PropTypes from "prop-types";
5+
import * as React from "react";
6+
7+
import GithubButton from "./lib/githubButton";
8+
9+
export interface IPropTypes {
10+
owner: string;
11+
repo: string;
12+
}
13+
14+
export interface IState {
15+
watchers_count: number;
16+
}
17+
18+
export default class Watch extends React.Component<IPropTypes, IState> {
19+
static propTypes = {
20+
owner: PropTypes.string.isRequired,
21+
repo: PropTypes.string.isRequired,
22+
};
23+
24+
constructor(props: IPropTypes) {
25+
super(props);
26+
this.state = {
27+
watchers_count: 0,
28+
};
29+
}
30+
31+
componentDidMount() {
32+
const { owner, repo } = this.props;
33+
fetch(`https://api.github.com/repos/${owner}/${repo}`).then((res) => res.json()).then((res) => {
34+
this.setState({ watchers_count: res.watchers_count });
35+
});
36+
}
37+
38+
render() {
39+
// const { owner, repo } = this.props;
40+
const { watchers_count } = this.state;
41+
return <GithubButton variant="watch" count={watchers_count} { ...this.props } />;
42+
}
43+
}

0 commit comments

Comments
 (0)