-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwithGithubClient.tsx
49 lines (39 loc) · 1.45 KB
/
withGithubClient.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Detail, environment, MenuBarExtra } from "@raycast/api";
import { GraphQLClient } from "graphql-request";
import { Octokit } from "octokit";
import { useMemo, useState } from "react";
import { authorize } from "../api/oauth";
import { getSdk } from "../generated/graphql";
let github: ReturnType<typeof getSdk> | null = null;
let octokit: Octokit | null = null;
export function withGithubClient(component: JSX.Element) {
const [x, forceRerender] = useState(0);
// we use a `useMemo` instead of `useEffect` to avoid a render
useMemo(() => {
(async function () {
const token = await authorize();
const authorization = `bearer ${token}`;
github = getSdk(new GraphQLClient("https://api.github.com/graphql", { headers: { authorization } }));
octokit = new Octokit({ auth: token });
forceRerender(x + 1);
})();
}, []);
if (!github || !octokit) {
if (environment.commandMode === "view") {
// Using the <List /> component makes the placeholder buggy
return <Detail isLoading />;
} else if (environment.commandMode === "menu-bar") {
return <MenuBarExtra isLoading />;
} else {
console.error("`withGithubClient` is only supported in `view` and `menu-bar` mode");
return null;
}
}
return component;
}
export function getGitHubClient() {
if (!github || !octokit) {
throw new Error("getGitHubClient must be used when authenticated");
}
return { github, octokit };
}