forked from open-sauced/open-sauced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
148 lines (134 loc) · 5.59 KB
/
index.jsx
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import React, {useState, useEffect} from "react";
import ReactDOM from "react-dom";
import App from "./containers/App";
import Config from "./config";
import {getUserFromJwt} from "./lib/identityActions";
import "./index.css";
import "react-loading-skeleton/dist/skeleton.css";
import reportWebVitals from './reportWebVitals';
import OneGraphApolloClient from "onegraph-apollo-client";
import {ApolloProvider, InMemoryCache} from "@apollo/client";
import api from "./lib/apiGraphQL";
import {getAppVersion} from "./lib/appVersion";
import {validateToken} from "./lib/validateToken";
import { registerSW } from 'virtual:pwa-register';
import { initiatePostHog, capturePostHogAnalytics } from "./lib/analytics";
const apolloClient = new OneGraphApolloClient({
oneGraphAuth: Config.auth,
cache: new InMemoryCache()
});
function Index() {
const [user, setUser] = useState(null);
const [loggedInStatus, setLogin] = useState(JSON.parse(localStorage.getItem("isLoggedIn")) && validateToken(Config.auth));
const [isAdmin, setIsAdmin] = useState(null);
useEffect(() => {
!!!import.meta.env.DEV && initiatePostHog();
registerSW({
immediate: true,
onNeedRefresh: () => {
console.log('SW needs refresh');
},
onOfflineReady: () => {
console.log('SW is ready to handle offline requests.');
},
onRegistered: () => {
console.log('SW registered');
},
onRegisterError: (e) => {
console.log('SW registration failed', e);
}
});
console.log(`%c
██████╗ ██████╗ ███████╗███╗ ██╗ ███████╗ █████╗ ██╗ ██╗ ██████╗███████╗██████╗
██╔═══██╗██╔══██╗██╔════╝████╗ ██║ ██╔════╝██╔══██╗██║ ██║██╔════╝██╔════╝██╔══██╗
██║ ██║██████╔╝█████╗ ██╔██╗ ██║ ███████╗███████║██║ ██║██║ █████╗ ██║ ██║
██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║ ╚════██║██╔══██║██║ ██║██║ ██╔══╝ ██║ ██║
╚██████╔╝██║ ███████╗██║ ╚████║ ███████║██║ ██║╚██████╔╝╚██████╗███████╗██████╔╝
╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚══════╝╚═════╝%c v${getAppVersion()}`,
"color:#f6d82b",
"color:green;font-weight:bold");
const auth = Config.auth;
auth.isLoggedIn("github").then(isLoggedIn => {
if (isLoggedIn) {
const user = getUserFromJwt(auth);
setUser(user);
api
.fetchMemberStatus()
.then(res => {
const viewerIsAMember = res.data.gitHub.viewer.organization === null ? false : true;
setIsAdmin(viewerIsAMember);
})
.catch(e => {
console.log(e);
});
setLogin(isLoggedIn);
localStorage.setItem("isLoggedIn", isLoggedIn);
return user;
} else {
console.warn("User is not logged into GitHub");
}
});
}, []);
const _handleLogIn = () => {
const auth = Config.auth;
capturePostHogAnalytics('User Login', 'userLogin', 'true');
auth
.login("github")
.then(() => {
auth.isLoggedIn("github").then(isLoggedIn => {
if (isLoggedIn) {
// Pull the user-data we care about from the JWT and
// store it in component local state for the rest of the
// app
const user = getUserFromJwt(auth);
setUser(user);
localStorage.setItem("isLoggedIn", isLoggedIn);
setLogin(isLoggedIn);
} else {
console.warn("User did not grant auth for GitHub");
}
});
})
.catch(e => console.error("Problem logging in", e));
};
const _handleLogOut = () => {
// Set the local react states so that rogue requests aren't made
// after log out but before we re-render.
setUser(null);
setIsAdmin(false);
setLogin(false);
const auth = Config.auth;
auth.logout("github").then(() => {
// Remove the local onegraph-auth storage
localStorage.removeItem("oneGraph:" + Config.appId);
// Remove the local AdminStats bar status storage
localStorage.removeItem("adminBar");
// Remove the local logged in status storage
localStorage.removeItem("isLoggedIn");
});
};
return (
<div>
<ApolloProvider client={apolloClient}>
<App
user={user}
isLoggedIn={loggedInStatus}
isAdmin={isAdmin}
userId={user && user.id}
handleLogIn={() => _handleLogIn()}
handleLogOut={() => _handleLogOut()}
/>
</ApolloProvider>
</div>
);
}
ReactDOM.render(
<React.StrictMode>
<Index />
</React.StrictMode>,
document.getElementById( "root")
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();