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

102 lines
3.1 KiB
React
Raw Normal View History

import variables from 'modules/variables';
2021-08-14 19:10:48 +00:00
import { PureComponent } from 'react';
import { MdStar, MdStarBorder } from 'react-icons/md';
2021-11-17 21:50:34 +00:00
//import Hotkeys from 'react-hot-keys';
2021-01-17 18:07:26 +00:00
2021-08-14 19:10:48 +00:00
export default class Favourite extends PureComponent {
buttons = {
favourited: <MdStar onClick={() => this.favourite()} className="topicons" />,
unfavourited: <MdStarBorder onClick={() => this.favourite()} className="topicons" />,
};
2021-03-23 13:10:34 +00:00
constructor() {
super();
this.state = {
favourited: localStorage.getItem('favourite')
? this.buttons.favourited
: this.buttons.unfavourited,
};
}
2020-09-16 11:32:09 +00:00
favourite() {
if (localStorage.getItem('favourite')) {
localStorage.removeItem('favourite');
this.setState({
favourited: this.buttons.unfavourited,
});
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('feature', 'Background favourite');
} else {
const type = localStorage.getItem('backgroundType');
switch (type) {
case 'colour':
return;
case 'random_colour':
case 'random_gradient':
localStorage.setItem(
'favourite',
JSON.stringify({
type: localStorage.getItem('backgroundType'),
url: document.getElementById('backgroundImage').style.background,
}),
);
break;
default:
const url = document
.getElementById('backgroundImage')
.style.backgroundImage.replace('url("', '')
.replace('")', '')
.replace(variables.constants.DDG_IMAGE_PROXY, '');
if (!url) {
return;
}
2021-08-20 10:52:05 +00:00
if (type === 'custom') {
localStorage.setItem(
'favourite',
JSON.stringify({
type,
url,
}),
);
} else {
// photo information now hides information if it isn't sent, unless if photoinformation hover is hidden
const location = document.getElementById('infoLocation');
const camera = document.getElementById('infoCamera');
localStorage.setItem(
'favourite',
JSON.stringify({
type,
url,
credit: document.getElementById('credit').textContent || '',
location: location ? location.innerText : 'N/A',
camera: camera ? camera.innerText : 'N/A',
resolution: document.getElementById('infoResolution').textContent || '',
}),
);
}
}
this.setState({
favourited: this.buttons.favourited,
});
2021-09-28 22:04:04 +00:00
variables.stats.postEvent('feature', 'Background unfavourite');
}
2020-09-16 11:32:09 +00:00
}
render() {
if (localStorage.getItem('backgroundType') === 'colour') {
2021-01-16 22:43:46 +00:00
return null;
}
return (
<>
{this.state.favourited}
2021-11-17 21:50:34 +00:00
{/*variables.keybinds.favouriteBackground && variables.keybinds.favouriteBackground !== '' ? <Hotkeys keyName={variables.keybinds.favouriteBackground} onKeyDown={() => this.favourite()} /> : null*/}
</>
);
2020-09-16 11:32:09 +00:00
}
}