import variables from 'modules/variables'; import { useState, memo } from 'react'; import Favourite from './Favourite'; import { MdInfo, MdLocationOn, MdPhotoCamera, MdCrop as Resolution, MdGetApp as Download, MdVisibility as Views, MdIosShare as Share, MdSource as Source, } from 'react-icons/md'; import Tooltip from '../../helpers/tooltip/Tooltip'; import Modal from 'react-modal'; import ShareModal from '../../helpers/sharemodal/ShareModal'; 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); variables.stats.postEvent('feature', 'Background download'); }; function PhotoInformation({ info, url, api }) { const [width, setWidth] = useState(0); const [height, setHeight] = useState(0); const [usePhotoMap, setPhotoMap] = useState(false); const [setMapIcon] = useState(true); const [showExtraInfo, setshowExtraInfo] = useState(false); //const [showOld, setShowOld] = useState(true); const [other, setOther] = useState(false); const [shareModal, openShareModal] = useState(false); if (info.hidden === true || !info.credit) { return null; } // remove unsplash and pexels text const unsplash = variables.getMessage('widgets.background.unsplash'); const pexels = variables.getMessage('widgets.background.pexels'); let credit = info.credit; let photo = variables.getMessage('widgets.background.credit'); // unsplash and pexels credit if (info.photographerURL && info.photographerURL !== '' && !info.offline && api) { if (api === 'unsplash') { photo = ( {photo} ); credit = ( <> {info.credit} {' '} {unsplash} ); } else { photo = ( {photo} ); credit = ( <> {info.credit} {' '} {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:') ? variables.constants.DDG_IMAGE_PROXY + url : url; // info is still there because we want the favourite button to work if (localStorage.getItem('photoInformation') === 'false') { return (
{credit} {info.location || 'N/A'} {info.camera || 'N/A'} {width}x{height}
); } let showingPhotoMap = false; const photoMap = () => { if ( localStorage.getItem('photoMap') !== 'true' || !info.latitude || !info.longitude || usePhotoMap === false ) { return null; } const zoom = 12; const tile = `${variables.constants.MAPBOX_URL}/styles/v1/mapbox/streets-v11/static/pin-s+555555(${info.longitude},${info.latitude})/${info.longitude},${info.latitude},${zoom},0/300x100?access_token=${info.maptoken}`; showingPhotoMap = true; return ( location ); }; // only request map image if the user looks at the photo information // this is to reduce requests to the api try { document.getElementsByClassName('photoInformation')[0].onmouseover = () => { try { setPhotoMap(true); setMapIcon(false); } catch (e) {} }; } catch (e) {} return (
setOther(true)} onMouseLeave={() => setOther(false)} > openShareModal(false)} > openShareModal(false)} /> {localStorage.getItem('widgetStyle') === 'legacy' && (
{photo} {credit}
)} {localStorage.getItem('widgetStyle') !== 'legacy' || other ? (
setshowExtraInfo(true)} onMouseLeave={() => setshowExtraInfo(false)} >
{photoMap()}
{showingPhotoMap ? (
{' '} © Mapbox{' '} {' '} •{' '} {' '} © OpenStreetMap{' '} {' '} •{' '} {' '} Improve this map{' '}
) : null}
{info.location} {credit} {info.views && info.downloads !== null ? (
{info.views.toLocaleString()}
{info.downloads.toLocaleString()}
) : null}
{showExtraInfo || other ? ( <>
{!info.offline ? ( openShareModal(true)} /> ) : null} {!info.offline ? ( downloadImage(info)} /> ) : null}
{variables.getMessage('widgets.background.information')} {info.location && info.location !== 'N/A' ? (
{info.location}
) : null} {info.camera && info.camera !== 'N/A' ? (
{info.camera}
) : null}
{width}x{height}
{api ? (
{api.charAt(0).toUpperCase() + api.slice(1)}
) : null}
) : null}
) : null}
); } export default memo(PhotoInformation);