|
1 | 1 | import getTemplateDefinition from '@codesandbox/common/lib/templates'; |
2 | 2 | import codesandbox from '@codesandbox/common/lib/themes/codesandbox.json'; |
3 | | -import { inject, observer } from 'app/componentConnectors'; |
4 | 3 | import Modal from 'app/components/Modal'; |
5 | 4 | import getVSCodeTheme from 'app/src/app/pages/Sandbox/Editor/utils/get-vscode-theme'; |
6 | 5 | import Loadable from 'app/utils/Loadable'; |
7 | 6 | 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'; |
9 | 9 | import { ThemeProvider } from 'styled-components'; |
10 | 10 |
|
11 | 11 | import CommitModal from './CommitModal'; |
@@ -135,68 +135,70 @@ const modals = { |
135 | 135 | }, |
136 | 136 | }; |
137 | 137 |
|
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 }), { |
140 | 151 | theme: { |
141 | 152 | colors: {}, |
142 | 153 | vscodeTheme: codesandbox, |
143 | 154 | }, |
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 | + }); |
162 | 157 |
|
| 158 | + const loadTheme = useCallback(async () => { |
163 | 159 | try { |
164 | 160 | const theme = await getVSCodeTheme('', customVSCodeTheme); |
165 | | - this.setState({ theme, customVSCodeTheme }); |
| 161 | + setState({ theme, customVSCodeTheme }); |
166 | 162 | } catch (e) { |
167 | 163 | console.error(e); |
168 | 164 | } |
169 | | - }; |
| 165 | + }, [customVSCodeTheme]); |
170 | 166 |
|
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]); |
175 | 170 |
|
176 | | - const modal = store.currentModal && modals[store.currentModal]; |
| 171 | + useEffect(() => { |
| 172 | + if (customVSCodeTheme !== state.customVSCodeTheme) { |
| 173 | + loadTheme(); |
| 174 | + } |
| 175 | + }); |
177 | 176 |
|
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]; |
201 | 181 |
|
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 }; |
0 commit comments