Repository navigation
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
kumar303
left a comment
There was a problem hiding this comment.
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.
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.
sure, that works for me!
| const thumbnail = document.querySelectorAll('.pswp-thumbnails')[index]; | ||
| getThumbBoundsFn: ( | ||
| index: number, | ||
| _document: typeof document = document, |
There was a problem hiding this comment.
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.
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.
| getThumbBoundsFn: ( | ||
| index: number, | ||
| _document: typeof document = document, | ||
| _window: typeof window = window |
There was a problem hiding this comment.
This should also be _window = typeof window !== 'undefined' ? window : null to avoid a ReferenceError in SSR mode.
| // Overload getThumbsBoundsFn as workaround to | ||
| // Overload getThumbBoundsFn as workaround to | ||
| // https://github.com/minhtranite/react-photoswipe/issues/23 | ||
| getThumbBoundsFn: /* istanbul ignore next */ function getThumbBoundsFn(index) { |
There was a problem hiding this comment.
Thanks for adding test coverage ❤️
| 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.
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.
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.
| const formatPreviews = (previews) => ( | ||
| type ExternalPreview = {| | ||
| caption: string, | ||
| image_size: [number, number], |
There was a problem hiding this comment.
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.
ah snap, there is no named tuples in Flow, so we cannot name the elements in the array.
| caption: string, | ||
| image_size: [number, number], | ||
| image_url: string, | ||
| thumbnail_size: [number, number], |
There was a problem hiding this comment.
Same here: thumbnail_size: [width: number, height: number]
| list.scrollLeft += offset - list.getBoundingClientRect().left; | ||
| } | ||
|
|
||
| viewport: HTMLElement | null; |
There was a problem hiding this comment.
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.
Me too but ESLint complains.. Maybe time to disable the react/sort-comp rule globally?
There was a problem hiding this comment.
Gah! react/sort-comp is the worst. I always disable it. Yeah, we should just disable it globally.
|
|
||
| const bounds = getThumbBoundsFn(0, fakeDocument); | ||
|
|
||
| expect(bounds).toBeInstanceOf(Object); |
There was a problem hiding this comment.
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 |
| 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.
@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.
Looks like a Flow bug that hasn't been fixed: meta/flow#183
6a70457 to
ef462ae
Compare
ef462ae to
b0ff064
Compare
Fix mozilla/addons#11923
This PR fixes the
Screenshotscomponent 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
ClientRectdid not have axattribute, which is correct according to https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect. That's why I changedxbyleftin 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.