@@ -4,7 +4,7 @@ import styled from 'styled-components';
44import { camelizeKeys } from 'humps' ;
55import 'whatwg-fetch' ;
66
7- import type { Sandbox } from 'common/types' ;
7+ import type { Sandbox , Module } from 'common/types' ;
88import Centered from 'app/components/flex/Centered' ;
99import Title from 'app/components/text/Title' ;
1010import SubTitle from 'app/components/text/SubTitle' ;
@@ -13,6 +13,10 @@ import Header from './components/Header';
1313import Content from './components/Content' ;
1414import Sidebar from './components/Sidebar' ;
1515import { getSandboxOptions } from '../common/url' ;
16+ import {
17+ findCurrentModule ,
18+ findMainModule ,
19+ } from 'app/store/entities/sandboxes/modules/selectors' ;
1620
1721const Container = styled . div `
1822 display: flex;
@@ -106,16 +110,10 @@ export default class App extends React.PureComponent {
106110 . then ( res => res . json ( ) )
107111 . then ( camelizeKeys ) ;
108112
109- const currentModule =
110- this . state . currentModule ||
111- response . data . modules . find (
112- m => m . title === 'index.js' && m . directoryShortid == null ,
113- ) . shortid ;
114-
115113 document . title = response . data . title
116114 ? `${ response . data . title } - CodeSandbox`
117115 : 'Embed - CodeSandbox' ;
118- this . setState ( { sandbox : response . data , currentModule } ) ;
116+ this . setState ( { sandbox : response . data } ) ;
119117 } catch ( e ) {
120118 this . setState ( { notFound : true } ) ;
121119 }
@@ -144,6 +142,18 @@ export default class App extends React.PureComponent {
144142 setProjectView = ( sandboxId : string , isOpen : boolean ) =>
145143 this . setState ( { isInProjectView : isOpen } ) ;
146144
145+ getCurrentModuleFromPath = ( ) => {
146+ const { sandbox, currentModule : currentModulePath } = this . state ;
147+ if ( ! sandbox ) return null ;
148+
149+ return findCurrentModule (
150+ sandbox . modules ,
151+ sandbox . directories ,
152+ currentModulePath ,
153+ findMainModule ( sandbox . modules ) ,
154+ ) ;
155+ } ;
156+
147157 content = ( ) => {
148158 if ( this . state . notFound ) {
149159 return (
@@ -183,7 +193,7 @@ export default class App extends React.PureComponent {
183193 isInProjectView = { isInProjectView }
184194 setProjectView = { this . setProjectView }
185195 sandbox = { this . state . sandbox }
186- currentModule = { this . state . currentModule }
196+ currentModule = { this . getCurrentModuleFromPath ( ) . id }
187197 hideNavigation = { this . state . hideNavigation }
188198 autoResize = { this . state . autoResize }
189199 fontSize = { this . state . fontSize }
@@ -199,7 +209,7 @@ export default class App extends React.PureComponent {
199209 { this . state . sandbox &&
200210 < Sidebar
201211 setCurrentModule = { this . setCurrentModule }
202- currentModule = { this . state . currentModule }
212+ currentModule = { this . getCurrentModuleFromPath ( ) . id }
203213 sandbox = { this . state . sandbox }
204214 /> }
205215 < Moving sidebarOpen = { this . state . sidebarOpen } >
0 commit comments