import variables from 'config/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, MdFavorite as MdFavourite, MdCategory as Category, MdVisibilityOff as VisibilityOff, } from 'react-icons/md'; import { Tooltip } from 'components/Elements'; import Modal from 'react-modal'; import { ShareModal } from 'components/Elements'; import ExcludeModal from './ExcludeModal'; /** * It takes a URL, fetches the resource, and returns a URL to the resource. * @param {string} url The URL to fetch. * @returns A promise that resolves to a blob URL. */ const toDataURL = async (url) => { const res = await fetch(url); return URL.createObjectURL(await res.blob()); }; /** * It takes a string, makes it lowercase, removes commas, and replaces spaces with dashes. * @param {string} text The string to format. * @returns A function that takes a string and returns a string. */ const formatText = (text) => { return text.toLowerCase().replaceAll(',', '').replaceAll(' ', '-'); }; /** * It downloads an image from a URL and saves it to the user's computer. * @param {object} info The photo information. */ 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`; // image is more likely to be webp or avif btw 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 [useMapIcon, setMapIcon] = useState(true); const [showExtraInfo, setshowExtraInfo] = useState(false); //const [showOld, setShowOld] = useState(true); const [other, setOther] = useState(false); const [shareModal, openShareModal] = useState(false); const [excludeModal, openExcludeModal] = useState(false); const [favouriteTooltipText, setFavouriteTooltipText] = useState( variables.getMessage('widgets.quote.favourite'), ); if (info.hidden === true || !info.credit) { return null; } let credit = info.credit; let photo = variables.getMessage('widgets.background.credit'); // unsplash credit if (info.photographerURL && info.photographerURL !== '' && !info.offline && api) { photo = ( {photo} ); credit = ( <> {info.credit} ); } // get resolution const img = new Image(); img.onload = (event) => { setWidth(event.target.width); setHeight(event.target.height); }; img.src = url; // info is still there because we want the favourite button to work if (localStorage.getItem('photoInformation') === 'false') { return (
{credit} {info.location} {info.camera} {width}x{height}
); } let showingPhotoMap = false; const photoMap = () => { if ( localStorage.getItem('photoMap') !== 'true' || !info.latitude || !info.longitude || usePhotoMap === false ) { return null; } const tile = variables.constants.API_URL + `/map?latitude=${info.latitude}&longitude=${info.longitude}`; showingPhotoMap = true; return ( location ); }; const InformationItems = () => { return (
{info.location && info.location !== 'N/A' ? (
{info.location}
) : null} {info.camera && info.camera !== 'N/A' ? (
{info.camera}
) : null}
{width}x{height}
{info.category && (
{info.category[0].toUpperCase() + info.category.slice(1)}
)} {api && (
{info.photoURL ? ( {api.charAt(0).toUpperCase() + api.slice(1)} ) : ( {api.charAt(0).toUpperCase() + api.slice(1)} )}
)}
); }; const ActionButtons = () => { return (
{!info.offline && ( openShareModal(true)} /> )} setFavouriteTooltipText(text)} /> {!info.offline && ( downloadImage(info)} /> )} {info.pun && info.category && ( openExcludeModal(true)} /> )}
); }; const UnsplashStats = () => { return (
{info.views.toLocaleString()}
{info.downloads.toLocaleString()}
{info.likes ? (
{info.likes.toLocaleString()}
) : null}
); }; let photoMapClassList = 'map-concept'; if (photoMap() !== null) { photoMapClassList += ' photoMap'; } // 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 { // Ignore errors } }; } catch { // Element not found } const widgetStyle = localStorage.getItem('widgetStyle'); return (
setOther(true)} onMouseLeave={() => setOther(false)} > openShareModal(false)} > openShareModal(false)} /> openExcludeModal(false)} > openExcludeModal(false)} /> {widgetStyle === 'legacy' && (
{photo} {credit}
)} {widgetStyle !== 'legacy' || other ? (
setshowExtraInfo(true)} onMouseLeave={() => setshowExtraInfo(false)} >
{useMapIcon || photoMap() === null ? : ''} {photoMap()}
{showingPhotoMap && (
{' '} © Mapbox{' '} {' '} •{' '} {' '} © OpenStreetMap{' '} {' '} •{' '} {' '} Improve this map{' '}
)}
{(showExtraInfo || other) && info.description ? info.description.length > 40 ? info.description.substring(0, 40) + '...' : info.description : info.location?.split(',').slice(-2).join(', ').trim()} {photo} {credit}
{info.views && info.downloads !== null ? : null}
{(showExtraInfo || other) && excludeModal === false ? ( <> {variables.getMessage('widgets.background.information')} ) : null}
) : null}
); } export default memo(PhotoInformation);