-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Migrate AttachmentCarousel/index.js to function component #23072
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
Merged
joelbettner
merged 8 commits into
Expensify:main
from
jczekalski:migrate-attachment-carousel
Jul 27, 2023
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
67c8369
WIP
jczekalski d708756
remove disable-next-line comments, add useCallbacks
jczekalski fa61790
fix keyboard shortcuts
jczekalski 797bb5d
extract prop types, helper function
jczekalski 24ca183
fix issue with reversing attachments on mobile
jczekalski caea303
remove unnecessary effect
jczekalski c92817e
naming change
jczekalski dc46c7c
CR fixes
jczekalski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/components/AttachmentCarousel/attachmentCarouselPropTypes.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import PropTypes from 'prop-types'; | ||
import {withLocalizePropTypes} from '../withLocalize'; | ||
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.objectOf(PropTypes.shape(reportActionPropTypes)), | ||
|
||
/** The report currently being looked at */ | ||
report: reportPropTypes.isRequired, | ||
|
||
...withLocalizePropTypes, | ||
}; | ||
|
||
const defaultProps = { | ||
source: '', | ||
reportActions: {}, | ||
onNavigate: () => {}, | ||
}; | ||
|
||
export {propTypes, defaultProps}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.