Skip to content

Commit

Permalink
Merge pull request #34487 from kubabutkiewicz/ts-migration/ReportActi…
Browse files Browse the repository at this point in the history
…onItem2/component

[TS migration] Migrate 'ReportActionItem.js' component to TypeScript
  • Loading branch information
luacmartins authored Mar 7, 2024
2 parents c270b5e + 329c724 commit 844ee32
Show file tree
Hide file tree
Showing 42 changed files with 493 additions and 461 deletions.
4 changes: 2 additions & 2 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {OnyxEntry} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type CONST from './CONST';
import type * as FormTypes from './types/form';
Expand Down Expand Up @@ -589,7 +589,7 @@ type OnyxFormDraftKey = keyof OnyxFormDraftValuesMapping;
type OnyxValueKey = keyof OnyxValuesMapping;

type OnyxKey = OnyxValueKey | OnyxCollectionKey | OnyxFormKey | OnyxFormDraftKey;
type OnyxValue<TOnyxKey extends OnyxKey> = OnyxEntry<OnyxValues[TOnyxKey]>;
type OnyxValue<TOnyxKey extends OnyxKey> = TOnyxKey extends keyof OnyxCollectionValuesMapping ? OnyxCollection<OnyxValues[TOnyxKey]> : OnyxEntry<OnyxValues[TOnyxKey]>;

