Skip to content

Commit d8705ea

Browse files
authored
feat(vscode): support insiders login (codesandbox#6836)
* feat(vscode): support insiders login * feat(vscode): display insiders notifications * chore: add myself to contributors * chore: update "protocol-handlers" to 0.1.3 * fix(vscode): add trailing slash to the vscode url * fix(vscode): use a button and onClick handler
1 parent 560d0f6 commit d8705ea

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,15 @@
16721672
"contributions": [
16731673
"doc"
16741674
]
1675+
},
1676+
{
1677+
"login": "kettanaito",
1678+
"name": "Artem",
1679+
"avatar_url": "https://github.com/kettanaito.png",
1680+
"profile": "https://github.com/kettanaito",
1681+
"contributions": [
1682+
"code"
1683+
]
16751684
}
16761685
],
16771686
"contributorsPerLine": 7,

packages/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
"posthtml-parser": "^0.4.1",
183183
"posthtml-render": "^1.1.0",
184184
"prism-react-renderer": "^1.0.2",
185+
"protocol-handlers": "^0.1.3",
185186
"punycode": "^2.1.1",
186187
"qrcode.react": "^0.8.0",
187188
"qs": "^6.5.0",
@@ -193,8 +194,8 @@
193194
"react-color": "^2.17.3",
194195
"react-content-loader": "^4.2.2",
195196
"react-day-picker": "^7.2.4",
196-
"react-devtools-inline_legacy": "npm:[email protected]",
197197
"react-devtools-inline": "^4.22.0",
198+
"react-devtools-inline_legacy": "npm:[email protected]",
198199
"react-dnd": "^9.4.0",
199200
"react-dnd-html5-backend": "^9.4.0",
200201
"react-dom": "^16.9.0",

packages/app/src/app/pages/VSCodeAuth/Prompt/index.tsx

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import React, {
2+
FunctionComponent,
3+
useCallback,
4+
useEffect,
5+
useMemo,
6+
} from 'react';
17
import { Button } from '@codesandbox/components';
2-
import React, { FunctionComponent, useEffect, useState } from 'react';
8+
import { notificationState } from '@codesandbox/common/lib/utils/notifications';
9+
import { NotificationStatus } from '@codesandbox/notifications';
10+
import { useLocation } from 'react-router-dom';
11+
import { openUrl, UnsupportedProtocolError } from 'protocol-handlers';
312

413
import { SubTitle } from 'app/components/SubTitle';
514
import { Title } from 'app/components/Title';
@@ -17,23 +26,74 @@ export const Prompt: FunctionComponent = () => {
1726
user,
1827
isLoggedIn,
1928
} = useAppState();
29+
const location = useLocation();
30+
const query = useMemo(() => new URLSearchParams(location.search), [
31+
location.search,
32+
]);
33+
const isInsiders = query.get('insiders') === 'true';
2034

21-
const [deepLink, setDeepLink] = useState('');
2235
const actions = useActions();
2336
useEffect(() => {
2437
if (isLoggedIn && !authToken && !isLoadingAuthToken) {
2538
actions.internal.authorize();
2639
}
2740
}, [isLoggedIn]);
2841

29-
useEffect(() => {
30-
const deeplinkUrl = `vscode://CodeSandbox-io.codesandbox-projects/auth-completion?token=${authToken}`;
42+
const vscodeUrl = useMemo(() => {
43+
const url = new URL(
44+
'auth-completion',
45+
'vscode://CodeSandbox-io.codesandbox-projects/'
46+
);
47+
url.searchParams.set('token', authToken);
48+
49+
if (isInsiders) {
50+
url.protocol = 'vscode-insiders://';
51+
}
52+
53+
return url;
54+
}, [authToken, isInsiders]);
3155

32-
if (authToken) {
33-
setDeepLink(deeplinkUrl);
34-
window.open(deeplinkUrl);
56+
const openInVsCode = useCallback(() => {
57+
if (!authToken) {
58+
return;
3559
}
36-
}, [authToken]);
60+
61+
openUrl(vscodeUrl).catch(openVsCodeError => {
62+
if (openVsCodeError instanceof UnsupportedProtocolError) {
63+
notificationState.addNotification({
64+
status: NotificationStatus.WARNING,
65+
message: 'Visual Studio Insiders is not installed',
66+
actions: {
67+
primary: {
68+
label: 'Install',
69+
run: () => {
70+
window.open(
71+
'https://code.visualstudio.com/insiders/',
72+
'_blank',
73+
'noopener,noreferrer'
74+
);
75+
},
76+
},
77+
},
78+
});
79+
return;
80+
}
81+
82+
notificationState.addNotification({
83+
status: NotificationStatus.ERROR,
84+
message: 'Failed to launch Visual Studio Code',
85+
actions: {
86+
primary: {
87+
label: 'Try again',
88+
run: () => openInVsCode(),
89+
},
90+
},
91+
});
92+
});
93+
}, [vscodeUrl, authToken]);
94+
95+
// Attempt to open VS Code when the page mounts.
96+
useEffect(() => openInVsCode(), [openInVsCode]);
3797

3898
if (error) {
3999
return (
@@ -101,12 +161,13 @@ export const Prompt: FunctionComponent = () => {
101161

102162
<Buttons>
103163
<Button
104-
as="a"
105164
autoWidth
106-
href={deepLink}
107165
style={{ fontSize: 16, height: 40, width: '100%', marginTop: '1rem' }}
166+
onClick={openInVsCode}
108167
>
109-
Open VSCode
168+
{isInsiders
169+
? 'Open Visual Studio Code Insiders'
170+
: 'Open Visual Studio Code'}
110171
</Button>
111172
</Buttons>
112173
</Container>

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13680,7 +13680,7 @@ esprima@^4.0.0, esprima@^4.0.1, esprima@~4.0.0:
1368013680
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
1368113681
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
1368213682

13683-
esquery@1.0.1, esquery@^1.0.0, esquery@^1.0.1:
13683+
esquery@^1.0.0, esquery@^1.0.1:
1368413684
version "1.0.1"
1368513685
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.0.1.tgz#406c51658b1f5991a5f9b62b1dc25b00e3e5c708"
1368613686
integrity sha512-SmiyZ5zIWH9VM+SRUReLS5Q8a7GxtRdxEBVZpm98rJM7Sb+A9DVCndXfkeFUd3byderg+EbDkfnevfCwynWaNA==
@@ -26852,6 +26852,11 @@ proto-list@~1.2.1:
2685226852
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
2685326853
integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=
2685426854

26855+
protocol-handlers@^0.1.3:
26856+
version "0.1.3"
26857+
resolved "https://registry.yarnpkg.com/protocol-handlers/-/protocol-handlers-0.1.3.tgz#38a22831c4f9f06cec7035fb5f7eb7319a9f4786"
26858+
integrity sha512-m9nCcmseJMLSdfpKA8K8Glrp6IUFIDsiLoQhE66ClGwEmGE0GY0lR5XviHhXMGW8e8FvPn1IzTbeF+aOClcl1Q==
26859+
2685526860
protocols@^1.1.0, protocols@^1.4.0:
2685626861
version "1.4.7"
2685726862
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.7.tgz#95f788a4f0e979b291ffefcf5636ad113d037d32"

0 commit comments

Comments
 (0)