-
Notifications
You must be signed in to change notification settings - Fork 205
β¨ feat(tracker): Improve Tracker page UI/UX & add Repositories tab #658
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
Mayank251125
wants to merge
3
commits into
GitMetricsLab:main
Choose a base branch
from
Mayank251125:feature/improve-tracker-ui-ux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { useState, useCallback } from 'react'; | ||
| import { Octokit } from '@octokit/core'; | ||
|
|
||
| export interface GitHubRepo { | ||
| id: number; | ||
| name: string; | ||
| full_name: string; | ||
| html_url: string; | ||
| description: string | null; | ||
| language: string | null; | ||
| stargazers_count: number; | ||
| forks_count: number; | ||
| open_issues_count: number; | ||
| visibility: string; | ||
| fork: boolean; | ||
| pushed_at: string; | ||
| created_at: string; | ||
| updated_at: string; | ||
| topics: string[]; | ||
| license: { name: string } | null; | ||
| default_branch: string; | ||
| size: number; | ||
| } | ||
|
|
||
| export const useGitHubRepos = (getOctokit: () => Octokit | null) => { | ||
| const [repos, setRepos] = useState<GitHubRepo[]>([]); | ||
| const [allRepos, setAllRepos] = useState<GitHubRepo[]>([]); | ||
| const [totalRepos, setTotalRepos] = useState(0); | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState(''); | ||
|
|
||
| const fetchRepos = useCallback( | ||
| async (username: string, page = 1, perPage = 12, token?: string) => { | ||
| const octokit = getOctokit(); | ||
| if (!octokit || !username.trim()) return; | ||
|
|
||
| setLoading(true); | ||
| setError(''); | ||
|
|
||
| try { | ||
| let endpoint: 'GET /user/repos' | 'GET /users/{username}/repos'; | ||
|
|
||
| let params: Record<string, unknown> = { | ||
| per_page: perPage, | ||
| page, | ||
| sort: 'pushed', | ||
| direction: 'desc', | ||
| }; | ||
|
|
||
| if (token) { | ||
| const authUser = await octokit.request('GET /user'); | ||
| const authenticatedLogin = authUser.data.login.toLowerCase(); | ||
| const requestedLogin = username.trim().toLowerCase(); | ||
|
|
||
| if (!requestedLogin || requestedLogin === authenticatedLogin) { | ||
| endpoint = 'GET /user/repos'; | ||
|
|
||
| params = { | ||
| ...params, | ||
| visibility: 'all', | ||
| affiliation: 'owner', | ||
| }; | ||
| } else { | ||
| endpoint = 'GET /users/{username}/repos'; | ||
|
|
||
| params = { | ||
| ...params, | ||
| username, | ||
| type: 'owner', | ||
| }; | ||
| } | ||
| } else { | ||
| endpoint = 'GET /users/{username}/repos'; | ||
|
|
||
| params = { | ||
| ...params, | ||
| username, | ||
| type: 'owner', | ||
| }; | ||
| } | ||
|
|
||
| const response = await octokit.request(endpoint, params); | ||
|
|
||
| const linkHeader = | ||
| typeof response.headers?.link === 'string' | ||
| ? response.headers.link | ||
| : ''; | ||
|
|
||
| const lastMatch = linkHeader.match(/page=(\d+)>; rel="last"/); | ||
|
|
||
| let total: number; | ||
|
|
||
| if (lastMatch) { | ||
| const lastPage = parseInt(lastMatch[1], 10); | ||
|
|
||
| if (page === 1) { | ||
| const lastParams = { ...params, page: lastPage }; | ||
| const lastResponse = await octokit.request(endpoint, lastParams); | ||
|
|
||
| total = | ||
| (lastPage - 1) * perPage + | ||
| (lastResponse.data as GitHubRepo[]).length; | ||
| } else { | ||
| total = (lastPage - 1) * perPage + perPage; | ||
| } | ||
| } else { | ||
| total = (page - 1) * perPage + (response.data as GitHubRepo[]).length; | ||
| } | ||
|
|
||
| const pageRepos = response.data as GitHubRepo[]; | ||
|
|
||
| setRepos(pageRepos); | ||
| setTotalRepos(total); | ||
|
|
||
| if (page === 1) { | ||
| if (!lastMatch) { | ||
| setAllRepos(pageRepos); | ||
| } else { | ||
| const allParams = { | ||
| ...params, | ||
| per_page: 100, | ||
| page: 1, | ||
| }; | ||
|
|
||
| const allResponse = await octokit.request(endpoint, allParams); | ||
|
|
||
| let all = [...(allResponse.data as GitHubRepo[])]; | ||
|
|
||
| const allLink = | ||
| typeof allResponse.headers?.link === 'string' | ||
| ? allResponse.headers.link | ||
| : ''; | ||
|
|
||
| const allLast = allLink.match(/page=(\d+)>; rel="last"/); | ||
|
|
||
| if (allLast) { | ||
| const totalPages = parseInt(allLast[1], 10); | ||
|
|
||
| const rest = await Promise.all( | ||
| Array.from({ length: totalPages - 1 }, (_, i) => | ||
| octokit.request(endpoint, { | ||
| ...allParams, | ||
| page: i + 2, | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| rest.forEach((r) => { | ||
| all = all.concat(r.data as GitHubRepo[]); | ||
| }); | ||
| } | ||
|
|
||
| setAllRepos(all); | ||
| setTotalRepos(all.length); | ||
| } | ||
| } | ||
| } catch (err: unknown) { | ||
| const errorObj = err as { | ||
| status?: number; | ||
| message?: string; | ||
| }; | ||
|
|
||
| const status = errorObj.status; | ||
| const message = errorObj.message?.toLowerCase() ?? ''; | ||
|
|
||
| let errorMsg: string; | ||
|
|
||
| if (status === 403) { | ||
| errorMsg = | ||
| 'GitHub API rate limit exceeded. Please provide a PAT to continue.'; | ||
| } else if (status === 404 || message.includes('not found')) { | ||
| errorMsg = | ||
| 'User not found. Please check the GitHub username.'; | ||
| } else if (status === 401) { | ||
| errorMsg = | ||
| 'Invalid token. Please check your Personal Access Token.'; | ||
| } else { | ||
| errorMsg = | ||
| 'Unable to fetch repositories. Please verify the username or network connection.'; | ||
| } | ||
|
|
||
| setError(errorMsg); | ||
| throw new Error(errorMsg); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }, | ||
| [getOctokit] | ||
| ); | ||
|
|
||
| return { | ||
| repos, | ||
| allRepos, | ||
| totalRepos, | ||
| loading, | ||
| error, | ||
| fetchRepos, | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totalReposgets overwritten with inaccurate estimate on page > 1 fetches.When
page > 1and alastMatchexists, line 104 computestotal = lastPage * perPage, which overestimates when the last page is partial. Line 113 unconditionally setstotalReposto this estimate, overwriting the accurate value that was set viaall.lengthduring the initial page-1 fetch.This causes pagination UI (context snippet at Tracker.tsx:562-578) to display incorrect page counts after navigating away from page 1.
π Proposed fix: preserve totalRepos on page > 1 when already populated
This preserves the accurate
totalRepos(set fromall.lengthon page 1) during subsequent pagination.π€ Prompt for AI Agents