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

[TS migration] Migrate 'GrowlNotification' component to TypeScript #32942

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import {Animated} from 'react-native';
import useSafeAreaInsets from '@hooks/useSafeAreaInsets';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import GrowlNotificationContainerProps from './types';

function GrowlNotificationContainer({children, translateY}: GrowlNotificationContainerProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const insets = useSafeAreaInsets();

return <Animated.View style={[StyleUtils.getSafeAreaPadding(insets), styles.growlNotificationContainer, styles.growlNotificationTranslateY(translateY)]}>{children}</Animated.View>;
}

GrowlNotificationContainer.displayName = 'GrowlNotificationContainer';

export default GrowlNotificationContainer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import {Animated} from 'react-native';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import GrowlNotificationContainerProps from './types';

function GrowlNotificationContainer({children, translateY}: GrowlNotificationContainerProps) {
const styles = useThemeStyles();
const {isSmallScreenWidth} = useWindowDimensions();

return (
<Animated.View
style={[styles.growlNotificationContainer, styles.growlNotificationDesktopContainer, styles.growlNotificationTranslateY(translateY), isSmallScreenWidth && styles.mwn]}
>
{children}
</Animated.View>
);
}

GrowlNotificationContainer.displayName = 'GrowlNotificationContainer';

export default GrowlNotificationContainer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Animated} from 'react-native';

type GrowlNotificationContainerProps = {
children: React.ReactNode;
cdOut marked this conversation as resolved.
Show resolved Hide resolved

translateY: Animated.Value;
};

export default GrowlNotificationContainerProps;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {Animated, View} from 'react-native';
import {Directions, FlingGestureHandler, State} from 'react-native-gesture-handler';
import {SvgProps} from 'react-native-svg';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Pressables from '@components/Pressable';
Expand All @@ -11,20 +12,22 @@ import * as Growl from '@libs/Growl';
import useNativeDriver from '@libs/useNativeDriver';
import CONST from '@src/CONST';
import GrowlNotificationContainer from './GrowlNotificationContainer';
import {GrowlNotificationProps} from './types';

const INACTIVE_POSITION_Y = -255;

const PressableWithoutFeedback = Pressables.PressableWithoutFeedback;

function GrowlNotification(_, ref) {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
function GrowlNotification({_, ref}: GrowlNotificationProps) {
const translateY = useRef(new Animated.Value(INACTIVE_POSITION_Y)).current;
const [bodyText, setBodyText] = useState('');
const [type, setType] = useState('success');
const [duration, setDuration] = useState();
const [duration, setDuration] = useState<number | undefined>();
cdOut marked this conversation as resolved.
Show resolved Hide resolved
const theme = useTheme();
const styles = useThemeStyles();

const types = {
const types: Record<string, {icon: React.FC<SvgProps>; iconColor: string}> = {
cdOut marked this conversation as resolved.
Show resolved Hide resolved
[CONST.GROWL.SUCCESS]: {
icon: Expensicons.Checkmark,
iconColor: theme.success,
Expand All @@ -46,7 +49,7 @@ function GrowlNotification(_, ref) {
* @param {String} type
* @param {Number} duration
*/
const show = useCallback((text, growlType, growlDuration) => {
const show = useCallback((text: string, growlType: string, growlDuration: number) => {
setBodyText(text);
setType(growlType);
setDuration(growlDuration);
Expand All @@ -58,13 +61,11 @@ function GrowlNotification(_, ref) {
* @param {Number} val
*/
const fling = useCallback(
(val = INACTIVE_POSITION_Y) => {
(val = INACTIVE_POSITION_Y) =>
cdOut marked this conversation as resolved.
Show resolved Hide resolved
Animated.spring(translateY, {
toValue: val,
duration: 80,
cdOut marked this conversation as resolved.
Show resolved Hide resolved
useNativeDriver,
}).start();
},
}).start(),
[translateY],
);

Expand Down
14 changes: 14 additions & 0 deletions src/components/GrowlNotification/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ForwardedRef} from 'react';

type GrowlRef = {
cdOut marked this conversation as resolved.
Show resolved Hide resolved
show?: (bodyText: string, type: string, duration: number) => void;
};

type GrowlNotificationProps = {
// eslint-disable-next-line @typescript-eslint/naming-convention
_: unknown;

ref: ForwardedRef<GrowlRef>;
};

export type {GrowlRef, GrowlNotificationProps};
5 changes: 1 addition & 4 deletions src/libs/Growl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from 'react';
import {GrowlRef} from '@components/GrowlNotification/types';
import CONST from '@src/CONST';

type GrowlRef = {
show?: (bodyText: string, type: string, duration: number) => void;
};

const growlRef = React.createRef<GrowlRef>();
let resolveIsReadyPromise: undefined | ((value?: unknown) => void);
const isReadyPromise = new Promise((resolve) => {
Expand Down
Loading