Skip to content

Commit 7dafe74

Browse files
chinmaychaudharySaraVieira
authored andcommitted
🔨 Refactored 🧠 Overmind Hacktober /pages/Sandbox/Editor/Heade… (#2639)
1 parent 17559aa commit 7dafe74

File tree

4 files changed

+181
-159
lines changed

4 files changed

+181
-159
lines changed

‎packages/app/src/app/pages/Sandbox/Editor/Header/CollectionInfo/elements.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ export const SandboxName = styled.span`
1818
text-overflow: ellipsis;
1919
`;
2020

21+
export const SandboxForm = styled.form`
22+
position: 'absolute';
23+
left: 0;
24+
right: 0;
25+
display: 'flex';
26+
align-items: 'center';
27+
justify-content: 'center';
28+
`;
29+
2130
export const SandboxInput = styled(AutosizeInput)`
2231
input {
2332
display: inline-block;

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

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import React, { useCallback, useState } from 'react';
2+
import { basename } from 'path';
3+
import { useOvermind } from 'app/overmind';
4+
import Media from 'react-media';
5+
6+
import { Spring } from 'react-spring/renderprops';
7+
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';
12+
import {
13+
Container,
14+
SandboxName,
15+
SandboxForm,
16+
SandboxInput,
17+
FolderName,
18+
} from './elements';
19+
20+
interface ICollectionInfoProps {
21+
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('');
31+
const {
32+
actions: {
33+
workspace: { sandboxInfoUpdated, valueChanged },
34+
modalOpened,
35+
},
36+
} = useOvermind();
37+
38+
const sandboxName = useCallback(() => getSandboxName(sandbox) || 'Untitled', [
39+
sandbox,
40+
]);
41+
42+
const updateSandboxInfo = useCallback(() => {
43+
sandboxInfoUpdated();
44+
setUpdatingName(false);
45+
}, [sandboxInfoUpdated]);
46+
47+
const submitNameChange = useCallback(
48+
e => {
49+
e.preventDefault();
50+
updateSandboxInfo();
51+
52+
track('Change Sandbox Name From Header');
53+
},
54+
[updateSandboxInfo]
55+
);
56+
57+
const handleNameClick = useCallback(() => {
58+
setUpdatingName(true);
59+
setNameValue(sandboxName());
60+
}, [sandboxName]);
61+
62+
const handleKeyUp = useCallback(
63+
e => {
64+
if (e.keyCode === ESC) {
65+
updateSandboxInfo();
66+
}
67+
},
68+
[updateSandboxInfo]
69+
);
70+
71+
const handleBlur = useCallback(() => {
72+
updateSandboxInfo();
73+
}, [updateSandboxInfo]);
74+
75+
const handleInputUpdate = useCallback(
76+
e => {
77+
valueChanged({
78+
field: 'title',
79+
value: e.target.value,
80+
});
81+
82+
setNameValue(e.target.value);
83+
},
84+
[valueChanged]
85+
);
86+
87+
const value = nameValue !== 'Untitled' && updatingName ? nameValue : '';
88+
89+
const folderName = sandbox.collection
90+
? basename(sandbox.collection.path) ||
91+
(sandbox.team ? sandbox.team.name : 'My Sandboxes')
92+
: 'My Sandboxes';
93+
94+
return (
95+
<Spring<{ opacity: number; pointerEvents: 'none' | 'initial' }>
96+
from={{
97+
opacity: 1,
98+
}}
99+
to={
100+
updatingName
101+
? {
102+
opacity: 0,
103+
pointerEvents: 'none',
104+
}
105+
: {
106+
opacity: 1,
107+
pointerEvents: 'initial',
108+
}
109+
}
110+
>
111+
{style => (
112+
<Media
113+
query="(min-width: 826px)"
114+
render={() => (
115+
<Container>
116+
<Media
117+
query="(min-width: 950px)"
118+
render={() => (
119+
<div
120+
style={{
121+
...style,
122+
overflow: 'hidden',
123+
}}
124+
>
125+
{isLoggedIn ? (
126+
<FolderName
127+
onClick={() => {
128+
modalOpened({
129+
modal: 'moveSandbox',
130+
});
131+
}}
132+
>
133+
{folderName}
134+
</FolderName>
135+
) : (
136+
'Anonymous '
137+
)}
138+
/{' '}
139+
</div>
140+
)}
141+
/>
142+
{updatingName ? (
143+
<SandboxForm onSubmit={submitNameChange}>
144+
<SandboxInput
145+
autoFocus
146+
innerRef={el => {
147+
if (el) {
148+
el.focus();
149+
}
150+
}}
151+
onKeyUp={handleKeyUp}
152+
onBlur={handleBlur}
153+
onChange={handleInputUpdate}
154+
placeholder={nameValue}
155+
value={value}
156+
/>
157+
</SandboxForm>
158+
) : (
159+
<SandboxName onClick={handleNameClick}>
160+
{sandboxName()}
161+
</SandboxName>
162+
)}
163+
</Container>
164+
)}
165+
/>
166+
)}
167+
</Spring>
168+
);
169+
};
170+
171+
export { CollectionInfo };

‎packages/app/src/app/pages/Sandbox/Editor/Header/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import ShareIcon from 'react-icons/lib/md/share';
2222
import PatronBadge from '-!svg-react-loader!@codesandbox/common/lib/utils/badges/svg/patron-4.svg';
2323

2424
import { Action } from './Buttons/Action';
25-
import CollectionInfo from './CollectionInfo';
25+
import { CollectionInfo } from './CollectionInfo';
2626
import {
2727
Centered,
2828
Container,

0 commit comments

Comments
 (0)