Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub components for Repos, Issues, PullRequests #2367

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/web/src/components/Posts/GitHubInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createStyles, makeStyles, Theme, ListSubheader } from '@material-ui/core';
import Repos from './Repos';
import Issues from './Issues';
import PullRequests from './Pullrequests';
menghif marked this conversation as resolved.
Show resolved Hide resolved

const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
padding: '0',
display: 'flex',
borderLeft: '1.5px solid #999999',
flexDirection: 'column',
[theme.breakpoints.up('lg')]: {
width: '21rem',
},
color: theme.palette.text.secondary,
},
GitHubInfoContainer: {
margin: '2rem 0 0 1rem',
},
})
);

type Props = {
ghUrls: string[];
};

const filterGitHubUrls = (ghUrls: string[]) => {
const issues: string[] = [];
const pullRequests: string[] = [];
const repos: string[] = [];

ghUrls.forEach((ghUrl) => {
humphd marked this conversation as resolved.
Show resolved Hide resolved
// add unique repo urls to repos array
if (
ghUrl.match(/https:\/\/github\.com\/[^\/]+\/[^\/]+/) &&
humphd marked this conversation as resolved.
Show resolved Hide resolved
humphd marked this conversation as resolved.
Show resolved Hide resolved
!repos.includes(ghUrl.replace(/(https:\/\/github\.com\/[^\/]+\/[^\/]+)\/.*/, '$1'))
) {
repos.push(ghUrl.replace(/(https:\/\/github\.com\/[^\/]+\/[^\/]+)\/.*/, '$1'));
}

// remove trailing characters after issue or pullRequest number from ghUrl
const trimmedUrl: string = ghUrl.replace(
/(https:\/\/github\.com\/[^\/]+\/[^\/]+\/(issues|pull)\/[0-9]+).*/,
'$1'
);

// add unique issue urls to issues array
if (trimmedUrl.match(/https:\/\/github\.com\/[^\/]+\/[^\/]+\/issues\/[0-9]+/)) {
if (!issues.includes(trimmedUrl)) issues.push(trimmedUrl);
}

// add unique pull request urls to pullRequests array
if (trimmedUrl.match(/https:\/\/github\.com\/[^\/]+\/[^\/]+\/pull\/[0-9]+/)) {
if (!pullRequests.includes(trimmedUrl)) pullRequests.push(trimmedUrl);
}
});
return { repos, issues, pullRequests };
};

const GitHubInfo = ({ ghUrls }: Props) => {
const classes = useStyles();
const { repos, issues, pullRequests } = filterGitHubUrls(ghUrls);

return (
<ListSubheader className={classes.root}>
<div className={classes.GitHubInfoContainer}>
{repos.length ? <Repos repoUrls={repos} /> : null}
{issues.length ? <Issues issueUrls={issues} /> : null}
{pullRequests.length ? <PullRequests prUrls={pullRequests} /> : null}
DukeManh marked this conversation as resolved.
Show resolved Hide resolved
</div>
</ListSubheader>
);
};

export default GitHubInfo;
63 changes: 63 additions & 0 deletions src/web/src/components/Posts/Issues.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { VscIssues } from 'react-icons/vsc';
humphd marked this conversation as resolved.
Show resolved Hide resolved
import { createStyles, makeStyles, Theme } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
link: {
textDecoration: 'none',
color: theme.palette.text.primary,
'&:hover': {
textDecorationLine: 'underline',
},
},
GitHubInfo: {
lineHeight: '2rem',
fontSize: '1.2rem',
},
GitHubLinkTitle: {
fontSize: '1.4rem',
margin: 0,
paddingTop: '1rem',
},
icon: {
fontSize: '2rem',
marginRight: '1rem',
verticalAlign: 'text-bottom',
},
issues: {
display: 'flex',
margin: 0,
},
issue: {
marginRight: '2rem',
},
})
);

type Props = {
issueUrls: string[];
};

const Issues = ({ issueUrls }: Props) => {
const classes = useStyles();

return (
<div className={classes.GitHubInfo}>
<h1 className={classes.GitHubLinkTitle}>
<VscIssues className={classes.icon}></VscIssues>
{issueUrls.length === 1 ? 'Issue' : 'Issues'}
menghif marked this conversation as resolved.
Show resolved Hide resolved
</h1>
<p className={classes.issues}>
{issueUrls.map((issue) => (
<p key={issue} className={classes.issue}>
<a href={issue} rel="bookmark" className={classes.link}>
menghif marked this conversation as resolved.
Show resolved Hide resolved
#{issue.replace(/https:\/\/github\.com\/.+\/issues\/([0-9]+).*/, '$1')}
</a>
</p>
))}
</p>
</div>
);
};

