Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
rewrite CoveredImage
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 31, 2021
1 parent c105b30 commit 5438523
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 62 deletions.
2 changes: 0 additions & 2 deletions client/package.json
Expand Up @@ -10,12 +10,10 @@
"develop": "webpack serve --mode=development"
},
"dependencies": {
"buffer": "6.0.3",
"classnames": "2.3.1",
"core-js": "3.19.1",
"fast-average-color": "7.0.1",
"gifler": "github:themadcreator/gifler#v0.3.0",
"image-size": "1.0.0",
"normalize.css": "8.0.1",
"omggif": "1.0.10",
"pako": "2.0.4",
Expand Down
40 changes: 3 additions & 37 deletions client/src/components/foundation/CoveredImage/CoveredImage.jsx
@@ -1,10 +1,5 @@
import classNames from 'classnames';
import sizeOf from 'image-size';
import React from 'react';

import { useFetch } from '../../../hooks/use_fetch';
import { fetchBinary } from '../../../utils/fetchers';

/**
* @typedef {object} Props
* @property {string} alt
Expand All @@ -16,41 +11,12 @@ import { fetchBinary } from '../../../utils/fetchers';
* @type {React.VFC<Props>}
*/
const CoveredImage = ({ alt, src }) => {
const { data, isLoading } = useFetch(src, fetchBinary);

const imageSize = React.useMemo(() => {
return data !== null ? sizeOf(Buffer.from(data)) : null;
}, [data]);

const blobUrl = React.useMemo(() => {
return data !== null ? URL.createObjectURL(new Blob([data])) : null;
}, [data]);

const [containerSize, setContainerSize] = React.useState({ height: 0, width: 0 });
/** @type {React.RefCallback<HTMLDivElement>} */
const callbackRef = React.useCallback((el) => {
setContainerSize({
height: el?.clientHeight ?? 0,
width: el?.clientWidth ?? 0,
});
}, []);

if (isLoading || data === null || blobUrl === null) {
return null;
}

const containerRatio = containerSize.height / containerSize.width;
const imageRatio = imageSize?.height / imageSize?.width;

return (
<div ref={callbackRef} className="relative w-full h-full overflow-hidden">
<div className="w-full h-full overflow-hidden">
<img
alt={alt}
className={classNames('absolute left-1/2 top-1/2 max-w-none transform -translate-x-1/2 -translate-y-1/2', {
'w-auto h-full': containerRatio > imageRatio,
'w-full h-auto': containerRatio <= imageRatio,
})}
src={blobUrl}
className="w-full h-full max-w-none object-cover"
src={src}
/>
</div>
);
Expand Down
1 change: 0 additions & 1 deletion client/webpack.config.js
Expand Up @@ -51,7 +51,6 @@ const config = {
plugins: [
new webpack.ProvidePlugin({
AudioContext: ['standardized-audio-context', 'AudioContext'],
Buffer: ['buffer', 'Buffer'],
}),
new webpack.EnvironmentPlugin({
BUILD_DATE: new Date().toISOString(),
Expand Down
22 changes: 0 additions & 22 deletions yarn.lock
Expand Up @@ -1808,14 +1808,6 @@ buffer-indexof@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-indexof/-/buffer-indexof-1.1.1.tgz#52fabcc6a606d1a00302802648ef68f639da268c"
integrity sha512-4/rOEg86jivtPTeOUUT61jJO1Ya1TrR/OkqCSZDyq84WJh3LuuiphBYJN+fm5xufIk4XAFcEwte/8WzC8If/1g==

buffer@6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
dependencies:
base64-js "^1.3.1"
ieee754 "^1.2.1"

buffer@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
Expand Down Expand Up @@ -3337,13 +3329,6 @@ ignore@^5.1.4:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.9.tgz#9ec1a5cbe8e1446ec60d4420060d43aa6e7382fb"
integrity sha512-2zeMQpbKz5dhZ9IwL0gbxSW5w0NK/MSAMtNuhgIHEPmaU3vPdKPL0UdvUCXs5SS4JAwsBxysK5sFMW8ocFiVjQ==

image-size@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/image-size/-/image-size-1.0.0.tgz#58b31fe4743b1cec0a0ac26f5c914d3c5b2f0750"
integrity sha512-JLJ6OwBfO1KcA+TvJT+v8gbE6iWbj24LyDNFgFEN0lzegn6cC6a/p3NIDaepMsJjQjlUWqIC7wJv8lBFxPNjcw==
dependencies:
queue "6.0.2"

import-cwd@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-3.0.0.tgz#20845547718015126ea9b3676b7592fb8bd4cf92"
Expand Down Expand Up @@ -5134,13 +5119,6 @@ queue-microtask@^1.2.2:
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==

queue@6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/queue/-/queue-6.0.2.tgz#b91525283e2315c7553d2efa18d83e76432fed65"
integrity sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==
dependencies:
inherits "~2.0.3"

quick-lru@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
Expand Down

0 comments on commit 5438523

Please sign in to comment.