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

refactor: extrace Desc component #1

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
48 changes: 26 additions & 22 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState } from "react";
import { useCallback, useState, memo } from "react";

import { useQueryResponse } from "./hooks/useQueryResponse";
import { SearchBox } from "./components/SearchBox";
Expand All @@ -8,6 +8,30 @@ import { Pagination } from "./components/Pagination";

import "./App.css";

const Desc = memo(() => {
return (
<div className="app-desc">
<div>This project implements a demo that searches the GitHub repository.</div>
<div>
<strong>
For unauthenticated requests, the rate limit allows you to make up to 10 requests per
minute. Read more about{" "}
<a
href="https://docs.github.com/en/rest/search?apiVersion=2022-11-28#rate-limit"
target="_blank"
rel="noreferrer"
>
Github API Rate limit
</a>
</strong>
</div>
<div>If you encounter this problem, please try again later.</div>
</div>
);
});

Comment on lines +11 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Desc component is correctly memoized for performance optimization. However, consider adding a comment explaining why memoization is beneficial for this component, especially since it does not accept props.

Desc.displayName = "Desc";

function App() {
const [searchParams, setSearchParams] = useState<SearchRepositoryParameters>({
q: "",
Expand Down Expand Up @@ -36,26 +60,6 @@ function App() {
[searchParams],
);

const desc = (
<div className="app-desc">
<div>This project implements a demo that searches the GitHub repository.</div>
<div>
<strong>
For unauthenticated requests, the rate limit allows you to make up to 10 requests per
minute. Read more about{" "}
<a
href="https://docs.github.com/en/rest/search?apiVersion=2022-11-28#rate-limit"
target="_blank"
rel="noreferrer"
>
Github API Rate limit
</a>
</strong>
</div>
<div>If you encounter this problem, please try again later.</div>
</div>
);

return (
<div className="App">
<SearchBox
Expand All @@ -81,7 +85,7 @@ function App() {
) : null}
</>
) : (
desc
<Desc />
)}
</div>
</div>
Expand Down
55 changes: 30 additions & 25 deletions src/components/SearchList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ type SearchListProps = {
q: string;
};

const ListItem = (props: { item: SearchRepositoryResponse["items"][number] }) => {
const { item } = props;
const dateTimeFormat = new Intl.DateTimeFormat();
return (
<li role="listitem">
<div className="repo-title">
<a href={item.html_url} target="_blank" rel="noreferrer" role="link" tabIndex={0}>
{item.full_name}
</a>
</div>
<div className="repo-desc">{item.description}</div>
{item.stargazers_count ? (
<div className="repo-info">
<div className="repo-star">
<a href={`${item.html_url}/stargazers`} target="_blank" rel="noreferrer">
<Star /> {item.stargazers_count}
</a>
</div>
<div className="repo-update">
Updated on {dateTimeFormat.format(new Date(item.updated_at))}
</div>
</div>
) : null}
</li>
);
};

export function SearchList(props: SearchListProps) {
const { result, q } = props;
if (isSearchRepositoryResponse(result)) {
const { items } = result;
const dateTimeFormat = new Intl.DateTimeFormat();
return items.length ? (
<div className="search-list">
<h2>{result.total_count} repository results</h2>
<ul role="list">
{items.map((item) => {
return (
<li role="listitem" key={item.id}>
<div className="repo-title">
<a href={item.html_url} target="_blank" rel="noreferrer" role="link" tabIndex={0}>
{item.full_name}
</a>
</div>
<div className="repo-desc">{item.description}</div>
{item.stargazers_count ? (
<div className="repo-info">
<div className="repo-star">
<a href={`${item.html_url}/stargazers`} target="_blank" rel="noreferrer">
<Star /> {item.stargazers_count}
</a>
</div>
<div className="repo-update">
Updated on {dateTimeFormat.format(new Date(item.updated_at))}
</div>
</div>
) : null}
</li>
);
})}
{items.map((item) => (
<ListItem item={item} key={item.id} />
))}
</ul>
</div>
) : (
Expand Down
Loading