Skip to content

Commit 80ce07f

Browse files
committed
Properly handle github urls and alias urls on fork
1 parent 3bea628 commit 80ce07f

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

packages/app/scripts/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function addMiddleware(devServer, index) {
208208
devServer.use(
209209
'/api',
210210
proxy({
211-
target: 'https://codesandbox.io',
211+
target: 'https://codesandbox.stream',
212212
changeOrigin: true,
213213
})
214214
);

packages/app/src/app/pages/Sandbox/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,21 @@ class SandboxPage extends React.Component {
4545
}
4646

4747
fetchSandbox = () => {
48-
const id = this.props.match.params.id;
48+
let id = this.props.match.params.id;
49+
50+
// /**
51+
// * We do this as an optimization. Most urls end with the shortid (5 chars), if we can extract that
52+
// * we try to do so. This check is also handled on the server, but this way the sequence of `sandboxChanged`
53+
// * won't try to fetch the sandbox twice if it doesn't find the "id" in the state.
54+
// */
55+
// const split = id.split('-');
56+
// if (
57+
// split.length > 1 &&
58+
// !id.startsWith('github') &&
59+
// split[split.length - 1].length === 5
60+
// ) {
61+
// id = split.pop();
62+
// }
4963

5064
this.props.signals.editor.sandboxChanged({ id });
5165
};

packages/app/src/app/store/actions.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import track, {
55
identify,
66
setUserId,
77
} from '@codesandbox/common/lib/utils/analytics';
8+
import {
9+
sandboxUrl,
10+
editorUrl,
11+
} from '@codesandbox/common/lib/utils/url-generator';
812

913
import { parseConfigurations } from './utils/parse-configurations';
1014
import { mainModule, defaultOpenedModule } from './utils/main-module';
@@ -27,6 +31,28 @@ export function getSandbox({ props, api, path }) {
2731
});
2832
}
2933

34+
/**
35+
* Sometimes the alias is set as "id" in the url, if we already have that
36+
* sandbox in the state we need to make sure that the id is set correctly.
37+
* This function is reps
38+
*/
39+
export function setIdFromAlias({ props, state }) {
40+
if (state.get(`editor.sandboxes.${props.id}`)) {
41+
return {};
42+
}
43+
44+
const sandboxes = state.get(`editor.sandboxes`).toJSON();
45+
const matchingSandbox = Object.keys(sandboxes).find(
46+
id => sandboxUrl(sandboxes[id]) === `${editorUrl()}${props.id}`
47+
);
48+
49+
if (matchingSandbox) {
50+
return { id: matchingSandbox };
51+
}
52+
53+
return {};
54+
}
55+
3056
export function callVSCodeCallback({ props }) {
3157
const { cbID } = props;
3258
if (cbID) {

packages/app/src/app/store/providers/Api.js

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { logError } from '@codesandbox/common/lib/utils/analytics';
1+
import track, { logError } from '@codesandbox/common/lib/utils/analytics';
2+
import { notificationState } from '@codesandbox/common/lib/utils/notifications';
3+
import { NotificationStatus } from '@codesandbox/notifications';
4+
import { patronUrl } from '@codesandbox/common/lib/utils/url-generator';
25
import { values } from 'lodash-es';
36

47
import { camelizeKeys, decamelizeKeys } from 'humps';
@@ -44,13 +47,53 @@ const getMessage = (error: Error & { response: ?Object }) => {
4447
return error.message;
4548
};
4649

50+
function showNotification(controller, errorMessage: string) {
51+
if (errorMessage.startsWith('You need to sign in to create more than')) {
52+
// Error for "You need to sign in to create more than 10 sandboxes"
53+
track('Anonymous Sandbox Limit Reached', { errorMessage });
54+
55+
notificationState.addNotification({
56+
title: errorMessage,
57+
status: NotificationStatus.ERROR,
58+
actions: {
59+
primary: [
60+
{
61+
label: 'Sign in',
62+
run: () => {
63+
controller.getSignal('signInClicked')({});
64+
},
65+
},
66+
],
67+
},
68+
});
69+
} else if (errorMessage.startsWith('You reached the maximum of')) {
70+
track('Non-Patron Sandbox Limit Reached', { errorMessage });
71+
72+
notificationState.addNotification({
73+
title: errorMessage,
74+
status: NotificationStatus.ERROR,
75+
actions: {
76+
primary: [
77+
{
78+
label: 'Open Patron Page',
79+
run: () => {
80+
window.open(patronUrl(), '_blank');
81+
},
82+
},
83+
],
84+
},
85+
});
86+
} else {
87+
controller.runSignal(
88+
'notificationAdded',
89+
addNotification(errorMessage, 'error')
90+
);
91+
}
92+
}
93+
4794
const showError = (error, controller) => {
4895
const errorMessage = getMessage(error);
49-
50-
controller.runSignal(
51-
'notificationAdded',
52-
addNotification(errorMessage, 'error')
53-
);
96+
showNotification(controller, errorMessage);
5497

5598
error.apiMessage = errorMessage; // eslint-disable-line no-param-reassign
5699
};

packages/app/src/app/store/sequences.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ export const signInCli = [
476476
export const loadSandbox = factories.withLoadApp([
477477
set(state`editor.error`, null),
478478

479+
actions.setIdFromAlias,
480+
479481
when(
480482
state`editor.sandboxes.${props`id`}`,
481483
sandbox => sandbox && !sandbox.team

0 commit comments

Comments
 (0)