Skip to content

Commit

Permalink
Merge pull request #10 from wpdas/feature/new-features
Browse files Browse the repository at this point in the history
feature: new features
  • Loading branch information
wpdas authored Apr 4, 2024
2 parents 2c27dc9 + 890987f commit ce2f362
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 87 deletions.
1 change: 1 addition & 0 deletions gateway/config/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const flags = {
process.env.BOS_LOADER_WS || config.bosLoaderWs || "ws://127.0.0.1:4040",
enableHotReload:
process.env.ENABLE_HOT_RELOAD ?? config.enableHotReload ?? true,
hideAlemBar: config.hideAlemBar || false,
};
23 changes: 0 additions & 23 deletions gateway/public/dev_index.html_

This file was deleted.

3 changes: 2 additions & 1 deletion gateway/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { useHashRouterLegacy } from "./useHashRouterLegacy";
import { useAuth } from "./useAuth";
import { NavigationWrapper } from "./navigation/NavigationWrapper";
import { flags } from "../config/flags";

function Viewer({ widgetSrc, code }) {
const [widgetProps, setWidgetProps] = useState({});
Expand Down Expand Up @@ -123,7 +124,7 @@ function App() {
path="*"
element={
<>
<NavigationWrapper {...passProps} />
{!flags.hideAlemBar && <NavigationWrapper {...passProps} />}
<Viewer {...passProps} />
</>
}
Expand Down
14 changes: 14 additions & 0 deletions lib/actions/applyOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { read_alem_config } = require("../config");

// Apply options provided by alem.config.json
const applyOptions = (bundle) => {
const config = read_alem_config();
bundle = bundle.replace(
'":::KEEP_ROUTE:::"',
config.options?.keepRoute || false,
);

return bundle;
};

module.exports = applyOptions;
8 changes: 4 additions & 4 deletions lib/actions/compilerOptions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require("path");
const { read_bos_config } = require("../config");
const { read_alem_config } = require("../config");

/**
* Trata as configurações do compilador na qual o usuário pode definir os valores.
* Ver arquivo bos.config.json -> compilerOptions
* Ver arquivo alem.config.json -> compilerOptions
* @param {*} config
* @returns
*/
Expand Down Expand Up @@ -34,7 +34,7 @@ function getConfiguredPaths(config) {
}

const compilerOptions = () => {
const config = read_bos_config();
const config = read_alem_config();
const paths = getConfiguredPaths(config);

return {
Expand All @@ -58,7 +58,7 @@ const replacePathAlias = (path) => {
/**
* Check if path is using path alias
*
* ex: @app/foo/bar => return true because of "@app" (if available inside bos.config.json -> compilerOptions)
* ex: @app/foo/bar => return true because of "@app" (if available inside alem.config.json -> compilerOptions)
* @param {string} path
* @returns
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/saveFinalBundleFile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require("path");
const fs = require("fs");
const { read_bos_config } = require("../config");
const { read_alem_config } = require("../config");

// Save final bundle file
// Note: must save inside a ./src folder. This is the only folder bos-clir-rs recognizes
const saveFinalBundleFile = (bundleContent) => {
const config = read_bos_config();
const config = read_alem_config();
const finalFileName = config.isIndex
? "Index"
: config.name.replaceAll(" ", "-").toLowerCase();
Expand Down
19 changes: 13 additions & 6 deletions lib/alem-vm/alem-vm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type LinkProps = {
style?: React.CSSProperties;
onClick?: () => void;
children?: JSX.Element | JSX.Element[] | string | number;
params?: Record<string, any>;
};

type URLRouterProps = {
Expand Down Expand Up @@ -92,7 +93,10 @@ export type RouteType = "URLBased" | "ContentBased";
*
* This is NOT going to update the URL path.
*/
export declare const navigate: (routePath: string) => void;
export declare const navigate: (
routePath: string,
params?: Record<string, string>,
) => void;

/**
* Create Route child
Expand Down Expand Up @@ -127,19 +131,22 @@ export declare const getLocation: () => {
isRoutesReady: boolean;
};

/**
* Use Routes Context props. This can be useful if you'd like to perform some side effect whenever some context data changes.
* @returns
*/
export declare const useRoutes: () => {
export type UseRoutesProps = {
routesInitialized: boolean;
activeRoute: string;
routeParameterName: string;
routes: string[];
routeType: string;
routeBlocked: boolean;
routeParams: Record<string, any>;
};

/**
* Use Routes Context props. This can be useful if you'd like to perform some side effect whenever some context data changes.
* @returns
*/
export declare const useRoutes: () => UseRoutesProps;

// ======= Context =======

/**
Expand Down
44 changes: 26 additions & 18 deletions lib/alem-vm/components/AppIndexer.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
return (
<AlemTheme>
{iframeModulesCode && (
<iframe
style={{ height: 0, width: 0 }}
srcDoc={iframeModulesCode}
message={"init modules"}
onMessage={(message) => {
console.log("Message:", message);
}}
const AlemApp = useMemo(() => {
if (!props.alem.ready) {
return "";
}

return (
<AlemTheme>
{iframeModulesCode && (
<iframe
style={{ height: 0, width: 0 }}
srcDoc={iframeModulesCode}
message={"init modules"}
onMessage={(message) => {
console.log("Message:", message);
}}
/>
)}
<Widget
loading=" "
code={props.alem.componentsCode.App}
props={{ alem: props.alem }}
/>
)}
<Widget
loading=" "
code={props.alem.componentsCode.App}
props={{ alem: props.alem }}
/>
</AlemTheme>
);
</AlemTheme>
);
}, [props.alem.ready, props.alem.alemExternalStylesBody]);

return AlemApp;
14 changes: 12 additions & 2 deletions lib/alem-vm/importable/RouteLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LinkProps, navigate, useContext } from "../alem-vm";
*/
export const RouteLink = ({
to,
params,
label,
className,
style,
Expand All @@ -24,17 +25,26 @@ export const RouteLink = ({
}

if (routeContext.routeType === "ContentBased") {
navigate(to);
navigate(to, params);
}
};

if (routeContext.routeType === "URLBased") {
let strParams = "";

// Process params
if (params) {
Object.keys(params).forEach((paramKey) => {
strParams += `&${paramKey}=${params[paramKey]}`;
});
}

return (
<a
onClick={onClickHandler}
className={className}
style={{ cursor: "pointer", textDecoration: "none", ...style }}
href={`?${routeContext.routeParameterName || "path"}=${to}`}
href={`?${routeContext.routeParameterName || "path"}=${to}${strParams}`}
>
{label || children}
</a>
Expand Down
7 changes: 6 additions & 1 deletion lib/alem-vm/importable/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route, RouteType, useEffect } from "../alem-vm";
import { Route, RouteType, Storage, useEffect } from "../alem-vm";

type RouterProps = {
routes: Route[];
Expand Down Expand Up @@ -101,6 +101,11 @@ const Router = (props: RouterProps) => {
: routes[0].path;
}

// Checa se o config.keepRoute esta ativado e se tiver, se tem uma rota salva
if (alem.keepRoute && type === "ContentBased") {
_activeRoute = Storage.privateGet("alem::keep-route");
}

// Se nenhuma rota está ativa, define o primeiro item das rotas como o ativo
if (!_activeRoute) {
_activeRoute = routes[0].path;
Expand Down
16 changes: 15 additions & 1 deletion lib/alem-vm/importable/RouterProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { RouteType, createContext, useContext } from "../alem-vm";
import {
RouteType,
Storage,
createContext,
props,
useContext,
} from "../alem-vm";

const ALEM_ROUTES_CONTEXT_KEY = "alemRoutes";

Expand All @@ -25,6 +31,7 @@ const RouterProvider = () => {
// ==================================== Routes ====================================
routesInitialized: false,
activeRoute: "",
routeParams: {},
routeParameterName: "path",
routes: [] as string[],
routeType: "URLBased", // URLBased | ContentBased
Expand All @@ -50,14 +57,21 @@ const RouterProvider = () => {
routeType?: RouteType;
activeRoute?: string;
routeBlocked?: boolean;
routeParams?: Record<string, any>;
}) => {
updateAlemRoutesState({
routes: routeProps.routes || alemRoutesState().routes,
routeType: routeProps.routeType || alemRoutesState().routeType,
activeRoute: routeProps.activeRoute || alemRoutesState().activeRoute,
routeBlocked: routeProps.routeBlocked || alemRoutesState().routeBlocked,
routeParams: routeProps.routeParams || alemRoutesState().routeParams,
routesInitialized: true,
});

// If config.keepRoute is activated, store the current route to be used later
if (props.alem.keepRoute && routeProps.activeRoute) {
Storage.privateSet("alem::keep-route", routeProps.activeRoute);
}
},
});
};
Expand Down
8 changes: 6 additions & 2 deletions lib/alem-vm/importable/navigate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from "../alem-vm";

const navigate = (route: string) => {
const navigate = (route: string, params: Record<string, any>) => {
const routeContext = useContext<any>("alemRoutes");
if (!routeContext) {
console.error("navigate is being used without Router on top of it.");
Expand All @@ -11,7 +11,11 @@ const navigate = (route: string) => {
// Isso se deve devido a quando se altera um state, o componente onde ele está é
// renderizado novamente. Ou seja, o Router também vai ser renderizado novamente
// e as propriedades precisam se manter, caso contrário, voltam para o estado inicial.
routeContext.updateRouteParameters({ ...routeContext, activeRoute: route });
routeContext.updateRouteParameters({
...routeContext,
activeRoute: route,
routeParams: params || {},
});
}
};

Expand Down
4 changes: 3 additions & 1 deletion lib/alem-vm/importable/useContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const useContext = <D>(contextKey: string) => {

const wasContextInitialized = props[contextKey].initialized;
if (!wasContextInitialized) {
console.error(`Context "${contextKey}" not found.`);
// if (props.alem.isDevelopment) {
// console.warn(`Context "${contextKey}" not ready or not found.`);
// }
return {};
}
const contextKeys: string[] = props[contextKey].keys;
Expand Down
23 changes: 12 additions & 11 deletions lib/alem-vm/importable/useRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { useContext } from "../alem-vm";

type UseRoutesProps = {
routesInitialized: boolean;
activeRoute: string;
routeParameterName: string;
routes: string[];
routeType: string;
routeBlocked: boolean;
};
import { UseRoutesProps, useContext } from "../alem-vm";

/**
* Use Routes Context props. This can be useful if you'd like to perform some side effect whenever some context data changes.
Expand All @@ -21,7 +12,17 @@ const useRoutes = () => {
if (!contextData) {
console.error("useRoutes: You need to call `RouterProvider()` first.");
}
return contextData!;

const data = {
routesInitialized: contextData.routesInitialized,
activeRoute: contextData.activeRoute,
routeParameterName: contextData.routeParameterName,
routes: contextData.routes,
routeType: contextData.routeType,
routeParams: contextData.routeParams,
};

return data!;
};

export default useRoutes;
Loading

0 comments on commit ce2f362

Please sign in to comment.