feat(smart-app): implement complete mobile app MVP

- App.tsx: full navigation (Auth stack + Main tabs with 5 screens)
- Auth: LoginScreen, RegisterScreen, ForgotPasswordScreen
- HomeScreen: dashboard with IoT metrics, weather widget, alerts, quick actions, sensors
- MapScreen: interactive map with layer toggles (6 layers)
- MarketplaceScreen: categories (6), products (5), search
- ChatScreen: AI chat with quick prompts (4), bot responses
- ProfileScreen: user info, stats, menu (9 items), logout
- AlertsScreen: alert list with severity, acknowledge
- SensorsScreen: sensor list with type filters (6 types), search
- ZonesScreen: zone cards with stats
- SettingsScreen: language picker (FR/EN/ES/DE), privacy, about
- Stores: iotStore (sensors, zones, alerts), notificationStore, uiStore + i18n
- Hooks: useSensors, useAlerts, useNotifications, useLocation
- Components: Card, Button, LoadingSpinner, ErrorBoundary, Header
- Services: iotService, notificationService (with axios API client)
- Utils: formatters (temp, AQI, noise, dates), validators (email, password, IBAN)
- Theme: colors.ts with full design system (Blue Ocean palette)
- Ditto: fixed MongoDB connection, new JWT secrets, official gateway image
This commit is contained in:
Eric FELIXINE
2026-06-01 18:00:35 -04:00
parent 08ca495bde
commit e30ae8ed09
35578 changed files with 3703534 additions and 43 deletions

View File

@@ -0,0 +1,81 @@
import { nanoid } from 'nanoid/non-secure';
import type {
CommonNavigationAction,
NavigationState,
PartialState,
} from './types';
/**
* Base router object that can be used when writing custom routers.
* This provides few helper methods to handle common actions such as `RESET`.
*/
const BaseRouter = {
getStateForAction<State extends NavigationState>(
state: State,
action: CommonNavigationAction
): State | PartialState<State> | null {
switch (action.type) {
case 'SET_PARAMS': {
const index = action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (index === -1) {
return null;
}
return {
...state,
routes: state.routes.map((r, i) =>
i === index
? { ...r, params: { ...r.params, ...action.payload.params } }
: r
),
};
}
case 'RESET': {
const nextState = action.payload as State | PartialState<State>;
if (
nextState.routes.length === 0 ||
nextState.routes.some(
(route: { name: string }) => !state.routeNames.includes(route.name)
)
) {
return null;
}
if (nextState.stale === false) {
if (
state.routeNames.length !== nextState.routeNames.length ||
nextState.routeNames.some(
(name) => !state.routeNames.includes(name)
)
) {
return null;
}
return {
...nextState,
routes: nextState.routes.map((route) =>
route.key ? route : { ...route, key: `${route.name}-${nanoid()}` }
),
};
}
return nextState;
}
default:
return null;
}
},
shouldActionChangeFocus(action: CommonNavigationAction) {
return action.type === 'NAVIGATE';
},
};
export default BaseRouter;

View File

