Skip to content

Commit

Permalink
Refactor project descriptions fetching logic to use parallel requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-Kwan committed Dec 22, 2023
1 parent e25afe1 commit 7f057c0
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions pages/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ const projectsWithDescriptions = [
const loadingMessage =
'Fetching GitHub Repository Description... Please wait...';
// const projectsWithDescriptions = [''];

function LoadingCard() {
return (
<div className="flex items-center justify-center p-4">
<div className="animate-spin rounded-full h-32 w-32 border-t-2 border-b-2 border-blue-500"></div>
</div>
);
}
interface ProjectsProps {
descriptions: { [key: string]: string };
}
export default function Projects({ descriptions }: ProjectsProps) {
const isLoading = Object.values(descriptions).includes(loadingMessage);
return (
<Layout>
<Head>
Expand Down Expand Up @@ -183,12 +192,20 @@ export async function getServerSideProps() {
const descriptions: { [key: string]: string } = {};

try {
for (const project of projectsWithDescriptions) {
const description = await getRepoDescription(
`https://github.com/Kevin-Kwan/${project}`
);
descriptions[project] = description;
}
// Fetch all descriptions in parallel
const descriptionPromises = projectsWithDescriptions.map((project) =>
getRepoDescription(`https://github.com/Kevin-Kwan/${project}`)
.then((description) => {
descriptions[project] = description;
})
.catch((error) => {
console.error(`Error fetching description for ${project}: ${error}`);
descriptions[project] =
'Error: Could not fetch GitHub Repository description. Please try again later.';
})
);

await Promise.all(descriptionPromises);
} catch (error) {
console.error(`Error fetching descriptions: ${error}`);
}
Expand Down

0 comments on commit 7f057c0

Please sign in to comment.