Skip to content

Commit a2715aa

Browse files
committed
🔨 Switch CollectionInfo to use useOvermind
1 parent 4f2755e commit a2715aa

File tree

4 files changed

+74
-89
lines changed

4 files changed

+74
-89
lines changed

‎packages/app/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
"@types/react-dom": "^16.8.3",
240240
"@types/react-helmet": "^5.0.11",
241241
"@types/react-icons": "2.2.7",
242+
"@types/react-input-autosize": "^2.0.1",
242243
"@types/react-router-dom": "^4.3.1",
243244
"@types/react-stripe-elements": "^1.3.2",
244245
"@types/resolve": "^0.0.8",
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import styled from 'styled-components';
21
import AutosizeInput from 'react-input-autosize';
2+
import styled, { css } from 'styled-components';
33

44
export const Container = styled.div`
55
display: flex;
@@ -11,20 +11,22 @@ export const Container = styled.div`
1111
`;
1212

1313
export const SandboxName = styled.span`
14-
color: ${props => (props.theme.light ? 'black' : 'white')};
15-
margin-left: 0.25rem;
14+
${({ theme }) => css`
15+
color: ${theme.light ? 'black' : 'white'};
16+
margin-left: 0.25rem;
1617
17-
cursor: pointer;
18-
text-overflow: ellipsis;
18+
cursor: pointer;
19+
text-overflow: ellipsis;
20+
`};
1921
`;
2022

2123
export const SandboxForm = styled.form`
22-
position: 'absolute';
24+
position: absolute;
2325
left: 0;
2426
right: 0;
25-
display: 'flex';
26-
align-items: 'center';
27-
justify-content: 'center';
27+
display: flex;
28+
align-items: center;
29+
justify-content: center;
2830
`;
2931

3032
export const SandboxInput = styled(AutosizeInput)`
@@ -45,13 +47,12 @@ export const FolderName = styled.button`
4547
cursor: pointer;
4648
transition: 0.3s ease color;
4749
padding: 0;
48-
margin: 0;
50+
margin: 0 0.25rem 0 0;
4951
outline: 0;
5052
border: 0;
5153
background-color: transparent;
5254
color: inherit;
5355
54-
margin-right: 0.25rem;
5556
&:hover {
5657
color: white;
5758
}
Lines changed: 54 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,84 @@
1-
import React, { useCallback, useState } from 'react';
1+
import { Sandbox } from '@codesandbox/common/lib/types';
2+
import track from '@codesandbox/common/lib/utils/analytics';
3+
import { getSandboxName as getSandboxNameBase } from '@codesandbox/common/lib/utils/get-sandbox-name';
4+
import { ESC } from '@codesandbox/common/lib/utils/keycodes';
25
import { basename } from 'path';
3-
import { useOvermind } from 'app/overmind';
6+
import React, {
7+
ChangeEvent,
8+
FormEvent,
9+
FunctionComponent,
10+
KeyboardEvent,
11+
useState,
12+
} from 'react';
413
import Media from 'react-media';
5-
614
import { Spring } from 'react-spring/renderprops';
715

8-
import track from '@codesandbox/common/lib/utils/analytics';
9-
import { ESC } from '@codesandbox/common/lib/utils/keycodes';
10-
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
11-
import { Sandbox } from '@codesandbox/common/lib/types';
16+
import { useOvermind } from 'app/overmind';
17+
1218
import {
1319
Container,
14-
SandboxName,
20+
FolderName,
1521
SandboxForm,
1622
SandboxInput,
17-
FolderName,
23+
SandboxName,
1824
} from './elements';
1925

20-
interface ICollectionInfoProps {
26+
type Props = {
2127
sandbox: Sandbox;
22-
isLoggedIn: boolean;
23-
}
24-
25-
const CollectionInfo: React.FC<ICollectionInfoProps> = ({
26-
sandbox,
27-
isLoggedIn,
28-
}) => {
29-
const [updatingName, setUpdatingName] = useState(false);
30-
const [nameValue, setNameValue] = useState('');
28+
};
29+
export const CollectionInfo: FunctionComponent<Props> = ({ sandbox }) => {
3130
const {
3231
actions: {
33-
workspace: { sandboxInfoUpdated, valueChanged },
3432
modalOpened,
33+
workspace: { sandboxInfoUpdated, valueChanged },
3534
},
35+
state: { isLoggedIn },
3636
} = useOvermind();
37+
const [nameValue, setNameValue] = useState('');
38+
const [updatingName, setUpdatingName] = useState(false);
3739

38-
const sandboxName = useCallback(() => getSandboxName(sandbox) || 'Untitled', [
39-
sandbox,
40-
]);
41-
42-
const updateSandboxInfo = useCallback(() => {
40+
const getSandboxName = () => getSandboxNameBase(sandbox) || 'Untitled';
41+
const updateSandboxInfo = () => {
4342
sandboxInfoUpdated();
44-
setUpdatingName(false);
45-
}, [sandboxInfoUpdated]);
46-
47-
const submitNameChange = useCallback(
48-
e => {
49-
e.preventDefault();
50-
updateSandboxInfo();
5143

52-
track('Change Sandbox Name From Header');
53-
},
54-
[updateSandboxInfo]
55-
);
56-
57-
const handleNameClick = useCallback(() => {
58-
setUpdatingName(true);
59-
setNameValue(sandboxName());
60-
}, [sandboxName]);
44+
setUpdatingName(false);
45+
};
6146

62-
const handleKeyUp = useCallback(
63-
e => {
64-
if (e.keyCode === ESC) {
65-
updateSandboxInfo();
66-
}
67-
},
68-
[updateSandboxInfo]
69-
);
47+
const submitNameChange = ({ preventDefault }: FormEvent<HTMLFormElement>) => {
48+
preventDefault();
7049

71-
const handleBlur = useCallback(() => {
7250
updateSandboxInfo();
73-
}, [updateSandboxInfo]);
74-
75-
const handleInputUpdate = useCallback(
76-
e => {
77-
valueChanged({
78-
field: 'title',
79-
value: e.target.value,
80-
});
8151

82-
setNameValue(e.target.value);
83-
},
84-
[valueChanged]
85-
);
52+
track('Change Sandbox Name From Header');
53+
};
54+
const handleKeyUp = ({ keyCode }: KeyboardEvent<HTMLInputElement>) => {
55+
if (keyCode === ESC) {
56+
updateSandboxInfo();
57+
}
58+
};
59+
const handleBlur = () => {
60+
updateSandboxInfo();
61+
};
62+
const handleInputUpdate = ({
63+
target: { value },
64+
}: ChangeEvent<HTMLInputElement>) => {
65+
valueChanged({ field: 'title', value });
66+
67+
setNameValue(value);
68+
};
69+
const handleNameClick = () => {
70+
setNameValue(getSandboxName());
71+
setUpdatingName(true);
72+
};
8673

8774
const value = nameValue !== 'Untitled' && updatingName ? nameValue : '';
88-
8975
const folderName = sandbox.collection
9076
? basename(sandbox.collection.path) ||
9177
(sandbox.team ? sandbox.team.name : 'My Sandboxes')
9278
: 'My Sandboxes';
9379

9480
return (
95-
<Spring<{ opacity: number; pointerEvents: 'none' | 'initial' }>
81+
<Spring
9682
from={{
9783
opacity: 1,
9884
}}
@@ -116,19 +102,10 @@ const CollectionInfo: React.FC<ICollectionInfoProps> = ({
116102
<Media
117103
query="(min-width: 950px)"
118104
render={() => (
119-
<div
120-
style={{
121-
...style,
122-
overflow: 'hidden',
123-
}}
124-
>
105+
<div style={{ ...style, overflow: 'hidden' }}>
125106
{isLoggedIn ? (
126107
<FolderName
127-
onClick={() => {
128-
modalOpened({
129-
modal: 'moveSandbox',
130-
});
131-
}}
108+
onClick={() => modalOpened({ modal: 'moveSandbox' })}
132109
>
133110
{folderName}
134111
</FolderName>
@@ -139,6 +116,7 @@ const CollectionInfo: React.FC<ICollectionInfoProps> = ({
139116
</div>
140117
)}
141118
/>
119+
142120
{updatingName ? (
143121
<SandboxForm onSubmit={submitNameChange}>
144122
<SandboxInput
@@ -157,7 +135,7 @@ const CollectionInfo: React.FC<ICollectionInfoProps> = ({
157135
</SandboxForm>
158136
) : (
159137
<SandboxName onClick={handleNameClick}>
160-
{sandboxName()}
138+
{getSandboxName()}
161139
</SandboxName>
162140
)}
163141
</Container>
@@ -167,5 +145,3 @@ const CollectionInfo: React.FC<ICollectionInfoProps> = ({
167145
</Spring>
168146
);
169147
};
170-
171-
export { CollectionInfo };

‎yarn.lock‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,6 +4119,13 @@
41194119
"@types/react" "*"
41204120
"@types/react-icon-base" "*"
41214121

4122+
"@types/react-input-autosize@^2.0.1":
4123+
version "2.0.1"
4124+
resolved "https://registry.yarnpkg.com/@types/react-input-autosize/-/react-input-autosize-2.0.1.tgz#04ac4b532128c98532352042b6c7af8b5d3f52ca"
4125+
integrity sha512-vsmqA6Pp5IyMZv3C7x7mNM8lqikfWQ4lCWsoVk/w+HZ2Y3KOdFk6ad4jsQeltXn/UaO+YUZLtWdMIjLLIeCxBA==
4126+
dependencies:
4127+
"@types/react" "*"
4128+
41224129
"@types/react-native@*":
41234130
version "0.57.42"
41244131
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.57.42.tgz#06ad92cd1378146402b7667de13cb7b935d194d6"

0 commit comments

Comments
 (0)