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 'Policy.js' lib to TypeScript #33691

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c037865
Rename Policy file
BartoszGrajdek Nov 28, 2023
ddaf7b9
TS migration
BartoszGrajdek Nov 30, 2023
66fbaf9
Merge remote-tracking branch 'origin/main' into ts-migration/policy-a…
BartoszGrajdek Nov 30, 2023
a5ea5d1
TS migration of Policy.js
BartoszGrajdek Dec 6, 2023
bc82922
Resolve merge conflicts
BartoszGrajdek Dec 6, 2023
ecb7dfe
Add changes to ReportUtils & merge main
BartoszGrajdek Dec 13, 2023
a793486
add remaining typescript typing to the policy lib
cdOut Dec 28, 2023
ab6b95f
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 2, 2024
1097bc1
add review suggested changes
cdOut Jan 2, 2024
fa05286
Fix typecheck and lint
blazejkustra Jan 2, 2024
f9ec088
Merge branch 'main' into ts-migration/policy-action-lib
blazejkustra Jan 2, 2024
18a8891
Final Policy adjustements
blazejkustra Jan 2, 2024
55648a3
Add missing return types and change Record to stricter types
blazejkustra Jan 2, 2024
87610b2
Merge branch 'main' into ts-migration/policy-action-lib
blazejkustra Jan 2, 2024
fa42c08
Make isHarvestingEnabled optional
blazejkustra Jan 2, 2024
11c7a4c
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 2, 2024
46e4bba
Merge branch 'main' into ts-migration/policy-action-lib
blazejkustra Jan 3, 2024
4f1adec
add suggested review changes and refactor
cdOut Jan 4, 2024
d0e0cbb
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 4, 2024
bd95ad8
fix correct type imports
cdOut Jan 4, 2024
81d9208
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 8, 2024
b0a9ed9
implement fix and review suggestions
cdOut Jan 9, 2024
6d8d468
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 9, 2024
97e9b11
add minor code refactors
cdOut Jan 9, 2024
7613c69
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 9, 2024
d9ef0b2
fix if statement in unit and rate update
cdOut Jan 10, 2024
5a2541f
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 10, 2024
7e534e9
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 11, 2024
ef4c055
implement finallyData and fix merge errors
cdOut Jan 11, 2024
5ba57d1
include required areChatRoomsEnabled in policy creation
cdOut Jan 11, 2024
58ac053
change properties from optional to required for CustomUnit
cdOut Jan 11, 2024
ce0205d
remove NewCustomUnit type and refactor rest accordingly
cdOut Jan 11, 2024
2d67506
refactor two api writes for finallyData
cdOut Jan 11, 2024
cdc8486
refactor return type of getNewPersonalDetailsOnyxData
cdOut Jan 17, 2024
59556b2
fix CustomUnit rates typing
cdOut Jan 17, 2024
6c72ef8
remove usage of isNotEmptyObject for in PR related files
cdOut Jan 17, 2024
b1e2aeb
Merge branch 'main' into ts-migration/policy-action-lib
cdOut Jan 17, 2024
b9f4551
fix double negation lint error
cdOut Jan 17, 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 assets/emojis/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const emojis: PickerEmojis = [
code: '😊',
},
{
name: 'innocent',
name: ':innocent:',
cdOut marked this conversation as resolved.
Show resolved Hide resolved
code: '😇',
},
{
Expand Down
8 changes: 6 additions & 2 deletions src/libs/PersonalDetailsUtils.js
cdOut marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function getAccountIDsByLogins(logins) {
/**
* Given a list of accountIDs, find the associated personal detail and return related logins.
*
* @param {Array<number>} accountIDs Array of user accountIDs
* @param {Array<number | string>} accountIDs Array of user accountIDs
* @returns {Array} - Array of logins according to passed accountIDs
*/
function getLoginsByAccountIDs(accountIDs) {
Expand All @@ -101,7 +101,11 @@ function getLoginsByAccountIDs(accountIDs) {
*
* @param {Array<string>} logins Array of user logins
* @param {Array<number>} accountIDs Array of user accountIDs
* @returns {Object} - Object with optimisticData, successData and failureData (object of personal details objects)
* @typedef {Object} OnyxData
* @property {Array<Object>} optimisticData
* @property {Array<Object>} successData
* @property {Array<Object>} failureData
* @returns {OnyxData} - Object with optimisticData, successData and failureData (object of personal details objects)
*/
function getNewPersonalDetailsOnyxData(logins, accountIDs) {
const optimisticData = {};
Expand Down
54 changes: 17 additions & 37 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import lodashEscape from 'lodash/escape';
import lodashFindLastIndex from 'lodash/findLastIndex';
import lodashIntersection from 'lodash/intersection';
import lodashIsEqual from 'lodash/isEqual';
import React from 'react';
import Onyx, {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import {SvgProps} from 'react-native-svg';
import {ValueOf} from 'type-fest';
Expand All @@ -16,10 +15,11 @@ import CONST from '@src/CONST';
import {ParentNavigationSummaryParams, TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {Beta, Login, PersonalDetails, Policy, PolicyTags, Report, ReportAction, Session, Transaction} from '@src/types/onyx';
import {Beta, Login, PersonalDetails, PersonalDetailsList, Policy, PolicyTags, Report, ReportAction, Session, Transaction} from '@src/types/onyx';
import {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon';
import {ChangeLog, IOUMessage, OriginalMessageActionName} from '@src/types/onyx/OriginalMessage';
import {Message, ReportActions} from '@src/types/onyx/ReportAction';
import {ChangeLog, IOUMessage, OriginalMessageActionName, OriginalMessageCreated} from '@src/types/onyx/OriginalMessage';
import {NotificationPreference} from '@src/types/onyx/Report';
import {Message, ReportActionBase, ReportActions} from '@src/types/onyx/ReportAction';
import {Receipt, WaypointCollection} from '@src/types/onyx/Transaction';
import DeepValueOf from '@src/types/utils/DeepValueOf';
import {EmptyObject, isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject';
Expand Down Expand Up @@ -187,10 +187,8 @@ type OptimisticClosedReportAction = Pick<
'actionName' | 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'originalMessage' | 'pendingAction' | 'person' | 'reportActionID' | 'shouldShow'
>;

type OptimisticCreatedReportAction = Pick<
ReportAction,
'actionName' | 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'
>;
type OptimisticCreatedReportAction = OriginalMessageCreated &
Pick<ReportActionBase, 'actorAccountID' | 'automatic' | 'avatar' | 'created' | 'message' | 'person' | 'reportActionID' | 'shouldShow' | 'pendingAction'>;

type OptimisticChatReport = Pick<
Report,
Expand Down Expand Up @@ -1409,7 +1407,7 @@ function getDisplayNameForParticipant(accountID?: number, shouldUseShortForm = f
}

function getDisplayNamesWithTooltips(
personalDetailsList: PersonalDetails[] | Record<string, PersonalDetails>,
personalDetailsList: PersonalDetails[] | PersonalDetailsList,
isMultipleParticipantReport: boolean,
shouldFallbackToHidden = true,
): DisplayNameWithTooltips {
Expand All @@ -1423,7 +1421,7 @@ function getDisplayNamesWithTooltips(
const avatar = UserUtils.getDefaultAvatar(accountID);

let pronouns = user.pronouns;
if (pronouns && pronouns.startsWith(CONST.PRONOUNS.PREFIX)) {
if (pronouns?.startsWith(CONST.PRONOUNS.PREFIX)) {
const pronounTranslationKey = pronouns.replace(CONST.PRONOUNS.PREFIX, '');
pronouns = Localize.translateLocal(`pronouns.${pronounTranslationKey}` as TranslationPaths);
}
Expand Down Expand Up @@ -1547,9 +1545,9 @@ function isUnreadWithMention(report: OnyxEntry<Report> | OptionData): boolean {

/**
* Determines if the option requires action from the current user. This can happen when it:
- is unread and the user was mentioned in one of the unread comments
- is for an outstanding task waiting on the user
- has an outstanding child money request that is waiting for an action from the current user (e.g. pay, approve, add bank account)
- is unread and the user was mentioned in one of the unread comments
- is for an outstanding task waiting on the user
- has an outstanding child money request that is waiting for an action from the current user (e.g. pay, approve, add bank account)
*
* @param option (report or optionItem)
* @param parentReportAction (the report action the current report is a thread of)
Expand Down Expand Up @@ -2376,7 +2374,7 @@ function getParsedComment(text: string): string {
return text.length <= CONST.MAX_MARKUP_LENGTH ? parser.replace(text) : lodashEscape(text);
}

function buildOptimisticAddCommentReportAction(text?: string, file?: File & {source: string; uri: string}): OptimisticReportAction {
function buildOptimisticAddCommentReportAction(text?: string, file?: File): OptimisticReportAction {
const parser = new ExpensiMark();
const commentText = getParsedComment(text ?? '');
const isAttachment = !text && file !== undefined;
Expand Down Expand Up @@ -3052,7 +3050,7 @@ function buildOptimisticChatReport(
oldPolicyName = '',
visibility: ValueOf<typeof CONST.REPORT.VISIBILITY> | undefined = undefined,
writeCapability: ValueOf<typeof CONST.REPORT.WRITE_CAPABILITIES> | undefined = undefined,
notificationPreference: string | number = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
notificationPreference: NotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS,
parentReportActionID = '',
parentReportID = '',
welcomeMessage = '',
Expand Down Expand Up @@ -4252,33 +4250,16 @@ function shouldDisableWelcomeMessage(report: OnyxEntry<Report>, policy: OnyxEntr
return isMoneyRequestReport(report) || isArchivedRoom(report) || !isChatRoom(report) || isChatThread(report) || !PolicyUtils.isPolicyAdmin(policy);
}

function shouldAutoFocusOnKeyPress(event: KeyboardEvent): boolean {
if (event.key.length > 1) {
return false;
}

// If a key is pressed in combination with Meta, Control or Alt do not focus
if (event.ctrlKey || event.metaKey) {
return false;
}

if (event.code === 'Space') {
return false;
}

return true;
}

/**
* Navigates to the appropriate screen based on the presence of a private note for the current user.
*/
function navigateToPrivateNotes(report: Report, session: Session) {
if (isEmpty(report) || isEmpty(session)) {
if (isEmpty(report) || isEmpty(session) || !session.accountID) {
return;
}
const currentUserPrivateNote = report.privateNotes?.[String(session.accountID)]?.note ?? '';
const currentUserPrivateNote = report.privateNotes?.[session.accountID]?.note ?? '';
if (isEmpty(currentUserPrivateNote)) {
Navigation.navigate(ROUTES.PRIVATE_NOTES_EDIT.getRoute(report.reportID, String(session.accountID)));
Navigation.navigate(ROUTES.PRIVATE_NOTES_EDIT.getRoute(report.reportID, session.accountID));
return;
}
Navigation.navigate(ROUTES.PRIVATE_NOTES_LIST.getRoute(report.reportID));
Expand Down Expand Up @@ -4451,7 +4432,6 @@ export {
shouldDisableWelcomeMessage,
navigateToPrivateNotes,
canEditWriteCapability,
shouldAutoFocusOnKeyPress,
};

export type {OptionData};
export type {OptionData, OptimisticChatReport, OptimisticClosedReportAction};
Loading
Loading