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

Fix: Getting the size from github rest api #38

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 0 additions & 4 deletions page/.vscode/extensions.json

This file was deleted.

11 changes: 0 additions & 11 deletions page/.vscode/launch.json

This file was deleted.

2 changes: 1 addition & 1 deletion page/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "astro/tsconfigs/strict",
"include": ["../src/scripts/**/*.ts"]
"include": ["../src/scripts/**/*.ts"],
}
37 changes: 37 additions & 0 deletions src/scripts/internal/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,40 @@ export async function getRepoInfo(
});
return response;
}

/**
* Get the size of the repository in the specified branch.
* @param repo - The repository name in the format 'owner/repo'
* @param branch - The branch name
* @returns The size of the repository in KB
*/
export async function getRepoSize(
username: string | undefined,
reponame: string | undefined
): Promise<number> {
const token = await getToken();
const headers = new Headers({
'User-Agent': 'Your-User-Agent', // Replace with your user agent
Accept: 'application/vnd.github.v3+json',
...(token && { Authorization: `Bearer ${token}` }),
});

try {
const response = await fetch(
`https://api.github.com/repos/${username}/${reponame}`,
{ headers }
);
if (!response.ok) {
throw new Error(
`Failed to fetch repo details: ${response.status} ${response.statusText}`
);
}

const data = await response.json();
const size = data.size;
return size;
} catch (error) {
console.error('Error fetching repository size for branch:', error);
return -1;
}
}
16 changes: 7 additions & 9 deletions src/scripts/internal/dom-manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import {
createSizeSpan,
createEmptySizeSpan,
createTotalSizeElement,
formatBytes,
getFileAnchors,
getFirstTd,
getNavButtons,
getPathObject,
getRepoInfo,
getSize,
getSizeLabel,
getRepoSize,
getTable,
getThead,
getTotalSizeButton,
getTotalSizeSpan,
formatKilobytes,
} from '.';
import type { GRSUpdate, GitHubTree } from './types';
import type { GRSUpdate, PathObject } from './types';

/**
* Insert the size label element into the table head.
Expand Down Expand Up @@ -100,7 +101,7 @@ function insertToFileExplorer(
*
* @param repoInfo - The repo info
*/
function setTotalSize(repoInfo: GitHubTree) {
async function setTotalSize(pathObject: PathObject) {
const navButtons = getNavButtons();
if (!navButtons) {
return;
Expand Down Expand Up @@ -128,12 +129,9 @@ function setTotalSize(repoInfo: GitHubTree) {
return;
}

let totalSize = 0;
repoInfo.tree.forEach((item) => {
totalSize += item.size ?? 0;
});
const totalSize = await getRepoSize(pathObject.owner, pathObject.repo);

span.innerText = formatBytes(totalSize);
span.innerText = formatKilobytes(totalSize);

navButtons.appendChild(totalSizeButton);
}
Expand Down Expand Up @@ -213,7 +211,7 @@ export async function updateDOM() {

insertSizeColumn();

setTotalSize(repoInfo);
setTotalSize(pathObject);

updates.forEach(({ anchor, span, index }) => {
// for some reason the rows have two td's with name of each file
Expand Down
29 changes: 29 additions & 0 deletions src/scripts/internal/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,32 @@ export function formatBytes(bytes: number, decimals: number = 2): string {
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

/**
* Formats a number in kilobytes to a string with a given amount of decimals.
*
* @param kilobytes - The number in kilobytes to format
* @param decimals - The number of decimals
* @returns The formatted string
* @example
* ```ts
* formatKilobytes(1024);
* // '1 KB'
* ```
* @example
* ```ts
* formatKilobytes(1024, 2);
* // '1.00 KB'
* ```
*/
export function formatKilobytes(
kilobytes: number,
decimals: number = 2
): string {
if (kilobytes === 0) return '0 KB';
const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(kilobytes) / Math.log(k));
return parseFloat((kilobytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"types": [
"bun-types", // add Bun type definitions
"./types.d.ts", // add your own type definitions
"@types/chrome"
"@types/chrome",
],

// enable latest features
Expand All @@ -27,6 +27,6 @@
// best practices
"strict": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
}
"skipLibCheck": true,
},
}
Loading