From cf822dbb8cebb2ed208e40842c1539d6e2b7dd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 29 Nov 2023 15:45:43 +0900 Subject: [PATCH] feat: caching --- static/main.ts | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/static/main.ts b/static/main.ts index b48a56ea..c81c1db2 100644 --- a/static/main.ts +++ b/static/main.ts @@ -4,22 +4,27 @@ export async function mainModule() { await fetchIssues(); async function fetchIssues() { - function sortIssuesByComments(issues: GitHubIssue[]) { - return issues.sort((a, b) => b.comments - a.comments); - } - const container = document.getElementById("issues-container"); + const container = document.getElementById('issues-container'); if (!container) { - throw new Error("No issues container found"); + throw new Error('Could not find issues container'); } - - container.innerHTML = "

Loading issues...

"; + container.innerHTML = '

Loading issues...

'; try { - const response = await fetch("https://api.github.com/repos/ubiquity/devpool-directory/issues"); - if (!response.ok) { - throw new Error(`Error: ${response.status}`); + const cachedIssues = localStorage.getItem('githubIssues'); + let issues; + + if (cachedIssues) { + issues = JSON.parse(cachedIssues); + } else { + const response = await fetch('https://api.github.com/repos/ubiquity/devpool-directory/issues'); + if (!response.ok) { + throw new Error(`Error: ${response.status}`); + } + issues = await response.json(); + localStorage.setItem('githubIssues', JSON.stringify(issues)); } - const issues = await response.json(); + const sortedIssues = sortIssuesByComments(issues); displayIssues(sortedIssues); } catch (error) { @@ -38,6 +43,19 @@ export async function mainModule() { container?.appendChild(issueElement); }); } + + function sortIssuesByComments(issues: GitHubIssue[]) { + return issues.sort((a, b) => { + if (a.comments > b.comments) { + return -1; + } + if (a.comments < b.comments) { + return 1; + } + return 0; + }); + } + } mainModule() .then(() => {