Skip to content

Commit

Permalink
feat: get meta only when login
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Sep 30, 2024
1 parent cd19c93 commit a104745
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
6 changes: 2 additions & 4 deletions frontend/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { GitHubMeta, JWTPayload } from "./types";
import { apiEndpoint } from "./const";

export let githubMeta: GitHubMeta;

export const fetchGitHubMeta = async () => {
export const fetchGitHubMeta = async (): Promise<GitHubMeta> => {
const res = await fetch(`${apiEndpoint}meta/github-app`, {
method: "GET",
});
Expand All @@ -12,7 +10,7 @@ export const fetchGitHubMeta = async () => {
throw res;
}

if (!githubMeta) githubMeta = (await res.json()).data;
return (await res.json()).data;
};

export const handleOAuthToken = () => {
Expand Down
7 changes: 2 additions & 5 deletions frontend/lib/dom/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import iconEdit from "iconify/edit";
import iconDelete from "iconify/delete";
import iconInfoOutlineRounded from "iconify/info-outline-rounded";
import { Comment } from "../types";
import { getJWT, decodeJWT, logout, githubMeta } from "../auth";
import { getJWT, decodeJWT, logout, fetchGitHubMeta } from "../auth";
import { apiEndpoint } from "../const";
import { groupBy, dateTimeFormatter } from "../util";

Expand Down Expand Up @@ -691,10 +691,7 @@ export const renderComments = async (comments: Comment[]) => {
break;
}
case "login": {
if (!githubMeta) {
console.log("githubMeta not ready");
return;
}
const githubMeta = await fetchGitHubMeta();
window.location.href = `https://github.com/login/oauth/authorize?client_id=${githubMeta.client_id}&state=${encodeURIComponent(JSON.stringify({ redirect: window.location.href }))}`;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions frontend/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./style.css";
import iconComment from "iconify/comment-outline-rounded";
import iconClose from "iconify/close";
import { setApiEndpoint } from "./const";
import { fetchGitHubMeta, handleOAuthToken } from "./auth";
import { handleOAuthToken } from "./auth";
import {
closeContextMenu,
createContextMenu,
Expand Down Expand Up @@ -71,7 +71,6 @@ export function setupReview(
resetCommentsCache();

updateAvailableComments();
fetchGitHubMeta();

if (globalInitialized) {
closeCommentsPanel();
Expand Down

0 comments on commit a104745

Please sign in to comment.