Skip to content

Commit 7d04dad

Browse files
Cleanup Server screen (codesandbox#4025)
1 parent 8505f3a commit 7d04dad

File tree

11 files changed

+290
-265
lines changed

11 files changed

+290
-265
lines changed

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,10 @@ export const updateEnvironmentVariables: AsyncAction<EnvironmentVariable> = asyn
868868
effects.codesandboxApi.restartSandbox();
869869
};
870870

871-
export const deleteEnvironmentVariable: AsyncAction<{
872-
name: string;
873-
}> = async ({ state, effects }, { name }) => {
871+
export const deleteEnvironmentVariable: AsyncAction<string> = async (
872+
{ effects, state },
873+
name
874+
) => {
874875
if (!state.editor.currentSandbox) {
875876
return;
876877
}

packages/app/src/app/overmind/namespaces/server/actions.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const restartSandbox: Action = ({ effects }) => {
1313
effects.executor.emit('sandbox:restart');
1414
};
1515

16-
export const restartContainer: Action = ({ state, effects }) => {
16+
export const restartContainer: Action = ({ effects, state }) => {
1717
state.server.containerStatus = ServerContainerStatus.INITIALIZING;
1818
effects.executor.emit('sandbox:restart-container');
1919
};
@@ -117,9 +117,7 @@ export const onSSEMessage: Action<{
117117
actions: {
118118
primary: {
119119
label: 'Open Browser Pane',
120-
run: () => {
121-
actions.server.onBrowserFromPortOpened({ port });
122-
},
120+
run: () => actions.server.onBrowserFromPortOpened(port),
123121
},
124122
},
125123
});
@@ -172,16 +170,13 @@ export const onCodeSandboxAPIMessage: Action<{
172170
};
173171

174172
type BrowserOptions = { title?: string; url?: string } & (
175-
| {
176-
port: number;
177-
}
173+
| { port: number }
178174
| { url: string }
179175
);
180-
181176
export const onBrowserTabOpened: Action<{
182177
closeable?: boolean;
183178
options?: BrowserOptions;
184-
}> = ({ actions, state }, { options, closeable }) => {
179+
}> = ({ actions, state }, { closeable, options }) => {
185180
const tab: ViewTab = {
186181
id: 'codesandbox.browser',
187182
};
@@ -206,17 +201,18 @@ export const onBrowserTabOpened: Action<{
206201
}
207202
};
208203

209-
export const onBrowserFromPortOpened: Action<{
210-
port: ServerPort;
211-
}> = ({ actions }, { port }) => {
212-
if (port.main) {
204+
export const onBrowserFromPortOpened: Action<ServerPort> = (
205+
{ actions },
206+
{ hostname, main, port }
207+
) => {
208+
if (main) {
213209
actions.server.onBrowserTabOpened({});
214210
} else {
215211
actions.server.onBrowserTabOpened({
216212
closeable: true,
217213
options: {
218-
port: port.port,
219-
url: `https://${port.hostname}`,
214+
port,
215+
url: `https://${hostname}`,
220216
},
221217
});
222218
}

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Server/Control.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import React from 'react';
21
import {
2+
Button,
33
Collapsible,
4-
Text,
54
Element,
6-
Button,
75
Stack,
6+
Text,
87
} from '@codesandbox/components';
8+
import React, { FunctionComponent } from 'react';
9+
910
import { useOvermind } from 'app/overmind';
11+
1012
import { RestartServerIcon } from './Icons';
1113

12-
export const Control = () => {
14+
export const Control: FunctionComponent = () => {
1315
const {
1416
actions: {
1517
server: { restartContainer, restartSandbox },
@@ -24,21 +26,22 @@ export const Control = () => {
2426
<Collapsible defaultOpen title="Control Container">
2527
<Element paddingX={2}>
2628
<Button
27-
variant="secondary"
2829
disabled={disconnected || containerStatus !== 'sandbox-started'}
2930
onClick={restartSandbox}
31+
variant="secondary"
3032
>
31-
<Stack gap={2} align="center">
33+
<Stack align="center" gap={2}>
3234
<RestartServerIcon /> <Text>Restart Sandbox</Text>
3335
</Stack>
3436
</Button>
37+
3538
<Button
36-
marginTop={2}
37-
variant="secondary"
3839
disabled={disconnected || containerStatus === 'initializing'}
40+
marginTop={2}
3941
onClick={restartContainer}
42+
variant="secondary"
4043
>
41-
<Stack gap={2} align="center">
44+
<Stack align="center" gap={2}>
4245
<RestartServerIcon /> <Text>Restart Server</Text>
4346
</Stack>
4447
</Button>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {
2+
Button,
3+
Element,
4+
FormField,
5+
Input,
6+
Stack,
7+
} from '@codesandbox/components';
8+
import React, {
9+
FormEvent,
10+
FunctionComponent,
11+
KeyboardEvent,
12+
useState,
13+
} from 'react';
14+
15+
const noop = () => undefined;
16+
17+
type Props = {
18+
name?: string;
19+
onCancel?: () => void;
20+
onSubmit: (args: { name: string; value: string }) => void;
21+
value?: string;
22+
};
23+
export const VarForm: FunctionComponent<Props> = ({
24+
name: nameProp = '',
25+
onCancel: onCancelProp = noop,
26+
onSubmit: onSubmitProp,
27+
value: valueProp = '',
28+
}) => {
29+
const [name, setName] = useState(nameProp);
30+
const [value, setValue] = useState(valueProp);
31+
32+
const onSubmit = (event: FormEvent<HTMLFormElement>) => {
33+
event.preventDefault();
34+
35+
if (name && value) {
36+
onSubmitProp({ name, value });
37+
setName('');
38+
setValue('');
39+
}
40+
};
41+
42+
const onCancel = ({ key }: KeyboardEvent<HTMLInputElement>) => {
43+
if (key === 'Escape') {
44+
onCancelProp();
45+
}
46+
};
47+
48+
return (
49+
<form onSubmit={onSubmit}>
50+
<Element marginTop={4}>
51+
<FormField
52+
direction="vertical"
53+
hideLabel
54+
label="Environment Variable Name"
55+
>
56+
<Input
57+
onChange={({ target }) => setName(target.value)}
58+
onKeyDown={onCancel}
59+
placeholder="Name"
60+
required
61+
value={name}
62+
/>
63+
</FormField>
64+
</Element>
65+
66+
<FormField
67+
direction="vertical"
68+
hideLabel
69+
label="Environment Variable Value"
70+
>
71+
<Input
72+
onChange={({ target }) => setValue(target.value)}
73+
onKeyDown={onCancel}
74+
placeholder="Value"
75+
required
76+
value={value}
77+
/>
78+
</FormField>
79+
80+
<Stack paddingX={2} marginTop={2}>
81+
{nameProp && valueProp ? (
82+
<Button css={{ flex: 1 }} onClick={onCancelProp} variant="link">
83+
Cancel
84+
</Button>
85+
) : null}
86+
87+
<Button
88+
css={{ flex: 1 }}
89+
disabled={!name || !value}
90+
type="submit"
91+
variant="secondary"
92+
>
93+
{nameProp && valueProp ? 'Save' : 'Add Secret'}
94+
</Button>
95+
</Stack>
96+
</form>
97+
);
98+
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { useOvermind } from 'app/overmind';
2-
import React, { useEffect, useState } from 'react';
3-
41
import {
52
Collapsible,
63
Text,
@@ -9,60 +6,70 @@ import {
96
ListItem,
107
Stack,
118
} from '@codesandbox/components';
12-
import { DeleteIcon, EditIcon } from './Icons';
9+
import React, { FunctionComponent, useEffect, useState } from 'react';
10+
11+
import { useOvermind } from 'app/overmind';
12+
13+
import { DeleteIcon, EditIcon } from '../Icons';
14+
1315
import { VarForm } from './VarForm';
1416

15-
export const EnvVars = () => {
16-
const [editMode, setEditMode] = useState(null);
17+
export const EnvVars: FunctionComponent = () => {
1718
const {
18-
actions: { editor },
19+
actions: {
20+
editor: {
21+
deleteEnvironmentVariable,
22+
fetchEnvironmentVariables,
23+
updateEnvironmentVariables,
24+
},
25+
},
1926
state: {
2027
editor: { currentSandbox },
2128
},
2229
} = useOvermind();
30+
const [editMode, setEditMode] = useState(null);
2331

2432
useEffect(() => {
25-
editor.fetchEnvironmentVariables();
26-
}, [editor]);
27-
28-
const deleteEnv = (name: string) => {
29-
editor.deleteEnvironmentVariable({ name });
30-
};
33+
fetchEnvironmentVariables();
34+
}, [fetchEnvironmentVariables]);
3135
const envVars = currentSandbox.environmentVariables;
3236

3337
return (
34-
<Collapsible title="Secret Keys" defaultOpen>
38+
<Collapsible defaultOpen title="Secret Keys">
3539
<Element paddingX={2}>
36-
<Text variant="muted" block>
40+
<Text block variant="muted">
3741
Secrets are available as environment variables. They are kept private
3842
and will not be transferred between forks.
3943
</Text>
4044
</Element>
45+
4146
{envVars ? (
4247
<List paddingTop={4}>
4348
{Object.keys(envVars).map(keyName => (
4449
<>
4550
{editMode === keyName || !envVars[keyName] ? (
4651
<VarForm
4752
name={keyName}
48-
value={envVars[keyName]}
4953
onCancel={() => setEditMode(null)}
5054
onSubmit={({ name, value }) => {
51-
editor.updateEnvironmentVariables({ name, value });
55+
updateEnvironmentVariables({ name, value });
5256
setEditMode(null);
5357
}}
58+
value={envVars[keyName]}
5459
/>
5560
) : (
5661
<ListItem justify="space-between" marginTop={editMode ? 4 : 0}>
5762
<Text>{keyName}</Text>
63+
5864
<Stack gap={2}>
5965
<EditIcon
60-
style={{ cursor: 'pointer' }}
6166
onClick={() => setEditMode(keyName)}
67+
style={{ cursor: 'pointer' }}
6268
/>
69+
6370
<DeleteIcon
71+
onClick={() => deleteEnvironmentVariable(keyName)}
6472
style={{ cursor: 'pointer' }}
65-
onClick={() => deleteEnv(keyName)}
6673
/>
6774
</Stack>
6875
</ListItem>
@@ -72,11 +79,7 @@ export const EnvVars = () => {
7279
</List>
7380
) : null}
7481

75-
<VarForm
76-
onSubmit={({ name, value }) =>
77-
editor.updateEnvironmentVariables({ name, value })
78-
}
79-
/>
82+
<VarForm onSubmit={updateEnvironmentVariables} />
8083
</Collapsible>
8184
);
8285
};

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Server/Icons.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import React from 'react';
1+
import React, { FunctionComponent, SVGProps } from 'react';
22

3-
export const EditIcon = props => (
3+
type IconProps = SVGProps<SVGSVGElement>;
4+
5+
export const EditIcon: FunctionComponent<IconProps> = props => (
46
<svg width={16} height={16} fill="none" viewBox="0 0 16 16" {...props}>
57
<path
68
fill="#757575"
@@ -9,7 +11,7 @@ export const EditIcon = props => (
911
</svg>
1012
);
1113

12-
export const DeleteIcon = props => (
14+
export const DeleteIcon: FunctionComponent<IconProps> = props => (
1315
<svg width={16} height={16} fill="none" viewBox="0 0 16 16" {...props}>
1416
<path
1517
fill="#757575"
@@ -18,7 +20,7 @@ export const DeleteIcon = props => (
1820
</svg>
1921
);
2022

21-
export const RestartServerIcon = props => (
23+
export const RestartServerIcon: FunctionComponent<IconProps> = props => (
2224
<svg width={12} height={11} fill="none" viewBox="0 0 12 11" {...props}>
2325
<path
2426
fill="#fff"

0 commit comments

Comments
 (0)