-
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
Merged
hayata-suenaga
merged 10 commits into
Expensify:main
from
software-mansion-labs:fix-chatlist-scroll
Mar 26, 2024
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f1bcac
save scroll position on HOME screen
adamgrzybowski 6246bc8
fix tests
adamgrzybowski 8f28c26
Merge branch 'main' into fix-chatlist-scroll
adamgrzybowski a792bb1
save scroll position for settings tab
adamgrzybowski 3620d73
Merge branch 'main' into fix-chatlist-scroll
adamgrzybowski 77c62ee
Merge branch 'main' into fix-chatlist-scroll
adamgrzybowski 0c68a22
reset saved scroll positions for HOME if the priority mode changed
adamgrzybowski 873add6
use usePrevious instead useRef
adamgrzybowski d546a38
Merge branch 'main' into fix-chatlist-scroll
adamgrzybowski 830fb27
reset scroll position on option mode change
adamgrzybowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We can use
usePrevious
hook to compare with prev props value.Btw, is this reasonable approach to reset scroll of HOME screen here? This is general context provider.
I thought of handling in
LHNOptionsList
.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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I am fine with current approach.