- Notifications
You must be signed in to change notification settings - Fork39
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
c383d2a
be25c8a
dec9354
69190ec
409df47
1109c29
82d22a4
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,70 @@ | ||
import * as React from 'react' | ||
import { createMachine } from '../../services/state/machine' | ||
import { useMachine } from '../../services/xstate-react' | ||
import Route from './Route' | ||
import onError from '../../services/sentry/onError' | ||
interface Props { | ||
children: any | ||
} | ||
declare let acquireVsCodeApi: any | ||
const editor = acquireVsCodeApi() | ||
// router finds first state match of <Route path='' /> | ||
const useRouter = () => { | ||
const [state, send] = useMachine(createMachine({ editorSend: editor.postMessage })) | ||
// event bus listener | ||
React.useEffect(() => { | ||
const listener = 'message' | ||
// propograte channel event to state machine | ||
const handler = (action: any) => { | ||
// NOTE: must call event.data, cannot destructure. VSCode acts odd | ||
const event = action.data | ||
// ignore browser events from plugins | ||
if (event.source) { | ||
return | ||
} | ||
send(event) | ||
} | ||
window.addEventListener(listener, handler) | ||
return () => { | ||
window.removeEventListener(listener, handler) | ||
} | ||
}, []) | ||
const Router = ({ children }: Props): React.ReactElement<any> | 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 | ||
} | ||
return { | ||
context: state.context, | ||
send, | ||
Router, | ||
Route, | ||
} | ||
} | ||
export defaultuseRouter |
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.