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

Migrate 'SettingsProfile' page to TypeScript #35446

Merged
merged 26 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e643deb
Migrate 'SettingsProfile' page to TypeScript
abzokhattab Jan 31, 2024
47fad9c
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Feb 14, 2024
42aa582
Updating profile page
abzokhattab Feb 14, 2024
ae6d226
migrating the status page
abzokhattab Feb 14, 2024
add3ffa
converting statuspage to ts
abzokhattab Feb 17, 2024
52dc566
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Feb 17, 2024
6f64f20
migrating pronouns
abzokhattab Feb 17, 2024
fa93bf3
minor edit
abzokhattab Feb 17, 2024
038be0d
Minor
abzokhattab Feb 17, 2024
81c69a0
Minor edit
abzokhattab Feb 17, 2024
5f96f12
move clearDraftCustomStatus inside runAfterInteractions
abzokhattab Feb 18, 2024
8a8bc8e
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Feb 26, 2024
b48b293
ts fix
abzokhattab Feb 26, 2024
a95a366
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Feb 26, 2024
72c16bf
ts fix
abzokhattab Feb 26, 2024
3f65db5
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 1, 2024
b2288b0
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 3, 2024
d66e770
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 11, 2024
0360331
fixes
abzokhattab Mar 11, 2024
fe9dbc0
ts fixes
abzokhattab Mar 11, 2024
18bb08b
moving clearDraftCustomStatus out of the runAfterInteractions
abzokhattab Mar 11, 2024
b46f64c
minor
abzokhattab Mar 12, 2024
b60ebb3
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 12, 2024
6b1e7c3
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 12, 2024
22d161c
Merge remote-tracking branch 'origin/main' into migrate-settings-prof…
abzokhattab Mar 13, 2024
f128c27
FormProvider ref type
abzokhattab Mar 13, 2024
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
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ type OnyxFormValuesMapping = {
[ONYXKEYS.FORMS.MONEY_REQUEST_HOLD_FORM]: FormTypes.MoneyRequestHoldReasonForm;
[ONYXKEYS.FORMS.NEW_CONTACT_METHOD_FORM]: FormTypes.Form;
[ONYXKEYS.FORMS.WAYPOINT_FORM]: FormTypes.Form;
[ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM]: FormTypes.Form;
[ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM]: FormTypes.SettingsStatusSetForm;
[ONYXKEYS.FORMS.SETTINGS_STATUS_CLEAR_DATE_FORM]: FormTypes.Form;
[ONYXKEYS.FORMS.SETTINGS_STATUS_SET_CLEAR_AFTER_FORM]: FormTypes.Form;
[ONYXKEYS.FORMS.PRIVATE_NOTES_FORM]: FormTypes.PrivateNotesForm;
Expand Down
2 changes: 1 addition & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const ROUTES = {
},
PROFILE_AVATAR: {
route: 'a/:accountID/avatar',
getRoute: (accountID: string) => `a/${accountID}/avatar` as const,
getRoute: (accountID: string | number) => `a/${accountID}/avatar` as const,
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
},

TRANSITION_BETWEEN_APPS: 'transition',
Expand Down
10 changes: 3 additions & 7 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import lodashIsEqual from 'lodash/isEqual';
import type {ForwardedRef, MutableRefObject, ReactNode} from 'react';
import type {ForwardedRef, MutableRefObject, ReactNode, RefAttributes} from 'react';
import React, {createRef, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import type {NativeSyntheticEvent, StyleProp, TextInputSubmitEditingEventData, ViewStyle} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
Expand All @@ -18,7 +18,7 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {RegisterInput} from './FormContext';
import FormContext from './FormContext';
import FormWrapper from './FormWrapper';
import type {FormInputErrors, FormOnyxValues, FormProps, InputComponentBaseProps, InputRefs, ValueTypeKey} from './types';
import type {FormInputErrors, FormOnyxValues, FormProps, FormRef, InputComponentBaseProps, InputRefs, ValueTypeKey} from './types';

// In order to prevent Checkbox focus loss when the user are focusing a TextInput and proceeds to toggle a CheckBox in web and mobile web.
// 200ms delay was chosen as a result of empirical testing.
Expand Down Expand Up @@ -73,10 +73,6 @@ type FormProviderProps<TFormID extends OnyxFormKey = OnyxFormKey> = FormProvider
submitFlexEnabled?: boolean;
};

type FormRef<TFormID extends OnyxFormKey = OnyxFormKey> = {
resetForm: (optionalValue: FormOnyxValues<TFormID>) => void;
};

function FormProvider(
{
formID,
Expand Down Expand Up @@ -393,4 +389,4 @@ export default withOnyx<FormProviderProps, FormProviderOnyxProps>({
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
key: (props) => `${props.formID}Draft` as any,
},
})(forwardRef(FormProvider)) as <TFormID extends OnyxFormKey>(props: Omit<FormProviderProps<TFormID>, keyof FormProviderOnyxProps>) => ReactNode;
})(forwardRef(FormProvider)) as <TFormID extends OnyxFormKey>(props: Omit<FormProviderProps<TFormID> & RefAttributes<FormRef>, keyof FormProviderOnyxProps>) => ReactNode;
19 changes: 18 additions & 1 deletion src/components/Form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,25 @@ type FormProps<TFormID extends OnyxFormKey = OnyxFormKey> = {
disablePressOnEnter?: boolean;
};

