Skip to content

Commit

Permalink
WIP linking utils
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Sep 30, 2023
1 parent 5cd7e4c commit 65edb74
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions src/libs/ReportActionsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,163 @@ function getSortedReportActions(reportActions, shouldSortInDescendingOrder = fal
.value();
}

// /**
// * Given an object of reportActions, sorts them, and then adds the previousReportActionID to each item except the first.
// * @param {Object} reportActions
// * @returns {Array}
// */
// function processReportActions(reportActions) {
// // TODO:
// // Separate new and sorted reportActions
// const newReportActions = _.filter(reportActions, (action) => !action.previousReportActionID);
// const sortedReportActions = _.filter(reportActions, (action) => action.previousReportActionID);

// // console.log(
// // 'getChat.Sort.N.0',
// // newReportActions.length,
// // newReportActions.map(({message}) => message[0].text),
// // );
// // console.log(
// // 'getChat.Sort.N.0.0',
// // newReportActions.length,
// // newReportActions.map(({previousReportActionID}) => previousReportActionID),
// // );
// // console.log(
// // 'getChat.Sort.S.0',
// // sortedReportActions.length,
// // sortedReportActions.map(({message}) => message[0].text),
// // );
// // console.log(
// // 'getChat.Sort.S.0.0',
// // sortedReportActions.length,
// // sortedReportActions.map(({previousReportActionID}) => previousReportActionID),
// // );
// // Sort the new reportActions
// const sortedNewReportActions = getSortedReportActionsForDisplay(newReportActions);

// // console.log(
// // 'getChat.SORT.SS',
// // JSON.stringify(
// // sortedNewReportActions.map(({message, reportActionID, previousReportActionID}) => ({
// // message: message[0].text,
// // reportActionID,
// // previousReportActionID,
// // })),
// // ),
// // );

// // Then, iterate through the sorted new reportActions and add the previousReportActionID to each item except the first
// const processedReportActions = sortedNewReportActions.map((action, index) => {
// if (index === sortedNewReportActions.length - 1) {
// return action; // Return the first item as is
// }
// return {
// ...action,
// previousReportActionID: sortedNewReportActions[index + 1].reportActionID,
// };
// });

// // console.log(
// // 'getChat.SORT.BEFORE',
// // JSON.stringify(
// // processedReportActions.map(({message, reportActionID, previousReportActionID}) => ({
// // message: message[0].text,
// // reportActionID,
// // previousReportActionID,
// // })),
// // ),
// // );
// if (processedReportActions[processedReportActions.length - 1]?.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED) {
// processedReportActions.pop();
// }
// // console.log(
// // 'getChat.SORT.AFTER',
// // JSON.stringify(
// // processedReportActions.map(({message, reportActionID, previousReportActionID}) => ({
// // message: message[0].text,
// // reportActionID,
// // previousReportActionID,
// // })),
// // ),
// // );

// // Determine the order of merging based on reportActionID values
// const lastSortedReportActionID = _.last(sortedReportActions)?.reportActionTimestamp || 0;
// const firstProcessedReportActionID = _.first(processedReportActions)?.reportActionTimestamp || Infinity;

// // console.log('getChat.Sort.1', getSortedReportActionsForDisplay(reportActions).length, [...sortedReportActions, ...processedReportActions].length);
// // console.log('getChat.Sort.1.1', _.last(sortedReportActions), _.first(processedReportActions));
// if (firstProcessedReportActionID > lastSortedReportActionID) {
// // console.log(
// // 'getChat.Sort.2',
// // [...sortedReportActions, ...processedReportActions].map(({message}) => message[0].text),
// // );
// return [...sortedReportActions, ...processedReportActions];
// } else {
// // console.log(
// // 'getChat.Sort.3',
// // [...processedReportActions, ...sortedReportActions].map(({message}) => message[0].text),
// // );
// return [...processedReportActions, ...sortedReportActions];
// }
// }

// Usage:
// const reportActions = [
// { reportActionID: '1' },
// { reportActionID: '2', previousReportActionID: '1' },
// { reportActionID: '3' }
// ];
// const updatedActions = processReportActions(reportActions);
// console.log(updatedActions);

function getRangeFromArrayByID(array, id) {
// without gaps
let index;

if (id) {
index = array.findIndex((obj) => obj.reportActionID === id);
} else {
index = 0;
}

if (index === -1) {
return [];
}

let startIndex = index;
let endIndex = index;

// Move down the list and compare reportActionID with previousReportActionID
while (endIndex < array.length - 1 && array[endIndex].previousReportActionID === array[endIndex + 1].reportActionID) {
endIndex++;
}

// Move up the list and compare previousReportActionID with reportActionID
while (startIndex > 0 && array[startIndex].reportActionID === array[startIndex - 1].previousReportActionID) {
startIndex--;
}

return array.slice(startIndex, endIndex + 1);
// return array.slice(startIndex, endIndex);
}

function getSlicedRangeFromArrayByID(array, id) {
let index;
if (id) {
index = array.findIndex((obj) => obj.reportActionID === id);
} else {
index = array.length - 1;
}

if (index === -1) {
return [];
}

// return array.slice(0, index+1);
return array.slice(index, array.length);
}

/**
* Finds most recent IOU request action ID.
*
Expand Down Expand Up @@ -696,4 +853,7 @@ export {
getAllReportActions,
isReportActionAttachment,
isNotifiableReportAction,
// processReportActions,
getRangeFromArrayByID,
getSlicedRangeFromArrayByID,
};

0 comments on commit 65edb74

Please sign in to comment.