-
Notifications
You must be signed in to change notification settings - Fork 0
Pulling hotfixes down into develop #1257
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
Changes from 14 commits
2ad9770
47653e6
7790858
0df6005
72345f4
2a24628
76344b0
f82ccd2
7a78cd1
9e9c287
050490f
6348592
d5f01af
5e5f3f6
fac6b54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
REACT_APP_DAS_HOST=https://root.dev.pamdas.org | ||
REACT_APP_GA_TRACKING_ID=UA-128569083-12 | ||
REACT_APP_DAS_HOST='https://root.dev.pamdas.org' | ||
REACT_APP_GA4_TRACKING_ID=G-1MVMZ0CMWF | ||
REACT_APP_MOCK_EVENTS_API=false | ||
REACT_APP_MOCK_EVENTTYPES_V2_API=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
GENERATE_SOURCEMAP=false | ||
REACT_APP_DAS_HOST='' | ||
REACT_APP_ROUTE_PREFIX=/ | ||
REACT_APP_GA_TRACKING_ID=UA-128569083-1 | ||
REACT_APP_GA4_TRACKING_ID=G-B9CJEDN0BN | ||
# PUBLIC_URL=/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { DAS_HOST } from '../constants'; | |
const urlContainsOwnHost = url => url.includes('http'); | ||
const imgIsDataUrl = url => url.includes('data:image'); | ||
const imgIsFromStaticMedia = url => /^(\/static\/media)/.test(url); | ||
const isObjectURL = url => url && typeof url === 'string' && url.startsWith('blob:'); | ||
|
||
const imgNeedsHostAppended = url => { | ||
if (urlContainsOwnHost(url)) return false; | ||
|
@@ -11,42 +12,81 @@ const imgNeedsHostAppended = url => { | |
return true; | ||
}; | ||
|
||
export const imgElFromSrc = (src, width = 30, height = null) => new Promise((resolve, reject) => { | ||
let img = new Image(); | ||
const imageCache = new Map(); | ||
|
||
const generateImageCacheKey = (src, width, height) => { | ||
const w = width === null ? 'null' : (width === undefined ? 'undefined' : width); | ||
const h = height === null ? 'null' : (height === undefined ? 'undefined' : height); | ||
return `${src}:${w}:${h}`; | ||
}; | ||
|
||
export const imgElFromSrc = (src, baseUnit = null) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I still see some of the confusing variable names I suggested to change in the feedback of the hotfix PR: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hotfixes are urgent code repairs, and I think it's a reasonable perspective to take on general feedback/code improvements separately from hotfixes -- especially for long-lived existing code like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agreed. I think what I mean is not to improve old code like we do with new PRs (boyscout rule). But I notice new code being added as part of the fix, and I would argue that the new code should come with the high standards we have. That said, given the urgency and the fact that indeed the code of this file is too old and could take a big refactor, I wouldn't block this. |
||
if (!src) { | ||
return Promise.reject('no src provided'); | ||
} | ||
|
||
const cacheKey = generateImageCacheKey(src, baseUnit, null); | ||
|
||
if (imageCache.has(cacheKey)) { | ||
return imageCache.get(cacheKey); | ||
} | ||
|
||
const img = new Image(); | ||
img.setAttribute('crossorigin', 'anonymous'); | ||
|
||
img.addEventListener('load', () => { | ||
if (width && height) { | ||
img.width = width; | ||
img.height = height; | ||
} else { | ||
const baseUnit = width || height; | ||
const { naturalHeight, naturalWidth } = img; | ||
const largest = Math.max(naturalHeight, naturalWidth) || baseUnit; | ||
const smallest = Math.min(naturalHeight, naturalWidth) || baseUnit; | ||
const widthIsLarger = largest === naturalWidth; | ||
const aspectRatio = smallest / largest; | ||
if (widthIsLarger) { | ||
const imagePromise = new Promise((resolve, reject) => { | ||
const cleanupAndResolve = () => { | ||
if (baseUnit && img.naturalWidth && img.naturalHeight) { | ||
const widthIsLarger = img.naturalWidth > img.naturalHeight; | ||
|
||
const aspectRatio = widthIsLarger ? | ||
img.naturalHeight / img.naturalWidth : | ||
img.naturalWidth / img.naturalHeight; | ||
|
||
if (widthIsLarger) { | ||
img.width = baseUnit; | ||
img.height = Math.round(baseUnit * aspectRatio); | ||
} else { | ||
img.height = baseUnit; | ||
img.width = Math.round(baseUnit * aspectRatio); | ||
} | ||
} else if (baseUnit) { | ||
img.width = baseUnit; | ||
img.height = baseUnit * aspectRatio; | ||
} else { | ||
img.height = baseUnit; | ||
img.width = baseUnit * aspectRatio; | ||
} | ||
} | ||
resolve(img); | ||
}, { once: true }); | ||
|
||
img.onerror = (e) => { | ||
console.warn('image error', src, e); | ||
reject('could not load image'); | ||
}; | ||
img.src = src; | ||
}); | ||
|
||
|
||
|
||
resolve(img); | ||
}; | ||
|
||
img.addEventListener('load', cleanupAndResolve, { once: true }); | ||
|
||
img.onerror = (e) => { | ||
console.warn('image error', src, e); | ||
|
||
if (isObjectURL(src)) { | ||
URL.revokeObjectURL(src); | ||
} | ||
|
||
imageCache.delete(cacheKey); | ||
img.onload = null; | ||
img.onerror = null; | ||
reject('could not load image'); | ||
}; | ||
|
||
img.src = src; | ||
}); | ||
|
||
imageCache.set(cacheKey, imagePromise); | ||
|
||
return imagePromise; | ||
}; | ||
|
||
export const calcImgIdFromUrlForMapImages = (src, width = null, height = null) => { | ||
const path = calcUrlForImage(src); | ||
return `${path}-${width ? width: 'x'}-${height ? height: 'x'}`; | ||
return `${path}-${width ? width : 'x'}-${height ? height : 'x'}`; | ||
}; | ||
|
||
export const calcUrlForImage = imagePath => { | ||
|
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 thought we agreed on not commenting this test, but just removing the failing part of it:
The test is still useful to make sure the callback is triggered, we just can't tell exactly which timezone will be.
Uh oh!
There was an error while loading. Please reload this page.
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.
Agreed. I'll fix this directly in develop.