Skip to content

Commit

Permalink
Convert app to use new app pattern (#239)
Browse files Browse the repository at this point in the history
* Move app to every.app pattern

* fmt

* fix merge conflict

---------

Co-authored-by: Elliot Braem <[email protected]>
  • Loading branch information
itexpert120 and elliotBraem authored Apr 10, 2024
1 parent d0552fe commit b39ae69
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 72 deletions.
2 changes: 1 addition & 1 deletion apps/builddao/widget/Proposals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ return (
</div>
</div>
</Header>
<NotificationModal />
{showNotificationModal && <NotificationModal />}
<div className="d-flex flex-column gap-4">{proposalsComponent}</div>
{!proposalId && (
<div className="d-flex justify-content-center my-4">
Expand Down
73 changes: 73 additions & 0 deletions apps/builddao/widget/Router.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { href } = VM.require("buildhub.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, ...passProps }}
loading={<div style={{ height: "100%", width: "100%" }} />}
/>
</Content>
);
}
}

return { Router };
169 changes: 102 additions & 67 deletions apps/builddao/widget/app.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,103 @@
const { page, tab, ...passProps } = props;

const { routes } = VM.require("${config_account}/widget/config.app") ?? {
routes: {},
};

const { AppLayout } = VM.require(
"${config_account}/widget/template.AppLayout",
) || {
AppLayout: () => <></>,
const config = {
theme: {
// add key values to define colors
"--main-color": "black",
"--secondary-color": "white",
background: "var(--main-color)",
color: "var(--secondary-color)",
},
layout: {
src: "devs.near/widget/Layout",
props: {
variant: "standard",
},
},
blocks: {
// these get passed to the layout and children
Header: () => (
// customize your header
<Widget
src="${config_account}/widget/components.Navbar"
props={{ routes: config.router.routes, ...passProps }}
/>
),
Footer: () => <></>, // customize your footer
},
router: {
param: "page",
routes: {
home: {
path: "${config_account}/widget/page.home",
blockHeight: "final",
init: {
name: "Home",
},
default: true,
},
feed: {
path: "${config_account}/widget/page.feed",
blockHeight: "final",
init: {
name: "Feed",
},
},
proposal: {
path: "${config_account}/widget/page.projects",
blockHeight: "final",
init: {
name: "Projects",
},
},
resources: {
path: "${config_account}/widget/page.resources",
blockHeight: "final",
init: {
name: "Resources",
},
},
library: {
path: "${config_account}/widget/page.library",
blockHeight: "final",
init: {
name: "Library",
},
},
profile: {
path: "${config_account}/widget/page.profile",
blockHeight: "final",
init: {
name: "Profile",
},
hide: true,
},
inspect: {
path: "${config_account}/widget/page.inspect",
blockHeight: "final",
init: {
name: "Inspect",
},
hide: true,
},
projects: {
path: "${config_account}/widget/page.project-feed",
blockHeight: "final",
init: {
name: "Project Feed",
},
hide: true,
},
project: {
path: "${config_account}/widget/page.project",
blockHeight: "final",
init: {
name: "Project Page",
},
hide: true,
},
},
},
};

if (!page) page = Object.keys(routes)[0] || "home";

const Root = styled.div`
--stroke-color: rgba(255, 255, 255, 0.2);
--bg-1: #000;
Expand Down Expand Up @@ -214,62 +300,11 @@ const Root = styled.div`
}
`;

function Router({ active, routes }) {
// this may be converted to a module at devs.near/widget/Router
const routeParts = active.split(".");

let currentRoute = routes;
let src = "";
let defaultProps = {};

for (let part of routeParts) {
if (currentRoute[part]) {
currentRoute = currentRoute[part];
src = currentRoute.path;

if (currentRoute.init) {
defaultProps = { ...defaultProps, ...currentRoute.init };
}
} else {
// Handle 404 or default case for unknown routes
return <p>404 Not Found</p>;
}
}

return (
<div key={active}>
<Widget
src={src}
props={{
currentPath: `/${config_account}/widget/app?page=${page}`,
page: tab,
...passProps,
...defaultProps,
}}
/>
</div>
);
}

const Container = styled.div`
display: flex;
height: 100%;
font-family: InterVariable, sans-serif;
`;

const Content = styled.div`
width: 100%;
height: 100%;
`;

return (
<Root>
<Container>
<AppLayout page={page} routes={routes} {...props}>
<Content>
<Router active={page} routes={routes} />
</Content>
</AppLayout>
</Container>
<Widget
src="${config_account}/widget/app.view"
props={{ config, ...props }}
/>
</Root>
);
Loading

0 comments on commit b39ae69

Please sign in to comment.