-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.ts
38 lines (31 loc) · 1.26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {useCallback, useContext} from 'react';
import {ActionListContext} from '@pages/home/ReportScreenContext';
import type ReportScrollManagerData from './types';
function useReportScrollManager(): ReportScrollManagerData {
const {flatListRef} = useContext(ActionListContext);
/**
* Scroll to the provided index. On non-native implementations we do not want to scroll when we are scrolling because
* we are editing a comment.
*/
const scrollToIndex = (index: number, isEditing?: boolean) => {
if (!flatListRef?.current || isEditing) {
return;
}
flatListRef.current.scrollToIndex({index, animated: true});
};
/**
* Scroll to the bottom of the flatlist.
*/
const scrollToBottom = useCallback(() => {
// We're deferring execution here because on iOS: mWeb (WebKit based browsers)
// scrollToOffset method doesn't work unless called on the next tick
requestAnimationFrame(() => {
if (!flatListRef?.current) {
return;
}
flatListRef.current.scrollToOffset({animated: false, offset: 0});
});
}, [flatListRef]);
return {ref: flatListRef, scrollToIndex, scrollToBottom};
}
export default useReportScrollManager;