modular-arch
modular-arch is the foundation of the Core framework. It defines the core types for building modular applications: Feature (via the Module type), Screen, Service, Layout, and Shell.
Source: frontend/packages/core/modular-arch/
Feature (Module type)
A Feature is defined using the Module<ScreenParamList> type. It is the primary building block — a cohesive, declarative unit of functionality contributed to an application.
import type { Module } from "@alaneu/modular-arch";
type MyScreens = {
admins: { id: string };
adminDetail: { adminId: string };
};
export const myFeature: Module<MyScreens> = {
name: "my-feature",
// Other features this one depends on
dependencies: [otherFeature],
// Singletons that manage state and logic
services: [myService],
// Screens contributed to the host app's navigation
screens: {
admins: {
path: "admins",
screen: AdminsScreen,
when: "logged-in",
},
adminDetail: {
path: "admins/:adminId",
screen: AdminDetailScreen,
when: "logged-in",
},
},
// i18n translations scoped to a namespace
translations: {
namespace: "myFeature",
resources: {
fr: require("./translations/fr.json"),
en: require("./translations/en.json"),
},
},
// Called when the feature is registered
init: ({ addDisposer }) => {
const cleanup = subscribeToSomething();
addDisposer(cleanup);
},
// Called when the feature is unregistered
dispose: () => {},
};
Module properties
| Property | Type | Required | Description |
|---|---|---|---|
dependencies | Module<any>[] | No | Dependencies of the module. Dependencies are registered before the module itself and disposed after the module. Services exposed by dependencies are always invoked before services exposed by the module. |
dispose | () => void | No | Called when the module is unregistered (either when <WithModules> is unmounted or with moduleManager.unregisterModule()). It can be used to clean up configurations (e.g. unregistering suggestion cards in the Clinic) |
init | (params: ModuleInitParams) => ModuleDisposer | void | No | Called when the module is registered (either with <WithModules> or moduleManager.registerModule()). It can be used to configure dependencies (e.g. registering suggestion cards in the Clinic). It can return a disposer function that will be called when the module is unregistered (before the module.dispose() function). |
linkHandlers | LinkHandler<ScreenParamList>[] | No | Link handlers provided by the module. They are used to handle deep links (hyperlinks or push notifications) and redirect to a screen of the module or invoke some logic. |
name | string | Yes | Name of the module. Used for logging. |
screens | Screens<ScreenParamList> | No | Screens provided by the module. These screens will be registered to the logged-in navigation stack of the host application/ |
services | Service[] | No | Services provided by the module. They are are singletons implementing the logic of the module. They are initialized when the module is registered and disposed when the module is unregistered. |
translations | { namespace: string; resources: unknown } | No | Translations provided by the module. Each module provides translations for a specific namespace. |
useInit | () => void | No | Hook version of init(), for setup that needs React hooks or contexts. Prefer init() when possible: it runs on registration and works outside React. init() is still called when both are provided. Unlike init(), it is only called while the user is logged in: the host app mounts `<ModulesInitHooks />` inside its innermost logged-in provider, so it can read every root context. It unmounts on logout. Clean up with the hooks' own mechanisms (e.g. a `useEffect` cleanup). This will likely be removed in the future. |
ShellModule
When a feature targets a specific product shell, use ShellModule<T> to tag it with a __moduleType discriminator:
import type { ShellModule } from "@alaneu/modular-arch";
// Base type for modules targeting the admin dashboard shell
type DashboardModule = ShellModule<"admindashboard">;
export const myDashboardFeature: DashboardModule = {
__moduleType: "admindashboard",
name: "my-dashboard-feature",
screens: {/* ... */},
};
The __moduleType tag lets shells filter and collect modules meant for them. Shells declare which ShellModule type they accept, and return one standard Module the app registers (see product shells).
Product shells can extend ShellModule with extra fields. For example, CustomerDashboardModule adds optional menuItems:
import type { CustomerDashboardModule } from "@alan-eu/global-customer-dashboard/module";
export const myDashboardFeature: CustomerDashboardModule = {
__moduleType: "admindashboard",
name: "my-dashboard-feature",
screens: {/* ... */},
menuItems: [{ relativePath: "my-page", iconVisual: "MyIcon", labelKey: "myFeature:menuLabel" }],
};
Screen
A Screen is a view mapped to a route. It can be a plain component or a component associated with a Layout.
// Plain screen
{
path: "admins",
screen: AdminsScreen,
when: "logged-in",
}
// Screen with a layout
{
path: "admins",
screen: {
layout: CustomerDashboardLayout,
props: {
children: <AdminsScreen />,
title: () => tString("admin_plural"),
},
},
}
Screen options
| Property | Description |
|---|---|
path | Route path (relative to the parent) |
screen | Component or { layout, props } |
screens | Nested child screens |
when | Visibility guard: "always", "logged-in", "logged-out", "root" |
checkCapabilities | Route guard based on capability flags |
Service
A Service is a singleton that manages logic and state. It follows the module lifecycle.
import { Service } from "@alaneu/modular-arch";
class MyService extends Service {
// Called when the owning module is registered
init() {}
// Called after all modules' init() have run
postInit() {}
// Refresh data at specific moments
refresh(when: "appLaunch" | "appForeground" | "login" | "manual") {}
// App lifecycle
appWillEnterForeground() {}
appDidEnterBackground() {}
didLogin() {}
willLogout() {}
didLogout() {}
// Called when the owning module is unregistered
dispose() {}
}
Services can declare persistence to survive app restarts:
class MyService extends Service {
persist = {
storageKey: "my-service",
whitelist: ["someProperty"],
};
}
Layout
See the dedicated Layouts page.
Navigation hooks
modular-arch exports navigation hooks usable in any screen:
| Hook | Purpose |
|---|---|
useNavigation() | Navigate programmatically |
useParams() | Read route parameters |
useSearchParams() | Read/write query string |
useLinkBuilder() | Build links to named screens |
useIsActiveScreen() | Check if a screen is active |
<Link> | Declarative navigation component |