-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5842da3
commit cd431d2
Showing
6 changed files
with
155 additions
and
4 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,73 @@ | ||
const { href } = VM.require("devs.near/widget/lib.url") || { | ||
href: () => "/", | ||
}; | ||
|
||
const Content = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
`; | ||
|
||
function findDefaultRoute(routesObject) { | ||
const routeKey = | ||
routesObject && | ||
Object.keys(routesObject).find((key) => { | ||
const route = routesObject[key]; | ||
return route.default === true; | ||
}); | ||
|
||
if (routeKey) { | ||
return routesObject[routeKey]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function Router({ config, ...passProps }) { | ||
const { routes, PageNotFound, debug, param } = config; | ||
|
||
if (!param) param = "page"; | ||
|
||
const defaultRoute = | ||
findDefaultRoute(routes) ?? | ||
(routes && Object.keys(routes).length && routes[Object.keys(routes)[0]]); | ||
const activeRoute = | ||
(routes && | ||
routes.hasOwnProperty(passProps[param]) && | ||
routes[passProps[param]]) || | ||
defaultRoute; | ||
|
||
if (!PageNotFound) PageNotFound = () => <p>404 Not Found</p>; | ||
|
||
if (!activeRoute) { | ||
// Handle 404 or default case for unknown routes | ||
return <PageNotFound />; | ||
} | ||
|
||
// An improvement may be to "lazy load", e.g. load all widgets at once and only "display" the active one | ||
// potentionally add a "lazy: true" prop to the route object | ||
|
||
// for each route, if lazy, load the widget and store it in a map | ||
// set display for the active route | ||
|
||
// we may want to convert this to a widget for that purpose, to manage state? | ||
if (debug) { | ||
return ( | ||
<div key={JSON.stringify(activeRoute)}> | ||
<pre>{JSON.stringify(activeRoute, null, 2)}</pre> | ||
<pre>{JSON.stringify(props, null, 2)}</pre> | ||
</div> | ||
); | ||
} else { | ||
return ( | ||
<Content key={param + JSON.stringify(activeRoute)}> | ||
<Widget | ||
src={activeRoute.path} | ||
props={activeRoute.init} | ||
loading={<div style={{ height: "100%", width: "100%" }} />} | ||
/> | ||
</Content> | ||
); | ||
} | ||
} | ||
|
||
return { Router }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
return ( | ||
<div className="header"> | ||
<h1>everything</h1> | ||
<h3>bos-workspace</h3> | ||
</div> | ||
); |
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,20 @@ | ||
const pages = { | ||
home: { | ||
path: "${config_account}/widget/home", | ||
blockHeight: "final", | ||
init: { | ||
name: "Home", | ||
}, | ||
default: true, | ||
}, | ||
}; | ||
|
||
return ( | ||
<div className="sidebar"> | ||
{Object.keys(pages).map((pageKey) => ( | ||
<button className="button" key={pageKey}> | ||
{pages[pageKey].init.name} | ||
</button> | ||
))} | ||
</div> | ||
); |
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