1- import { useContext } from 'react' ;
1+ import { useCallback , useContext } from 'react' ;
22// eslint-disable-next-line no-restricted-imports
33import type { ScrollView } from 'react-native' ;
44import { ActionListContext } from '@pages/inbox/ReportScreenContext' ;
55import type ReportScrollManagerData from './types' ;
66
7- function useReportScrollManager ( ) : ReportScrollManagerData {
8- const { flatListRef, scrollPositionRef } = useContext ( ActionListContext ) ;
7+ function useReportScrollManager ( isInverted = true ) : ReportScrollManagerData {
8+ const { flatListRef} = useContext ( ActionListContext ) ;
99
1010 /**
1111 * Scroll to the provided index.
1212 */
13- const scrollToIndex = ( index : number ) => {
14- if ( ! flatListRef ?. current ) {
15- return ;
16- }
17- flatListRef . current . scrollToIndex ( { index} ) ;
18- } ;
13+ const scrollToIndex = useCallback (
14+ ( index : number ) => {
15+ if ( ! flatListRef ?. current ) {
16+ return ;
17+ }
18+
19+ flatListRef . current . scrollToIndex ( { index} ) ;
20+ } ,
21+ [ flatListRef ] ,
22+ ) ;
1923
2024 /**
21- * Scroll to the bottom of the inverted FlatList.
22- * When FlatList is inverted it's "bottom" is really it's top
25+ * Scroll to the visual bottom of the list.
2326 */
24- const scrollToBottom = ( ) => {
27+ const scrollToBottom = useCallback ( ( ) => {
2528 if ( ! flatListRef ?. current ) {
2629 return ;
2730 }
2831
29- scrollPositionRef . current = { offset : 0 } ;
30- flatListRef . current ?. scrollToOffset ( { animated : false , offset : 0 } ) ;
31- } ;
32+ if ( isInverted ) {
33+ flatListRef . current . scrollToOffset ( { animated : false , offset : 0 } ) ;
34+ return ;
35+ }
36+
37+ flatListRef . current . scrollToEnd ( { animated : false } ) ;
38+ } , [ flatListRef , isInverted ] ) ;
3239
3340 /**
3441 * Scroll to the end of the FlatList.
3542 */
36- const scrollToEnd = ( ) => {
43+ const scrollToEnd = useCallback ( ( ) => {
3744 if ( ! flatListRef ?. current ) {
3845 return ;
3946 }
@@ -46,14 +53,18 @@ function useReportScrollManager(): ReportScrollManagerData {
4653 }
4754
4855 flatListRef . current . scrollToEnd ( { animated : false } ) ;
49- } ;
56+ } , [ flatListRef ] ) ;
5057
51- const scrollToOffset = ( offset : number ) => {
52- if ( ! flatListRef ?. current ) {
53- return ;
54- }
55- flatListRef . current . scrollToOffset ( { offset, animated : false } ) ;
56- } ;
58+ const scrollToOffset = useCallback (
59+ ( offset : number ) => {
60+ if ( ! flatListRef ?. current ) {
61+ return ;
62+ }
63+
64+ flatListRef . current . scrollToOffset ( { offset, animated : false } ) ;
65+ } ,
66+ [ flatListRef ] ,
67+ ) ;
5768
5869 return { ref : flatListRef , scrollToIndex, scrollToBottom, scrollToEnd, scrollToOffset} ;
5970}
0 commit comments