Skip to content

Commit a6c090b

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
Extract MostUsedSandbox from CreateNewSandbox (codesandbox#3101)
1 parent 615dbcc commit a6c090b

File tree

4 files changed

+84
-94
lines changed

4 files changed

+84
-94
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Link } from 'react-router-dom';
2+
3+
import { Container } from '../elements';
4+
5+
export const ContainerLink = Container.withComponent(Link);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Template } from '@codesandbox/common/lib/types';
2+
import { sandboxUrl } from '@codesandbox/common/lib/utils/url-generator';
3+
import React, { FunctionComponent } from 'react';
4+
5+
import { Container } from '../elements';
6+
7+
import { ContainerLink } from './elements';
8+
9+
type Props = {
10+
collectionId?: string;
11+
createSandbox: (template: Pick<Template, 'shortid'>) => void;
12+
template: Template | null;
13+
};
14+
export const MostUsedSandbox: FunctionComponent<Props> = ({
15+
collectionId,
16+
createSandbox,
17+
template,
18+
}) => {
19+
if (!template) {
20+
return null;
21+
}
22+
23+
const buttonName = `Create ${template.niceName} Sandbox`;
24+
return collectionId ? (
25+
<Container
26+
color={template.color}
27+
onClick={() => createSandbox(template)}
28+
role="button"
29+
tabIndex={0}
30+
>
31+
{buttonName}
32+
</Container>
33+
) : (
34+
<ContainerLink
35+
color={template.color}
36+
to={sandboxUrl({ alias: null, id: template.shortid })}
37+
>
38+
{buttonName}
39+
</ContainerLink>
40+
);
41+
};

packages/app/src/app/components/CreateNewSandbox/elements.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
1-
import { Link } from 'react-router-dom';
2-
import { animated } from 'react-spring/renderprops';
3-
import styled, { css, keyframes } from 'styled-components';
4-
5-
const fadeIn = keyframes`
6-
0% { opacity: 0; }
7-
100% { opacity: 0.5; }
8-
`;
9-
10-
export const DarkBG = styled.div<{ closing: boolean }>`
11-
${({ closing }) => css`
12-
position: fixed;
13-
top: 0;
14-
left: 0;
15-
bottom: 0;
16-
right: 0;
17-
background-color: black;
18-
opacity: 0;
19-
transition: 0.3s ease opacity;
20-
21-
${!closing &&
22-
css`
23-
animation: ${fadeIn} 0.3s;
24-
animation-fill-mode: forwards;
25-
`};
26-
`}
27-
`;
1+
import styled, { css } from 'styled-components';
282

293
export const ButtonsContainer = styled.div`
304
display: flex;
@@ -73,16 +47,3 @@ export const Container = styled.div<{ hide?: boolean; color?: any }>`
7347
}
7448
`}
7549
`;
76-
77-
export const ContainerLink = Container.withComponent(Link);
78-
79-
export const AnimatedModalContainer = styled(animated.div)<{
80-
forking: boolean;
81-
}>`
82-
${({ forking }) =>
83-
forking &&
84-
css`
85-
position: fixed;
86-
transition: 0.15s ease all;
87-
`};
88-
`;

packages/app/src/app/components/CreateNewSandbox/index.tsx

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,69 @@
1-
import React from 'react';
2-
import history from 'app/utils/history';
1+
import Template from '@codesandbox/common/lib/templates/template';
32
import { ENTER } from '@codesandbox/common/lib/utils/keycodes';
43
import { sandboxUrl } from '@codesandbox/common/lib/utils/url-generator';
4+
import React, {
5+
ComponentProps,
6+
FunctionComponent,
7+
HTMLAttributes,
8+
KeyboardEvent,
9+
} from 'react';
10+
511
import { useOvermind } from 'app/overmind';
6-
import Template from '@codesandbox/common/lib/templates/template';
7-
import { ButtonsContainer, Container, ContainerLink } from './elements';
12+
import history from 'app/utils/history';
813

9-
interface ICreateNewSandboxProps {
10-
style: React.CSSProperties;
11-
collectionId?: string;
12-
mostUsedSandboxTemplate: Template;
13-
}
14+
import { ButtonsContainer, Container } from './elements';
15+
import { MostUsedSandbox } from './MostUsedSandbox';
1416

15-
export const CreateNewSandboxButton = ({
16-
style,
17+
type Props = {
18+
collectionId?: string;
19+
mostUsedSandboxTemplate: ComponentProps<typeof MostUsedSandbox>['template'];
20+
} & Pick<HTMLAttributes<HTMLDivElement>, 'style'>;
21+
export const CreateNewSandboxButton: FunctionComponent<Props> = ({
1722
collectionId,
1823
mostUsedSandboxTemplate,
19-
}: ICreateNewSandboxProps) => {
20-
const { actions } = useOvermind();
24+
style,
25+
}) => {
26+
const {
27+
actions: {
28+
dashboard: { createSandboxClicked },
29+
modalOpened,
30+
},
31+
} = useOvermind();
2132

22-
const createSandbox = (template: Template) => {
23-
if (!collectionId) {
33+
const createSandbox = ({ shortid }: Pick<Template, 'shortid'>) => {
34+
if (collectionId) {
35+
createSandboxClicked({ body: { collectionId }, sandboxId: shortid });
36+
} else {
2437
setTimeout(() => {
25-
history.push(sandboxUrl({ id: template.shortid, alias: null }));
38+
history.push(sandboxUrl({ alias: null, id: shortid }));
2639
}, 300);
27-
} else {
28-
actions.dashboard.createSandboxClicked({
29-
sandboxId: template.shortid,
30-
body: {
31-
collectionId,
32-
},
33-
});
3440
}
3541
};
3642

3743
const handleClick = () => {
38-
actions.modalOpened({ modal: 'newSandbox' });
44+
modalOpened({ modal: 'newSandbox' });
3945
};
4046

41-
let mostUsedSandboxComponent;
42-
43-
if (mostUsedSandboxTemplate) {
44-
const buttonName = `Create ${mostUsedSandboxTemplate.niceName} Sandbox`;
45-
if (collectionId) {
46-
mostUsedSandboxComponent = (
47-
<Container
48-
onClick={() => createSandbox(mostUsedSandboxTemplate)}
49-
color={mostUsedSandboxTemplate.color}
50-
tabIndex={0}
51-
role="button"
52-
>
53-
{buttonName}
54-
</Container>
55-
);
56-
} else {
57-
mostUsedSandboxComponent = (
58-
<ContainerLink
59-
to={sandboxUrl({ id: mostUsedSandboxTemplate.shortid, alias: null })}
60-
color={mostUsedSandboxTemplate.color}
61-
>
62-
{buttonName}
63-
</ContainerLink>
64-
);
65-
}
66-
}
67-
6847
return (
6948
<div style={style}>
7049
<ButtonsContainer>
7150
<Container
7251
onClick={handleClick}
7352
tabIndex={0}
7453
role="button"
75-
onKeyDown={e => {
76-
if (e.keyCode === ENTER) {
54+
onKeyDown={({ keyCode }: KeyboardEvent) => {
55+
if (keyCode === ENTER) {
7756
handleClick();
7857
}
7958
}}
8059
>
8160
Create Sandbox
8261
</Container>
83-
{mostUsedSandboxComponent}
62+
63+
<MostUsedSandbox
64+
createSandbox={createSandbox}
65+
template={mostUsedSandboxTemplate}
66+
/>
8467
</ButtonsContainer>
8568
</div>
8669
);

0 commit comments

Comments
 (0)