-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathNetlifyLoginDialog.tsx
More file actions
233 lines (216 loc) · 8.51 KB
/
NetlifyLoginDialog.tsx
File metadata and controls
233 lines (216 loc) · 8.51 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {ChangeEvent, FormEvent} from 'react';
import type {CustomLoginDialogProps} from 'reviewstack/src/LoginDialog';
import Footer from './Footer';
import InlineCode from './InlineCode';
import {Box, Button, Heading, Text, TextInput} from '@primer/react';
import React, {useCallback, useEffect, useState} from 'react';
import AppHeader from 'reviewstack/src/AppHeader';
import Link from 'reviewstack/src/Link';
export default function NetlifyLoginDialog(props: CustomLoginDialogProps): React.ReactElement {
return (
<Box display="flex" flexDirection="column" height="100vh">
<Box flex="0 0 auto">
<AppHeader orgAndRepo={null} />
</Box>
<Box flex="1 1 auto" overflowY="auto">
<Box
display="flex"
flexDirection={["column", "row"]}
justifyContent="space-between"
paddingX={3}
paddingY={2}>
<Box minWidth={300} maxWidth={800}>
<EndUserInstructions {...props} />
</Box>
<Box>
<img src="/reviewstack-demo.gif" width={800} />
<Box textAlign="center" width={800}>
<Text fontStyle="italic" fontSize={1}>
ReviewStack makes it possible to view code and the timeline side-by-side
<br />
in addition to facilitating navigation up and down the stack.
</Text>
</Box>
</Box>
</Box>
</Box>
<Footer />
</Box>
);
}
function EndUserInstructions(props: CustomLoginDialogProps): React.ReactElement {
const {setTokenAndHostname} = props;
const [isButtonDisabled, setButtonDisabled] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
// Check for OAuth token in URL hash on mount (from OAuth callback)
useEffect(() => {
const hash = window.location.hash;
const tokenMatch = hash.match(/token=([^&]+)/);
if (tokenMatch) {
const token = tokenMatch[1];
// Clear the hash from URL
window.history.replaceState(null, '', window.location.pathname + window.location.search);
setTokenAndHostname(token, 'github.com');
}
// Check for error in URL params (from failed OAuth)
const params = new URLSearchParams(window.location.search);
const error = params.get('error');
if (error) {
setErrorMessage(error);
// Clear error from URL
window.history.replaceState(null, '', window.location.pathname);
}
}, [setTokenAndHostname]);
const onClick = useCallback(() => {
setButtonDisabled(true);
// Redirect to OAuth login endpoint
window.location.href = '/_oauth/login';
}, []);
return (
<Box>
<Heading>Welcome to ReviewStack</Heading>
<Box>
<Text as="p" pb={2}>
<Link href="https://sapling-scm.com/docs/addons/reviewstack">ReviewStack</Link> is a novel
user interface for GitHub pull requests with custom support for{' '}
<Text fontStyle="italic">stacked changes</Text>. For tools like{' '}
<Link href="http://sapling-scm.com/">Sapling</Link> or{' '}
<Link href="https://github.com/ezyang/ghstack">ghstack</Link> that create separate pull
requests for independent commits in a stack, ReviewStack facilitates navigating the stack
and ensuring that only the code that was meant to be considered for review is displayed
for each pull request.
</Text>
<Text as="p">
ReviewStack (reviewstack.dev) is owned and operated by Meta Platforms, Inc. Note that{' '}
<Link href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/authorizing-oauth-apps">
you must authorize ReviewStack to access your GitHub data
</Link>{' '}
in order to use ReviewStack. Once authorized, ReviewStack will store your GitHub access
token, and other data fetched from GitHub, locally in the browser. Clicking{' '}
<Text fontWeight="bold">Logout</Text> will remove the data that is stored locally, but it
will not delete your data from GitHub.
</Text>
</Box>
{errorMessage != null ? (
<Box pb={2}>
<Text color="danger.fg">{errorMessage}</Text>
</Box>
) : null}
<H3>github.com Users</H3>
<Box pb={4}>
To view code hosted on github.com using ReviewStack, you can use the OAuth flow below to
authenticate with GitHub:
</Box>
<Box pb={4}>
<Button onClick={onClick} disabled={isButtonDisabled}>
Use OAuth to Authorize ReviewStack to access GitHub
</Button>
</Box>
<H3>GitHub Enterprise Users</H3>
<Box pb={4}>
To use ReviewStack to view code on your <Text fontWeight="bold">GitHub Enterprise</Text>{' '}
account, you must specify the hostname along with a{' '}
<Link href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token">
Personal Access Token (PAT)
</Link>{' '}
for the account. If you have authenticated with the{' '}
<Link href="https://cli.github.com/">GitHub CLI</Link>, you can use{' '}
<InlineCode>gh auth status --show-token --hostname HOSTNAME</InlineCode> to use your
existing token, though note the PAT for <InlineCode>gh</InlineCode> may have access to a
broader set of{' '}
<Link href="https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps">
scopes
</Link>{' '}
than you may be willing to grant to a third-party tool.
</Box>
<EnterpriseForm {...props} />
<H3>Building From Source</H3>
<Box>
Finally, if you want to run your own instance of ReviewStack,{' '}
<Link href="https://github.com/facebook/sapling/tree/main/eden/contrib/reviewstack">
the source code is available on GitHub
</Link>
. For example, you may want to deploy your own instance of ReviewStack that is hardcoded to
work with your GitHub Enterprise instance combined with an in-house SSO login flow. The
codebase is designed so that the ReviewStack UI can be used as a reusable React component
with minimal dependencies on the host environment.
</Box>
</Box>
);
}
function EnterpriseForm({setTokenAndHostname}: CustomLoginDialogProps): React.ReactElement {
const [hostname, setHostname] = useState('github.com');
const [token, setToken] = useState('');
const onChangeHostname = useCallback(
(e: ChangeEvent) => setHostname((e.target as HTMLInputElement).value),
[],
);
const onChangeToken = useCallback(
(e: ChangeEvent) => setToken((e.target as HTMLInputElement).value),
[],
);
const onSubmit = useCallback(
(e: FormEvent) => {
e.preventDefault();
setTokenAndHostname(token.trim(), hostname.trim());
return false;
},
[token, hostname, setTokenAndHostname],
);
// FIXME: mention `gh auth status`
const isInputValid = isGitHubEnterpriseInputValid(token, hostname);
return (
<Box>
<form onSubmit={onSubmit}>
<Box pb={2}>
GitHub Enterprise Hostname: <br />
<TextInput
value={hostname}
onChange={onChangeHostname}
sx={{width: '400px'}}
monospace
aria-label="hostname"
placeholder="github.com"
/>
</Box>
<Box>
Personal Access Token: <br />
<TextInput
value={token}
onChange={onChangeToken}
type="password"
sx={{width: '400px'}}
monospace
aria-label="personal access token"
placeholder="github_pat_abcdefg123456789"
/>
</Box>
<Box paddingY={4}>
<Button disabled={!isInputValid} type="submit">
Use your GitHub Enterprise account with ReviewStack
</Button>
</Box>
</form>
</Box>
);
}
function isGitHubEnterpriseInputValid(token: string, hostname: string): boolean {
if (token.trim() === '') {
return false;
}
const normalizedHostname = hostname.trim();
return normalizedHostname !== '' && normalizedHostname.indexOf('.') !== -1;
}
function H3({children}: {children: React.ReactNode}): React.ReactElement {
return (
<Heading as="h3" sx={{fontSize: 3, mb: 2}}>
{children}
</Heading>
);
}