-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This function makes it easier to deal with browser vs. gadget modes and handles waiting for the gadget to connect before creating anything.
- Loading branch information
Showing
5 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Av } from '@aardvarkxr/aardvark-shared'; | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import { AvGadget } from './aardvark_gadget'; | ||
import { DefaultLanding } from './default_landing_page'; | ||
|
||
|
||
/** Uses ReactDOM to render the appropriate main component. If The gadget is running in Aardvark, | ||
* this function will wait for it to connect to the local Aardvark server and then render gadgetMain. | ||
* If the gadget is running in another browser this function will render browserMain. | ||
* | ||
* @param element the element to render on or the ID of an element | ||
* @param gadgetMain The React component to render when running in Aardvark. | ||
* @param browserMain The React component to render when running in another browser. If this is undefined | ||
* an instance of DefaultLanding will be rendered. | ||
*/ | ||
export async function renderAardvarkRoot( element: HTMLElement | string, | ||
gadgetMain: JSX.Element | ( () => JSX.Element ), browserMain?: JSX.Element | ( () => JSX.Element ) ) | ||
{ | ||
if( typeof element == "string" ) | ||
{ | ||
element = document.getElementById( element ); | ||
} | ||
|
||
let main: JSX.Element; | ||
if( !Av() ) | ||
{ | ||
if( !browserMain ) | ||
{ | ||
browserMain = <DefaultLanding/>; | ||
} | ||
else if( typeof browserMain == "function" ) | ||
{ | ||
browserMain = browserMain(); | ||
} | ||
|
||
main = browserMain; | ||
} | ||
else | ||
{ | ||
await AvGadget.instance().waitForConnect(); | ||
|
||
if( typeof gadgetMain == "function" ) | ||
{ | ||
gadgetMain = gadgetMain(); | ||
} | ||
main = gadgetMain; | ||
} | ||
|
||
ReactDOM.render( main, element ); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters