mue/src/components/widgets/background/Background.jsx

545 lines
17 KiB
React
Raw Normal View History

/* eslint-disable no-unused-expressions */
// todo: rewrite this mess
2021-09-29 18:19:03 +00:00
import variables from 'modules/variables';
2021-08-11 12:06:02 +00:00
import { PureComponent } from 'react';
2021-01-17 18:07:26 +00:00
2021-08-15 21:28:37 +00:00
import PhotoInformation from './PhotoInformation';
2021-08-28 14:34:12 +00:00
import EventBus from 'modules/helpers/eventbus';
import {
videoCheck,
offlineBackground,
getGradient,
randomColourStyleBuilder,
} from 'modules/helpers/background/widget';
2020-11-29 14:32:08 +00:00
import './scss/index.scss';
2020-04-22 14:22:22 +00:00
2021-08-11 12:06:02 +00:00
export default class Background extends PureComponent {
2021-03-23 13:10:34 +00:00
constructor() {
super();
2021-03-20 12:05:14 +00:00
this.state = {
style: '',
url: '',
currentAPI: '',
2022-09-11 10:40:13 +00:00
firstTime: false,
2021-03-20 12:05:14 +00:00
photoInfo: {
2021-04-27 13:52:04 +00:00
hidden: false,
offline: false,
photographerURL: '',
photoURL: '',
},
2021-03-20 12:05:14 +00:00
};
}
setBackground() {
const backgroundImage = document.getElementById('backgroundImage');
2021-04-14 11:51:11 +00:00
if (this.state.url !== '') {
let url = this.state.url;
if (
localStorage.getItem('ddgProxy') === 'true' &&
this.state.photoInfo.offline !== true &&
!this.state.url.startsWith('data:')
) {
url = variables.constants.DDG_IMAGE_PROXY + this.state.url;
}
2021-04-12 10:45:33 +00:00
const photoInformation = document.querySelector('.photoInformation');
// just set the background
if (localStorage.getItem('bgtransition') === 'false') {
2022-09-07 15:04:06 +00:00
photoInformation?.[(photoInformation.style.display = 'flex')];
return (backgroundImage.style.background = `url(${url})`);
}
// firstly we set the background as hidden and make sure there is no background set currently
backgroundImage.classList.add('backgroundPreload');
backgroundImage.style.background = null;
2021-04-14 11:51:11 +00:00
// same with photo information if not using custom background
photoInformation?.classList.add('backgroundPreload');
// preloader for background transition, required, so it loads in nice
const preloader = document.createElement('img');
preloader.src = url;
// once image has loaded, add the fade-in transition
preloader.addEventListener('load', () => {
backgroundImage.classList.remove('backgroundPreload');
backgroundImage.classList.add('fade-in');
backgroundImage.style.background = `url(${url})`;
// remove the preloader element we created earlier
2021-04-14 11:51:11 +00:00
preloader.remove();
2021-04-14 11:51:11 +00:00
if (photoInformation) {
2021-04-12 10:45:33 +00:00
photoInformation.classList.remove('backgroundPreload');
2021-04-14 11:51:11 +00:00
photoInformation.classList.add('fade-in');
}
});
} else {
// custom colour
backgroundImage.setAttribute('style', this.state.style);
}
}
2021-03-20 12:05:14 +00:00
// Main background getting function
async getBackground() {
let offline = localStorage.getItem('offlineMode') === 'true';
if (localStorage.getItem('showWelcome') === 'true') {
offline = true;
}
const setFavourited = ({ type, url, credit, location, camera }) => {
if (type === 'random_colour' || type === 'random_gradient') {
return this.setState({
type: 'colour',
style: `background:${url}`,
});
}
this.setState({
url,
photoInfo: {
credit,
location,
camera,
},
});
};
const favourited = JSON.parse(localStorage.getItem('favourite'));
if (favourited) {
return setFavourited(favourited);
}
const type = localStorage.getItem('backgroundType');
switch (type) {
2021-03-24 12:02:30 +00:00
case 'api':
if (offline) {
return this.setState(offlineBackground('api'));
}
2021-03-24 12:02:30 +00:00
// API background
const backgroundAPI = localStorage.getItem('backgroundAPI');
const apiCategory = localStorage.getItem('apiCategory');
2021-06-13 15:36:43 +00:00
const apiQuality = localStorage.getItem('apiQuality');
const photoMap = localStorage.getItem('photoMap') === 'true';
2021-03-24 12:02:30 +00:00
let requestURL, data;
switch (backgroundAPI) {
case 'unsplash':
requestURL = `${variables.constants.PROXY_URL}/images/unsplash?quality=${apiQuality}&map=${photoMap}`;
break;
2021-04-30 21:49:04 +00:00
case 'pexels':
2021-09-28 22:04:04 +00:00
requestURL = `${variables.constants.PROXY_URL}/images/pexels?quality=${apiQuality}`;
2021-04-30 21:49:04 +00:00
break;
2021-03-24 12:02:30 +00:00
// Defaults to Mue
default:
2021-09-28 22:04:04 +00:00
requestURL = `${variables.constants.API_URL}/images/random?category=${apiCategory}&quality=${apiQuality}`;
2021-03-24 12:02:30 +00:00
break;
2021-03-20 12:05:14 +00:00
}
2021-01-16 22:43:46 +00:00
2021-03-24 12:02:30 +00:00
try {
data = await (await fetch(requestURL)).json();
} catch (e) {
// if requesting to the API fails, we get an offline image
return this.setState(offlineBackground('api'));
2021-08-11 12:06:02 +00:00
}
2021-01-16 22:43:46 +00:00
let photoURL, photographerURL;
2021-05-06 16:30:13 +00:00
if (backgroundAPI === 'unsplash' || backgroundAPI === 'pexels') {
2021-05-06 16:30:13 +00:00
photoURL = data.photo_page;
photographerURL = data.photographer_page;
2021-05-05 14:44:39 +00:00
}
const object = {
2021-03-24 12:02:30 +00:00
url: data.file,
type: 'api',
currentAPI: backgroundAPI,
2021-03-20 12:05:14 +00:00
photoInfo: {
hidden: false,
credit: data.photographer,
location: data.location,
2021-04-14 11:51:11 +00:00
camera: data.camera,
2021-04-27 13:52:04 +00:00
url: data.file,
photographerURL,
photoURL,
latitude: data.latitude || null,
longitude: data.longitude || null,
// location map token from mapbox
maptoken: data.maptoken || null,
views: data.views || null,
downloads: data.downloads || null,
},
2021-08-21 10:11:00 +00:00
};
this.setState(object);
localStorage.setItem('currentBackground', JSON.stringify(object));
break;
2021-03-24 12:02:30 +00:00
case 'colour':
const gradient = getGradient();
if (gradient) {
this.setState(gradient);
2021-03-24 12:02:30 +00:00
}
break;
2021-03-24 12:02:30 +00:00
case 'random_colour':
case 'random_gradient':
this.setState(randomColourStyleBuilder(type));
break;
2021-03-24 12:02:30 +00:00
case 'custom':
let customBackground = [];
const customSaved = localStorage.getItem('customBackground');
try {
customBackground = JSON.parse(customSaved);
} catch (e) {
if (customSaved !== '') {
// move to new format
customBackground = [customSaved];
}
localStorage.setItem('customBackground', JSON.stringify(customBackground));
}
// pick random
customBackground = customBackground[Math.floor(Math.random() * customBackground.length)];
// allow users to use offline images
if (offline && !customBackground.startsWith('data:')) {
return this.setState(offlineBackground('custom'));
}
if (
customBackground !== '' &&
customBackground !== 'undefined' &&
customBackground !== undefined
) {
2021-09-19 19:10:22 +00:00
const object = {
2021-03-24 12:02:30 +00:00
url: customBackground,
type: 'custom',
video: videoCheck(customBackground),
2021-03-24 12:02:30 +00:00
photoInfo: {
hidden: true,
},
2021-09-19 19:10:22 +00:00
};
this.setState(object);
localStorage.setItem('currentBackground', JSON.stringify(object));
2021-03-24 12:02:30 +00:00
}
break;
2021-03-24 12:02:30 +00:00
case 'photo_pack':
if (offline) {
return this.setState(offlineBackground('photo_pack'));
}
const photofavourited = JSON.parse(localStorage.getItem('favourite'));
if (photofavourited) {
return setFavourited(photofavourited);
}
const photoPack = [];
const installed = JSON.parse(localStorage.getItem('installed'));
installed.forEach((item) => {
if (item.type === 'photos') {
photoPack.push(...item.photos);
}
});
2021-03-24 12:02:30 +00:00
if (photoPack) {
2022-10-29 20:28:04 +00:00
const randomNumber = Math.floor(Math.random() * photoPack.length);
const randomPhoto = photoPack[randomNumber];
if (
2022-11-06 11:59:59 +00:00
(localStorage.getItem('backgroundchange') === 'refresh' &&
this.state.firstTime === true) ||
2022-10-29 20:28:04 +00:00
(localStorage.getItem('backgroundchange') === null && this.state.firstTime === true)
) {
2022-10-29 20:28:04 +00:00
localStorage.setItem('marketplaceNumber', randomNumber);
this.setState({
firstTime: false,
url: randomPhoto.url.default,
type: 'photo_pack',
photoInfo: {
hidden: false,
credit: randomPhoto.photographer,
location: randomPhoto.location || 'N/A',
},
});
} else {
2022-11-06 11:59:59 +00:00
if (
Number(
Number(localStorage.getItem('backgroundStartTime')) +
Number(localStorage.getItem('backgroundchange')) >=
Number(Date.now()),
)
) {
const randomPhoto = photoPack[localStorage.getItem('marketplaceNumber')];
if (this.state.firstTime !== true) {
this.setState({
2022-10-29 20:28:04 +00:00
url: randomPhoto.url.default,
type: 'photo_pack',
photoInfo: {
hidden: false,
credit: randomPhoto.photographer,
location: randomPhoto.location || 'N/A',
},
});
2022-11-06 11:59:59 +00:00
} else {
this.setState({ firstTime: true });
2022-10-29 20:28:04 +00:00
}
2022-11-06 11:59:59 +00:00
this.setState({ firstTime: true });
} else {
localStorage.setItem('marketplaceNumber', randomNumber);
return this.setState({
url: randomPhoto.url.default,
type: 'photo_pack',
photoInfo: {
hidden: false,
credit: randomPhoto.photographer,
location: randomPhoto.location || 'N/A',
},
});
}
}
2021-03-24 12:02:30 +00:00
}
break;
2021-08-11 12:06:02 +00:00
default:
break;
}
2019-10-20 12:39:01 +00:00
}
componentDidMount() {
const element = document.getElementById('backgroundImage');
2021-01-16 22:43:46 +00:00
// this resets it so the fade in and getting background all works properly
const refresh = () => {
element.classList.remove('fade-in');
this.setState({
url: '',
style: '',
type: '',
video: false,
photoInfo: {
hidden: true,
},
});
this.getBackground();
2021-04-16 11:26:56 +00:00
};
EventBus.on('refresh', (data) => {
if (data === 'welcomeLanguage') {
localStorage.setItem('welcomeImage', JSON.stringify(this.state));
}
if (data === 'background') {
if (localStorage.getItem('background') === 'false') {
// user is using custom colour or image
if (this.state.photoInfo.hidden === false) {
document.querySelector('.photoInformation').style.display = 'none';
}
// video backgrounds
if (this.state.video === true) {
return (document.getElementById('backgroundVideo').style.display = 'none');
} else {
return (element.style.display = 'none');
}
}
2021-08-11 12:06:02 +00:00
// video backgrounds
if (this.state.video === true) {
document.getElementById('backgroundVideo').style.display = 'block';
} else {
if (this.state.photoInfo.hidden === false) {
try {
2022-09-07 15:04:06 +00:00
document.querySelector('.photoInformation').style.display = 'flex';
} catch (e) {
// Disregard exception
}
}
element.style.display = 'block';
}
2021-04-14 11:51:11 +00:00
const backgroundType = localStorage.getItem('backgroundType');
if (this.state.photoInfo.offline !== true) {
// basically check to make sure something has changed before we try getting another background
if (
backgroundType !== this.state.type ||
(this.state.type === 'api' &&
localStorage.getItem('backgroundAPI') !== this.state.currentAPI) ||
(this.state.type === 'custom' &&
localStorage.getItem('customBackground') !== this.state.url)
) {
return refresh();
}
} else if (backgroundType !== this.state.type) {
return refresh();
}
// background effects so we don't get another image again
const backgroundFilterSetting = localStorage.getItem('backgroundFilter');
const backgroundFilter = backgroundFilterSetting && backgroundFilterSetting !== 'none';
2021-05-02 12:40:11 +00:00
2021-04-14 11:51:11 +00:00
if (this.state.video === true) {
document.getElementById(
'backgroundVideo',
).style.webkitFilter = `blur(${localStorage.getItem(
'blur',
)}px) brightness(${localStorage.getItem('brightness')}%) ${
backgroundFilter
? backgroundFilterSetting +
'(' +
localStorage.getItem('backgroundFilterAmount') +
'%)'
: ''
}`;
2021-04-14 11:51:11 +00:00
} else {
element.style.webkitFilter = `blur(${localStorage.getItem(
'blur',
)}px) brightness(${localStorage.getItem('brightness')}%) ${
backgroundFilter
? backgroundFilterSetting +
'(' +
localStorage.getItem('backgroundFilterAmount') +
'%)'
: ''
}`;
2021-04-14 11:51:11 +00:00
}
}
// uninstall photo pack reverts your background to what you had previously
if (
data === 'marketplacebackgrounduninstall' ||
data === 'backgroundwelcome' ||
data === 'backgroundrefresh'
) {
refresh();
}
});
if (localStorage.getItem('welcomeTab')) {
return this.setState(JSON.parse(localStorage.getItem('welcomeImage')));
}
2022-09-11 10:45:38 +00:00
if (
localStorage.getItem('backgroundchange') === 'refresh' ||
localStorage.getItem('backgroundchange') === null
2022-09-11 10:45:38 +00:00
) {
2022-09-05 07:14:21 +00:00
try {
document.getElementById('backgroundImage').classList.remove('fade-in');
document.getElementsByClassName('photoInformation')[0].classList.remove('fade-in');
} catch (e) {
// Disregard exception
}
this.getBackground();
localStorage.setItem('backgroundStartTime', Date.now());
}
const test = localStorage.getItem('backgroundchange');
2022-09-05 07:14:21 +00:00
this.interval = setInterval(() => {
const targetTime = Number(Number(localStorage.getItem('backgroundStartTime')) + Number(test));
2022-09-05 07:14:21 +00:00
const currentTime = Number(Date.now());
2022-11-06 11:59:59 +00:00
const type = localStorage.getItem('backgroundType');
if (test !== null && test !== 'refresh') {
if (currentTime >= targetTime) {
element.classList.remove('fade-in');
this.getBackground();
localStorage.setItem('backgroundStartTime', Date.now());
} else {
try {
const current = JSON.parse(localStorage.getItem('currentBackground'));
if (current.type !== type) {
this.getBackground();
}
const offline = localStorage.getItem('offlineMode');
if (current.url.startsWith('http') && offline === 'false') {
2022-09-11 10:45:38 +00:00
if (this.state.firstTime !== true) {
this.setState(current);
}
} else if (current.url.startsWith('http')) {
this.setState(offlineBackground());
}
2022-09-11 10:40:13 +00:00
if (this.state.firstTime !== true) {
this.setState(current);
}
2022-09-11 10:45:38 +00:00
this.setState({ firstTime: true });
} catch (e) {
this.setBackground();
2021-08-01 17:20:38 +00:00
}
}
}
2022-09-05 07:14:21 +00:00
});
2021-03-20 12:05:14 +00:00
}
2021-03-20 12:05:14 +00:00
// only set once we've got the info
componentDidUpdate() {
if (this.state.video === true) {
return;
}
2021-03-20 12:05:14 +00:00
this.setBackground();
2019-10-20 12:39:01 +00:00
}
componentWillUnmount() {
EventBus.off('refresh');
2022-09-05 07:14:21 +00:00
clearInterval(this.interval);
}
2019-10-20 12:39:01 +00:00
render() {
if (this.state.video === true) {
const enabled = (setting) => {
return localStorage.getItem(setting) === 'true';
};
return (
<video
autoPlay
muted={enabled('backgroundVideoMute')}
loop={enabled('backgroundVideoLoop')}
style={{
WebkitFilter: `blur(${localStorage.getItem(
'blur',
)}px) brightness(${localStorage.getItem('brightness')}%)`,
}}
id="backgroundVideo"
>
<source src={this.state.url} />
</video>
);
}
2021-05-02 12:40:11 +00:00
const backgroundFilter = localStorage.getItem('backgroundFilter');
2021-03-20 12:05:14 +00:00
return (
<>
<div
style={{
WebkitFilter: `blur(${localStorage.getItem(
'blur',
)}px) brightness(${localStorage.getItem('brightness')}%) ${
backgroundFilter && backgroundFilter !== 'none'
? backgroundFilter + '(' + localStorage.getItem('backgroundFilterAmount') + '%)'
: ''
}`,
}}
id="backgroundImage"
/>
{this.state.photoInfo.credit !== '' ? (
<PhotoInformation
info={this.state.photoInfo}
api={this.state.currentAPI}
url={this.state.url}
/>
) : null}
</>
2021-03-20 12:05:14 +00:00
);
2019-10-20 12:39:01 +00:00
}
2020-08-29 16:47:34 +00:00
}