Skip to content

Commit

Permalink
chore: feedback and await early
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Nov 21, 2024
1 parent 359e095 commit d8b6470
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 55 deletions.
40 changes: 12 additions & 28 deletions static/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { AuthService } from "./scripts/authentication";
import { ManifestFetcher } from "./scripts/fetch-manifest";
import { ManifestRenderer } from "./scripts/render-manifest";
import { renderOrgPicker } from "./scripts/rendering/org-select";
import { ManifestPreDecode } from "./types/plugins";
import { manifestGuiBody } from "./utils/element-helpers";
import { toastNotification } from "./utils/toaster";

async function handleAuth() {
Expand All @@ -15,38 +13,30 @@ async function handleAuth() {
export async function mainModule() {
const auth = await handleAuth();
const renderer = new ManifestRenderer(auth);
renderer.manifestGuiBody.dataset.loading = "false";

try {
const ubiquityOrgsToFetchOfficialConfigFrom = ["ubiquity-os"];
const fetcher = new ManifestFetcher(ubiquityOrgsToFetchOfficialConfigFrom, auth.octokit);
const cache = fetcher.checkManifestCache();
if (!manifestGuiBody) {
throw new Error("Manifest GUI body not found");
}
manifestGuiBody.dataset.loading = "false";

if (auth.isActiveSession()) {
const userOrgs = await auth.getGitHubUserOrgs();
let fetchPromise: Promise<Record<string, ManifestPreDecode>> = Promise.resolve(cache);

if (Object.keys(cache).length === 0) {
renderer.manifestGuiBody.dataset.loading = "true";
const killNotification = toastNotification("Fetching manifest data...", { type: "info", shouldAutoDismiss: true });
manifestGuiBody.dataset.loading = "true";
renderOrgPicker(renderer, []);

const manifestCache = await fetcher.fetchMarketplaceManifests();
localStorage.setItem("manifestCache", JSON.stringify(manifestCache));

// eslint-disable-next-line no-async-promise-executor
fetchPromise = new Promise(async (resolve) => {
if (!manifestGuiBody) {
throw new Error("Manifest GUI body not found");
}
const manifestCache = await fetcher.fetchMarketplaceManifests();
localStorage.setItem("manifestCache", JSON.stringify(manifestCache));
await fetcher.fetchOfficialPluginConfig();
manifestGuiBody.dataset.loading = "false";
resolve(manifestCache);
killNotification();
});
await fetcher.fetchOfficialPluginConfig();
killNotification();
renderer.manifestGuiBody.dataset.loading = "false";
}

renderOrgPicker(renderer, userOrgs, fetchPromise);
renderOrgPicker(renderer, userOrgs);
} else {
renderOrgPicker(renderer, []);
}
Expand All @@ -59,10 +49,4 @@ export async function mainModule() {
}
}

mainModule()
.then(() => {
console.log("mainModule loaded");
})
.catch((error) => {
console.error(error);
});
mainModule().catch(console.error);
4 changes: 1 addition & 3 deletions static/scripts/rendering/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ function handleBackButtonClick(renderer: ManifestRenderer): void {

const step = renderer.currentStep;

if (step === "pluginSelector") {
if (step === "pluginSelector" || step === "orgPicker") {
renderOrgPicker(renderer, renderer.orgs);
} else if (step === "configEditor") {
renderPluginSelector(renderer);
} else if (step === "orgPicker") {
renderOrgPicker(renderer, renderer.orgs);
}
}
30 changes: 6 additions & 24 deletions static/scripts/rendering/org-select.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ManifestPreDecode } from "../../types/plugins";
import { createElement } from "../../utils/element-helpers";
import { STRINGS } from "../../utils/strings";
import { toastNotification } from "../../utils/toaster";
Expand All @@ -7,7 +6,7 @@ import { controlButtons } from "./control-buttons";
import { renderPluginSelector } from "./plugin-select";
import { closeAllSelect, updateGuiTitle } from "./utils";

export function renderOrgPicker(renderer: ManifestRenderer, orgs: string[], fetchPromise?: Promise<Record<string, ManifestPreDecode>>) {
export function renderOrgPicker(renderer: ManifestRenderer, orgs: string[]) {
renderer.currentStep = "orgPicker";
controlButtons({ hide: true });
renderer.backButton.style.display = "none";
Expand Down Expand Up @@ -59,7 +58,7 @@ export function renderOrgPicker(renderer: ManifestRenderer, orgs: string[], fetc
optionDiv.appendChild(textSpan);

optionDiv.addEventListener("click", () => {
handleOrgSelection(renderer, org, fetchPromise);
handleOrgSelection(renderer, org);
selectSelected.textContent = org;
localStorage.setItem("selectedOrg", org);
});
Expand All @@ -69,45 +68,28 @@ export function renderOrgPicker(renderer: ManifestRenderer, orgs: string[], fetc

selectSelected.addEventListener("click", (e) => {
e.stopPropagation();
closeAllSelect();
selectItems.classList.toggle(STRINGS.SELECT_HIDE);
selectSelected.classList.toggle(STRINGS.SELECT_ARROW_ACTIVE);
});

document.addEventListener("click", closeAllSelect);
}

function handleOrgSelection(renderer: ManifestRenderer, org: string, fetchPromise?: Promise<Record<string, ManifestPreDecode>>): void {
function handleOrgSelection(renderer: ManifestRenderer, org: string): void {
if (!org) {
throw new Error("No org selected");
}

localStorage.setItem("selectedOrg", org);

if (fetchPromise) {
fetchPromise
.then((manifestCache) => {
localStorage.setItem("manifestCache", JSON.stringify(manifestCache));
})
.catch((error) => {
console.error("Error fetching manifest cache:", error);
toastNotification(`An error occurred while fetching the manifest cache: ${String(error)}`, {
type: "error",
shouldAutoDismiss: true,
});
});

fetchOrgConfig(renderer, org).catch(console.error);
} else {
renderPluginSelector(renderer);
}
fetchOrgConfig(renderer, org).catch(console.error);
}

async function fetchOrgConfig(renderer: ManifestRenderer, org: string): Promise<void> {
const kill = toastNotification("Fetching organization config...", { type: "info", shouldAutoDismiss: true });
const octokit = renderer.auth.octokit;
if (!octokit) {
throw new Error("No org or octokit found");
}
await renderer.configParser.fetchUserInstalledConfig(org, octokit);
renderPluginSelector(renderer);
kill();
}

0 comments on commit d8b6470

Please sign in to comment.