diff --git a/page/tsconfig.json b/page/tsconfig.json index ff647ec..174cade 100644 --- a/page/tsconfig.json +++ b/page/tsconfig.json @@ -1,4 +1,4 @@ { "extends": "astro/tsconfigs/strict", - "include": ["../src/scripts/**/*.ts"] + "include": ["../src/scripts/**/*.ts"], } diff --git a/src/scripts/internal/api.ts b/src/scripts/internal/api.ts index 2254eac..1f24767 100644 --- a/src/scripts/internal/api.ts +++ b/src/scripts/internal/api.ts @@ -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 { + 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; + } +} diff --git a/src/scripts/internal/dom-manipulation.ts b/src/scripts/internal/dom-manipulation.ts index 92f5170..f730424 100644 --- a/src/scripts/internal/dom-manipulation.ts +++ b/src/scripts/internal/dom-manipulation.ts @@ -3,7 +3,6 @@ import { createSizeSpan, createEmptySizeSpan, createTotalSizeElement, - formatBytes, getFileAnchors, getFirstTd, getNavButtons, @@ -11,12 +10,14 @@ import { 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. @@ -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; @@ -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); } @@ -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 diff --git a/src/scripts/internal/format.ts b/src/scripts/internal/format.ts index 9683e26..771bbcc 100644 --- a/src/scripts/internal/format.ts +++ b/src/scripts/internal/format.ts @@ -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]; +} diff --git a/tsconfig.json b/tsconfig.json index 36d909a..d2afef5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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 @@ -27,6 +27,6 @@ // best practices "strict": true, "forceConsistentCasingInFileNames": true, - "skipLibCheck": true - } + "skipLibCheck": true, + }, }