-
Notifications
You must be signed in to change notification settings - Fork 400
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
Use thumbnail images in Screenshots component #5337
Conversation
ae6c737
to
528c831
Compare
Codecov Report
@@ Coverage Diff @@
## master mozilla/addons-frontend#5337 +/- ##
==========================================
+ Coverage 96.97% 97.06% +0.09%
==========================================
Files 215 215
Lines 5382 5553 +171
Branches 1049 1089 +40
==========================================
+ Hits 5219 5390 +171
Misses 145 145
Partials 18 18
Continue to review full report at Codecov.
|
528c831
to
140314d
Compare
6daa703
to
aafb848
Compare
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.
EEsh, my eyes. I didn't know we had some bad hacks in here. Oh well.
Thanks for the clean-up and Flow/test coverage. I mainly requested minor changes.
pageYOffset: 20, | ||
}; | ||
|
||
const bounds = getThumbBoundsFn(0, fakeDocument, fakeWindow); |
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 know this signature is convenient but it's hard to maintain calling code like this. I would rather see it done with a a parameter object:
const bounds = getThumbBoundsFn(0, {
_document: fakeDocument,
_window: fakeWindow,
});
Otherwise it's too easy to make mistakes around positional arguments when refactoring without Flow. If we had Flow coverage on the test file I'd be less worried about it but we do not.
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.
sure, that works for me!
src/amo/components/ScreenShots.js
Outdated
const thumbnail = document.querySelectorAll('.pswp-thumbnails')[index]; | ||
getThumbBoundsFn: ( | ||
index: number, | ||
_document: typeof document = document, |
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.
This should be _document = typeof document !== 'undefined' ? document : null
to make sure it won't crash with a ReferenceError
in SSR mode. I'm not sure that SSR code would trigger it but we should make it safe just in case.
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.
mmh, that was to typehint the parameter, not to check for undefined.
I should still protect against undefined references though.
Note: I did use typeof document
instead of Document
, which is not white-listed for ESLint and because I was using typeof window
for the _window
parameter.
src/amo/components/ScreenShots.js
Outdated
getThumbBoundsFn: ( | ||
index: number, | ||
_document: typeof document = document, | ||
_window: typeof window = window |
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.
This should also be _window = typeof window !== 'undefined' ? window : null
to avoid a ReferenceError
in SSR mode.
src/amo/components/ScreenShots.js
Outdated
// https://github.com/minhtranite/react-photoswipe/issues/23 | ||
getThumbBoundsFn: /* istanbul ignore next */ function getThumbBoundsFn(index) { |
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.
Thanks for adding test coverage ❤️
src/amo/components/ScreenShots.js
Outdated
@@ -17,59 +23,100 @@ const PHOTO_SWIPE_OPTIONS = { | |||
counterEl: true, | |||
arrowEl: true, | |||
preloaderEl: true, | |||
// Overload getThumbsBoundsFn as workaround to | |||
// Overload getThumbBoundsFn as workaround to | |||
// https://github.com/minhtranite/react-photoswipe/issues/23 |
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.
Ouch. This hack is really unfortunate and will be fragile for us to maintain. It looks like a fix may land soon: minhtranite/react-photoswipe#36
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.
A year ago and still nothing.. Mid-term plan is to replace this gallery anyway, because it does not look super nice and we could do better.
src/amo/components/ScreenShots.js
Outdated
const formatPreviews = (previews) => ( | ||
type ExternalPreview = {| | ||
caption: string, | ||
image_size: [number, number], |
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.
It would be nice to name these like image_size: [width: number, height: number]
(or w
and h
would be fine)
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.
ah snap, there is no named tuples in Flow, so we cannot name the elements in the array.
src/amo/components/ScreenShots.js
Outdated
caption: string, | ||
image_size: [number, number], | ||
image_url: string, | ||
thumbnail_size: [number, number], |
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.
Same here: thumbnail_size: [width: number, height: number]
src/amo/components/ScreenShots.js
Outdated
} | ||
|
||
viewport: HTMLElement | null; |
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 think it's nice to put class variable types at the top of the class. We don't have a rule about it though.
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.
Me too but ESLint complains.. Maybe time to disable the react/sort-comp
rule globally?
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.
Gah! react/sort-comp
is the worst. I always disable it. Yeah, we should just disable it globally.
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.
Opened #5357
|
||
const bounds = getThumbBoundsFn(0, fakeDocument); | ||
|
||
expect(bounds).toBeInstanceOf(Object); |
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 don't think you need this because .toEqual()
below it will produce an error saying that it did not receive an Object
.
Moving this to a |
src/amo/components/ScreenShots.js
Outdated
getThumbBoundsFn: /* istanbul ignore next */ function getThumbBoundsFn(index) { | ||
const thumbnail = document.querySelectorAll('.pswp-thumbnails')[index]; | ||
getThumbBoundsFn: (index: number, { | ||
_document = typeof document !== 'undefined' ? document : null, |
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.
@kumar303 Flow complains about this line because the maybe/union type typeof document | null
does not work. I don't know why and I am stuck right now.
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.
Looks like a Flow bug that hasn't been fixed: facebook/flow#183
6a70457
to
ef462ae
Compare
ef462ae
to
b0ff064
Compare
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.
⚡️
Fix mozilla/addons#11923
This PR fixes the
Screenshots
component to use the thumbnails supplied by the API.I have also added Flow coverage and more tests. When adding Flow coverage, Flow told me that
ClientRect
did not have ax
attribute, which is correct according to https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect. That's why I changedx
byleft
in this patch.There is one more thing I'd like to do: moving the code (CSS + JS) into a directory named
Screenshots
. I did not do it yet because the diff would be unreadable, but that would be cool to do it before merging this PR, I guess.