Skip to content

Feature/cleanup #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup router code
  • Loading branch information
ShMcK committed Jan 29, 2020
commit 1109c290be8ac02509ff8e77a09c1e7938ef74c7
23 changes: 10 additions & 13 deletions web-app/src/Routes.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import * as CR from 'typings'
import Router from './components/Router'
import useRouter from './components/Router'
import Workspace from './components/Workspace'
import ContinuePage from './containers/Continue'
import LoadingPage from './containers/LoadingPage'
Expand All @@ -9,44 +9,41 @@ import OverviewPage from './containers/Overview'
import CompletedPage from './containers/Tutorial/CompletedPage'
import LevelSummaryPage from './containers/Tutorial/LevelPage'

const { Route } = Router

const tempSend = (action: any) => console.log('sent')

const Routes = () => {
const { context, send, Router, Route } = useRouter()
// TODO refactor for typescript to understand send & context passed into React.cloneElement's
return (
<Workspace>
<Router>
<Route path={['Start.Startup', 'Start.Authenticate', 'Start.NewOrContinue']}>
<LoadingPage text="Launching..." context={{} as CR.MachineContext} />
<LoadingPage text="Launching..." context={context} />
</Route>
<Route path={'Start.Error'}>
<div>Something went wrong wrong</div>
</Route>
<Route path="Start.SelectTutorial">
<NewPage send={tempSend} context={{} as CR.MachineContext} />
<NewPage send={send} context={context} />
</Route>
<Route path="Start.ContinueTutorial">
<ContinuePage send={tempSend} context={{} as CR.MachineContext} />
<ContinuePage send={send} context={context} />
</Route>
<Route path={'Tutorial.Error'}>
<div>Something went wrong wrong</div>
</Route>
<Route path="Tutorial.Initialize">
<LoadingPage text="Initializing..." context={{} as CR.MachineContext} />
<LoadingPage text="Initializing..." context={context} />
</Route>
<Route path="Tutorial.LoadNext">
<LoadingPage text="Loading..." context={{} as CR.MachineContext} />
<LoadingPage text="Loading..." context={context} />
</Route>
<Route path="Tutorial.Summary">
<OverviewPage send={tempSend} context={{} as CR.MachineContext} />
<OverviewPage send={send} context={context} />
</Route>
<Route path="Tutorial.Level">
<LevelSummaryPage send={tempSend} context={{} as CR.MachineContext} />
<LevelSummaryPage send={send} context={context} />
</Route>
<Route path="Tutorial.Completed">
<CompletedPage send={tempSend} context={{} as CR.MachineContext} />
<CompletedPage send={send} context={context} />
</Route>
</Router>
</Workspace>
Expand Down
54 changes: 30 additions & 24 deletions web-app/src/components/Router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react'
import * as CR from 'typings'
import { createMachine } from '../../services/state/machine'
import { useMachine } from '../../services/xstate-react'
import debuggerWrapper from '../Debugger/debuggerWrapper'
import Route from './Route'
import onError from '../../services/sentry/onError'

Expand All @@ -20,7 +19,7 @@ declare let acquireVsCodeApi: any
const editor = acquireVsCodeApi()

// router finds first state match of <Route path='' />
const Router = ({ children }: Props): React.ReactElement<CloneElementProps> | null => {
const useRouter = () => {
const [state, send] = useMachine(createMachine({ editorSend: editor.postMessage }))

// event bus listener
Expand All @@ -42,29 +41,36 @@ const Router = ({ children }: Props): React.ReactElement<CloneElementProps> | nu
}
}, [])

const childArray = React.Children.toArray(children)
for (const child of childArray) {
const { path } = child.props
let pathMatch
if (typeof path === 'string') {
pathMatch = state.matches(path)
} else if (Array.isArray(path)) {
pathMatch = path.some(p => state.matches(p))
} else {
throw new Error(`Invalid route path ${JSON.stringify(path)}`)
}
if (pathMatch) {
// @ts-ignore
const element = React.cloneElement<CloneElementProps>(child.props.children, { send, context: state.context })
return debuggerWrapper(element, state)
const Router = ({ children }: Props): React.ReactElement<CloneElementProps> | null => {
const childArray = React.Children.toArray(children)
for (const child of childArray) {
// match path
const { path } = child.props
let pathMatch
if (typeof path === 'string') {
pathMatch = state.matches(path)
} else if (Array.isArray(path)) {
pathMatch = path.some(p => state.matches(p))
} else {
throw new Error(`Invalid route path ${JSON.stringify(path)}`)
}
if (pathMatch) {
// @ts-ignore
return child.props.children
}
}
const message = `No Route matches for ${JSON.stringify(state)}`
onError(new Error(message))
console.warn(message)
return null
}
const message = `No Route matches for ${JSON.stringify(state)}`
onError(new Error(message))
console.warn(message)
return null
}

Router.Route = Route
return {
context: state.context,
send,
Router,
Route,
}
}

export default Router
export default useRouter