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

Reduce string unions in Route type (part 2) #55065

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
10 changes: 5 additions & 5 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ const ROUTES = {
},
PROFILE: {
route: 'a/:accountID',
getRoute: (accountID?: string | number, backTo?: string, login?: string) => {
const baseRoute = getUrlWithBackToParam(`a/${accountID as string}`, backTo);
getRoute: (accountID?: number, backTo?: string, login?: string) => {
const baseRoute = getUrlWithBackToParam(`a/${accountID}`, backTo);
const loginParam = login ? `?login=${encodeURIComponent(login)}` : '';
return `${baseRoute}${loginParam}` as const;
},
},
PROFILE_AVATAR: {
route: 'a/:accountID/avatar',
getRoute: (accountID: string | number) => `a/${accountID as string}/avatar` as const,
getRoute: (accountID: number) => `a/${accountID}/avatar` as const,
},

GET_ASSISTANCE: {
Expand Down Expand Up @@ -411,15 +411,15 @@ const ROUTES = {
},
PRIVATE_NOTES_EDIT: {
route: 'r/:reportID/notes/:accountID/edit',
getRoute: (reportID: string, accountID: string | number, backTo?: string) => getUrlWithBackToParam(`r/${reportID}/notes/${accountID as string}/edit` as const, backTo),
getRoute: (reportID: string, accountID: number, backTo?: string) => getUrlWithBackToParam(`r/${reportID}/notes/${accountID}/edit` as const, backTo),
},
ROOM_MEMBERS: {
route: 'r/:reportID/members',
getRoute: (reportID: string, backTo?: string) => getUrlWithBackToParam(`r/${reportID}/members` as const, backTo),
},
ROOM_MEMBER_DETAILS: {
route: 'r/:reportID/members/:accountID',
getRoute: (reportID: string, accountID: string | number, backTo?: string) => getUrlWithBackToParam(`r/${reportID}/members/${accountID as string}` as const, backTo),
getRoute: (reportID: string, accountID: number, backTo?: string) => getUrlWithBackToParam(`r/${reportID}/members/${accountID}` as const, backTo),
},
ROOM_INVITE: {
route: 'r/:reportID/invite/:role?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalD
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as LocalePhoneNumber from '@libs/LocalePhoneNumber';
import * as LoginUtils from '@libs/LoginUtils';
import {formatPhoneNumber} from '@libs/LocalePhoneNumber';
import {areEmailsFromSamePrivateDomain} from '@libs/LoginUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {getAccountIDsByLogins, getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import {isArchivedNonExpenseReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -49,7 +49,7 @@ function MentionUserRenderer({style, tnode, TDefaultRenderer, currentUserPersona
}

// If the emails are not in the same private domain, we also return the displayText
if (!LoginUtils.areEmailsFromSamePrivateDomain(displayText, currentUserPersonalDetails.login ?? '')) {
if (!areEmailsFromSamePrivateDomain(displayText, currentUserPersonalDetails.login ?? '')) {
return displayText;
}

Expand All @@ -60,16 +60,16 @@ function MentionUserRenderer({style, tnode, TDefaultRenderer, currentUserPersona
if (!isEmpty(htmlAttribAccountID) && personalDetails?.[htmlAttribAccountID]) {
const user = personalDetails[htmlAttribAccountID];
accountID = parseInt(htmlAttribAccountID, 10);
mentionDisplayText = LocalePhoneNumber.formatPhoneNumber(user?.login ?? '') || PersonalDetailsUtils.getDisplayNameOrDefault(user);
mentionDisplayText = formatPhoneNumber(user?.login ?? '') || getDisplayNameOrDefault(user);
mentionDisplayText = getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID, user?.login ?? '') ?? '';
navigationRoute = ROUTES.PROFILE.getRoute(htmlAttribAccountID, Navigation.getReportRHPActiveRoute());
navigationRoute = ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute());
} else if ('data' in tnodeClone && !isEmptyObject(tnodeClone.data)) {
// We need to remove the LTR unicode and leading @ from data as it is not part of the login
mentionDisplayText = tnodeClone.data.replace(CONST.UNICODE.LTR, '').slice(1);
// We need to replace tnode.data here because we will pass it to TNodeChildrenRenderer below
asMutable(tnodeClone).data = tnodeClone.data.replace(mentionDisplayText, Str.removeSMSDomain(getShortMentionIfFound(mentionDisplayText, htmlAttributeAccountID) ?? ''));

accountID = PersonalDetailsUtils.getAccountIDsByLogins([mentionDisplayText])?.at(0) ?? -1;
accountID = getAccountIDsByLogins([mentionDisplayText])?.at(0) ?? -1;
navigationRoute = ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute(), mentionDisplayText);
mentionDisplayText = Str.removeSMSDomain(mentionDisplayText);
} else {
Expand All @@ -91,12 +91,12 @@ function MentionUserRenderer({style, tnode, TDefaultRenderer, currentUserPersona
if (isDisabled) {
return;
}
showContextMenuForReport(event, anchor, report?.reportID, action, checkIfContextMenuActive, ReportUtils.isArchivedNonExpenseReport(report, reportNameValuePairs));
showContextMenuForReport(event, anchor, report?.reportID, action, checkIfContextMenuActive, isArchivedNonExpenseReport(report, reportNameValuePairs));
}}
onPress={(event) => {
event.preventDefault();
if (!isEmpty(htmlAttribAccountID)) {
Navigation.navigate(ROUTES.PROFILE.getRoute(htmlAttribAccountID, Navigation.getReportRHPActiveRoute()));
Navigation.navigate(ROUTES.PROFILE.getRoute(parseInt(htmlAttribAccountID, 10), Navigation.getReportRHPActiveRoute()));
return;
}
Navigation.navigate(ROUTES.PROFILE.getRoute(accountID, Navigation.getReportRHPActiveRoute(), mentionDisplayText));
Expand Down
2 changes: 1 addition & 1 deletion src/components/RoomHeaderAvatars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function RoomHeaderAvatars({icons, reportID}: RoomHeaderAvatarsProps) {
}

if (icon.id) {
Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(icon.id));
Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(Number(icon.id)));
}
};

Expand Down
13 changes: 7 additions & 6 deletions src/pages/PrivateNotes/PrivateNotesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ function PrivateNotesListPage({report, accountID: sessionAccountID}: PrivateNote
*/
const privateNotes = useMemo(() => {
const privateNoteBrickRoadIndicator = (accountID: number) => (report.privateNotes?.[accountID].errors ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined);
return Object.keys(report.privateNotes ?? {}).map((accountID: string) => {
const privateNote = report.privateNotes?.[Number(accountID)];
return Object.keys(report.privateNotes ?? {}).map((privateNoteAccountID: string) => {
const accountID = Number(privateNoteAccountID);
const privateNote = report.privateNotes?.[accountID];
return {
reportID: report.reportID,
accountID,
title: Number(sessionAccountID) === Number(accountID) ? translate('privateNotes.myNote') : personalDetailsList?.[accountID]?.login ?? '',
accountID: privateNoteAccountID,
title: Number(sessionAccountID) === accountID ? translate('privateNotes.myNote') : personalDetailsList?.[privateNoteAccountID]?.login ?? '',
action: () => Navigation.navigate(ROUTES.PRIVATE_NOTES_EDIT.getRoute(report.reportID, accountID, backTo)),
brickRoadIndicator: privateNoteBrickRoadIndicator(Number(accountID)),
brickRoadIndicator: privateNoteBrickRoadIndicator(accountID),
note: privateNote?.note ?? '',
disabled: Number(sessionAccountID) !== Number(accountID),
disabled: Number(sessionAccountID) !== accountID,
};
});
}, [report, personalDetailsList, sessionAccountID, translate, backTo]);
Expand Down
66 changes: 36 additions & 30 deletions src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import {parsePhoneNumber} from '@libs/PhoneNumber';
import * as ReportUtils from '@libs/ReportUtils';
import * as UserUtils from '@libs/UserUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import {
findSelfDMReportID,
getChatByParticipants,
getReportNotificationPreference,
hasAutomatedExpensifyAccountIDs,
isConciergeChatReport,
isHiddenForCurrentUser as isReportHiddenForCurrentUser,
navigateToPrivateNotes,
} from '@libs/ReportUtils';
import {generateAccountID} from '@libs/UserUtils';
import {isValidAccountRoute} from '@libs/ValidationUtils';
import type {ProfileNavigatorParamList} from '@navigation/types';
import * as LinkActions from '@userActions/Link';
import * as PersonalDetailsActions from '@userActions/PersonalDetails';
import * as ReportActions from '@userActions/Report';
import * as SessionActions from '@userActions/Session';
import {openExternalLink} from '@userActions/Link';
import {openPublicProfilePage} from '@userActions/PersonalDetails';
import {hasErrorInPrivateNotes} from '@userActions/Report';
import {checkIfActionIsAllowed, isAnonymousUser as isAnonymousUserSession} from '@userActions/Session';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -85,14 +93,12 @@ function ProfilePage({route}: ProfilePageProps) {
selector: (account) => account?.guideCalendarLink,
});

const accountID = Number(route.params?.accountID ?? -1);
const accountID = Number(route.params?.accountID ?? CONST.DEFAULT_NUMBER_ID);
const isCurrentUser = session?.accountID === accountID;
const reportKey = useMemo(() => {
const reportID = isCurrentUser
? ReportUtils.findSelfDMReportID()
: ReportUtils.getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID ?? '-1';
const reportID = isCurrentUser ? findSelfDMReportID() : getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID;

if (SessionActions.isAnonymousUser() || !reportID) {
if (isAnonymousUserSession() || !reportID) {
return `${ONYXKEYS.COLLECTION.REPORT}0` as const;
}
return `${ONYXKEYS.COLLECTION.REPORT}${reportID}` as const;
Expand All @@ -102,7 +108,7 @@ function ProfilePage({route}: ProfilePageProps) {
const styles = useThemeStyles();
const {translate, formatPhoneNumber} = useLocalize();

const isValidAccountID = ValidationUtils.isValidAccountRoute(accountID);
const isValidAccountID = isValidAccountRoute(accountID);
const loginParams = route.params?.login;

const details = useMemo((): OnyxEntry<PersonalDetails> => {
Expand All @@ -120,11 +126,11 @@ function ProfilePage({route}: ProfilePageProps) {
return foundDetails;
}
// If we don't have the personal details in Onyx, we can create an optimistic account
const optimisticAccountID = UserUtils.generateAccountID(loginParams);
const optimisticAccountID = generateAccountID(loginParams);
return {accountID: optimisticAccountID, login: loginParams, displayName: loginParams};
}, [personalDetails, accountID, loginParams, isValidAccountID]);

const displayName = formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(details, undefined, undefined, isCurrentUser));
const displayName = formatPhoneNumber(getDisplayNameOrDefault(details, undefined, undefined, isCurrentUser));
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const fallbackIcon = details?.fallbackIcon ?? '';
const login = details?.login ?? '';
Expand All @@ -134,7 +140,7 @@ function ProfilePage({route}: ProfilePageProps) {

// If we have a reportID param this means that we
// arrived here via the ParticipantsPage and should be allowed to navigate back to it
const shouldShowLocalTime = !ReportUtils.hasAutomatedExpensifyAccountIDs([accountID]) && !isEmptyObject(timezone) && isParticipantValidated;
const shouldShowLocalTime = !hasAutomatedExpensifyAccountIDs([accountID]) && !isEmptyObject(timezone) && isParticipantValidated;
let pronouns = details?.pronouns ?? '';
if (pronouns?.startsWith(CONST.PRONOUNS.PREFIX)) {
const localeKey = pronouns.replace(CONST.PRONOUNS.PREFIX, '');
Expand All @@ -156,17 +162,17 @@ function ProfilePage({route}: ProfilePageProps) {

const navigateBackTo = route?.params?.backTo;

const notificationPreferenceValue = ReportUtils.getReportNotificationPreference(report);
const notificationPreferenceValue = getReportNotificationPreference(report);

const shouldShowNotificationPreference = !isEmptyObject(report) && !isCurrentUser && !ReportUtils.isHiddenForCurrentUser(notificationPreferenceValue);
const shouldShowNotificationPreference = !isEmptyObject(report) && !isCurrentUser && !isReportHiddenForCurrentUser(notificationPreferenceValue);
const notificationPreference = shouldShowNotificationPreference
? translate(`notificationPreferencesPage.notificationPreferences.${notificationPreferenceValue}` as TranslationPaths)
: '';

// eslint-disable-next-line rulesdir/prefer-early-return
useEffect(() => {
if (ValidationUtils.isValidAccountRoute(accountID) && !loginParams) {
PersonalDetailsActions.openPublicProfilePage(accountID);
if (isValidAccountRoute(accountID) && !loginParams) {
openPublicProfilePage(accountID);
}
}, [accountID, loginParams]);

Expand All @@ -177,13 +183,13 @@ function ProfilePage({route}: ProfilePageProps) {
}

// If it's a self DM, we only want to show the Message button if the self DM report exists because we don't want to optimistically create a report for self DM
if ((!isCurrentUser || report) && !SessionActions.isAnonymousUser()) {
if ((!isCurrentUser || report) && !isAnonymousUserSession()) {
result.push(PromotedActions.message({reportID: report?.reportID, accountID, login: loginParams}));
}
return result;
}, [accountID, isCurrentUser, loginParams, report]);

const isConcierge = ReportUtils.isConciergeChatReport(report);
const isConcierge = isConciergeChatReport(report);

return (
<ScreenWrapper testID={ProfilePage.displayName}>
Expand All @@ -197,7 +203,7 @@ function ProfilePage({route}: ProfilePageProps) {
<View style={[styles.avatarSectionWrapper, styles.pb0]}>
<PressableWithoutFocus
style={[styles.noOutline, styles.mb4]}
onPress={() => Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(String(accountID)))}
onPress={() => Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(accountID))}
accessibilityLabel={translate('common.profile')}
accessibilityRole={CONST.ROLE.BUTTON}
disabled={!hasAvatar}
Expand Down Expand Up @@ -239,7 +245,7 @@ function ProfilePage({route}: ProfilePageProps) {
)}

{/* Don't display email if current user is anonymous */}
{!(isCurrentUser && SessionActions.isAnonymousUser()) && login ? (
{!(isCurrentUser && isAnonymousUserSession()) && login ? (
<View style={[styles.mb6, styles.detailsPageSectionContainer, styles.w100]}>
<Text
style={[styles.textLabelSupporting, styles.mb1]}
Expand All @@ -248,7 +254,7 @@ function ProfilePage({route}: ProfilePageProps) {
{translate(isSMSLogin ? 'common.phoneNumber' : 'common.email')}
</Text>
<CommunicationsLink value={phoneOrEmail ?? ''}>
<UserDetailsTooltip accountID={details?.accountID ?? -1}>
<UserDetailsTooltip accountID={details?.accountID ?? CONST.DEFAULT_NUMBER_ID}>
<Text
numberOfLines={1}
style={styles.w100}
Expand Down Expand Up @@ -285,19 +291,19 @@ function ProfilePage({route}: ProfilePageProps) {
title={`${translate('privateNotes.title')}`}
titleStyle={styles.flex1}
icon={Expensicons.Pencil}
onPress={() => ReportUtils.navigateToPrivateNotes(report, session, navigateBackTo)}
onPress={() => navigateToPrivateNotes(report, session, navigateBackTo)}
wrapperStyle={styles.breakAll}
shouldShowRightIcon
brickRoadIndicator={ReportActions.hasErrorInPrivateNotes(report) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
brickRoadIndicator={hasErrorInPrivateNotes(report) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
/>
)}
{isConcierge && !!guideCalendarLink && (
<MenuItem
title={translate('videoChatButtonAndMenu.tooltip')}
icon={Expensicons.Phone}
isAnonymousAction={false}
onPress={SessionActions.checkIfActionIsAllowed(() => {
LinkActions.openExternalLink(guideCalendarLink);
onPress={checkIfActionIsAllowed(() => {
openExternalLink(guideCalendarLink);
})}
/>
)}
Expand Down
Loading
Loading