type MissingOnyxKeysError = `Error: Types don't match, OnyxKey type is missing: ${Exclude<AllOnyxKeys, OnyxKey>}`;
/** If this type errors, it means that the `OnyxKey` type is missing some keys. */
Expand Down
2 changes: 1 addition & 1 deletion src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function Button(
accessibilityLabel = '',
...rest
}: ButtonProps,
ref: ForwardedRef<View>,
ref: ForwardedRef<View | HTMLDivElement>,
) {
const theme = useTheme();
const styles = useThemeStyles();
Expand Down
11 changes: 6 additions & 5 deletions src/components/FloatingActionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useEffect, useRef} from 'react';
import type {GestureResponderEvent, Role} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {GestureResponderEvent, Role, Text} from 'react-native';
import {Platform, View} from 'react-native';
import Animated, {createAnimatedPropAdapter, Easing, interpolateColor, processColor, useAnimatedProps, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import Svg, {Path} from 'react-native-svg';
Expand Down Expand Up @@ -58,12 +59,12 @@ type FloatingActionButtonProps = {
role: Role;
};

function FloatingActionButton({onPress, isActive, accessibilityLabel, role}: FloatingActionButtonProps, ref: ForwardedRef<HTMLDivElement | View>) {
function FloatingActionButton({onPress, isActive, accessibilityLabel, role}: FloatingActionButtonProps, ref: ForwardedRef<HTMLDivElement | View | Text>) {
const {success, buttonDefaultBG, textLight, textDark} = useTheme();
const styles = useThemeStyles();
const borderRadius = styles.floatingActionButton.borderRadius;
const {translate} = useLocalize();
const fabPressable = useRef<HTMLDivElement | View | null>(null);
const fabPressable = useRef<HTMLDivElement | View | Text | null>(null);
const sharedValue = useSharedValue(isActive ? 1 : 0);
const buttonRef = ref;

Expand Down Expand Up @@ -112,9 +113,9 @@ function FloatingActionButton({onPress, isActive, accessibilityLabel, role}: Flo
<View style={styles.bottomTabBarItem}>
<AnimatedPressable
ref={(el) => {
fabPressable.current = el;
fabPressable.current = el ?? null;
if (buttonRef && 'current' in buttonRef) {
buttonRef.current = el;
buttonRef.current = el ?? null;
}
}}
accessibilityLabel={accessibilityLabel}
Expand Down
12 changes: 6 additions & 6 deletions src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {Dimensions} from 'react-native';
import type {EmitterSubscription, GestureResponderEvent} from 'react-native';
import type {EmitterSubscription, GestureResponderEvent, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu';
Expand Down Expand Up @@ -69,8 +69,8 @@ function KYCWall({
walletTerms,
shouldShowPersonalBankAccountOption = false,
}: BaseKYCWallProps) {
const anchorRef = useRef<HTMLDivElement>(null);
const transferBalanceButtonRef = useRef<HTMLElement | null>(null);
const anchorRef = useRef<HTMLDivElement | View | null>(null);
const transferBalanceButtonRef = useRef<HTMLDivElement | View | null>(null);

const [shouldShowAddPaymentMenu, setShouldShowAddPaymentMenu] = useState(false);

Expand Down Expand Up @@ -111,7 +111,7 @@ function KYCWall({
return;
}

const buttonPosition = getClickedTargetLocation(transferBalanceButtonRef.current);
const buttonPosition = getClickedTargetLocation(transferBalanceButtonRef.current as HTMLDivElement);
const position = getAnchorPosition(buttonPosition);

setPositionAddPaymentMenu(position);
Expand Down Expand Up @@ -162,7 +162,7 @@ function KYCWall({
}

// Use event target as fallback if anchorRef is null for safety
const targetElement = anchorRef.current ?? (event?.currentTarget as HTMLElement);
const targetElement = anchorRef.current ?? (event?.currentTarget as HTMLDivElement);

transferBalanceButtonRef.current = targetElement;

Expand All @@ -181,7 +181,7 @@ function KYCWall({
return;
}

const clickedElementLocation = getClickedTargetLocation(targetElement);
const clickedElementLocation = getClickedTargetLocation(targetElement as HTMLDivElement);
const position = getAnchorPosition(clickedElementLocation);

setPositionAddPaymentMenu(position);
Expand Down
6 changes: 4 additions & 2 deletions src/components/OnyxProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import createOnyxContext from './createOnyxContext';
const [withNetwork, NetworkProvider, NetworkContext] = createOnyxContext(ONYXKEYS.NETWORK);
const [withPersonalDetails, PersonalDetailsProvider, , usePersonalDetails] = createOnyxContext(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [withCurrentDate, CurrentDateProvider] = createOnyxContext(ONYXKEYS.CURRENT_DATE);
const [withReportActionsDrafts, ReportActionsDraftsProvider] = createOnyxContext(ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS);
const [withBlockedFromConcierge, BlockedFromConciergeProvider] = createOnyxContext(ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE);
const [withReportActionsDrafts, ReportActionsDraftsProvider, , useReportActionsDrafts] = createOnyxContext(ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS);
const [withBlockedFromConcierge, BlockedFromConciergeProvider, , useBlockedFromConcierge] = createOnyxContext(ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE);
const [withBetas, BetasProvider, BetasContext, useBetas] = createOnyxContext(ONYXKEYS.BETAS);
const [withReportCommentDrafts, ReportCommentDraftsProvider] = createOnyxContext(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT);
const [withPreferredTheme, PreferredThemeProvider, PreferredThemeContext] = createOnyxContext(ONYXKEYS.PREFERRED_THEME);
Expand Down Expand Up @@ -66,5 +66,7 @@ export {
useFrequentlyUsedEmojis,
withPreferredEmojiSkinTone,
PreferredEmojiSkinToneContext,
useBlockedFromConcierge,
useReportActionsDrafts,
useSession,
};
5 changes: 3 additions & 2 deletions src/components/Popover/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {RefObject} from 'react';
import type {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {Text, View} from 'react-native';
import type {PopoverAnchorPosition} from '@components/Modal/types';
import type BaseModalProps from '@components/Modal/types';
import type {WindowDimensionsProps} from '@components/withWindowDimensions/types';
Expand All @@ -20,7 +21,7 @@ type PopoverProps = BaseModalProps &
anchorAlignment?: AnchorAlignment;

/** The anchor ref of the popover */
anchorRef: RefObject<View | HTMLDivElement>;
anchorRef: RefObject<View | HTMLDivElement | Text>;

/** Whether disable the animations */
disableAnimation?: boolean;
Expand Down
7 changes: 4 additions & 3 deletions src/components/PopoverProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {RefObject} from 'react';
import React, {createContext, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {Text, View} from 'react-native';
import type {AnchorRef, PopoverContextProps, PopoverContextValue} from './types';

const PopoverContext = createContext<PopoverContextValue>({
Expand All @@ -10,7 +11,7 @@ const PopoverContext = createContext<PopoverContextValue>({
isOpen: false,
});

function elementContains(ref: RefObject<View | HTMLElement> | undefined, target: EventTarget | null) {
function elementContains(ref: RefObject<View | HTMLElement | Text> | undefined, target: EventTarget | null) {
if (ref?.current && 'contains' in ref.current && ref?.current?.contains(target as Node)) {
return true;
}
Expand All @@ -21,7 +22,7 @@ function PopoverContextProvider(props: PopoverContextProps) {
const [isOpen, setIsOpen] = useState(false);
const activePopoverRef = useRef<AnchorRef | null>(null);

const closePopover = useCallback((anchorRef?: RefObject<View | HTMLElement>): boolean => {
const closePopover = useCallback((anchorRef?: RefObject<View | HTMLElement | Text>): boolean => {
if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) {
return false;
}
Expand Down
11 changes: 6 additions & 5 deletions src/components/PopoverProvider/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {ReactNode, RefObject} from 'react';
import type {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {Text, View} from 'react-native';

type PopoverContextProps = {
children: ReactNode;
Expand All @@ -8,14 +9,14 @@ type PopoverContextProps = {
type PopoverContextValue = {
onOpen?: (popoverParams: AnchorRef) => void;
popover?: AnchorRef | Record<string, never> | null;
close: (anchorRef?: RefObject<View | HTMLDivElement>) => void;
close: (anchorRef?: RefObject<View | HTMLDivElement | Text>) => void;
isOpen: boolean;
};

type AnchorRef = {
ref: RefObject<View | HTMLDivElement>;
close: (anchorRef?: RefObject<View | HTMLDivElement>) => void;
anchorRef: RefObject<View | HTMLDivElement>;
ref: RefObject<View | HTMLDivElement | Text>;
close: (anchorRef?: RefObject<View | HTMLDivElement | Text>) => void;
anchorRef: RefObject<View | HTMLDivElement | Text>;
};

export type {PopoverContextProps, PopoverContextValue, AnchorRef};
5 changes: 3 additions & 2 deletions src/components/PopoverWithoutOverlay/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {RefObject} from 'react';
import type {View} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {Text, View} from 'react-native';
import type BaseModalProps from '@components/Modal/types';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

Expand All @@ -14,7 +15,7 @@ type PopoverWithoutOverlayProps = ChildrenProps &
};

/** The anchor ref of the popover */
anchorRef: RefObject<View | HTMLDivElement>;
anchorRef: RefObject<View | HTMLDivElement | Text>;

/** A react-native-animatable animation timing for the modal display animation */
animationInTiming?: number;
Expand Down
5 changes: 3 additions & 2 deletions src/components/Pressable/GenericPressable/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {ElementRef, ForwardedRef, RefObject} from 'react';
import type {GestureResponderEvent, HostComponent, PressableStateCallbackType, PressableProps as RNPressableProps, StyleProp, View, ViewStyle} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import type {GestureResponderEvent, HostComponent, PressableStateCallbackType, PressableProps as RNPressableProps, Text as RNText, StyleProp, View, ViewStyle} from 'react-native';
import type {ValueOf} from 'type-fest';
import type {Shortcut} from '@libs/KeyboardShortcut';
import type CONST from '@src/CONST';
Expand Down Expand Up @@ -138,7 +139,7 @@ type PressableProps = RNPressableProps &
noDragArea?: boolean;
};

type PressableRef = ForwardedRef<HTMLDivElement | View>;
type PressableRef = ForwardedRef<HTMLDivElement | View | RNText | undefined>;

export default PressableProps;
export type {PressableRef};
2 changes: 1 addition & 1 deletion src/components/PressableWithSecondaryInteraction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {ParsableStyle} from '@styles/utils/types';

type PressableWithSecondaryInteractionProps = PressableWithFeedbackProps & {
/** The function that should be called when this pressable is pressed */
onPress: (event?: GestureResponderEvent) => void;
onPress?: (event?: GestureResponderEvent) => void;

/** The function that should be called when this pressable is pressedIn */
onPressIn?: (event?: GestureResponderEvent) => void;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Reactions/ReportActionItemEmojiReactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ReportActionItemEmojiReactionsProps = WithCurrentUserPersonalDetailsProps &
emojiReactions: OnyxEntry<ReportActionReactions>;

/** The user's preferred locale. */
preferredLocale: OnyxEntry<Locale>;
preferredLocale?: OnyxEntry<Locale>;

/** The report action that these reactions are for */
reportAction: ReportAction;
Expand Down Expand Up @@ -155,7 +155,7 @@ function ReportActionItemEmojiReactions({
shouldDisableOpacity={!!reportAction.pendingAction}
>
<EmojiReactionBubble
ref={(ref) => (popoverReactionListAnchors.current[reaction.reactionEmojiName] = ref)}
ref={(ref) => (popoverReactionListAnchors.current[reaction.reactionEmojiName] = ref ?? null)}
count={reaction.reactionCount}
emojiCodes={reaction.emojiCodes}
onPress={reaction.onPress}
Expand Down
3 changes: 2 additions & 1 deletion src/components/ReportActionItem/MoneyReportView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Str from 'expensify-common/lib/str';
import React, {useMemo} from 'react';
import type {StyleProp, TextStyle} from 'react-native';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
Expand All @@ -26,7 +27,7 @@ type MoneyReportViewProps = {
report: Report;

/** Policy that the report belongs to */
policy: Policy;
policy: OnyxEntry<Policy>;

/** Policy report fields */
policyReportFields: PolicyReportField[];
Expand Down
8 changes: 4 additions & 4 deletions src/components/createOnyxContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {ComponentType, ForwardedRef, ForwardRefExoticComponent, PropsWithou
import React, {createContext, forwardRef, useContext} from 'react';
import {withOnyx} from 'react-native-onyx';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import type {OnyxKey, OnyxValue, OnyxValues} from '@src/ONYXKEYS';
import type {OnyxKey, OnyxValue} from '@src/ONYXKEYS';
import type ChildrenProps from '@src/types/utils/ChildrenProps';

// Provider types
Expand Down Expand Up @@ -32,11 +32,11 @@ type CreateOnyxContext<TOnyxKey extends OnyxKey> = [
WithOnyxKey<TOnyxKey>,
ComponentType<Omit<ProviderPropsWithOnyx<TOnyxKey>, TOnyxKey>>,
React.Context<OnyxValue<TOnyxKey>>,
() => OnyxValues[TOnyxKey],
() => NonNullable<OnyxValue<TOnyxKey>>,
];

export default <TOnyxKey extends OnyxKey>(onyxKeyName: TOnyxKey): CreateOnyxContext<TOnyxKey> => {
const Context = createContext<OnyxValue<TOnyxKey>>(null);
const Context = createContext<OnyxValue<TOnyxKey>>(null as OnyxValue<TOnyxKey>);
function Provider(props: ProviderPropsWithOnyx<TOnyxKey>): ReactNode {
return <Context.Provider value={props[onyxKeyName]}>{props.children}</Context.Provider>;
}
Expand Down Expand Up @@ -86,7 +86,7 @@ export default <TOnyxKey extends OnyxKey>(onyxKeyName: TOnyxKey): CreateOnyxCont
if (value === null) {
throw new Error(`useOnyxContext must be used within a OnyxProvider [key: ${onyxKeyName}]`);
}
return value;
return value as NonNullable<OnyxValue<TOnyxKey>>;
};

return [withOnyxKey, ProviderWithOnyx, Context, useOnyxContext];
Expand Down
16 changes: 10 additions & 6 deletions src/libs/EmojiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,13 @@ function getFrequentlyUsedEmojis(newEmoji: Emoji | Emoji[]): FrequentlyUsedEmoji
/**
* Given an emoji item object, return an emoji code based on its type.
*/
const getEmojiCodeWithSkinColor = (item: Emoji, preferredSkinToneIndex: number): string => {
const getEmojiCodeWithSkinColor = (item: Emoji, preferredSkinToneIndex: OnyxEntry<string | number>): string | undefined => {
const {code, types} = item;
if (types?.[preferredSkinToneIndex]) {
if (!preferredSkinToneIndex) {
return;
}

if (typeof preferredSkinToneIndex === 'number' && types?.[preferredSkinToneIndex]) {
return types[preferredSkinToneIndex];
}

Expand Down Expand Up @@ -305,7 +309,7 @@ function getAddedEmojis(currentEmojis: Emoji[], formerEmojis: Emoji[]): Emoji[]
* Replace any emoji name in a text with the emoji icon.
* If we're on mobile, we also add a space after the emoji granted there's no text after it.
*/
function replaceEmojis(text: string, preferredSkinTone: number = CONST.EMOJI_DEFAULT_SKIN_TONE, lang: Locale = CONST.LOCALES.DEFAULT): ReplacedEmoji {
function replaceEmojis(text: string, preferredSkinTone: OnyxEntry<number | string> = CONST.EMOJI_DEFAULT_SKIN_TONE, lang: Locale = CONST.LOCALES.DEFAULT): ReplacedEmoji {
// emojisTrie is importing the emoji JSON file on the app starting and we want to avoid it
const emojisTrie = require('./EmojiTrie').default;

Expand Down Expand Up @@ -345,9 +349,9 @@ function replaceEmojis(text: string, preferredSkinTone: number = CONST.EMOJI_DEF

// Set the cursor to the end of the last replaced Emoji. Note that we position after
// the extra space, if we added one.
cursorPosition = newText.indexOf(emoji) + emojiReplacement.length;
cursorPosition = newText.indexOf(emoji) + (emojiReplacement?.length ?? 0);

newText = newText.replace(emoji, emojiReplacement);
newText = newText.replace(emoji, emojiReplacement ?? '');
}
}

Expand All @@ -369,7 +373,7 @@ function replaceEmojis(text: string, preferredSkinTone: number = CONST.EMOJI_DEF
/**
* Find all emojis in a text and replace them with their code.
*/
function replaceAndExtractEmojis(text: string, preferredSkinTone: number = CONST.EMOJI_DEFAULT_SKIN_TONE, lang: Locale = CONST.LOCALES.DEFAULT): ReplacedEmoji {
function replaceAndExtractEmojis(text: string, preferredSkinTone: OnyxEntry<number | string> = CONST.EMOJI_DEFAULT_SKIN_TONE, lang: Locale = CONST.LOCALES.DEFAULT): ReplacedEmoji {
const {text: convertedText = '', emojis = [], cursorPosition} = replaceEmojis(text, preferredSkinTone, lang);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/libs/ErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ export {
getMicroSecondOnyxErrorObject,
isReceiptError,
};

export type {OnyxDataWithErrors};
5 changes: 3 additions & 2 deletions src/libs/OnyxSelectors/reportWithoutHasDraftSelector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {OnyxValue} from '@src/ONYXKEYS';
import type {OnyxEntry} from 'react-native-onyx';
import type {Report} from '@src/types/onyx';

export default function reportWithoutHasDraftSelector(report: OnyxValue<'report_'>) {
export default function reportWithoutHasDraftSelector(report: OnyxEntry<Report>) {
if (!report) {
return report;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ function hasRequestFromCurrentAccount(reportID: string, currentAccountID: number
* Checks if a given report action corresponds to an actionable mention whisper.
* @param reportAction
*/
function isActionableMentionWhisper(reportAction: OnyxEntry<ReportAction>): boolean {
function isActionableMentionWhisper(reportAction: OnyxEntry<ReportAction>): reportAction is ReportActionBase & OriginalMessageActionableMentionWhisper {
return reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ACTIONABLEMENTIONWHISPER;
}

Expand Down
6 changes: 3 additions & 3 deletions src/libs/calculateAnchorPosition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-restricted-imports */
import type {Text as RNText, View} from 'react-native';
import type {ValueOf} from 'type-fest';
import type {ContextMenuAnchor} from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import CONST from '@src/CONST';
import type {AnchorPosition} from '@src/styles';

Expand All @@ -13,9 +13,9 @@ type AnchorOrigin = {
/**
* Gets the x,y position of the passed in component for the purpose of anchoring another component to it.
*/
export default function calculateAnchorPosition(anchorComponent: View | RNText, anchorOrigin?: AnchorOrigin): Promise<AnchorPosition> {
export default function calculateAnchorPosition(anchorComponent: ContextMenuAnchor, anchorOrigin?: AnchorOrigin): Promise<AnchorPosition> {
return new Promise((resolve) => {
if (!anchorComponent) {
if (!anchorComponent || !('measureInWindow' in anchorComponent)) {
resolve({horizontal: 0, vertical: 0});
return;
}
Expand Down
Loading

0 comments on commit 844ee32

Please sign in to comment.