Skip to content
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

[No QA] [TS Migration] Migrate useReportScrollManager and ReportScreenContext to TypeScript #29315

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?
If its necessary please make sure its not causing any regressions

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️ exactly 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it wasn't correct before, and it can cause a regression. Please test it 🙌

};

/**
* 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 @@ -249,7 +249,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 @@ -364,7 +364,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
Loading