import { useState, Fragment } from 'react'; import { Info, LocationOn, PhotoCamera, Crop as Resolution, Person as Photographer, GetApp as Download } from '@material-ui/icons'; import Hotkeys from 'react-hot-keys'; import { lat2tile, lon2tile } from 'modules/helpers/background/widget'; const toDataURL = async (url) => { const res = await fetch(url); return URL.createObjectURL(await res.blob()); }; const formatText = (text) => { return text.toLowerCase().replaceAll(',', '').replaceAll(' ', '-'); }; const downloadImage = async (info) => { const link = document.createElement('a'); link.href = await toDataURL(info.url); link.download = `mue-${formatText(info.credit)}-${formatText(info.location)}.jpg`; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.stats.postEvent('feature', 'Background download'); }; export default function PhotoInformation({ info, url, api }) { const [width, setWidth] = useState(0); const [height, setHeight] = useState(0); const language = window.language.widgets.background; if (info.hidden === true || !info.credit) { return null; } // remove unsplash and pexels text const photographer = info.credit.split(` ${language.unsplash}`)[0].split(` ${language.pexels}`); let credit = info.credit; let photo = language.credit; // unsplash and pexels credit if (info.photographerURL && info.photographerURL !== '' && !info.offline && api) { if (api === 'unsplash') { photo = {language.credit}; credit = <>{photographer} {language.unsplash}; } else { photo = {language.credit}; credit = <>{photographer} {language.pexels}; } } const ddgProxy = (localStorage.getItem('ddgProxy') === 'true'); // get resolution const img = new Image(); img.onload = (event) => { setWidth(event.target.width); setHeight(event.target.height); }; img.src = (ddgProxy && !info.offline && !url.startsWith('data:')) ? window.constants.DDG_IMAGE_PROXY + url : url; // info is still there because we want the favourite button to work if (localStorage.getItem('photoInformation') === 'false') { return (

{photo} {credit}

{info.location || 'N/A'} {info.camera || 'N/A'} {width}x{height}
); } const downloadEnabled = (localStorage.getItem('downloadbtn') === 'true') && !info.offline && !info.photographerURL; const downloadBackground = () => { if (downloadEnabled) { downloadImage(info); } }; const showBackgroundInformation = () => { const element = document.querySelector('.infoCard'); if (element) { if (element.style.display === 'none' || element.style.display === '') { element.style.display = 'block'; } else { element.style.display = 'none'; } } }; const photoMap = () => { if (localStorage.getItem('photoMap') !== 'true' || !info.latitude || !info.longitude) { return null; } const zoom = 12; const lat = lat2tile(info.latitude, zoom); const lon = lon2tile(info.longitude, zoom); const tile = `${window.constants.MAPBOX_URL}/styles/v1/mapbox/streets-v11/tiles/${zoom}/${lon}/${lat}?access_token=${info.maptoken}`; let icon = window.constants.CDN_URL + '/mapbox/mapbox-logo-dark.png'; if (document.body.classList.contains('dark')) { icon = window.constants.CDN_URL + '/mapbox/mapbox-logo-white.png'; } return ( location
mapbox logo © Mapbox, © OpenStreetMap. Improve this map.
); } return (

{photo} {credit}

{language.information}


{photoMap()} {/* fix console error by using fragment and key */} {info.location && info.location !== 'N/A' ? {info.location} : null} {info.camera && info.camera !== 'N/A' ? {info.camera} : null} {width}x{height} {photographer} {downloadEnabled ? <> downloadImage(info)}>{language.download} : null}
{window.keybinds.downloadBackground && window.keybinds.downloadBackground !== '' ? downloadBackground()} /> : null} {window.keybinds.showBackgroundInformation && window.keybinds.showBackgroundInformation !== '' ? showBackgroundInformation()} /> : null}
); }