Skip to content

Commit

Permalink
Merge pull request Expensify#29315 from JKobrynski/migrateUseReportSc…
Browse files Browse the repository at this point in the history
…rollManagerToTypeScript

[No QA] [TS Migration] Migrate useReportScrollManager and ReportScreenContext to TypeScript
  • Loading branch information
roryabraham authored Oct 26, 2023
2 parents 83e2b97 + f467ac2 commit b15fc21
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import {useContext, useCallback} from 'react';
import {ActionListContext} from '../../pages/home/ReportScreenContext';
import ReportScrollManagerData from './types';

function useReportScrollManager() {
function useReportScrollManager(): ReportScrollManagerData {
const flatListRef = useContext(ActionListContext);

/**
* Scroll to the provided index.
*
* @param {Object} index
*/
const scrollToIndex = (index) => {
if (!flatListRef.current) {
const scrollToIndex = (index: number) => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToIndex(index);
flatListRef.current.scrollToIndex({index});
};

/**
* Scroll to the bottom of the flatlist.
*/
const scrollToBottom = useCallback(() => {
if (!flatListRef.current) {
if (!flatListRef?.current) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import {useContext, useCallback} from 'react';
import {ActionListContext} from '../../pages/home/ReportScreenContext';
import ReportScrollManagerData from './types';

function useReportScrollManager() {
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.
*
* @param {Object} index
* @param {Boolean} isEditing
*/
const scrollToIndex = (index, isEditing) => {
if (!flatListRef.current || isEditing) {
const scrollToIndex = (index: number, isEditing?: boolean) => {
if (!flatListRef?.current || isEditing) {
return;
}

flatListRef.current.scrollToIndex(index);
flatListRef.current.scrollToIndex({index, animated: true});
};

/**
* Scroll to the bottom of the flatlist.
*/
const scrollToBottom = useCallback(() => {
if (!flatListRef.current) {
if (!flatListRef?.current) {
return;
}

Expand Down
9 changes: 9 additions & 0 deletions src/hooks/useReportScrollManager/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ActionListContextType} from '../../pages/home/ReportScreenContext';

type ReportScrollManagerData = {
ref: ActionListContextType;
scrollToIndex: (index: number, isEditing?: boolean) => void;
scrollToBottom: () => void;
};

export default ReportScrollManagerData;
6 changes: 0 additions & 6 deletions src/pages/home/ReportScreenContext.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/pages/home/ReportScreenContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {RefObject, createContext} from 'react';
import {FlatList, GestureResponderEvent} from 'react-native';

type ReactionListRef = {
showReactionList: (event: GestureResponderEvent | undefined, reactionListAnchor: Element, emojiName: string, reportActionID: string) => void;
hideReactionList: () => void;
isActiveReportAction: (actionID: number | string) => boolean;
};

type ActionListContextType = RefObject<FlatList<unknown>> | null;
type ReactionListContextType = RefObject<ReactionListRef> | null;

const ActionListContext = createContext<ActionListContextType>(null);
const ReactionListContext = createContext<ReactionListContextType>(null);

export {ActionListContext, ReactionListContext};
export type {ReactionListRef, ActionListContextType, ReactionListContextType};
4 changes: 2 additions & 2 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function ReportActionItemMessageEdit(props) {
// Scroll to the last comment after editing to make sure the whole comment is clearly visible in the report.
if (props.index === 0) {
const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
reportScrollManager.scrollToIndex({animated: true, index: props.index}, false);
reportScrollManager.scrollToIndex(props.index, false);
keyboardDidHideListener.remove();
});
}
Expand Down Expand Up @@ -406,7 +406,7 @@ function ReportActionItemMessageEdit(props) {
style={[styles.textInputCompose, styles.flex1, styles.bgTransparent]}
onFocus={() => {
setIsFocused(true);
reportScrollManager.scrollToIndex({animated: true, index: props.index}, true);
reportScrollManager.scrollToIndex(props.index, true);
setShouldShowComposeInputKeyboardAware(false);

// Clear active report action when another action gets focused
Expand Down

0 comments on commit b15fc21

Please sign in to comment.