Skip to content

Commit 18ff96d

Browse files
SaraVieiraMichaelDeBoey
authored andcommitted
Alias Support (codesandbox#1893)
* Update URLs for aliasing * refactor for alias * add patron check * refactor export * remove duplicate * Final changes for alias * Update packages/common/src/components/Preview/index.tsx Co-Authored-By: Michaël De Boey <[email protected]> * Update packages/app/src/app/pages/Dashboard/Content/SandboxCard/index.tsx Co-Authored-By: Michaël De Boey <[email protected]> * Update packages/app/src/app/pages/Dashboard/Content/SandboxCard/index.tsx Co-Authored-By: Michaël De Boey <[email protected]> * Update packages/app/src/app/pages/Dashboard/Content/SandboxCard/index.tsx Co-Authored-By: Michaël De Boey <[email protected]>
1 parent 05919ba commit 18ff96d

File tree

25 files changed

+167
-45
lines changed

25 files changed

+167
-45
lines changed

packages/app/src/app/pages/Dashboard/Content/SandboxCard/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Props = {
5858
undeleteSandboxes: () => void;
5959
removedAt?: number;
6060
style?: React.CSSProperties;
61+
alias: string | undefined;
6162

6263
// React-DnD, lazy typings
6364
connectDragSource: any;
@@ -271,21 +272,21 @@ class SandboxItem extends React.PureComponent<Props, State> {
271272
this.props.isPatron &&
272273
[
273274
this.props.privacy !== 0 && {
274-
title: `Make Sandbox Public`,
275+
title: 'Make Sandbox Public',
275276
action: () => {
276277
this.props.setSandboxesPrivacy(0);
277278
return true;
278279
},
279280
},
280281
this.props.privacy !== 1 && {
281-
title: `Make Sandbox Unlisted`,
282+
title: 'Make Sandbox Unlisted',
282283
action: () => {
283284
this.props.setSandboxesPrivacy(1);
284285
return true;
285286
},
286287
},
287288
this.props.privacy !== 2 && {
288-
title: `Make Sandbox Private`,
289+
title: 'Make Sandbox Private',
289290
action: () => {
290291
this.props.setSandboxesPrivacy(2);
291292
return true;
@@ -321,7 +322,7 @@ class SandboxItem extends React.PureComponent<Props, State> {
321322

322323
openSandbox = (openNewWindow = false) => {
323324
// @ts-ignore Git sandboxes aren't shown here anyway
324-
const url = sandboxUrl({ id: this.props.id });
325+
const url = sandboxUrl({ id: this.props.id, alias: this.props.alias });
325326

326327
if (!this.props.removedAt) {
327328
if (openNewWindow === true) {

packages/app/src/app/pages/Dashboard/Content/SandboxGrid/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Column from 'react-virtualized/dist/commonjs/Table/Column';
1313
import Table from 'react-virtualized/dist/commonjs/Table';
1414
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
1515
import downloadZip from 'app/store/providers/Utils/create-zip';
16+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
1617
import 'react-virtualized/styles.css';
1718

1819
import SandboxItem from '../SandboxCard';
@@ -281,7 +282,8 @@ class SandboxGrid extends React.Component<*, State> {
281282
<SandboxItem
282283
isScrolling={this.isScrolling}
283284
id={item.id}
284-
title={item.title || item.id}
285+
title={getSandboxName(item)}
286+
alias={item.alias}
285287
details={editedSince}
286288
style={style}
287289
key={key}

packages/app/src/app/pages/Dashboard/queries.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const SIDEBAR_COLLECTION_FRAGMENT = gql`
1111
const SANDBOX_FRAGMENT = gql`
1212
fragment Sandbox on Sandbox {
1313
id
14+
alias
1415
title
1516
description
1617
insertedAt

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { inject, observer } from 'mobx-react';
33
import { Link } from 'react-router-dom';
44

5+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
56
import Navigation from 'app/pages/common/Navigation';
67
import Fullscreen from '@codesandbox/common/lib/components/flex/Fullscreen';
78

@@ -173,7 +174,7 @@ class LivePage extends React.Component {
173174
const sandbox = store.editor.currentSandbox;
174175

175176
if (sandbox) {
176-
document.title = `${sandbox.title || sandbox.id} - CodeSandbox`;
177+
document.title = `${getSandboxName(sandbox)} - CodeSandbox`;
177178
}
178179

179180
return (

packages/app/src/app/pages/Profile/Showcase/SandboxInfo/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
2+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
33
import { sandboxUrl } from '@codesandbox/common/lib/utils/url-generator';
44
import Row from '@codesandbox/common/lib/components/flex/Row';
55
import Stat from 'app/components/Stat';
@@ -19,7 +19,7 @@ function SandboxInfo({ sandbox }) {
1919
<Container>
2020
<Row alignItems="center">
2121
<Title>
22-
{sandbox.title || 'Undefined'} <Like sandbox={sandbox} />
22+
{getSandboxName(sandbox)} <Like sandbox={sandbox} />
2323
</Title>
2424
</Row>
2525
<Row alignItems="flex-start">

packages/app/src/app/pages/Sandbox/Editor/Header/CollectionInfo/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Spring } from 'react-spring/renderprops';
77

88
import track from '@codesandbox/common/lib/utils/analytics';
99
import { ESC } from '@codesandbox/common/lib/utils/keycodes';
10-
10+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
1111
import { Container, SandboxName, SandboxInput, FolderName } from './elements';
1212

1313
class CollectionInfo extends React.Component {
@@ -17,7 +17,7 @@ class CollectionInfo extends React.Component {
1717
collectionNameWidth: undefined,
1818
};
1919

20-
sandboxName = () => this.props.sandbox.title || 'Untitled';
20+
sandboxName = () => getSandboxName(this.props.sandbox) || 'Untitled';
2121

2222
updateSandboxInfo = () => {
2323
this.props.signals.workspace.sandboxInfoUpdated();

packages/app/src/app/pages/Sandbox/Editor/Workspace/Project/elements.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export const Description = styled.div`
5454
margin-top: 0.5rem;
5555
`;
5656

57+
export const Alias = styled.div`
58+
font-size: 0.875rem;
59+
color: ${props =>
60+
props.theme.light ? 'rgba(0, 0, 0, 0.8)' : 'rgba(255, 255, 255, 0.8)'};
61+
margin-top: 0.5rem;
62+
`;
63+
5764
export const PropertyName = styled.span`
5865
display: inline-block;
5966
color: ${props =>

packages/app/src/app/pages/Sandbox/Editor/Workspace/Project/index.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import TeamIcon from 'react-icons/lib/md/people';
1111

1212
import { UserWithAvatar } from '@codesandbox/common/lib/components/UserWithAvatar';
1313
import Stats from 'app/pages/common/Stats';
14+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
1415
import PrivacyStatus from 'app/components/PrivacyStatus';
1516
import GithubBadge from '@codesandbox/common/lib/components/GithubBadge';
1617
import EditableTags from 'app/components/EditableTags';
@@ -27,6 +28,7 @@ import {
2728
StatsContainer,
2829
PrivacyContainer,
2930
Title,
31+
Alias,
3032
Description,
3133
EditPen,
3234
PropertyValue,
@@ -39,12 +41,17 @@ class Project extends React.Component {
3941
state = {
4042
editingTitle: false,
4143
editingDescription: false,
44+
editingAlias: false,
4245
};
4346

4447
setTitleEditing = () => {
4548
this.setState({ editingTitle: true });
4649
};
4750

51+
setAliasEditing = () => {
52+
this.setState({ editingAlias: true });
53+
};
54+
4855
setDescriptionEditing = () => {
4956
this.setState({ editingDescription: true });
5057
};
@@ -82,6 +89,7 @@ class Project extends React.Component {
8289
this.setState({
8390
editingTitle: false,
8491
editingDescription: false,
92+
editingAlias: false,
8593
});
8694
};
8795

@@ -101,7 +109,6 @@ class Project extends React.Component {
101109
const workspace = store.workspace;
102110

103111
const template = getTemplateDefinition(sandbox.template);
104-
105112
return (
106113
<div style={{ marginBottom: '1rem' }}>
107114
<Item style={{ marginTop: '.5rem' }}>
@@ -132,7 +139,7 @@ class Project extends React.Component {
132139
</WorkspaceInputContainer>
133140
) : (
134141
<Title>
135-
{workspace.project.title || sandbox.title || sandbox.id}
142+
{workspace.project.title || getSandboxName(sandbox)}
136143
{editable && <EditPen onClick={this.setTitleEditing} />}
137144
</Title>
138145
)}
@@ -177,6 +184,42 @@ class Project extends React.Component {
177184
{editable && <EditPen onClick={this.setDescriptionEditing} />}
178185
</Description>
179186
)}
187+
{/* Disable until we also moved SSE over */}
188+
{store.isPatron && false ? (
189+
<>
190+
{this.state.editingAlias ? (
191+
<WorkspaceInputContainer>
192+
<input
193+
value={workspace.project.alias}
194+
onChange={event => {
195+
signals.workspace.valueChanged({
196+
field: 'alias',
197+
value: event.target.value,
198+
});
199+
}}
200+
type="text"
201+
onBlur={this.updateSandboxInfo}
202+
onKeyUp={event => {
203+
if (event.keyCode === 13) {
204+
this.updateSandboxInfo();
205+
}
206+
}}
207+
ref={el => {
208+
if (el) {
209+
el.focus();
210+
}
211+
}}
212+
placeholder="Alias"
213+
/>
214+
</WorkspaceInputContainer>
215+
) : (
216+
<Alias>
217+
{workspace.project.alias || sandbox.alias}
218+
{editable && <EditPen onClick={this.setAliasEditing} />}
219+
</Alias>
220+
)}
221+
</>
222+
) : null}
180223
</Item>
181224

182225
{!sandbox.team && !!sandbox.author && (

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { inject, observer } from 'mobx-react';
33
import { Link } from 'react-router-dom';
44
import QuickActions from 'app/pages/Sandbox/QuickActions';
55

6+
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
67
import { Button } from '@codesandbox/common/lib/components/Button';
78
import NotFound from 'app/pages/common/NotFound';
89
import Navigation from 'app/pages/common/Navigation';
@@ -44,7 +45,16 @@ class SandboxPage extends React.Component {
4445
}
4546

4647
fetchSandbox = () => {
47-
const id = this.props.match.params.id;
48+
let id = this.props.match.params.id;
49+
50+
// If the id is in the form of "slugified-title-shortid" we can take the last
51+
// shortid and get the data with that. This solves the problem with urls becoming
52+
// invalid after giving a sandbox a new title.
53+
const split = id.split('-');
54+
if (split.length > 1) {
55+
id = split.pop();
56+
}
57+
4858
this.props.signals.editor.sandboxChanged({ id });
4959
};
5060

@@ -173,7 +183,7 @@ class SandboxPage extends React.Component {
173183
const sandbox = store.editor.currentSandbox;
174184

175185
if (sandbox) {
176-
document.title = `${sandbox.title || sandbox.id} - CodeSandbox`;
186+
document.title = `${getSandboxName(sandbox)} - CodeSandbox`;
177187
}
178188

179189
return (

packages/app/src/app/pages/Search/Results/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import Centered from '@codesandbox/common/lib/components/flex/Centered';
55
import SandboxCard from '@codesandbox/common/lib/components/SandboxCard';
66
import { sandboxUrl } from '@codesandbox/common/lib/utils/url-generator';
77
import Margin from '@codesandbox/common/lib/components/spacing/Margin';
8+
import getSandboxName from '@codesandbox/common/lib/utils/get-sandbox-name';
89

910
import ResultInfo from '../ResultInfo';
1011
import { Container } from './elements';
1112

1213
const Results = () => {
1314
const selectSandbox = hit =>
14-
window.open(sandboxUrl({ id: hit.objectID, git: hit.git }));
15+
window.open(
16+
sandboxUrl({ id: hit.objectID, alias: hit.alias, git: hit.git })
17+
);
1518

1619
return (
1720
<Container>
@@ -24,7 +27,11 @@ const Results = () => {
2427
noHeight
2528
sandbox={{
2629
...hit,
27-
title: hit.title || hit.objectID,
30+
title: getSandboxName({
31+
id: hit.objectID,
32+
alias: hit.alias,
33+
git: hit.git,
34+
}),
2835
id: hit.objectID,
2936
}}
3037
/>

0 commit comments

Comments
 (0)