export default Issues;
4 changes: 4 additions & 0 deletions src/web/src/components/Posts/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import AdminButtons from '../AdminButtons';
import Spinner from '../Spinner';
import PostDesktopInfo from './PostInfo';
import PostAvatar from './PostAvatar';
import GitHubInfo from './GitHubInfo';

type Props = {
postUrl: string;
Expand Down Expand Up @@ -243,6 +244,8 @@ const PostComponent = ({ postUrl }: Props) => {
// Grab the post data from our backend so we can render it
const { data: post, error } = useSWR<Post>(postUrl);
const [expandHeader, setExpandHeader] = useState(false);
// Extract all the github urls from the post
const extractedGitHubUrls: string[] = post?.html ? extractGitHubUrlsFromPost(post.html) : [];
DukeManh marked this conversation as resolved.
Show resolved Hide resolved

if (error) {
console.error(`Error loading post at ${postUrl}`, error);
Expand Down Expand Up @@ -331,6 +334,7 @@ const PostComponent = ({ postUrl }: Props) => {
postDate={formatPublishedDate(post.updated)}
blogUrl={post.feed.link}
/>
{extractedGitHubUrls.length ? <GitHubInfo ghUrls={extractedGitHubUrls} /> : null}
humphd marked this conversation as resolved.
Show resolved Hide resolved
</ListSubheader>
)}
<div className={classes.content}>
Expand Down
63 changes: 63 additions & 0 deletions src/web/src/components/Posts/Pullrequests.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { VscGitPullRequest } from 'react-icons/vsc';
import { createStyles, makeStyles, Theme } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
link: {
textDecoration: 'none',
color: theme.palette.text.primary,
'&:hover': {
textDecorationLine: 'underline',
},
},
GitHubInfo: {
lineHeight: '2rem',
fontSize: '1.2rem',
},
GitHubLinkTitle: {
fontSize: '1.4rem',
margin: 0,
paddingTop: '1rem',
},
icon: {
fontSize: '2rem',
marginRight: '1rem',
verticalAlign: 'text-bottom',
},
pullRequests: {
display: 'flex',
margin: 0,
},
pullRequest: {
marginRight: '2rem',
},
})
);

type Props = {
prUrls: string[];
};

const PullRequests = ({ prUrls }: Props) => {
const classes = useStyles();

return (
<div className={classes.GitHubInfo}>
<h1 className={classes.GitHubLinkTitle}>
<VscGitPullRequest className={classes.icon}></VscGitPullRequest>
{prUrls.length === 1 ? 'Pull Request' : 'Pull Requests'}
</h1>
<p className={classes.pullRequests}>
{prUrls.map((pullRequest) => (
<p key={pullRequest} className={classes.pullRequest}>
<a href={pullRequest} rel="bookmark" className={classes.link}>
#{pullRequest.replace(/https:\/\/github\.com\/.+\/pull\/([0-9]+).*/, '$1')}
</a>
</p>
))}
</p>
</div>
);
};

export default PullRequests;
59 changes: 59 additions & 0 deletions src/web/src/components/Posts/Repos.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { VscRepoForked } from 'react-icons/vsc';
import { createStyles, makeStyles, Theme } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
link: {
textDecoration: 'none',
color: theme.palette.text.primary,
'&:hover': {
textDecorationLine: 'underline',
},
},
GitHubInfo: {
lineHeight: '2rem',
fontSize: '1.2rem',
wordWrap: 'break-word',
},
GitHubLinkTitle: {
fontSize: '1.4rem',
margin: 0,
paddingTop: '1rem',
},
icon: {
fontSize: '2rem',
marginRight: '1rem',
verticalAlign: 'text-bottom',
},
repo: {
marginTop: '2rem',
lineHeight: '0.5rem',
},
})
);

type Props = {
repoUrls: string[];
};

const Repos = ({ repoUrls }: Props) => {
const classes = useStyles();

return (
<div className={classes.GitHubInfo}>
<h1 className={classes.GitHubLinkTitle}>
DukeManh marked this conversation as resolved.
Show resolved Hide resolved
<VscRepoForked className={classes.icon}></VscRepoForked>
{repoUrls.length === 1 ? 'Repo' : 'Repos'}
</h1>
{repoUrls.map((repo) => (
<p key={repo} className={classes.repo}>
<a href={repo} rel="bookmark" className={classes.link}>
{repo.replace(/https:\/\/github\.com\//, '')}
</a>
</p>
))}
</div>
);
};

export default Repos;