From 12248db8936136e828e6ed3b5aec86198ddedc06 Mon Sep 17 00:00:00 2001 From: David Ralph Date: Sat, 2 Mar 2024 22:46:28 +0000 Subject: [PATCH] refactor: once again move background utils, this time to api --- .../Form/Settings/FileUpload/FileUpload.jsx | 2 +- src/features/background/Background.jsx | 22 +++++++++---------- .../background/api}/avif.js | 0 .../background/api}/getOfflineImage.js | 4 ++-- .../background/api}/gradient/getGradient.js | 0 .../background/api}/gradient/hexToRgb.js | 0 .../background/api}/gradient/index.js | 0 .../background/api}/gradient/rgbToHex.js | 0 .../background/api}/gradient/rgbToHsv.js | 0 .../background/api}/gradient/setRgba.js | 0 .../background/api}/randomColour.js | 0 .../background/api}/videoCheck.js | 0 .../background}/offline_images.json | 0 src/features/background/options/Colour.jsx | 2 +- src/features/background/options/Custom.jsx | 2 +- .../background/options/CustomURLModal.jsx | 1 + src/utils/background/index.js | 6 ----- 17 files changed, 16 insertions(+), 23 deletions(-) rename src/{utils/background => features/background/api}/avif.js (100%) rename src/{utils/background => features/background/api}/getOfflineImage.js (91%) rename src/{utils/background => features/background/api}/gradient/getGradient.js (100%) rename src/{utils/background => features/background/api}/gradient/hexToRgb.js (100%) rename src/{utils/background => features/background/api}/gradient/index.js (100%) rename src/{utils/background => features/background/api}/gradient/rgbToHex.js (100%) rename src/{utils/background => features/background/api}/gradient/rgbToHsv.js (100%) rename src/{utils/background => features/background/api}/gradient/setRgba.js (100%) rename src/{utils/background => features/background/api}/randomColour.js (100%) rename src/{utils/background => features/background/api}/videoCheck.js (100%) rename src/{utils/data => features/background}/offline_images.json (100%) delete mode 100644 src/utils/background/index.js diff --git a/src/components/Form/Settings/FileUpload/FileUpload.jsx b/src/components/Form/Settings/FileUpload/FileUpload.jsx index 8c74eb49..fa97a208 100644 --- a/src/components/Form/Settings/FileUpload/FileUpload.jsx +++ b/src/components/Form/Settings/FileUpload/FileUpload.jsx @@ -2,7 +2,7 @@ import variables from 'config/variables'; import { PureComponent } from 'react'; import { toast } from 'react-toastify'; import { compressAccurately, filetoDataURL } from 'image-conversion'; -import { videoCheck } from 'utils/background'; +import videoCheck from 'features/background/api/videoCheck'; class FileUpload extends PureComponent { componentDidMount() { diff --git a/src/features/background/Background.jsx b/src/features/background/Background.jsx index 645a64e8..943feea9 100644 --- a/src/features/background/Background.jsx +++ b/src/features/background/Background.jsx @@ -6,14 +6,12 @@ import { PureComponent } from 'react'; import PhotoInformation from './components/PhotoInformation'; import EventBus from 'utils/eventbus'; -import { - videoCheck, - offlineBackground, - randomColourStyleBuilder, - supportsAVIF, -} from 'utils/background'; -import { getGradient } from 'utils/background/gradient'; +import { supportsAVIF } from './api/avif'; +import { getOfflineImage } from './api/getOfflineImage'; +import videoCheck from './api/videoCheck'; +import { getGradient } from './api/gradient'; +import { randomColourStyleBuilder } from './api/randomColour'; import './scss/index.scss'; import { decodeBlurHash } from 'fast-blurhash'; @@ -128,7 +126,7 @@ export default class Background extends PureComponent { data = await (await fetch(requestURL, { headers: { accept } })).json(); } catch (e) { // if requesting to the API fails, we get an offline image - this.setState(offlineBackground('api')); + this.setState(getOfflineImage('api')); return null; } @@ -201,7 +199,7 @@ export default class Background extends PureComponent { switch (type) { case 'api': if (offline) { - return this.setState(offlineBackground('api')); + return this.setState(getOfflineImage('api')); } // API background @@ -246,7 +244,7 @@ export default class Background extends PureComponent { // allow users to use offline images if (offline && !customBackground.startsWith('data:')) { - return this.setState(offlineBackground('custom')); + return this.setState(getOfflineImage('custom')); } if ( @@ -271,7 +269,7 @@ export default class Background extends PureComponent { case 'photo_pack': if (offline) { - return this.setState(offlineBackground('photo_pack')); + return this.setState(getOfflineImage('photo_pack')); } const photofavourited = JSON.parse(localStorage.getItem('favourite')); @@ -503,7 +501,7 @@ export default class Background extends PureComponent { this.setState(current); } } else if (current.url.startsWith('http')) { - this.setState(offlineBackground()); + this.setState(getOfflineImage()); } if (this.state.firstTime !== true) { this.setState(current); diff --git a/src/utils/background/avif.js b/src/features/background/api/avif.js similarity index 100% rename from src/utils/background/avif.js rename to src/features/background/api/avif.js diff --git a/src/utils/background/getOfflineImage.js b/src/features/background/api/getOfflineImage.js similarity index 91% rename from src/utils/background/getOfflineImage.js rename to src/features/background/api/getOfflineImage.js index fb598d9e..f45ba34b 100644 --- a/src/utils/background/getOfflineImage.js +++ b/src/features/background/api/getOfflineImage.js @@ -1,4 +1,4 @@ -import offlineImages from 'utils/data/offline_images.json'; +import offlineImages from '../offline_images.json'; /** * It gets a random photographer from the offlineImages.json file, then gets a random image from that @@ -12,7 +12,7 @@ import offlineImages from 'utils/data/offline_images.json'; * offline: A boolean that is true. * credit: A string that is the name of the photographer. */ -export function offlineBackground(type) { +export function getOfflineImage(type) { const photographers = Object.keys(offlineImages); const photographer = photographers[Math.floor(Math.random() * photographers.length)]; diff --git a/src/utils/background/gradient/getGradient.js b/src/features/background/api/gradient/getGradient.js similarity index 100% rename from src/utils/background/gradient/getGradient.js rename to src/features/background/api/gradient/getGradient.js diff --git a/src/utils/background/gradient/hexToRgb.js b/src/features/background/api/gradient/hexToRgb.js similarity index 100% rename from src/utils/background/gradient/hexToRgb.js rename to src/features/background/api/gradient/hexToRgb.js diff --git a/src/utils/background/gradient/index.js b/src/features/background/api/gradient/index.js similarity index 100% rename from src/utils/background/gradient/index.js rename to src/features/background/api/gradient/index.js diff --git a/src/utils/background/gradient/rgbToHex.js b/src/features/background/api/gradient/rgbToHex.js similarity index 100% rename from src/utils/background/gradient/rgbToHex.js rename to src/features/background/api/gradient/rgbToHex.js diff --git a/src/utils/background/gradient/rgbToHsv.js b/src/features/background/api/gradient/rgbToHsv.js similarity index 100% rename from src/utils/background/gradient/rgbToHsv.js rename to src/features/background/api/gradient/rgbToHsv.js diff --git a/src/utils/background/gradient/setRgba.js b/src/features/background/api/gradient/setRgba.js similarity index 100% rename from src/utils/background/gradient/setRgba.js rename to src/features/background/api/gradient/setRgba.js diff --git a/src/utils/background/randomColour.js b/src/features/background/api/randomColour.js similarity index 100% rename from src/utils/background/randomColour.js rename to src/features/background/api/randomColour.js diff --git a/src/utils/background/videoCheck.js b/src/features/background/api/videoCheck.js similarity index 100% rename from src/utils/background/videoCheck.js rename to src/features/background/api/videoCheck.js diff --git a/src/utils/data/offline_images.json b/src/features/background/offline_images.json similarity index 100% rename from src/utils/data/offline_images.json rename to src/features/background/offline_images.json diff --git a/src/features/background/options/Colour.jsx b/src/features/background/options/Colour.jsx index 5bd1e554..774702bc 100644 --- a/src/features/background/options/Colour.jsx +++ b/src/features/background/options/Colour.jsx @@ -4,7 +4,7 @@ import { ColorPicker } from '@muetab/react-color-gradient-picker'; import { toast } from 'react-toastify'; import { Row, Content, Action } from '../../../components/Layout/Settings/Item/SettingsItem'; -import { hexToRgb, rgbToHex } from 'utils/background/gradient'; +import { hexToRgb, rgbToHex } from '../api/gradient'; //import '@muetab/react-color-gradient-picker/dist/index.css'; import '../scss/react-color-picker-gradient-picker-custom-styles.scss'; diff --git a/src/features/background/options/Custom.jsx b/src/features/background/options/Custom.jsx index df94d0b5..fefd70dd 100644 --- a/src/features/background/options/Custom.jsx +++ b/src/features/background/options/Custom.jsx @@ -11,7 +11,7 @@ import { } from 'react-icons/md'; import EventBus from 'utils/eventbus'; import { compressAccurately, filetoDataURL } from 'image-conversion'; -import { videoCheck } from 'utils/background'; +import videoCheck from '../api/videoCheck'; import Checkbox from '../../../components/Form/Settings/Checkbox/Checkbox'; import FileUpload from '../../../components/Form/Settings/FileUpload/FileUpload'; diff --git a/src/features/background/options/CustomURLModal.jsx b/src/features/background/options/CustomURLModal.jsx index c903c30e..a90db453 100644 --- a/src/features/background/options/CustomURLModal.jsx +++ b/src/features/background/options/CustomURLModal.jsx @@ -2,6 +2,7 @@ import variables from 'config/variables'; import { useState, memo } from 'react'; import { MdClose, MdOutlineAddLink } from 'react-icons/md'; import { Tooltip } from 'components/Elements'; + function CustomURLModal({ modalClose, urlError, modalCloseOnly }) { const [url, setURL] = useState(); diff --git a/src/utils/background/index.js b/src/utils/background/index.js deleted file mode 100644 index 59d24da0..00000000 --- a/src/utils/background/index.js +++ /dev/null @@ -1,6 +0,0 @@ -import { supportsAVIF } from './avif'; -import { offlineBackground } from './getOfflineImage'; -import { randomColourStyleBuilder } from './randomColour'; -import videoCheck from './videoCheck'; - -export { supportsAVIF, offlineBackground, randomColourStyleBuilder, videoCheck };