Skip to content

Commit

Permalink
fix: forgot 'rest of function'
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Jan 24, 2024
1 parent 332a159 commit 05d82ef
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions src/home/fetch-github/fetch-issues-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getGitHubAccessToken } from "../getters/get-github-access-token";
import { getLocalStore } from "../getters/get-local-store";
import { GitHubIssue } from "../github-types";
import { PreviewToFullMapping } from "./preview-to-full-mapping";
import { saveImageToDB } from "../getters/get-indexed-db";

export const previewToFullMapping = new PreviewToFullMapping().getMapping();

Expand All @@ -26,27 +27,59 @@ export function fetchIssuesFull(previews: GitHubIssue[]) {

const { org, repo, issue_number } = match.groups;

return octokit.request("GET /repos/{org}/{repo}/issues/{issue_number}", { issue_number, repo, org }).then(({ data: response }) => {
const full = response as GitHubIssue;

// Update the cache with the fetched issue if it's more recent than the cached issue
const cachedIssues = (getLocalStore("gitHubIssuesFull") || []) as GitHubIssue[];
const cachedIssuesMap = new Map(cachedIssues.map((issue) => [issue.id, issue]));
const cachedIssue = cachedIssuesMap.get(full.id);
if (!cachedIssue || new Date(full.updated_at) > new Date(cachedIssue.updated_at)) {
cachedIssuesMap.set(full.id, full);
const updatedCachedIssues = Array.from(cachedIssuesMap.values());
localStorage.setItem("gitHubIssuesFull", JSON.stringify(updatedCachedIssues));
}

previewToFullMapping.set(preview.id, full);
const issueElement = document.querySelector(`[data-preview-id="${preview.id}"]`);
issueElement?.setAttribute("data-full-id", full.id.toString());

localStorage.setItem("gitHubIssuesFull", JSON.stringify(Array.from(previewToFullMapping.entries())));
return { full, issueElement };
});
// ... rest of the function
return octokit
.request("GET /repos/{org}/{repo}/issues/{issue_number}", { issue_number, repo, org })
.then(({ data: response }) => {
const full = response as GitHubIssue;

// Update the cache with the fetched issue if it's more recent than the cached issue
const cachedIssues = (getLocalStore("gitHubIssuesFull") || []) as GitHubIssue[];
const cachedIssuesMap = new Map(cachedIssues.map((issue) => [issue.id, issue]));
const cachedIssue = cachedIssuesMap.get(full.id);
if (!cachedIssue || new Date(full.updated_at) > new Date(cachedIssue.updated_at)) {
cachedIssuesMap.set(full.id, full);
const updatedCachedIssues = Array.from(cachedIssuesMap.values());
localStorage.setItem("gitHubIssuesFull", JSON.stringify(updatedCachedIssues));
}

previewToFullMapping.set(preview.id, full);
const issueElement = document.querySelector(`[data-preview-id="${preview.id}"]`);
issueElement?.setAttribute("data-full-id", full.id.toString());

localStorage.setItem("gitHubIssuesFull", JSON.stringify(Array.from(previewToFullMapping.entries())));
return { full, issueElement };
})
.then(({ full }) => {
const urlMatch = full.html_url.match(urlPattern);
const orgName = urlMatch?.groups?.org;

if (orgName) {
const orgCacheEntry = organizationImageCache.find((entry) => entry[orgName] !== undefined);
if (orgCacheEntry) {
return; // no redundant requests
} else {
organizationImageCache.push({ [orgName]: null });
}
return octokit.rest.orgs.get({ org: orgName }).then(({ data }) => {
const avatarUrl = data.avatar_url;
// Fetch the image as a Blob and save it to IndexedDB
return fetch(avatarUrl)
.then((response) => response.blob())
.then(async (blob) => {
await saveImageToDB({
dbName: "ImageDatabase",
storeName: "ImageStore",
keyName: "name",
orgName: `avatarUrl-${orgName}`,
avatarBlob: blob,
});
organizationImageCache.push({ [orgName]: blob });
return full;
});
});
}
return full;
});
});

return issueFetchPromises;
Expand Down

0 comments on commit 05d82ef

Please sign in to comment.