diff --git a/src/components/Projects/SubmitProjectModal.tsx b/src/components/Projects/SubmitProjectModal.tsx index cc9e34780..476825442 100644 --- a/src/components/Projects/SubmitProjectModal.tsx +++ b/src/components/Projects/SubmitProjectModal.tsx @@ -6,12 +6,15 @@ import { httpPost } from '../../lib/http'; import { GitHubIcon } from '../ReactIcons/GitHubIcon.tsx'; import { SubmissionRequirement } from './SubmissionRequirement.tsx'; import { useCopyText } from '../../hooks/use-copy-text.ts'; +import { getTopGitHubLanguages } from '../../lib/github.ts'; type SubmitProjectResponse = { repositoryUrl: string; submittedAt: Date; }; +type GitHubApiLanguagesResponse = Record; + type VerificationChecksType = { repositoryExists: 'pending' | 'success' | 'error'; readmeExists: 'pending' | 'success' | 'error'; @@ -170,12 +173,16 @@ export function SubmitProjectModal(props: SubmitProjectModalProps) { projectUrlExists: 'success', }); - const languagesUrl = `${mainApiUrl}/languages`; - const languagesResponse = await fetch(languagesUrl); + const languagesResponse = await fetch(`${mainApiUrl}/languages`); let languages: string[] = []; if (languagesResponse.ok) { - const languagesData = await languagesResponse.json(); - languages = Object.keys(languagesData || {})?.slice(0, 4); + const languagesData = + (await languagesResponse.json()) as GitHubApiLanguagesResponse; + + languages = getTopGitHubLanguages(languagesData); + if (languages?.length === 0) { + languages = Object.keys(languagesData || {})?.slice(0, 4); + } } const submitProjectUrl = `${import.meta.env.PUBLIC_API_URL}/v1-submit-project/${projectId}`; diff --git a/src/lib/github.ts b/src/lib/github.ts index ffe36c467..acc07450d 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -33,18 +33,22 @@ export async function getFormattedStars( return formatter.format(stars); } -const defaultRanking = "7th"; +const defaultRanking = '7th'; let ranking: string; export async function getRepositoryRank( repo = 'kamranahmedse/developer-roadmap', ): Promise { try { - const response = await fetch(`https://api.github.com/search/repositories?q=stars:>100000&o=desc&s=stars`); + const response = await fetch( + `https://api.github.com/search/repositories?q=stars:>100000&o=desc&s=stars`, + ); const json = await response.json(); const repositories = json.items || []; for (let rank = 1; rank <= repositories.length; rank++) { - if (repositories[rank - 1].full_name.toLowerCase() === repo.toLowerCase()) { + if ( + repositories[rank - 1].full_name.toLowerCase() === repo.toLowerCase() + ) { ranking = `${rank}${getOrdinalSuffix(rank)}`; } } @@ -70,4 +74,22 @@ function getOrdinalSuffix(rank: number): string { default: return 'th'; } -} \ No newline at end of file +} + +// fetches the top languages of a GitHub repository +// i.e. 90% of the bytes are in these languages +export function getTopGitHubLanguages(languages: Record) { + const total = Object.values(languages).reduce((a, b) => a + b, 0); + + let sum = 0; + let topLanguages = []; + for (const [language, bytes] of Object.entries(languages)) { + sum += bytes; + topLanguages.push(language); + if (sum / total >= 0.9) { + break; + } + } + + return topLanguages; +}