@@ -0,0 +1,89 @@
import type { NavigationState, PartialState, Route } from './types';
type ResetState =
| PartialState<NavigationState>
| NavigationState
| (Omit<NavigationState, 'routes'> & {
routes: Omit<Route<string>, 'key'>[];
});
export type Action =
| {
type: 'GO_BACK';
source?: string;
target?: string;
}
| {
type: 'NAVIGATE';
payload:
| {
key: string;
name?: undefined;
params?: object;
path?: string;
merge?: boolean;
}
| {
name: string;
key?: string;
params?: object;
path?: string;
merge?: boolean;
};
source?: string;
target?: string;
}
| {
type: 'RESET';
payload: ResetState | undefined;
source?: string;
target?: string;
}
| {
type: 'SET_PARAMS';
payload: { params?: object };
source?: string;
target?: string;
};
export function goBack(): Action {
return { type: 'GO_BACK' };
}
export function navigate(
options:
| { key: string; params?: object; path?: string; merge?: boolean }
| {
name: string;
key?: string;
params?: object;
path?: string;
merge?: boolean;
}
): Action;
// eslint-disable-next-line no-redeclare
export function navigate(name: string, params?: object): Action;
// eslint-disable-next-line no-redeclare
export function navigate(...args: any): Action {
if (typeof args[0] === 'string') {
return { type: 'NAVIGATE', payload: { name: args[0], params: args[1] } };
} else {
const payload = args[0] || {};
if (!payload.hasOwnProperty('key') && !payload.hasOwnProperty('name')) {
throw new Error(
'You need to specify name or key when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.'
);
}
return { type: 'NAVIGATE', payload };
}
}
export function reset(state: ResetState | undefined): Action {
return { type: 'RESET', payload: state };
}
export function setParams(params: object): Action {
return { type: 'SET_PARAMS', payload: { params } };
}

View File

@@ -0,0 +1,247 @@
import { nanoid } from 'nanoid/non-secure';
import TabRouter, {
TabActionHelpers,
TabActions,
TabActionType,
TabNavigationState,
TabRouterOptions,
} from './TabRouter';
import type {
CommonNavigationAction,
ParamListBase,
PartialState,
Router,
} from './types';
export type DrawerStatus = 'open' | 'closed';
export type DrawerActionType =
| TabActionType
| {
type: 'OPEN_DRAWER' | 'CLOSE_DRAWER' | 'TOGGLE_DRAWER';
source?: string;
target?: string;
};
export type DrawerRouterOptions = TabRouterOptions & {
defaultStatus?: DrawerStatus;
};
export type DrawerNavigationState<ParamList extends ParamListBase> = Omit<
TabNavigationState<ParamList>,
'type' | 'history'
> & {
/**
* Type of the router, in this case, it's drawer.
*/
type: 'drawer';
/**
* Default status of the drawer.
*/
default: DrawerStatus;
/**
* List of previously visited route keys and drawer open status.
*/
history: (
| { type: 'route'; key: string }
| { type: 'drawer'; status: DrawerStatus }
)[];
};
export type DrawerActionHelpers<ParamList extends ParamListBase> =
TabActionHelpers<ParamList> & {
/**
* Open the drawer sidebar.
*/
openDrawer(): void;
/**
* Close the drawer sidebar.
*/
closeDrawer(): void;
/**
* Open the drawer sidebar if closed, or close if opened.
*/
toggleDrawer(): void;
};
export const DrawerActions = {
...TabActions,
openDrawer(): DrawerActionType {
return { type: 'OPEN_DRAWER' };
},
closeDrawer(): DrawerActionType {
return { type: 'CLOSE_DRAWER' };
},
toggleDrawer(): DrawerActionType {
return { type: 'TOGGLE_DRAWER' };
},
};
export default function DrawerRouter({
defaultStatus = 'closed',
...rest
}: DrawerRouterOptions): Router<
DrawerNavigationState<ParamListBase>,
DrawerActionType | CommonNavigationAction
> {
const router = TabRouter(rest) as unknown as Router<
DrawerNavigationState<ParamListBase>,
TabActionType | CommonNavigationAction
>;
const isDrawerInHistory = (
state:
| DrawerNavigationState<ParamListBase>
| PartialState<DrawerNavigationState<ParamListBase>>
) => Boolean(state.history?.some((it) => it.type === 'drawer'));
const addDrawerToHistory = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (isDrawerInHistory(state)) {
return state;
}
return {
...state,
history: [
...state.history,
{
type: 'drawer',
status: defaultStatus === 'open' ? 'closed' : 'open',
},
],
};
};
const removeDrawerFromHistory = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (!isDrawerInHistory(state)) {
return state;
}
return {
...state,
history: state.history.filter((it) => it.type !== 'drawer'),
};
};
const openDrawer = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (defaultStatus === 'open') {
return removeDrawerFromHistory(state);
}
return addDrawerToHistory(state);
};
const closeDrawer = (
state: DrawerNavigationState<ParamListBase>
): DrawerNavigationState<ParamListBase> => {
if (defaultStatus === 'open') {
return addDrawerToHistory(state);
}
return removeDrawerFromHistory(state);
};
return {
...router,
type: 'drawer',
getInitialState({ routeNames, routeParamList, routeGetIdList }) {
const state = router.getInitialState({
routeNames,
routeParamList,
routeGetIdList,
});
return {
...state,
default: defaultStatus,
stale: false,
type: 'drawer',
key: `drawer-${nanoid()}`,
};
},
getRehydratedState(
partialState,
{ routeNames, routeParamList, routeGetIdList }
) {
if (partialState.stale === false) {
return partialState;
}
let state = router.getRehydratedState(partialState, {
routeNames,
routeParamList,
routeGetIdList,
});
if (isDrawerInHistory(partialState)) {
// Re-sync the drawer entry in history to correct it if it was wrong
state = removeDrawerFromHistory(state);
state = addDrawerToHistory(state);
}
return {
...state,
default: defaultStatus,
type: 'drawer',
key: `drawer-${nanoid()}`,
};
},
getStateForRouteFocus(state, key) {
const result = router.getStateForRouteFocus(state, key);
return closeDrawer(result);
},
getStateForAction(state, action, options) {
switch (action.type) {
case 'OPEN_DRAWER':
return openDrawer(state);
case 'CLOSE_DRAWER':
return closeDrawer(state);
case 'TOGGLE_DRAWER':
if (isDrawerInHistory(state)) {
return removeDrawerFromHistory(state);
}
return addDrawerToHistory(state);
case 'JUMP_TO':
case 'NAVIGATE': {
const result = router.getStateForAction(state, action, options);
if (result != null && result.index !== state.index) {
return closeDrawer(result as DrawerNavigationState<ParamListBase>);
}
return result;
}
case 'GO_BACK':
if (isDrawerInHistory(state)) {
return removeDrawerFromHistory(state);
}
return router.getStateForAction(state, action, options);
default:
return router.getStateForAction(state, action, options);
}
},
actionCreators: DrawerActions,
};
}

