-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Wave 8] [Ideal Nav] Save scroll position on HOME screen #38161
Changes from 7 commits
1f1bcac
6246bc8
8f28c26
a792bb1
3620d73
77c62ee
0c68a22
873add6
d546a38
830fb27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import type {ParamListBase, RouteProp} from '@react-navigation/native'; | ||
import React, {createContext, useCallback, useEffect, useMemo, useRef} from 'react'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import type {NavigationPartialRoute, State} from '@libs/Navigation/types'; | ||
import NAVIGATORS from '@src/NAVIGATORS'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import SCREENS from '@src/SCREENS'; | ||
import type {PriorityMode} from '@src/types/onyx'; | ||
|
||
type ScrollOffsetContextValue = { | ||
/** Save scroll offset of flashlist on given screen */ | ||
saveScrollOffset: (route: RouteProp<ParamListBase>, scrollOffset: number) => void; | ||
|
||
/** Get scroll offset value for given screen */ | ||
getScrollOffset: (route: RouteProp<ParamListBase>) => number | undefined; | ||
|
||
/** Clean scroll offsets of screen that aren't anymore in the state */ | ||
cleanStaleScrollOffsets: (state: State) => void; | ||
}; | ||
|
||
type ScrollOffsetContextProviderOnyxProps = { | ||
/** The chat priority mode */ | ||
priorityMode: PriorityMode; | ||
}; | ||
|
||
type ScrollOffsetContextProviderProps = ScrollOffsetContextProviderOnyxProps & { | ||
/** Actual content wrapped by this component */ | ||
children: React.ReactNode; | ||
}; | ||
|
||
const defaultValue: ScrollOffsetContextValue = { | ||
saveScrollOffset: () => {}, | ||
getScrollOffset: () => undefined, | ||
cleanStaleScrollOffsets: () => {}, | ||
}; | ||
|
||
const ScrollOffsetContext = createContext<ScrollOffsetContextValue>(defaultValue); | ||
|
||
/** This function is prepared to work with HOME screens. May need modification if we want to handle other types of screens. */ | ||
function getKey(route: RouteProp<ParamListBase> | NavigationPartialRoute): string { | ||
if (route.params && 'policyID' in route.params && typeof route.params.policyID === 'string') { | ||
return `${route.name}-${route.params.policyID}`; | ||
} | ||
return `${route.name}-global`; | ||
} | ||
|
||
function ScrollOffsetContextProvider({children, priorityMode}: ScrollOffsetContextProviderProps) { | ||
const scrollOffsetsRef = useRef<Record<string, number>>({}); | ||
const previousPriorityMode = useRef<PriorityMode>(priorityMode); | ||
|
||
useEffect(() => { | ||
if (previousPriorityMode.current === priorityMode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use Btw, is this reasonable approach to reset scroll of HOME screen here? This is general context provider. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like having this in one place rather than scattering it in different files makes it easier to understand. But I am open to hear different opinions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I am fine with current approach. |
||
return; | ||
} | ||
|
||
// If the priority mode changes, we need to clear the scroll offsets for the home screens because it affects the size of the elements and scroll positions wouldn't be correct. | ||
for (const key of Object.keys(scrollOffsetsRef.current)) { | ||
if (key.includes(SCREENS.HOME)) { | ||
delete scrollOffsetsRef.current[key]; | ||
} | ||
} | ||
|
||
previousPriorityMode.current = priorityMode; | ||
}, [priorityMode]); | ||
|
||
const saveScrollOffset: ScrollOffsetContextValue['saveScrollOffset'] = useCallback((route, scrollOffset) => { | ||
scrollOffsetsRef.current[getKey(route)] = scrollOffset; | ||
}, []); | ||
|
||
const getScrollOffset: ScrollOffsetContextValue['getScrollOffset'] = useCallback((route) => { | ||
if (!scrollOffsetsRef.current) { | ||
return; | ||
} | ||
return scrollOffsetsRef.current[getKey(route)]; | ||
}, []); | ||
|
||
const cleanStaleScrollOffsets: ScrollOffsetContextValue['cleanStaleScrollOffsets'] = useCallback((state) => { | ||
const bottomTabNavigator = state.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR); | ||
if (bottomTabNavigator?.state && 'routes' in bottomTabNavigator.state) { | ||
const bottomTabNavigatorRoutes = bottomTabNavigator.state.routes; | ||
const scrollOffsetkeysOfExistingScreens = bottomTabNavigatorRoutes.map((route) => getKey(route)); | ||
for (const key of Object.keys(scrollOffsetsRef.current)) { | ||
if (!scrollOffsetkeysOfExistingScreens.includes(key)) { | ||
delete scrollOffsetsRef.current[key]; | ||
} | ||
} | ||
} | ||
}, []); | ||
|
||
/** | ||
* The context this component exposes to child components | ||
* @returns currentReportID to share between central pane and LHN | ||
*/ | ||
const contextValue = useMemo( | ||
(): ScrollOffsetContextValue => ({ | ||
saveScrollOffset, | ||
getScrollOffset, | ||
cleanStaleScrollOffsets, | ||
}), | ||
[saveScrollOffset, getScrollOffset, cleanStaleScrollOffsets], | ||
); | ||
|
||
return <ScrollOffsetContext.Provider value={contextValue}>{children}</ScrollOffsetContext.Provider>; | ||
} | ||
|
||
export default withOnyx<ScrollOffsetContextProviderProps, ScrollOffsetContextProviderOnyxProps>({ | ||
priorityMode: { | ||
key: ONYXKEYS.NVP_PRIORITY_MODE, | ||
}, | ||
})(ScrollOffsetContextProvider); | ||
|
||
export {ScrollOffsetContext}; | ||
|
||
export type {ScrollOffsetContextProviderProps, ScrollOffsetContextValue}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Android, with account that has > 100 reports, if we switch between Reports (aka Search) and Inbox quickly, we can notice the flickers, this is because Flashlist needs to render the items before scrolling to that position.
More details:
#53605 (comment)