type FormRef<TFormID extends OnyxFormKey = OnyxFormKey> = {
resetForm: (optionalValue: FormOnyxValues<TFormID>) => void;
};

type InputRefs = Record<string, MutableRefObject<InputComponentBaseProps>>;

type FormInputErrors<TFormID extends OnyxFormKey = OnyxFormKey> = Partial<Record<FormOnyxKeys<TFormID>, TranslationPaths>>;

export type {FormProps, ValidInputs, InputComponentValueProps, FormValue, ValueTypeKey, FormOnyxValues, FormOnyxKeys, FormInputErrors, InputRefs, InputComponentBaseProps, ValueTypeMap};
export type {
FormProps,
ValidInputs,
InputComponentValueProps,
FormValue,
ValueTypeKey,
FormOnyxValues,
FormOnyxKeys,
FormInputErrors,
InputRefs,
InputComponentBaseProps,
ValueTypeMap,
FormRef,
};
2 changes: 1 addition & 1 deletion src/components/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ type MenuItemBaseProps = {
isSmallAvatarSubscriptMenu?: boolean;

/** The type of brick road indicator to show. */
brickRoadIndicator?: ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS>;
brickRoadIndicator?: ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | '' | null;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved

/** Should render the content in HTML format */
shouldRenderAsHTML?: boolean;
Expand Down
5 changes: 4 additions & 1 deletion src/components/TextInput/BaseTextInput/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {GestureResponderEvent, StyleProp, TextInputProps, TextStyle, ViewStyle} from 'react-native';
import type {GestureResponderEvent, Role, StyleProp, TextInputProps, TextStyle, ViewStyle} from 'react-native';
import type {AnimatedTextInputRef} from '@components/RNTextInput';
import type {MaybePhraseKey} from '@libs/Localize';
import type IconAsset from '@src/types/utils/IconAsset';
Expand Down Expand Up @@ -108,6 +108,9 @@ type CustomBaseTextInputProps = {

/** Type of autocomplete */
autoCompleteType?: string;

/** Keyboard type */
role?: Role;
};
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved

type BaseTextInputRef = HTMLFormElement | AnimatedTextInputRef;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type AvatarRange = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |

type AvatarSource = IconAsset | string;

type LoginListIndicator = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | '';
type LoginListIndicator = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | undefined;

/**
* Searches through given loginList for any contact method / login with an error.
Expand Down Expand Up @@ -59,7 +59,7 @@ function getLoginListBrickRoadIndicator(loginList: Record<string, Login>): Login
if (hasLoginListInfo(loginList)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
return '';
return undefined;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ValidationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function isValidPersonName(value: string) {
/**
* Checks if the provided string includes any of the provided reserved words
*/
function doesContainReservedWord(value: string, reservedWords: string[]): boolean {
function doesContainReservedWord(value: string, reservedWords: readonly string[]): boolean {
const valueToCheck = value.trim().toLowerCase();
return reservedWords.some((reservedWord) => valueToCheck.includes(reservedWord.toLowerCase()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import lodashGet from 'lodash/get';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {InteractionManager, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import EmojiPickerButtonDropdown from '@components/EmojiPicker/EmojiPickerButtonDropdown';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues, FormRef} from '@components/Form/types';
import HeaderPageLayout from '@components/HeaderPageLayout';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
Expand All @@ -13,57 +15,57 @@
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes} from '@components/withCurrentUserPersonalDetails';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import DateUtils from '@libs/DateUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/SettingsStatusSetForm';
import type {Status} from '@src/types/onyx/PersonalDetails';

const INPUT_IDS = {
EMOJI_CODE: 'emojiCode',
STATUS_TEXT: 'statusText',
type StatusPageOnyxProps = {
draftStatus: OnyxEntry<Status>;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
};

const propTypes = {
...withCurrentUserPersonalDetailsPropTypes,
};
type StatusPageProps = StatusPageOnyxProps & WithCurrentUserPersonalDetailsProps;

const initialEmoji = '💬';

function StatusPage({draftStatus, currentUserPersonalDetails}) {
function StatusPage({draftStatus, currentUserPersonalDetails}: StatusPageProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const formRef = useRef(null);
const [brickRoadIndicator, setBrickRoadIndicator] = useState('');
const currentUserEmojiCode = lodashGet(currentUserPersonalDetails, 'status.emojiCode', '');
const currentUserStatusText = lodashGet(currentUserPersonalDetails, 'status.text', '');
const currentUserClearAfter = lodashGet(currentUserPersonalDetails, 'status.clearAfter', '');
const draftEmojiCode = lodashGet(draftStatus, 'emojiCode');
const draftText = lodashGet(draftStatus, 'text');
const draftClearAfter = lodashGet(draftStatus, 'clearAfter');

const defaultEmoji = draftEmojiCode || currentUserEmojiCode;
const defaultText = draftText || currentUserStatusText;
const formRef = useRef<FormRef>(null);
const [brickRoadIndicator, setBrickRoadIndicator] = useState<ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | '' | null>('');
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
const currentUserEmojiCode = currentUserPersonalDetails?.status?.emojiCode ?? '';
const currentUserStatusText = currentUserPersonalDetails?.status?.text ?? '';
const currentUserClearAfter = currentUserPersonalDetails?.status?.clearAfter ?? '';
const draftEmojiCode = draftStatus?.emojiCode;
const draftText = draftStatus?.text;
const draftClearAfter = draftStatus?.clearAfter;

const defaultEmoji = draftEmojiCode ?? currentUserEmojiCode;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
const defaultText = draftText ?? currentUserStatusText;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved

const customClearAfter = useMemo(() => {
const dataToShow = draftClearAfter || currentUserClearAfter;
const dataToShow = draftClearAfter ?? currentUserClearAfter;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
return DateUtils.getLocalizedTimePeriodDescription(dataToShow);
}, [draftClearAfter, currentUserClearAfter]);

const isValidClearAfterDate = useCallback(() => {
const clearAfterTime = draftClearAfter || currentUserClearAfter;
if (clearAfterTime === CONST.CUSTOM_STATUS_TYPES.NEVER || clearAfterTime === '') {
const clearAfterTime = draftClearAfter ?? currentUserClearAfter;
if (clearAfterTime === CONST.CUSTOM_STATUS_TYPES.NEVER ?? clearAfterTime === '') {
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand All @@ -72,8 +74,9 @@

const navigateBackToPreviousScreen = useCallback(() => Navigation.goBack(), []);
const updateStatus = useCallback(
({emojiCode, statusText}) => {
const clearAfterTime = draftClearAfter || currentUserClearAfter || CONST.CUSTOM_STATUS_TYPES.NEVER;
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM>) => {
const {emojiCode, statusText} = values;
const clearAfterTime = draftClearAfter ?? currentUserClearAfter ?? CONST.CUSTOM_STATUS_TYPES.NEVER;
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
const isValid = DateUtils.isTimeAtLeastOneMinuteInFuture({dateTimeString: clearAfterTime});
if (!isValid && clearAfterTime !== CONST.CUSTOM_STATUS_TYPES.NEVER) {
setBrickRoadIndicator(isValidClearAfterDate() ? null : CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR);
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -100,7 +103,7 @@
emojiCode: '',
clearAfter: DateUtils.getEndOfToday(),
});
formRef.current.resetForm({[INPUT_IDS.EMOJI_CODE]: ''});
formRef.current?.resetForm?.({[INPUT_IDS.EMOJI_CODE]: ''});
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
InteractionManager.runAfterInteractions(() => {
navigateBackToPreviousScreen();
});
Expand All @@ -110,18 +113,18 @@

useEffect(() => {
if (!currentUserEmojiCode && !currentUserClearAfter && !draftClearAfter) {
User.updateDraftCustomStatus({clearAfter: DateUtils.getEndOfToday()});
User.updateDraftCustomStatus({clearAfter: DateUtils.getEndOfToday()} as Status);
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
} else {
User.updateDraftCustomStatus({clearAfter: currentUserClearAfter});
User.updateDraftCustomStatus({clearAfter: currentUserClearAfter} as Status);
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
}

return () => User.clearDraftCustomStatus();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const validateForm = useCallback(() => {
const validateForm = useCallback((): FormInputErrors<typeof ONYXKEYS.FORMS.SETTINGS_STATUS_SET_FORM> => {
if (brickRoadIndicator) {
return {clearAfter: ''};
return {clearAfter: '' as TranslationPaths};
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
}
return {};
}, [brickRoadIndicator]);
Expand Down Expand Up @@ -157,6 +160,7 @@
<InputWrapper
InputComponent={EmojiPickerButtonDropdown}
inputID={INPUT_IDS.EMOJI_CODE}
// @ts-expect-error TODO: Remove ts-expect-error when EmojiPickerButtonDropdown migration is done
accessibilityLabel={INPUT_IDS.EMOJI_CODE}
role={CONST.ACCESSIBILITY_ROLE.TEXT}
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
defaultValue={defaultEmoji}
Expand All @@ -166,7 +170,7 @@
InputComponent={TextInput}
ref={inputCallbackRef}
inputID={INPUT_IDS.STATUS_TEXT}
role={CONST.ACCESSIBILITY_ROLE.TEXT}

Check failure on line 173 in src/pages/settings/Profile/CustomStatus/StatusPage.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Type '"text"' is not assignable to type 'Role | undefined'.
label={translate('statusPage.message')}
accessibilityLabel={INPUT_IDS.STATUS_TEXT}
defaultValue={defaultText}
Expand All @@ -181,7 +185,7 @@
containerStyle={styles.pr2}
brickRoadIndicator={brickRoadIndicator}
/>
{(!!currentUserEmojiCode || !!currentUserStatusText) && (
{(!!currentUserEmojiCode ?? !!currentUserStatusText) && (
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
<MenuItem
title={translate('statusPage.clearStatus')}
titleStyle={styles.ml0}
Expand All @@ -198,13 +202,11 @@
}

StatusPage.displayName = 'StatusPage';
StatusPage.propTypes = propTypes;

export default compose(
withCurrentUserPersonalDetails,
withOnyx({
export default withCurrentUserPersonalDetails(
withOnyx<StatusPageProps, StatusPageOnyxProps>({
draftStatus: {
key: () => ONYXKEYS.CUSTOM_STATUS_DRAFT,
},
}),
)(StatusPage);
})(StatusPage),
);
Loading
Loading