diff --git a/src/shared/storage/CacheStore.ts b/src/shared/storage/CacheStore.ts index 804001f7..455dd7c5 100644 --- a/src/shared/storage/CacheStore.ts +++ b/src/shared/storage/CacheStore.ts @@ -1,18 +1,15 @@ import type { CachedData } from '@shared/types/CachedData'; import { createLocalStore, debugStore } from 'chrome-extension-toolkit'; -import { generateRandomId } from '../util/random'; - interface ICacheStore { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - github: Record>; + github: Record>; } /** * A store that is used for storing cached data such as GitHub contributors */ export const CacheStore = createLocalStore({ - github: {} + github: {}, }); debugStore({ cacheStore: CacheStore }); diff --git a/src/shared/types/CachedData.ts b/src/shared/types/CachedData.ts index 43ca8e57..03c8fc53 100644 --- a/src/shared/types/CachedData.ts +++ b/src/shared/types/CachedData.ts @@ -1,4 +1,8 @@ +/** + * Represents cached data with its fetch timestamp + * @template T The type of the cached data + */ export type CachedData = { data: T; dataFetched: number; -}; \ No newline at end of file +}; diff --git a/src/views/lib/getGitHubStats.ts b/src/views/lib/getGitHubStats.ts index 62586ac7..501f8b9e 100644 --- a/src/views/lib/getGitHubStats.ts +++ b/src/views/lib/getGitHubStats.ts @@ -1,7 +1,6 @@ import { Octokit } from '@octokit/rest'; import { CacheStore } from '@shared/storage/CacheStore'; import type { CachedData } from '@shared/types/CachedData'; -import { serialize } from 'chrome-extension-toolkit'; // Types type TeamMember = { @@ -66,7 +65,7 @@ export type LD_ADMIN_GITHUB_USERNAMES = (typeof LONGHORN_DEVELOPERS_ADMINS)[numb */ export class GitHubStatsService { private octokit: Octokit; - private cache: Record>; + private cache: Record>; constructor(githubToken?: string) { this.octokit = githubToken ? new Octokit({ auth: githubToken }) : new Octokit(); @@ -75,10 +74,14 @@ export class GitHubStatsService { private async getCachedData(key: string): Promise | null> { if (Object.keys(this.cache).length === 0) { - this.cache = await CacheStore.get('github') as Record>; + const githubCache = await CacheStore.get('github'); + if (githubCache && typeof githubCache === 'object') { + this.cache = githubCache as Record>; + } } - const cachedItem = this.cache[key]; - if (cachedItem && Date.now() - new Date(cachedItem.dataFetched).getTime() < CACHE_TTL) { + + const cachedItem = this.cache[key] as CachedData | undefined; + if (cachedItem && Date.now() - cachedItem.dataFetched < CACHE_TTL) { return cachedItem; } return null; @@ -86,9 +89,13 @@ export class GitHubStatsService { private async setCachedData(key: string, data: T): Promise { if (Object.keys(this.cache).length === 0) { - this.cache = await CacheStore.get('github') as Record>; + const githubCache = await CacheStore.get('github'); + if (githubCache && typeof githubCache === 'object') { + this.cache = githubCache as Record>; + } } - this.cache[key] = { data, dataFetched: (new Date()).getTime() }; + + this.cache[key] = { data, dataFetched: Date.now() }; await CacheStore.set('github', this.cache); } @@ -142,7 +149,7 @@ export class GitHubStatsService { private async fetchContributorNames(contributors: string[]): Promise> { const names: Record = {}; await Promise.all( - contributors.map(async (contributor) => { + contributors.map(async contributor => { const cacheKey = `contributor_name_${contributor}`; const cachedName = await this.getCachedData(cacheKey); let name = `@${contributor}`;