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 AttachmentCarousel/index.js to function component #23072

Merged
merged 8 commits into from
Jul 27, 2023
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
2 changes: 1 addition & 1 deletion src/components/AttachmentCarousel/CarouselActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Carousel(props) {
unsubscribeRightKey();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [props.onCycleThroughAttachments]);

return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import PropTypes from 'prop-types';
import reportActionPropTypes from '../../pages/home/report/reportActionPropTypes';
import reportPropTypes from '../../pages/reportPropTypes';

const propTypes = {
/** source is used to determine the starting index in the array of attachments */
source: PropTypes.string,

/** Callback to update the parent modal's state with a source and name from the attachments array */
onNavigate: PropTypes.func,

/** Object of report actions for this report */
reportActions: PropTypes.shape(reportActionPropTypes),

/** The report currently being looked at */
report: reportPropTypes.isRequired,
};

const defaultProps = {
source: '',
reportActions: {},
onNavigate: () => {},
};

export {propTypes, defaultProps};
65 changes: 65 additions & 0 deletions src/components/AttachmentCarousel/createInitialState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import _ from 'underscore';
import {Parser as HtmlParser} from 'htmlparser2';
import * as DeviceCapabilities from '../../libs/DeviceCapabilities';
import * as ReportActionsUtils from '../../libs/ReportActionsUtils';
import tryResolveUrlFromApiRoot from '../../libs/tryResolveUrlFromApiRoot';
import CONST from '../../CONST';

/**
* Constructs the initial component state from report actions
* @param {propTypes} props
* @returns {{page: Number, attachments: Array}}
*/
function createInitialState(props) {
const actions = [ReportActionsUtils.getParentReportAction(props.report), ...ReportActionsUtils.getSortedReportActions(_.values(props.reportActions))];
const attachments = [];

const htmlParser = new HtmlParser({
onopentag: (name, attribs) => {
if (name !== 'img' || !attribs.src) {
return;
}

const expensifySource = attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE];

// By iterating actions in chronological order and prepending each attachment
// we ensure correct order of attachments even across actions with multiple attachments.
attachments.unshift({
source: tryResolveUrlFromApiRoot(expensifySource || attribs.src),
isAuthTokenRequired: Boolean(expensifySource),
file: {name: attribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE]},
});
},
});

_.forEach(actions, (action, key) => {
if (!ReportActionsUtils.shouldReportActionBeVisible(action, key)) {
return;
}
htmlParser.write(_.get(action, ['message', 0, 'html']));
});
htmlParser.end();

// Inverting the list for touchscreen devices that can swipe or have an animation when scrolling
// promotes the natural feeling of swiping left/right to go to the next/previous image
// We don't want to invert the list for desktop/web because this interferes with mouse
// wheel or trackpad scrolling (in cases like document preview where you can scroll vertically)
if (DeviceCapabilities.canUseTouchScreen()) {
attachments.reverse();
}

const page = _.findIndex(attachments, (a) => a.source === props.source);
if (page === -1) {
throw new Error('Attachment not found');
}

// Update the parent modal's state with the source and name from the mapped attachments
props.onNavigate(attachments[page]);

return {
page,
attachments,
};
}

export default createInitialState;
Loading