-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
fix error when clicking on attachment note #40844
Merged
chiragsalian
merged 20 commits into
Expensify:main
from
nkdengineer:fix/38835-implement-attachment-screen
May 30, 2024
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ba9cd58
fix error when clicking on attachment note
nkdengineer 68f9115
fix navigation issue
nkdengineer 5d5fe29
fix remove unused change
nkdengineer 07cb16f
fix remove unused change
nkdengineer da2dbb1
fix merge main
nkdengineer 8d8867d
fix remove unused change
nkdengineer 7fbaabe
fix typecheck
nkdengineer c48b0aa
fix lint
nkdengineer aef5252
fix native issue
nkdengineer a27b27d
Merge branch 'main' into fix/38835-implement-attachment-screen
nkdengineer e59e1ab
fix lint
nkdengineer 313b263
fix merge main
nkdengineer d563e1f
fix merge main
nkdengineer 14589f8
fix create extractAttachments
nkdengineer ac97fb6
fix lint
nkdengineer a4ab1d7
fix remove redundant attachment type
nkdengineer 8a9b035
merge main
nkdengineer bc4418a
Merge branch 'main' into fix/38835-implement-attachment-screen
nkdengineer d4b0743
Merge branch 'main' into fix/38835-implement-attachment-screen
nkdengineer bbaf3bc
fix lint
nkdengineer 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 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
This file contains 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
This file contains 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
This file contains 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,22 @@ | ||
import {createContext} from 'react'; | ||
import type {ValueOf} from 'type-fest'; | ||
import type CONST from '@src/CONST'; | ||
|
||
type AttachmentContextProps = { | ||
type?: ValueOf<typeof CONST.ATTACHMENT_TYPE>; | ||
reportID?: string; | ||
accountID?: number; | ||
}; | ||
|
||
const AttachmentContext = createContext<AttachmentContextProps>({ | ||
type: undefined, | ||
reportID: undefined, | ||
accountID: undefined, | ||
}); | ||
|
||
AttachmentContext.displayName = 'AttachmentContext'; | ||
|
||
export { | ||
// eslint-disable-next-line import/prefer-default-export | ||
AttachmentContext, | ||
}; |
This file contains 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
80 changes: 80 additions & 0 deletions
80
src/components/Attachments/AttachmentCarousel/extractAttachmentsFromNote.ts
This file contains 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,80 @@ | ||
import {Parser as HtmlParser} from 'htmlparser2'; | ||
import type {Attachment} from '@components/Attachments/types'; | ||
import * as FileUtils from '@libs/fileDownload/FileUtils'; | ||
import {getReport} from '@libs/ReportUtils'; | ||
import tryResolveUrlFromApiRoot from '@libs/tryResolveUrlFromApiRoot'; | ||
import CONST from '@src/CONST'; | ||
|
||
/** | ||
* Constructs the initial component state from report actions | ||
*/ | ||
function extractAttachmentsFromNote(reportID: string, accountID: number) { | ||
const report = getReport(reportID); | ||
const privateNotes = report?.privateNotes; | ||
const targetNote = privateNotes?.[Number(accountID)]?.note ?? ''; | ||
const attachments: Attachment[] = []; | ||
|
||
// We handle duplicate image sources by considering the first instance as original. Selecting any duplicate | ||
// and navigating back (<) shows the image preceding the first instance, not the selected duplicate's position. | ||
const uniqueSources = new Set(); | ||
|
||
const htmlParser = new HtmlParser({ | ||
onopentag: (name, attribs) => { | ||
if (name === 'video') { | ||
const source = tryResolveUrlFromApiRoot(attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]); | ||
if (uniqueSources.has(source)) { | ||
return; | ||
} | ||
|
||
uniqueSources.add(source); | ||
const splittedUrl = attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE].split('/'); | ||
attachments.unshift({ | ||
source: tryResolveUrlFromApiRoot(attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]), | ||
isAuthTokenRequired: Boolean(attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]), | ||
file: {name: splittedUrl[splittedUrl.length - 1]}, | ||
duration: Number(attribs[CONST.ATTACHMENT_DURATION_ATTRIBUTE]), | ||
isReceipt: false, | ||
hasBeenFlagged: false, | ||
}); | ||
return; | ||
} | ||
|
||
if (name === 'img' && attribs.src) { | ||
const expensifySource = attribs[CONST.ATTACHMENT_SOURCE_ATTRIBUTE]; | ||
const source = tryResolveUrlFromApiRoot(expensifySource || attribs.src); | ||
if (uniqueSources.has(source)) { | ||
return; | ||
} | ||
|
||
uniqueSources.add(source); | ||
let fileName = attribs[CONST.ATTACHMENT_ORIGINAL_FILENAME_ATTRIBUTE] || FileUtils.getFileName(`${source}`); | ||
|
||
// Public image URLs might lack a file extension in the source URL, without an extension our | ||
// AttachmentView fails to recognize them as images and renders fallback content instead. | ||
// We apply this small hack to add an image extension and ensure AttachmentView renders the image. | ||
const fileInfo = FileUtils.splitExtensionFromFileName(fileName); | ||
if (!fileInfo.fileExtension) { | ||
fileName = `${fileInfo.fileName || 'image'}.jpg`; | ||
} | ||
|
||
// By iterating actions in chronological order and prepending each attachment | ||
// we ensure correct order of attachments even across actions with multiple attachments. | ||
attachments.unshift({ | ||
reportActionID: attribs['data-id'], | ||
source, | ||
isAuthTokenRequired: Boolean(expensifySource), | ||
file: {name: fileName}, | ||
isReceipt: false, | ||
hasBeenFlagged: attribs['data-flagged'] === 'true', | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
htmlParser.write(targetNote); | ||
htmlParser.end(); | ||
|
||
return attachments.reverse(); | ||
nkdengineer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export default extractAttachmentsFromNote; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give me example of type
a
, ands
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, these types are not being used. I think we can remove these and add them in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why these types were added then?
Just confirming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added them based on the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kidroca Can you help me understand why do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is just an example, you can skip the keys that don't have application here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nkdengineer Can you make the change here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shubham1206agra I removed it