Skip to content

Commit 2230824

Browse files
arthurdennerCompuIves
authored andcommitted
fix(storage-management): fix behavior outside of a sandbox context (#3081)
* fix(storage-management): only remove files that are selected * fix(storage-management): check for editor.currentSandbox when deleting file * fix(storage-management): disable add options outside of a sandbox context * refactor: wrap if statements in withOwnedSandbox factory
1 parent 6998149 commit 2230824

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

packages/app/src/app/overmind/factories.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,30 @@ export const withOwnedSandbox = <T>(
7777
): AsyncAction<T> => async (context, payload) => {
7878
const { state, actions } = context;
7979

80-
if (!state.editor.currentSandbox.owned) {
81-
if (state.editor.isForkingSandbox) {
82-
return cancelAction(context, payload);
83-
}
84-
85-
await actions.editor.internal.forkSandbox({
86-
sandboxId: state.editor.currentId,
87-
});
88-
} else if (
89-
state.editor.currentSandbox.isFrozen &&
90-
state.editor.sessionFrozen
91-
) {
92-
const modalResponse = await actions.modals.forkFrozenModal.open();
80+
if (state.editor.currentSandbox) {
81+
if (!state.editor.currentSandbox.owned) {
82+
if (state.editor.isForkingSandbox) {
83+
return cancelAction(context, payload);
84+
}
9385

94-
if (modalResponse === 'fork') {
9586
await actions.editor.internal.forkSandbox({
9687
sandboxId: state.editor.currentId,
9788
});
98-
} else if (modalResponse === 'unfreeze') {
99-
state.editor.sessionFrozen = false;
100-
} else if (modalResponse === 'cancel') {
101-
return cancelAction(context, payload);
89+
} else if (
90+
state.editor.currentSandbox.isFrozen &&
91+
state.editor.sessionFrozen
92+
) {
93+
const modalResponse = await actions.modals.forkFrozenModal.open();
94+
95+
if (modalResponse === 'fork') {
96+
await actions.editor.internal.forkSandbox({
97+
sandboxId: state.editor.currentId,
98+
});
99+
} else if (modalResponse === 'unfreeze') {
100+
state.editor.sessionFrozen = false;
101+
} else if (modalResponse === 'cancel') {
102+
return cancelAction(context, payload);
103+
}
102104
}
103105
}
104106

packages/app/src/app/pages/common/Modals/StorageManagementModal/AddFileToSandboxButton/elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import AddIcon from 'react-icons/lib/md/add';
44
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
55

66
export const AddFileToSandboxButton = styled(props => (
7-
<Tooltip content="Add file to sandbox">
7+
<Tooltip content="Add file to sandbox" isEnabled={!props.disabled}>
88
<button type="button" {...props}>
99
<AddIcon />
1010
</button>

packages/app/src/app/pages/common/Modals/StorageManagementModal/AddFileToSandboxButton/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default class AddFileToSandboxButtonContainer extends React.PureComponent
88
};
99

1010
render() {
11-
return <AddFileToSandboxButton onClick={this.addFileToSandbox} />;
11+
return (
12+
<AddFileToSandboxButton
13+
disabled={this.props.disabled}
14+
onClick={this.addFileToSandbox}
15+
/>
16+
);
1217
}
1318
}

packages/app/src/app/pages/common/Modals/StorageManagementModal/FilesList/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
Table,
1616
} from './elements';
1717

18-
const someSelected = obj =>
19-
Object.keys(obj).filter(key => obj[key] === true).length;
18+
const getSelectedFiles = obj =>
19+
Object.keys(obj).filter(key => obj[key] === true);
2020

2121
class FilesList extends Component {
2222
state = {};
@@ -44,20 +44,23 @@ class FilesList extends Component {
4444
deleteFiles,
4545
addFilesToSandbox,
4646
} = this.props;
47+
const canAddFile = Boolean(addFileToSandbox);
48+
const filesToRemove = getSelectedFiles(this.state);
49+
4750
return (
4851
<div css={{ margin: '0 2rem' }}>
4952
<Buttons>
5053
<Button
51-
disabled={!someSelected(this.state)}
54+
disabled={!filesToRemove.length || !canAddFile}
5255
small
5356
onClick={() => addFilesToSandbox(this.getSelection())}
5457
>
5558
Add all selected to project
5659
</Button>
5760
<Button
58-
disabled={!someSelected(this.state)}
61+
disabled={!filesToRemove.length}
5962
small
60-
onClick={() => deleteFiles(Object.keys(this.state))}
63+
onClick={() => deleteFiles(filesToRemove)}
6164
>
6265
Delete all selected
6366
</Button>
@@ -113,6 +116,7 @@ class FilesList extends Component {
113116
`}
114117
>
115118
<AddFileToSandboxButton
119+
disabled={!canAddFile}
116120
url={f.url}
117121
name={f.name}
118122
onAddFileToSandbox={addFileToSandbox}

packages/app/src/app/pages/common/Modals/StorageManagementModal/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import FilesList from './FilesList';
1717

1818
export const StorageManagementModal: FunctionComponent = () => {
1919
const {
20-
state: { usedStorage, maxStorage, uploadedFiles },
20+
state: {
21+
editor: { currentSandbox },
22+
usedStorage,
23+
maxStorage,
24+
uploadedFiles,
25+
},
2126
actions: {
2227
files: { deletedUploadedFile, addedFileToSandbox },
2328
},
@@ -50,7 +55,7 @@ export const StorageManagementModal: FunctionComponent = () => {
5055
deleteFile={deletedUploadedFile}
5156
deleteFiles={files => files.map(id => deletedUploadedFile({ id }))}
5257
addFilesToSandbox={files => files.map(addedFileToSandbox)}
53-
addFileToSandbox={addedFileToSandbox}
58+
addFileToSandbox={currentSandbox ? addedFileToSandbox : undefined}
5459
/>
5560
)}
5661

0 commit comments

Comments
 (0)