View File

@@ -0,0 +1,495 @@
import { nanoid } from 'nanoid/non-secure';
import BaseRouter from './BaseRouter';
import type {
CommonNavigationAction,
DefaultRouterOptions,
NavigationState,
ParamListBase,
Route,
Router,
} from './types';
export type StackActionType =
| {
type: 'REPLACE';
payload: { name: string; key?: string | undefined; params?: object };
source?: string;
target?: string;
}
| {
type: 'PUSH';
payload: { name: string; params?: object };
source?: string;
target?: string;
}
| {
type: 'POP';
payload: { count: number };
source?: string;
target?: string;
}
| {
type: 'POP_TO_TOP';
source?: string;
target?: string;
};
export type StackRouterOptions = DefaultRouterOptions;
export type StackNavigationState<ParamList extends ParamListBase> =
NavigationState<ParamList> & {
/**
* Type of the router, in this case, it's stack.
*/
type: 'stack';
};
export type StackActionHelpers<ParamList extends ParamListBase> = {
/**
* Replace the current route with a new one.
*
* @param name Route name of the new route.
* @param [params] Params object for the new route.
*/
replace<RouteName extends keyof ParamList>(
...args: undefined extends ParamList[RouteName]
? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
): void;
/**
* Push a new screen onto the stack.
*
* @param name Name of the route for the tab.
* @param [params] Params object for the route.
*/
push<RouteName extends keyof ParamList>(
...args: undefined extends ParamList[RouteName]
? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
): void;
/**
* Pop a screen from the stack.
*/
pop(count?: number): void;
/**
* Pop to the first route in the stack, dismissing all other screens.
*/
popToTop(): void;
};
export const StackActions = {
replace(name: string, params?: object): StackActionType {
return { type: 'REPLACE', payload: { name, params } };
},
push(name: string, params?: object): StackActionType {
return { type: 'PUSH', payload: { name, params } };
},
pop(count: number = 1): StackActionType {
return { type: 'POP', payload: { count } };
},
popToTop(): StackActionType {
return { type: 'POP_TO_TOP' };
},
};
export default function StackRouter(options: StackRouterOptions) {
const router: Router<
StackNavigationState<ParamListBase>,
CommonNavigationAction | StackActionType
> = {
...BaseRouter,
type: 'stack',
getInitialState({ routeNames, routeParamList }) {
const initialRouteName =
options.initialRouteName !== undefined &&
routeNames.includes(options.initialRouteName)
? options.initialRouteName
: routeNames[0];
return {
stale: false,
type: 'stack',
key: `stack-${nanoid()}`,
index: 0,
routeNames,
routes: [
{
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
},
],
};
},
getRehydratedState(partialState, { routeNames, routeParamList }) {
let state = partialState;
if (state.stale === false) {
return state;
}
const routes = state.routes
.filter((route) => routeNames.includes(route.name))
.map(
(route) =>
({
...route,
key: route.key || `${route.name}-${nanoid()}`,
params:
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
}
: route.params,
} as Route<string>)
);
if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined
? options.initialRouteName
: routeNames[0];
routes.push({
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}
return {
stale: false,
type: 'stack',
key: `stack-${nanoid()}`,
index: routes.length - 1,
routeNames,
routes,
};
},
getStateForRouteNamesChange(
state,
{ routeNames, routeParamList, routeKeyChanges }
) {
const routes = state.routes.filter(
(route) =>
routeNames.includes(route.name) &&
!routeKeyChanges.includes(route.name)
);
if (routes.length === 0) {
const initialRouteName =
options.initialRouteName !== undefined &&
routeNames.includes(options.initialRouteName)
? options.initialRouteName
: routeNames[0];
routes.push({
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
}
return {
...state,
routeNames,
routes,
index: Math.min(state.index, routes.length - 1),
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex((r) => r.key === key);
if (index === -1 || index === state.index) {
return state;
}
return {
...state,
index,
routes: state.routes.slice(0, index + 1),
};
},
getStateForAction(state, action, options) {
const { routeParamList } = options;
switch (action.type) {
case 'REPLACE': {
const index =
action.target === state.key && action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (index === -1) {
return null;
}
const { name, key, params } = action.payload;
if (!state.routeNames.includes(name)) {
return null;
}
return {
...state,
routes: state.routes.map((route, i) =>
i === index
? {
key: key !== undefined ? key : `${name}-${nanoid()}`,
name,
params:
routeParamList[name] !== undefined
? {
...routeParamList[name],
...params,
}
: params,
}
: route
),
};
}
case 'PUSH':
if (state.routeNames.includes(action.payload.name)) {
const getId = options.routeGetIdList[action.payload.name];
const id = getId?.({ params: action.payload.params });
const route = id
? state.routes.find(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
)
: undefined;
let routes: Route<string>[];
if (route) {
routes = state.routes.filter((r) => r.key !== route.key);
routes.push({
...route,
params:
routeParamList[action.payload.name] !== undefined
? {
...routeParamList[action.payload.name],
...action.payload.params,
}
: action.payload.params,
});
} else {
routes = [
...state.routes,
{
key: `${action.payload.name}-${nanoid()}`,
name: action.payload.name,
params:
routeParamList[action.payload.name] !== undefined
? {
...routeParamList[action.payload.name],
...action.payload.params,
}
: action.payload.params,
},
];
}
return {
...state,
index: routes.length - 1,
routes,
};
}
return null;
case 'POP': {
const index =
action.target === state.key && action.source
? state.routes.findIndex((r) => r.key === action.source)
: state.index;
if (index > 0) {
const count = Math.max(index - action.payload.count + 1, 1);
const routes = state.routes
.slice(0, count)
.concat(state.routes.slice(index + 1));
return {
...state,
index: routes.length - 1,
routes,
};
}
return null;
}
case 'POP_TO_TOP':
return router.getStateForAction(
state,
{
type: 'POP',
payload: { count: state.routes.length - 1 },
},
options
);
case 'NAVIGATE':
if (
action.payload.name !== undefined &&
!state.routeNames.includes(action.payload.name)
) {
return null;
}
if (action.payload.key || action.payload.name) {
// If the route already exists, navigate to that
let index = -1;
const getId =
// `getId` and `key` can't be used together
action.payload.key === undefined &&
action.payload.name !== undefined
? options.routeGetIdList[action.payload.name]
: undefined;
const id = getId?.({ params: action.payload.params });
if (id) {
index = state.routes.findIndex(
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
} else if (
(state.routes[state.index].name === action.payload.name &&
action.payload.key === undefined) ||
state.routes[state.index].key === action.payload.key
) {
index = state.index;
} else {
for (let i = state.routes.length - 1; i >= 0; i--) {
if (
(state.routes[i].name === action.payload.name &&
action.payload.key === undefined) ||
state.routes[i].key === action.payload.key
) {
index = i;
break;
}
}
}
if (
index === -1 &&
action.payload.key &&
action.payload.name === undefined
) {
return null;
}
if (index === -1 && action.payload.name !== undefined) {
const routes = [
...state.routes,
{
key:
action.payload.key ?? `${action.payload.name}-${nanoid()}`,
name: action.payload.name,
path: action.payload.path,
params:
routeParamList[action.payload.name] !== undefined
? {
...routeParamList[action.payload.name],
...action.payload.params,
}
: action.payload.params,
},
];
return {
...state,
routes,
index: routes.length - 1,
};
}
const route = state.routes[index];
let params;
if (action.payload.merge) {
params =
action.payload.params !== undefined ||
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params =
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...action.payload.params,
}
: action.payload.params;
}
return {
...state,
index,
routes: [
...state.routes.slice(0, index),
params !== route.params ||
(action.payload.path && action.payload.path !== route.path)
? {
...route,
path: action.payload.path ?? route.path,
params,
}
: state.routes[index],
],
};
}
return null;
case 'GO_BACK':
if (state.index > 0) {
return router.getStateForAction(
state,
{
type: 'POP',
payload: { count: 1 },
target: action.target,
source: action.source,
},
options
);
}
return null;
default:
return BaseRouter.getStateForAction(state, action);
}
},
actionCreators: StackActions,
};
return router;
}

View File

@@ -0,0 +1,407 @@
import { nanoid } from 'nanoid/non-secure';
import BaseRouter from './BaseRouter';
import type {
CommonNavigationAction,
DefaultRouterOptions,
NavigationState,
ParamListBase,
PartialState,
Route,
Router,
} from './types';
export type TabActionType = {
type: 'JUMP_TO';
payload: { name: string; params?: object };
source?: string;
target?: string;
};
export type BackBehavior =
| 'initialRoute'
| 'firstRoute'
| 'history'
| 'order'
| 'none';
export type TabRouterOptions = DefaultRouterOptions & {
backBehavior?: BackBehavior;
};
export type TabNavigationState<ParamList extends ParamListBase> = Omit<
NavigationState<ParamList>,
'history'
> & {
/**
* Type of the router, in this case, it's tab.
*/
type: 'tab';
/**
* List of previously visited route keys.
*/
history: { type: 'route'; key: string }[];
};
export type TabActionHelpers<ParamList extends ParamListBase> = {
/**
* Jump to an existing tab.
*
* @param name Name of the route for the tab.
* @param [params] Params object for the route.
*/
jumpTo<RouteName extends Extract<keyof ParamList, string>>(
...args: undefined extends ParamList[RouteName]
? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
: [screen: RouteName, params: ParamList[RouteName]]
): void;
};
const TYPE_ROUTE = 'route' as const;
export const TabActions = {
jumpTo(name: string, params?: object): TabActionType {
return { type: 'JUMP_TO', payload: { name, params } };
},
};
const getRouteHistory = (
routes: Route<string>[],
index: number,
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
const history = [{ type: TYPE_ROUTE, key: routes[index].key }];
let initialRouteIndex;
switch (backBehavior) {
case 'order':
for (let i = index; i > 0; i--) {
history.unshift({ type: TYPE_ROUTE, key: routes[i - 1].key });
}
break;
case 'firstRoute':
if (index !== 0) {
history.unshift({
type: TYPE_ROUTE,
key: routes[0].key,
});
}
break;
case 'initialRoute':
initialRouteIndex = routes.findIndex(
(route) => route.name === initialRouteName
);
initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
if (index !== initialRouteIndex) {
history.unshift({
type: TYPE_ROUTE,
key: routes[initialRouteIndex].key,
});
}
break;
case 'history':
// The history will fill up on navigation
break;
}
return history;
};
const changeIndex = (
state: TabNavigationState<ParamListBase>,
index: number,
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
let history;
if (backBehavior === 'history') {
const currentKey = state.routes[index].key;
history = state.history
.filter((it) => (it.type === 'route' ? it.key !== currentKey : false))
.concat({ type: TYPE_ROUTE, key: currentKey });
} else {
history = getRouteHistory(
state.routes,
index,
backBehavior,
initialRouteName
);
}
return {
...state,
index,
history,
};
};
export default function TabRouter({
initialRouteName,
backBehavior = 'firstRoute',
}: TabRouterOptions) {
const router: Router<
TabNavigationState<ParamListBase>,
TabActionType | CommonNavigationAction
> = {
...BaseRouter,
type: 'tab',
getInitialState({ routeNames, routeParamList }) {
const index =
initialRouteName !== undefined && routeNames.includes(initialRouteName)
? routeNames.indexOf(initialRouteName)
: 0;
const routes = routeNames.map((name) => ({
name,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}));
const history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
return {
stale: false,
type: 'tab',
key: `tab-${nanoid()}`,
index,
routeNames,
history,
routes,
};
},
getRehydratedState(partialState, { routeNames, routeParamList }) {
let state = partialState;
if (state.stale === false) {
return state;
}
const routes = routeNames.map((name) => {
const route = (
state as PartialState<TabNavigationState<ParamListBase>>
).routes.find((r) => r.name === name);
return {
...route,
name,
key:
route && route.name === name && route.key
? route.key
: `${name}-${nanoid()}`,
params:
routeParamList[name] !== undefined
? {
...routeParamList[name],
...(route ? route.params : undefined),
}
: route
? route.params
: undefined,
} as Route<string>;
});
const index = Math.min(
Math.max(routeNames.indexOf(state.routes[state?.index ?? 0]?.name), 0),
routes.length - 1
);
const history =
state.history?.filter((it) => routes.find((r) => r.key === it.key)) ??
[];
return changeIndex(
{
stale: false,
type: 'tab',
key: `tab-${nanoid()}`,
index,
routeNames,
history,
routes,
},
index,
backBehavior,
initialRouteName
);
},
getStateForRouteNamesChange(
state,
{ routeNames, routeParamList, routeKeyChanges }
) {
const routes = routeNames.map(
(name) =>
state.routes.find(
(r) => r.name === name && !routeKeyChanges.includes(r.name)
) || {
name,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}
);
const index = Math.max(
0,
routeNames.indexOf(state.routes[state.index].name)
);
let history = state.history.filter(
// Type will always be 'route' for tabs, but could be different in a router extending this (e.g. drawer)
(it) => it.type !== 'route' || routes.find((r) => r.key === it.key)
);
if (!history.length) {
history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
}
return {
...state,
history,
routeNames,
routes,
index,
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex((r) => r.key === key);
if (index === -1 || index === state.index) {
return state;
}
return changeIndex(state, index, backBehavior, initialRouteName);
},
getStateForAction(state, action, { routeParamList, routeGetIdList }) {
switch (action.type) {
case 'JUMP_TO':
case 'NAVIGATE': {
let index = -1;
if (action.type === 'NAVIGATE' && action.payload.key) {
index = state.routes.findIndex(
(route) => route.key === action.payload.key
);
} else {
index = state.routes.findIndex(
(route) => route.name === action.payload.name
);
}
if (index === -1) {
return null;
}
return changeIndex(
{
...state,
routes: state.routes.map((route, i) => {
if (i !== index) {
return route;
}
const getId = routeGetIdList[route.name];
const currentId = getId?.({ params: route.params });
const nextId = getId?.({ params: action.payload.params });
const key =
currentId === nextId
? route.key
: `${route.name}-${nanoid()}`;
let params;
if (
action.type === 'NAVIGATE' &&
action.payload.merge &&
currentId === nextId
) {
params =
action.payload.params !== undefined ||
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...route.params,
...action.payload.params,
}
: route.params;
} else {
params =
routeParamList[route.name] !== undefined
? {
...routeParamList[route.name],
...action.payload.params,
}
: action.payload.params;
}
const path =
action.type === 'NAVIGATE' && action.payload.path != null
? action.payload.path
: route.path;
return params !== route.params || path !== route.path
? { ...route, key, path, params }
: route;
}),
},
index,
backBehavior,
initialRouteName
);
}
case 'GO_BACK': {
if (state.history.length === 1) {
return null;
}
const previousKey = state.history[state.history.length - 2].key;
const index = state.routes.findIndex(
(route) => route.key === previousKey
);
if (index === -1) {
return null;
}
return {
...state,
history: state.history.slice(0, -1),
index,
};
}
default:
return BaseRouter.getStateForAction(state, action);
}
},
shouldActionChangeFocus(action) {
return action.type === 'NAVIGATE';
},
actionCreators: TabActions,
};
return router;
}

View File

@@ -0,0 +1,28 @@
import * as CommonActions from './CommonActions';
export { CommonActions };
export { default as BaseRouter } from './BaseRouter';
export type {
DrawerActionHelpers,
DrawerActionType,
DrawerNavigationState,
DrawerRouterOptions,
DrawerStatus,
} from './DrawerRouter';
export { DrawerActions, default as DrawerRouter } from './DrawerRouter';
export type {
StackActionHelpers,
StackActionType,
StackNavigationState,
StackRouterOptions,
} from './StackRouter';
export { StackActions, default as StackRouter } from './StackRouter';
export type {
TabActionHelpers,
TabActionType,
TabNavigationState,
TabRouterOptions,
} from './TabRouter';
export { TabActions, default as TabRouter } from './TabRouter';
export * from './types';

View File

@@ -0,0 +1,228 @@
import type * as CommonActions from './CommonActions';
export type CommonNavigationAction = CommonActions.Action;
type NavigationRoute<
ParamList extends ParamListBase,
RouteName extends keyof ParamList
> = Route<Extract<RouteName, string>, ParamList[RouteName]> & {
state?: NavigationState | PartialState<NavigationState>;
};
export type NavigationState<ParamList extends ParamListBase = ParamListBase> =
Readonly<{
/**
* Unique key for the navigation state.
*/
key: string;
/**
* Index of the currently focused route.
*/
index: number;
/**
* List of valid route names as defined in the screen components.
*/
routeNames: Extract<keyof ParamList, string>[];
/**
* Alternative entries for history.
*/
history?: unknown[];
/**
* List of rendered routes.
*/
routes: NavigationRoute<ParamList, keyof ParamList>[];
/**
* Custom type for the state, whether it's for tab, stack, drawer etc.
* During rehydration, the state will be discarded if type doesn't match with router type.
* It can also be used to detect the type of the navigator we're dealing with.
*/
type: string;
/**
* Whether the navigation state has been rehydrated.
*/
stale: false;
}>;
export type InitialState = Readonly<
Partial<Omit<NavigationState, 'stale' | 'routes'>> & {
routes: (Omit<Route<string>, 'key'> & { state?: InitialState })[];
}
>;
export type PartialRoute<R extends Route<string>> = Omit<R, 'key'> & {
key?: string;
state?: PartialState<NavigationState>;
};
export type PartialState<State extends NavigationState> = Partial<
Omit<State, 'stale' | 'routes'>
> &
Readonly<{
stale?: true;
routes: PartialRoute<Route<State['routeNames'][number]>>[];
}>;
export type Route<
RouteName extends string,
Params extends object | undefined = object | undefined
> = Readonly<{
/**
* Unique key for the route.
*/
key: string;
/**
* User-provided name for the route.
*/
name: RouteName;
/**
* Path associated with the route.
* Usually present when the screen was opened from a deep link.
*/
path?: string;
}> &
(undefined extends Params
? Readonly<{
/**
* Params for this route
*/
params?: Readonly<Params>;
}>
: Readonly<{
/**
* Params for this route
*/
params: Readonly<Params>;
}>);
export type ParamListBase = Record<string, object | undefined>;
export type NavigationAction = Readonly<{
/**
* Type of the action (e.g. `NAVIGATE`)
*/
type: string;
/**
* Additional data for the action
*/
payload?: object;
/**
* Key of the route which dispatched this action.
*/
source?: string;
/**
* Key of the navigator which should handle this action.
*/
target?: string;
}>;
export type ActionCreators<Action extends NavigationAction> = {
[key: string]: (...args: any) => Action;
};
export type DefaultRouterOptions<RouteName extends string = string> = {
/**
* Name of the route to focus by on initial render.
* If not specified, usually the first route is used.
*/
initialRouteName?: RouteName;
};
export type RouterFactory<
State extends NavigationState,
Action extends NavigationAction,
RouterOptions extends DefaultRouterOptions
> = (options: RouterOptions) => Router<State, Action>;
export type RouterConfigOptions = {
routeNames: string[];
routeParamList: ParamListBase;
routeGetIdList: Record<
string,
| ((options: { params?: Record<string, any> }) => string | undefined)
| undefined
>;
};
export type Router<
State extends NavigationState,
Action extends NavigationAction
> = {
/**
* Type of the router. Should match the `type` property in state.
* If the type doesn't match, the state will be discarded during rehydration.
*/
type: State['type'];
/**
* Initialize the navigation state.
*
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getInitialState(options: RouterConfigOptions): State;
/**
* Rehydrate the full navigation state from a given partial state.
*
* @param partialState Navigation state to rehydrate from.
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getRehydratedState(
partialState: PartialState<State> | State,
options: RouterConfigOptions
): State;
/**
* Take the current state and updated list of route names, and return a new state.
*
* @param state State object to update.
* @param options.routeNames New list of route names.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForRouteNamesChange(
state: State,
options: RouterConfigOptions & {
/**
* List of routes whose key has changed even if they still have the same name.
* This allows to remove screens declaratively.
*/
routeKeyChanges: string[];
}
): State;
/**
* Take the current state and key of a route, and return a new state with the route focused
*
* @param state State object to apply the action on.
* @param key Key of the route to focus.
*/
getStateForRouteFocus(state: State, key: string): State;
/**
* Take the current state and action, and return a new state.
* If the action cannot be handled, return `null`.
*
* @param state State object to apply the action on.
* @param action Action object to apply.
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForAction(
state: State,
action: Action,
options: RouterConfigOptions
): State | PartialState<State> | null;
/**
* Whether the action should also change focus in parent navigator
*
* @param action Action object to check.
*/
shouldActionChangeFocus(action: NavigationAction): boolean;
/**
* Action creators for the router.
*/
actionCreators?: ActionCreators<Action>;
};