Skip to content

Commit 9d940ec

Browse files
Cleanup Explorer screen (#4505)
Co-authored-by: Sara Vieira <[email protected]>
1 parent 768bde5 commit 9d940ec

File tree

7 files changed

+217
-198
lines changed

7 files changed

+217
-198
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Explorer/Dependencies/Dependency/BundleSizes.tsx

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useEffect, useState } from 'react';
21
import { Stack, Text } from '@codesandbox/components';
2+
import React, { FunctionComponent, useEffect, useState } from 'react';
33

44
const formatSize = (value: number) => {
55
let unit: string;
66
let size: number;
7+
78
if (Math.log10(value) < 3) {
89
unit = 'B';
910
size = value;
@@ -18,42 +19,52 @@ const formatSize = (value: number) => {
1819
return `${size.toFixed(1)}${unit}`;
1920
};
2021

22+
type Bundle = {
23+
gzip: number;
24+
size: number;
25+
};
2126
type Props = {
2227
dependency: string;
2328
version: string;
2429
};
25-
26-
export const BundleSizes = ({ dependency, version = '' }: Props) => {
27-
const [size, setSize] = useState(null);
30+
export const BundleSizes: FunctionComponent<Props> = ({
31+
dependency,
32+
version,
33+
}) => {
34+
const [bundle, setBundle] = useState<Bundle | null>(null);
2835
const [error, setError] = useState(null);
2936

30-
useEffect(() => {
31-
const cleanVersion = version.split('^');
32-
getSizeForPKG(`${dependency}@${cleanVersion[cleanVersion.length - 1]}`);
33-
}, [dependency, version]);
34-
3537
const getSizeForPKG = (pkg: string) => {
3638
fetch(`https://bundlephobia.com/api/size?package=${pkg}`)
37-
.then(rsp => rsp.json())
38-
.then(setSize)
39+
.then(response => response.json())
40+
.then(setBundle)
3941
.catch(setError);
4042
};
43+
useEffect(() => {
44+
const cleanVersion = version.split('^');
45+
getSizeForPKG(`${dependency}@${cleanVersion[cleanVersion.length - 1]}`);
46+
}, [dependency, version]);
4147

4248
if (error) {
4349
return (
44-
<Text variant="muted" marginBottom={2}>
50+
<Text marginBottom={2} variant="muted">
4551
There was a problem getting the size for {dependency}
4652
</Text>
4753
);
4854
}
4955

50-
return size ? (
56+
return bundle ? (
5157
<Stack justify="space-between" css={{ width: '100%' }}>
5258
<Text>
53-
<Text variant="muted">Gzip:</Text> {formatSize(size.gzip)}
59+
<Text variant="muted">Gzip: </Text>
60+
61+
{formatSize(bundle.gzip)}
5462
</Text>
63+
5564
<Text>
56-
<Text variant="muted">Size:</Text> {formatSize(size.size)}
65+
<Text variant="muted">Size: </Text>
66+
67+
{formatSize(bundle.size)}
5768
</Text>
5869
</Stack>
5970
) : null;

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Explorer/Dependencies/Dependency/index.tsx

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
import React, { useState, useEffect } from 'react';
2-
import CrossIcon from 'react-icons/lib/md/clear';
3-
import RefreshIcon from 'react-icons/lib/md/refresh';
4-
import ArrowDropDown from 'react-icons/lib/md/keyboard-arrow-down';
5-
import ArrowDropUp from 'react-icons/lib/md/keyboard-arrow-up';
6-
import algoliasearch from 'algoliasearch/lite';
7-
import compareVersions from 'compare-versions';
81
import Tooltip, {
92
SingletonTooltip,
103
} from '@codesandbox/common/lib/components/Tooltip';
114
import { formatVersion } from '@codesandbox/common/lib/utils/ci';
12-
13-
import css from '@styled-system/css';
145
import {
6+
Button,
7+
Link,
158
ListAction,
16-
Stack,
17-
SidebarRow,
189
Select,
10+
SidebarRow,
11+
Stack,
1912
Text,
20-
Link,
21-
Button,
2213
} from '@codesandbox/components';
14+
import css from '@styled-system/css';
15+
import algoliasearch from 'algoliasearch/lite';
16+
import compareVersions from 'compare-versions';
17+
import React, { FunctionComponent, useEffect, useState } from 'react';
18+
import CrossIcon from 'react-icons/lib/md/clear';
19+
import RefreshIcon from 'react-icons/lib/md/refresh';
20+
import ArrowDropDown from 'react-icons/lib/md/keyboard-arrow-down';
21+
import ArrowDropUp from 'react-icons/lib/md/keyboard-arrow-up';
2322

24-
import { BundleSizes } from './BundleSizes';
23+
import { useOvermind } from 'app/overmind';
2524

26-
interface Props {
27-
dependencies: { [dep: string]: string };
28-
dependency: string;
29-
onRemove: (dep: string) => void;
30-
onRefresh: (dep: string, version?: string) => void;
31-
}
25+
import { BundleSizes } from './BundleSizes';
3226

3327
const client = algoliasearch('OFCNCOG2CU', '00383ecd8441ead30b1b0ff981c426f5');
3428
const index = client.initIndex('npm-search');
3529

36-
export const Dependency = ({
30+
type Props = {
31+
dependencies: { [dependency: string]: string };
32+
dependency: string;
33+
};
34+
export const Dependency: FunctionComponent<Props> = ({
3735
dependencies,
3836
dependency,
39-
onRemove,
40-
onRefresh,
41-
}: Props) => {
42-
const [version, setVersion] = useState(null);
37+
}) => {
38+
const {
39+
actions: {
40+
editor: { addNpmDependency, npmDependencyRemoved },
41+
},
42+
} = useOvermind();
4343
const [open, setOpen] = useState(false);
44+
const [version, setVersion] = useState(null);
4445
const [versions, setVersions] = useState([]);
4546

4647
const setVersionsForLatestPkg = (pkg: string) => {
4748
fetch(`/api/v1/dependencies/${pkg}`)
4849
.then(response => response.json())
49-
.then(data => setVersion(data.data.version))
50+
.then(({ data }) => setVersion(data.version))
5051
.catch(err => {
5152
if (process.env.NODE_ENV === 'development') {
5253
console.error(err); // eslint-disable-line no-console
5354
}
5455
});
5556
};
56-
5757
useEffect(() => {
5858
// @ts-ignore
5959
index.getObject(dependency, ['versions']).then(({ versions: results }) => {
6060
const orderedVersions = Object.keys(results).sort((a, b) => {
6161
try {
6262
return compareVersions(b, a);
63-
} catch (e) {
63+
} catch {
6464
return 0;
6565
}
6666
});
@@ -84,15 +84,16 @@ export const Dependency = ({
8484
e.preventDefault();
8585
e.stopPropagation();
8686
}
87-
onRemove(dependency);
88-
};
8987

88+
npmDependencyRemoved(dependency);
89+
};
9090
const handleRefresh = e => {
9191
if (e) {
9292
e.preventDefault();
9393
e.stopPropagation();
9494
}
95-
onRefresh(dependency);
95+
96+
addNpmDependency({ name: dependency });
9697
};
9798

9899
if (typeof dependencies[dependency] !== 'string') {
@@ -102,67 +103,67 @@ export const Dependency = ({
102103
return (
103104
<>
104105
<ListAction
105-
justify="space-between"
106106
align="center"
107107
css={css({
108108
position: 'relative',
109+
109110
'.actions': {
110111
backgroundColor: 'sideBar.background',
111112
display: 'none',
112113
},
114+
113115
':hover .actions': {
114116
backgroundColor: 'list.hoverBackground',
115117
display: 'flex',
116118
},
117119
})}
120+
justify="space-between"
118121
>
119122
<Link
123+
css={{ maxWidth: '60%', position: 'absolute', zIndex: 2 }}
120124
href={`/examples/package/${dependency}`}
125+
maxWidth="60%"
121126
target="_blank"
122127
title={dependency}
123-
maxWidth="60%"
124-
css={{
125-
position: 'absolute',
126-
zIndex: 2,
127-
maxWidth: '60%',
128-
}}
129128
>
130129
{dependency}
131130
</Link>
132131

133132
<Stack
134133
align="center"
135-
justify="flex-end"
136134
css={css({
137135
position: 'absolute',
138136
right: 2,
139137
flexGrow: 0,
140138
flexShrink: 1,
141139
width: '100%',
142140
})}
141+
justify="flex-end"
143142
>
144-
<Text variant="muted" maxWidth="30%">
143+
<Text maxWidth="30%" variant="muted">
145144
{formatVersion(dependencies[dependency])}{' '}
146145
{version && <span>({formatVersion(version)})</span>}
147146
</Text>
148147
</Stack>
149148

150149
<Stack
151-
className="actions"
152150
align="center"
153-
justify="flex-end"
154151
css={css({
155152
position: 'absolute',
156153
right: 0,
157154
width: 'auto',
158155
zIndex: 2, // overlay on dependency name
159156
paddingLeft: 1,
160157
})}
158+
className="actions"
159+
justify="flex-end"
161160
>
162161
<Select
163162
css={{ width: '80px' }}
163+
onChange={({ target: { value } }) =>
164+
addNpmDependency({ name: dependency, version: value })
165+
}
164166
value={versions.find(v => v === dependencies[dependency])}
165-
onChange={e => onRefresh(dependency, e.target.value)}
166167
>
167168
{versions.map(a => (
168169
<option key={a}>{a}</option>
@@ -174,39 +175,41 @@ export const Dependency = ({
174175
<>
175176
<Tooltip
176177
content={open ? 'Hide sizes' : 'Show sizes'}
177-
style={{ outline: 'none' }}
178178
singleton={singleton}
179+
style={{ outline: 'none' }}
179180
>
180181
<Button
181-
variant="link"
182-
onClick={() => setOpen(!open)}
183182
css={css({ minWidth: 5 })}
183+
onClick={() => setOpen(show => !show)}
184+
variant="link"
184185
>
185186
{open ? <ArrowDropUp /> : <ArrowDropDown />}
186187
</Button>
187188
</Tooltip>
189+
188190
<Tooltip
189191
content="Update to latest"
190-
style={{ outline: 'none' }}
191192
singleton={singleton}
193+
style={{ outline: 'none' }}
192194
>
193195
<Button
194-
variant="link"
195-
onClick={handleRefresh}
196196
css={css({ minWidth: 5 })}
197+
onClick={handleRefresh}
198+
variant="link"
197199
>
198200
<RefreshIcon />
199201
</Button>
200202
</Tooltip>
203+
201204
<Tooltip
202205
content="Remove"
203206
style={{ outline: 'none' }}
204207
singleton={singleton}
205208
>
206209
<Button
207-
variant="link"
208-
onClick={handleRemove}
209210
css={css({ minWidth: 5 })}
211+
onClick={handleRemove}
212+
variant="link"
210213
>
211214
<CrossIcon />
212215
</Button>
@@ -216,6 +219,7 @@ export const Dependency = ({
216219
</SingletonTooltip>
217220
</Stack>
218221
</ListAction>
222+
219223
{open ? (
220224
<SidebarRow marginX={2}>
221225
<BundleSizes

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Explorer/Dependencies/index.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ import React, { FunctionComponent } from 'react';
1010

1111
import { Dependency } from './Dependency';
1212

13-
export const Dependencies: FunctionComponent<{ readonly?: boolean }> = ({
14-
readonly = false,
15-
}) => {
13+
type Props = {
14+
readonly: boolean;
15+
};
16+
export const Dependencies: FunctionComponent<Props> = ({ readonly }) => {
1617
const {
17-
actions: {
18-
modalOpened,
19-
editor: { addNpmDependency, npmDependencyRemoved },
20-
},
18+
actions: { modalOpened },
2119
state: {
2220
editor: { parsedConfigurations },
2321
},
@@ -32,7 +30,6 @@ export const Dependencies: FunctionComponent<{ readonly?: boolean }> = ({
3230
}
3331

3432
const { error, parsed } = parsedConfigurations.package;
35-
3633
if (error) {
3734
return (
3835
<SidebarRow marginX={2}>
@@ -42,9 +39,8 @@ export const Dependencies: FunctionComponent<{ readonly?: boolean }> = ({
4239
}
4340

4441
const { dependencies = {} } = parsed;
45-
4642
return (
47-
<Collapsible title="Dependencies" defaultOpen>
43+
<Collapsible defaultOpen title="Dependencies">
4844
<List marginBottom={2}>
4945
{Object.keys(dependencies)
5046
.sort()
@@ -53,16 +49,15 @@ export const Dependencies: FunctionComponent<{ readonly?: boolean }> = ({
5349
dependencies={dependencies}
5450
dependency={dependency}
5551
key={dependency}
56-
onRefresh={(name, version) => addNpmDependency({ name, version })}
57-
onRemove={npmDependencyRemoved}
5852
/>
5953
))}
6054
</List>
61-
{!readonly && (
55+
56+
{readonly ? null : (
6257
<SidebarRow marginX={2}>
6358
<Button
64-
variant="secondary"
6559
onClick={() => modalOpened({ modal: 'searchDependencies' })}
60+
variant="secondary"
6661
>
6762
Add dependency
6863
</Button>

0 commit comments

Comments
 (0)