Skip to content

Commit 881b3f0

Browse files
committed
StackContainer refactor analagous to TabsContainer
also small change in TabsContainer
1 parent 57c90f4 commit 881b3f0

4 files changed

Lines changed: 60 additions & 46 deletions

File tree

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,40 @@ export function StackContainer({ routeConfigs }: StackContainerProps) {
6161

6262
return (
6363
<Stack.Host ref={hostRef}>
64-
{stackNavState.stack.map(
65-
({ Component, options, activityMode, routeKey }) => {
66-
const stackNavigationContext: StackNavigationContextPayload = {
67-
routeKey,
68-
routeOptions: { ...options },
69-
push: navMethods.pushAction,
70-
pop: navMethods.popAction,
71-
preload: navMethods.preloadAction,
72-
batch: navMethods.batchAction,
73-
setRouteOptions: navMethods.setRouteOptions,
74-
};
64+
{stackNavState.stack.map(({ options, activityMode, routeKey, name }) => {
65+
const stackNavigationContext: StackNavigationContextPayload = {
66+
routeKey,
67+
routeOptions: { ...options },
68+
push: navMethods.pushAction,
69+
pop: navMethods.popAction,
70+
preload: navMethods.preloadAction,
71+
batch: navMethods.batchAction,
72+
setRouteOptions: navMethods.setRouteOptions,
73+
};
7574

76-
return (
77-
<Stack.Screen
78-
key={routeKey}
79-
{...options}
80-
activityMode={activityMode}
81-
screenKey={routeKey}
82-
onDismiss={onScreenDismissed}
83-
onNativeDismiss={onScreenNativelyDismissed}>
84-
<StackNavigationContext.Provider value={stackNavigationContext}>
85-
<Component />
86-
</StackNavigationContext.Provider>
87-
</Stack.Screen>
88-
);
89-
},
90-
)}
75+
const matchingConfig = routeConfigs.find(
76+
config => config.name === name,
77+
);
78+
if (!matchingConfig) {
79+
throw new Error(`[Stack] No config matches the "${name}" route name`);
80+
}
81+
82+
const Component = matchingConfig.Component;
83+
84+
return (
85+
<Stack.Screen
86+
key={routeKey}
87+
{...options}
88+
activityMode={activityMode}
89+
screenKey={routeKey}
90+
onDismiss={onScreenDismissed}
91+
onNativeDismiss={onScreenNativelyDismissed}>
92+
<StackNavigationContext.Provider value={stackNavigationContext}>
93+
<Component />
94+
</StackNavigationContext.Provider>
95+
</Stack.Screen>
96+
);
97+
})}
9198
</Stack.Host>
9299
);
93100
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export type StackRouteConfig = {
1717
options: StackRouteOptions;
1818
};
1919

20-
export type StackRoute = StackRouteConfig & {
20+
export type StackRoute = Omit<StackRouteConfig, 'Component'> & {
2121
activityMode: StackScreenProps['activityMode'];
2222
routeKey: StackScreenProps['screenKey'];
23-
isMarkedForDismissal: Boolean, // whether this route is during or after dismissal process
23+
isMarkedForDismissal: Boolean; // whether this route is during or after dismissal process
2424
};
2525

2626
/// StackContainer props

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

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

8789
if (renderedRouteIndex !== NOT_FOUND_INDEX) {
@@ -162,10 +164,14 @@ function navigationActionPopHandler(
162164
}
163165

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

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

@@ -271,8 +277,10 @@ function createRouteFromConfig(
271277
config: StackRouteConfig,
272278
activityMode: StackScreenActivityMode = 'detached',
273279
): StackRoute {
280+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
281+
const { Component, ...rest } = config;
274282
return {
275-
...config,
283+
...rest,
276284
activityMode,
277285
routeKey: generateRouteKeyForRouteName(config.name),
278286
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 });
@@ -153,9 +150,11 @@ function useTabsNavigationMethods(dispatch: React.Dispatch<TabsNavigationAction>
153150
[dispatch],
154151
);
155152

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

0 commit comments

Comments
 (0)