Skip to content

refactor: コンポーネントを切り出した #429

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
41 changes: 10 additions & 31 deletions pages/kusa/[username].tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Head from 'next/head';
import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import { FetchProvider, useFetch } from 'react-hooks-fetch';
import { Suspense } from 'react';
import Head from '../../src/components/kusa/head';
import Title from '../../src/components/kusa/title';
import Contributions from '../../src/components/kusa/contributions/contributions';
import { toGrassGraphImageUrl } from '../../src/lib/to-grass-graph-image-url';

type Props = {
username: string;
todayContributionCount: number;
yesterdayContributionCount: number;
currentStreak: number;
description: string;
};

export const getServerSideProps = async (
Expand All @@ -30,8 +30,9 @@ export const getServerSideProps = async (

const [todayContributionCount, yesterdayContributionCount] = contributions;
const currentStreak = todayContributionCount > 0 ? contributions.indexOf(0) : contributions.slice(1).indexOf(0);
const description = `Today: ${todayContributionCount}, Yesterday: ${yesterdayContributionCount}, Streak: ${currentStreak}`;

return { props: { username, todayContributionCount, yesterdayContributionCount, currentStreak } };
return { props: { username, description } };
};

const fetchFunc = async (username: string) => {
Expand All @@ -49,37 +50,15 @@ const Detail = ({ username }: { username: string }) => {

const Kusa = (props: Props) => {
const username = props.username;
const imgUrl = `https://grass-graph.appspot.com/images/${username}.png?${Date.now()}`;
const siteUrl = `https://tools.swfz.io/kusa/${username}`;
const title = `GitHub Contributions(kusa) in ${username}`;
const desc = `Today: ${props.todayContributionCount}, Yesterday: ${props.yesterdayContributionCount}, Streak: ${props.currentStreak}`;
const toGitHub = `https://github.com/${username}`;

return (
<>
<Head>
<title>Kusa</title>
<meta property="og:url" content={siteUrl} />
<meta property="og:type" content="article" />
<meta property="og:title" content={title} />
<meta property="og:description" content={desc} />
<meta property="og:site_name" content="swfz tools" />
<meta property="og:image" content={imgUrl} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content={imgUrl} />
</Head>
<Head username={username} description={props.description} />
<div>
<span className="text-4xl font-bold">
<a href={toGitHub} target="_blank" rel="noreferrer">
<span className="cursor-pointer font-bold text-blue-600 no-underline hover:underline">{username}</span>
</a>
&apos;s kusa
</span>
<Title username={username} />

<img src={imgUrl} alt="GitHub Contribution" />
<span>{desc}</span>
<img src={toGrassGraphImageUrl(username)} alt="GitHub Contribution" />
<span>{props.description}</span>
{/* @ts-ignore */}
<FetchProvider initialInputs={[[fetchFunc, username]]}>
<Suspense fallback={<span>Loading...</span>}>
Expand Down
26 changes: 26 additions & 0 deletions src/components/kusa/head.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import NextHead from 'next/head';
import { toGrassGraphImageUrl } from '../../lib/to-grass-graph-image-url';

const Head = ({ username, description }: { username: string; description: string }) => {
const imageUrl = toGrassGraphImageUrl(username);
const siteUrl = `https://tools.swfz.io/kusa/${username}`;
const title = `GitHub Contributions(kusa) in ${username}`;

return (
<NextHead>
<title>Kusa</title>
<meta property="og:url" content={siteUrl} />
<meta property="og:type" content="article" />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:site_name" content="swfz tools" />
<meta property="og:image" content={imageUrl} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content={imageUrl} />
</NextHead>
);
};

export default Head;
18 changes: 18 additions & 0 deletions src/components/kusa/title.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Props = {
username: string;
};

const Title = ({ username }: Props) => {
const toGitHub = `https://github.com/${username}`;

return (
<span className="text-4xl font-bold">
<a href={toGitHub} target="_blank" rel="noreferrer">
<span className="cursor-pointer font-bold text-blue-600 no-underline hover:underline">{username}</span>
</a>
&apos;s kusa
</span>
);
};

export default Title;
7 changes: 7 additions & 0 deletions src/lib/__tests__/to-grass-graph-image-url.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { toGrassGraphImageUrl } from '../to-grass-graph-image-url';

describe('toGrassGraphImageUrl', () => {
test('check to convert to grass graph image url', () => {
expect(toGrassGraphImageUrl('swfz')).toBe('https://grass-graph.appspot.com/images/swfz.png');
});
});
3 changes: 3 additions & 0 deletions src/lib/to-grass-graph-image-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const toGrassGraphImageUrl = (username: string) => {
return `https://grass-graph.appspot.com/images/${username}.png`;
};