Skip to content

Commit 8332b65

Browse files
🔨 Refactor, 🧠 Overmind Hactoberfest | Refactor /app/pages/com… (codesandbox#2902)
🔨 Refactor, 🧠 Overmind Hactoberfest | Refactor /app/pages/common/Modals/index.js
2 parents 7361ce8 + bed89b4 commit 8332b65

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

‎packages/app/src/app/overmind/actions.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type ModalName =
7878
| 'searchDependencies'
7979
| 'signInForTemplates'
8080
| 'userSurvey';
81+
8182
export const modalOpened: Action<{ modal: ModalName; message?: string }> = (
8283
{ state, effects },
8384
{ modal, message }

packages/app/src/app/pages/common/Modals/index.js renamed to packages/app/src/app/pages/common/Modals/index.tsx

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import getTemplateDefinition from '@codesandbox/common/lib/templates';
22
import codesandbox from '@codesandbox/common/lib/themes/codesandbox.json';
3-
import { inject, observer } from 'app/componentConnectors';
43
import Modal from 'app/components/Modal';
54
import getVSCodeTheme from 'app/src/app/pages/Sandbox/Editor/utils/get-vscode-theme';
65
import Loadable from 'app/utils/Loadable';
76
import { templateColor } from 'app/utils/template-color';
8-
import React, { Component } from 'react';
7+
import React, { useReducer, useEffect, useCallback } from 'react';
8+
import { useOvermind } from 'app/overmind';
99
import { ThemeProvider } from 'styled-components';
1010

1111
import CommitModal from './CommitModal';
@@ -135,68 +135,70 @@ const modals = {
135135
},
136136
};
137137

138-
class Modals extends Component {
139-
state = {
138+
const Modals: React.FC = () => {
139+
const {
140+
actions,
141+
state: {
142+
editor: { currentSandbox },
143+
preferences: {
144+
settings: { customVSCodeTheme },
145+
},
146+
currentModal,
147+
},
148+
} = useOvermind();
149+
150+
const [state, setState] = useReducer((s, a) => ({ ...s, ...a }), {
140151
theme: {
141152
colors: {},
142153
vscodeTheme: codesandbox,
143154
},
144-
customVSCodeTheme: this.props.store.preferences.settings.customVSCodeTheme,
145-
};
146-
147-
componentDidMount() {
148-
this.loadTheme();
149-
}
150-
151-
componentDidUpdate() {
152-
if (
153-
this.props.store.preferences.settings.customVSCodeTheme !==
154-
this.state.customVSCodeTheme
155-
) {
156-
this.loadTheme();
157-
}
158-
}
159-
160-
loadTheme = async () => {
161-
const { customVSCodeTheme } = this.props.store.preferences.settings;
155+
customVSCodeTheme,
156+
});
162157

158+
const loadTheme = useCallback(async () => {
163159
try {
164160
const theme = await getVSCodeTheme('', customVSCodeTheme);
165-
this.setState({ theme, customVSCodeTheme });
161+
setState({ theme, customVSCodeTheme });
166162
} catch (e) {
167163
console.error(e);
168164
}
169-
};
165+
}, [customVSCodeTheme]);
170166

171-
render() {
172-
const { signals, store } = this.props;
173-
const sandbox = store.editor.currentSandbox;
174-
const templateDef = sandbox && getTemplateDefinition(sandbox.template);
167+
useEffect(() => {
168+
loadTheme();
169+
}, [loadTheme]);
175170

176-
const modal = store.currentModal && modals[store.currentModal];
171+
useEffect(() => {
172+
if (customVSCodeTheme !== state.customVSCodeTheme) {
173+
loadTheme();
174+
}
175+
});
177176

178-
return (
179-
<ThemeProvider
180-
theme={{
181-
templateColor: templateColor(sandbox, templateDef),
182-
templateBackgroundColor: templateDef && templateDef.backgroundColor,
183-
...this.state.theme,
184-
}}
185-
>
186-
<Modal
187-
isOpen={Boolean(modal)}
188-
width={modal && modal.width}
189-
onClose={isKeyDown => signals.modalClosed({ isKeyDown })}
190-
>
191-
{modal
192-
? React.createElement(modal.Component, {
193-
closeModal: () => signals.modalClosed({ isKeyDown: false }),
194-
})
195-
: null}
196-
</Modal>
197-
</ThemeProvider>
198-
);
199-
}
200-
}
177+
const sandbox = currentSandbox;
178+
const templateDef = sandbox && getTemplateDefinition(sandbox.template);
179+
180+
const modal = currentModal && modals[currentModal];
201181

202-
export default inject('store', 'signals')(observer(Modals));
182+
return (
183+
<ThemeProvider
184+
theme={{
185+
templateColor: templateColor(sandbox, templateDef),
186+
templateBackgroundColor: templateDef && templateDef.backgroundColor,
187+
...state.theme,
188+
}}
189+
>
190+
<Modal
191+
isOpen={Boolean(modal)}
192+
width={modal && modal.width}
193+
onClose={isKeyDown => actions.modalClosed()}
194+
>
195+
{modal
196+
? React.createElement(modal.Component, {
197+
closeModal: () => actions.modalClosed(),
198+
})
199+
: null}
200+
</Modal>
201+
</ThemeProvider>
202+
);
203+
};
204+
export { Modals };

‎packages/app/src/app/pages/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Loadable from 'app/utils/Loadable';
1111
import { useOvermind } from 'app/overmind';
1212
import { ErrorBoundary } from './common/ErrorBoundary';
1313
import HTML5Backend from './common/HTML5BackendWithFolderSupport';
14-
import Modals from './common/Modals';
14+
import { Modals } from './common/Modals';
1515
import Sandbox from './Sandbox';
1616
import { NewSandbox } from './NewSandbox';
1717
import Dashboard from './Dashboard';

‎packages/app/src/app/utils/template-color.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { decorateSelector } from '@codesandbox/common/lib/utils/decorate-selector';
22
import { Sandbox, Template } from '@codesandbox/common/lib/types';
3+
import TEMPLATE from '@codesandbox/common/lib/templates/template';
34

4-
export const templateColor = (sandbox: Sandbox, templateDef: Template) => {
5+
export const templateColor = (
6+
sandbox: Sandbox,
7+
templateDef: Template | TEMPLATE
8+
) => {
59
if (sandbox && sandbox.customTemplate) {
610
return decorateSelector(() => sandbox.customTemplate.color);
711
}

0 commit comments

Comments
 (0)