Skip to content

Commit ced4f0c

Browse files
committed
StackContainer refactor analagous to TabsContainer
also small change in TabsContainer
1 parent d76cfbb commit ced4f0c

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

apps/src/shared/gamma/containers/stack/StackContainer.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ export function StackContainer({ routeConfigs }: StackContainerProps) {
6262
return (
6363
<Stack.Host ref={hostRef}>
6464
{stackNavState.stack.map(
65-
({ Component, options: { headerConfig, ...options }, activityMode, routeKey }) => {
65+
({
66+
options: { headerConfig, ...options },
67+
activityMode,
68+
routeKey,
69+
name,
70+
}) => {
6671
const stackNavigationContext: StackNavigationContextPayload = {
6772
routeKey,
6873
routeOptions: { ...options },
@@ -73,6 +78,17 @@ export function StackContainer({ routeConfigs }: StackContainerProps) {
7378
setRouteOptions: navMethods.setRouteOptions,
7479
};
7580

81+
const matchingConfig = routeConfigs.find(
82+
config => config.name === name,
83+
);
84+
if (!matchingConfig) {
85+
throw new Error(
86+
`[Stack] No config matches the "${name}" route name`,
87+
);
88+
}
89+
90+
const Component = matchingConfig.Component;
91+
7692
return (
7793
<Stack.Screen
7894
key={routeKey}

apps/src/shared/gamma/containers/stack/StackContainer.types.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type StackRouteConfig = {
2222
options: StackRouteOptions;
2323
};
2424

25-
export type StackRoute = StackRouteConfig & {
25+
export type StackRoute = Omit<StackRouteConfig, 'Component'> & {
2626
activityMode: StackScreenProps['activityMode'];
2727
routeKey: StackScreenProps['screenKey'];
2828
isMarkedForDismissal: Boolean; // whether this route is during or after dismissal process

apps/src/shared/gamma/containers/stack/reducer.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ function navigationActionPushHandler(
8080
const stack = state.stack;
8181
const renderedRouteIndex = stack.findIndex(
8282
route =>
83-
route.name === action.routeName && route.activityMode === 'detached' && !route.isMarkedForDismissal,
83+
route.name === action.routeName &&
84+
route.activityMode === 'detached' &&
85+
!route.isMarkedForDismissal,
8486
);
8587

8688
if (renderedRouteIndex !== NOT_FOUND_INDEX) {
@@ -161,10 +163,14 @@ function navigationActionPopHandler(
161163
}
162164

163165
// Pop operation on not-top screen is forbidden and might crash.
164-
const topAttachedRouteIndex = state.stack.findLastIndex(r => r.activityMode === 'attached');
166+
const topAttachedRouteIndex = state.stack.findLastIndex(
167+
r => r.activityMode === 'attached',
168+
);
165169

166170
if (topAttachedRouteIndex > routeIndex) {
167-
console.warn(`[Stack] Can not perform pop action on route: ${action.routeKey} - not a top screen`);
171+
console.warn(
172+
`[Stack] Can not perform pop action on route: ${action.routeKey} - not a top screen`,
173+
);
168174
return state;
169175
}
170176

@@ -270,8 +276,10 @@ function createRouteFromConfig(
270276
config: StackRouteConfig,
271277
activityMode: StackScreenActivityMode = 'detached',
272278
): StackRoute {
279+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
280+
const { Component, ...rest } = config;
273281
return {
274-
...config,
282+
...rest,
275283
activityMode,
276284
routeKey: generateRouteKeyForRouteName(config.name),
277285
isMarkedForDismissal: false,

apps/src/shared/gamma/containers/tabs/TabsContainer.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ import { TabsContainerItem } from './TabsContainerItem';
2525
export function TabsContainer(props: TabsContainerProps) {
2626
RNSLog.info('TabsContainer render');
2727

28-
const {
29-
routeConfigs,
30-
defaultRouteName,
31-
onTabSelected,
32-
...restProps
33-
} = props;
28+
const { routeConfigs, defaultRouteName, onTabSelected, ...restProps } = props;
3429

3530
useSanitizeRouteConfigs(routeConfigs);
3631

@@ -85,11 +80,11 @@ export function TabsContainer(props: TabsContainerProps) {
8580
route.routeKey === tabsNavState.suggestedState.selectedRouteKey;
8681

8782
const matchingConfig = routeConfigs.find(
88-
config => config.name === route.routeKey,
83+
config => config.name === route.name,
8984
);
9085
if (!matchingConfig) {
9186
throw new Error(
92-
`[Tabs] None config matches the "${route.routeKey}" routeKey`,
87+
`[Tabs] No config matches the "${route.name}" route name`,
9388
);
9489
}
9590

@@ -138,7 +133,9 @@ function useSanitizeRouteConfigs(routeConfigs: TabRouteConfig[]) {
138133
}
139134
}
140135

141-
function useTabsNavigationMethods(dispatch: React.Dispatch<TabsNavigationAction>): TabsNavigationMethods {
136+
function useTabsNavigationMethods(
137+
dispatch: React.Dispatch<TabsNavigationAction>,
138+
): TabsNavigationMethods {
142139
const setRouteOptions = React.useCallback(
143140
(routeKey: string, options: Partial<TabRouteOptions>) => {
144141
dispatch({ type: 'set-options', routeKey, options });
@@ -154,9 +151,11 @@ function useTabsNavigationMethods(dispatch: React.Dispatch<TabsNavigationAction>
154151
[dispatch],
155152
);
156153

157-
158-
return React.useMemo(() => ({
159-
setRouteOptions,
160-
selectTab
161-
}), [setRouteOptions, selectTab]);
154+
return React.useMemo(
155+
() => ({
156+
setRouteOptions,
157+
selectTab,
158+
}),
159+
[setRouteOptions, selectTab],
160+
);
162161
}

0 commit comments

Comments
 (0)