From eb6894b022ed91a2ac894aa56907be29b513125a Mon Sep 17 00:00:00 2001 From: samet Date: Thu, 1 Feb 2024 16:55:56 +0300 Subject: [PATCH] stars per repo feature --- components/stats/CreationDate.tsx | 5 +- components/stats/GistCreationDate.tsx | 5 +- components/stats/Languages.tsx | 5 +- components/stats/Licenses.tsx | 5 +- components/stats/StarsPerRepo.tsx | 82 +++++++++++++++++++++++++++ components/stats/Stats.tsx | 6 +- lib/utils/stats.ts | 23 ++++++++ 7 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 components/stats/StarsPerRepo.tsx diff --git a/components/stats/CreationDate.tsx b/components/stats/CreationDate.tsx index a07ceff..bf7d086 100644 --- a/components/stats/CreationDate.tsx +++ b/components/stats/CreationDate.tsx @@ -53,7 +53,10 @@ export default function CreationDate({ statsData }: Props) { {statsData .sort((a, b) => Number(b.value) - Number(a.value)) .map((data) => ( - + {data.category} {data.value} diff --git a/components/stats/GistCreationDate.tsx b/components/stats/GistCreationDate.tsx index 6a7e144..cb0be83 100644 --- a/components/stats/GistCreationDate.tsx +++ b/components/stats/GistCreationDate.tsx @@ -52,7 +52,10 @@ export default function GistCreationDate({ statsData }: Props) { {statsData .sort((a, b) => Number(b.value) - Number(a.value)) .map((data) => ( - + {data.category} {data.value} diff --git a/components/stats/Languages.tsx b/components/stats/Languages.tsx index 1f319dd..cdad9f9 100644 --- a/components/stats/Languages.tsx +++ b/components/stats/Languages.tsx @@ -87,7 +87,10 @@ export default function Languages({ {sortedLanguageData.map((data) => ( - + {data.name} {data.count} diff --git a/components/stats/Licenses.tsx b/components/stats/Licenses.tsx index 4ad7496..db29d1a 100644 --- a/components/stats/Licenses.tsx +++ b/components/stats/Licenses.tsx @@ -99,7 +99,10 @@ export default function Licenses({ licenses, count }: LicensesProps) { {Object.entries(licenses) .sort((a, b) => (b[1] as number) - (a[1] as number)) .map(([topic, count]) => ( - + {topic} {String(count)} diff --git a/components/stats/StarsPerRepo.tsx b/components/stats/StarsPerRepo.tsx new file mode 100644 index 0000000..05c2c37 --- /dev/null +++ b/components/stats/StarsPerRepo.tsx @@ -0,0 +1,82 @@ +"use client"; +import { Box, Card, Flex, Heading, Text } from "@radix-ui/themes"; +import { useState } from "react"; +import FilterChart from "./FilterChart"; +import { PieChart } from "@mui/x-charts/PieChart"; +import { + Accordion, + AccordionContent, + AccordionItem, + AccordionTrigger, +} from "@/components/ui/accordion"; +import { ScrollArea, Table } from "@radix-ui/themes"; +export default function StarsPerRepo({ + starsPerRepo, +}: { + starsPerRepo: { [key: string]: number }; +}) { + const [length, setLength] = useState( + Object.keys(starsPerRepo).length > 5 ? 5 : Object.keys(starsPerRepo).length, + ); + return ( + + + + Stars Per Repo | Top {length} + + + + + ({ + label: key, + value: value as number, + })), + }, + ]} + /> + + + + See All + + + + + + Repository + Stars + + + + {Object.entries(starsPerRepo).map(([key, value]) => ( + + {key} + {value} + + ))} + + + + + + + + ); +} diff --git a/components/stats/Stats.tsx b/components/stats/Stats.tsx index 1c1aeff..0bbd889 100644 --- a/components/stats/Stats.tsx +++ b/components/stats/Stats.tsx @@ -13,6 +13,7 @@ import { findOldestRepo, findRepoWithLongestUpdatePeriod, getCreationStatsByYear, + getStarsPerRepo, } from "@/lib/utils/stats"; import { useContext } from "react"; import Repository from "../repositories/Repository"; @@ -26,6 +27,7 @@ import CreationDate from "./CreationDate"; import GistCreationDate from "./GistCreationDate"; import DownloadData from "./DownloadData"; import StatTable from "./StatTable"; +import StarsPerRepo from "./StarsPerRepo"; export default function Stats() { const { repos, loading, gists } = useContext(GithubContext); @@ -54,6 +56,7 @@ export default function Stats() { value: count, }), ); + const starsPerRepo = getStarsPerRepo(repos); return ( @@ -74,6 +77,7 @@ export default function Stats() { totalStars={totalStars} averageStarsPerRepo={averageStarsPerRepo} /> + {starsPerRepo && } {language.length > 0 && ( )} @@ -93,7 +97,6 @@ export default function Stats() { )} - {oldestRepo && ( @@ -118,6 +121,7 @@ export default function Stats() { )} + )} diff --git a/lib/utils/stats.ts b/lib/utils/stats.ts index f7c8580..c7e9bbc 100644 --- a/lib/utils/stats.ts +++ b/lib/utils/stats.ts @@ -182,3 +182,26 @@ export const getCreationStatsByYear = (repos: GitHubRepo[]) => { return stats; }; + +export function getStarsPerRepo(repos: GitHubRepo[]) { + interface StarsPerRepo { + [key: string]: number; + } + + const starsPerRepo: StarsPerRepo = {}; + + repos.forEach((repo) => { + if (repo.stargazers_count > 0) { + starsPerRepo[repo.name] = repo.stargazers_count; + } + }); + + const sortedStarsPerRepo = Object.entries(starsPerRepo) + .sort((a, b) => b[1] - a[1]) + .reduce((acc, [name, stars]) => { + acc[name] = stars; + return acc; + }, {} as StarsPerRepo); + + return sortedStarsPerRepo; +}