import variables from 'config/variables'; import { useState, useEffect } from 'react'; import { MdSource, MdOutlineKeyboardArrowRight, MdOutlineAutoAwesome } from 'react-icons/md'; import { Header, PreferencesWrapper } from 'components/Layout/Settings'; import { Checkbox, Dropdown, Slider, Radio, Text, ChipSelect } from 'components/Form/Settings'; import { Row, Content, Action } from 'components/Layout/Settings/Item'; //import Text from 'components/Form/Settings/Text/Text'; import ColourSettings from './Colour'; import CustomSettings from './Custom'; import values from 'utils/data/slider_values.json'; import { APIQualityOptions, backgroundImageEffects, getBackgroundOptionItems } from './optionTypes'; import defaults from './default'; function BackgroundOptions() { const [backgroundType, setBackgroundType] = useState( localStorage.getItem('backgroundType') || defaults.backgroundType, ); const [backgroundFilter, setBackgroundFilter] = useState( localStorage.getItem('backgroundFilter') || 'none', ); const [backgroundCategories, setBackgroundCategories] = useState([ variables.getMessage('modals.main.loading'), ]); const [backgroundAPI, setBackgroundAPI] = useState( localStorage.getItem('backgroundAPI') || defaults.backgroundAPI, ); const [marketplaceEnabled, setMarketplaceEnabled] = useState(localStorage.getItem('photo_packs')); const [effects, setEffects] = useState(false); const [backgroundSettingsSection, setBackgroundSettingsSection] = useState(false); const controller = new AbortController(); async function getBackgroundCategories() { const data = await ( await fetch(variables.constants.API_URL + '/images/categories', { signal: controller.signal, }) ).json(); if (controller.signal.aborted === true) { return; } if (backgroundAPI !== 'mue') { // remove counts from unsplash categories data.forEach((category) => { delete category.count; }); } setBackgroundCategories(data); } function updateAPI(e) { localStorage.setItem('nextImage', null); if (e === 'mue') { setBackgroundCategories(backgroundCategories); setBackgroundAPI('mue'); } else { const data = backgroundCategories; data.forEach((category) => { delete category.count; }); setBackgroundAPI('unsplash'); setBackgroundCategories(data); } } useEffect(() => { if (navigator.onLine === false || localStorage.getItem('offlineMode') === 'true') { setBackgroundCategories([variables.getMessage('modals.update.offline.title')]); return; } getBackgroundCategories(); return () => { controller.abort(); }; }, []); const APISettings = ( <> {backgroundCategories[0] === variables.getMessage('modals.main.loading') ? ( <> ) : ( )} updateAPI(e)} /> {backgroundAPI === 'unsplash' && ( )} ); let backgroundSettings = APISettings; switch (backgroundType) { case 'custom': backgroundSettings = ; break; case 'colour': backgroundSettings = ; break; case 'random_colour': case 'random_gradient': backgroundSettings = <>; break; default: break; } if ( localStorage.getItem('photo_packs') && backgroundType !== 'custom' && backgroundType !== 'colour' && backgroundType !== 'api' ) { backgroundSettings = null; } const usingImage = backgroundType !== 'colour' && backgroundType !== 'random_colour' && backgroundType !== 'random_gradient'; let header; if (effects === true) { header = (
setEffects(false)} /> ); } else if (backgroundSettingsSection === true) { header = (
setBackgroundSettingsSection(false)} /> ); } else { header = (
); } return ( <> {header} {backgroundSettingsSection !== true && effects !== true ? ( <>
setBackgroundSettingsSection(true)}>
{variables.getMessage('settings:sections.background.source.title')} {variables.getMessage('settings:sections.background.source.subtitle')}
this.setState({ backgroundType: value })} category="background" items={getBackgroundOptionItems(marketplaceEnabled)} />
{backgroundType === 'api' || backgroundType === 'custom' || marketplaceEnabled ? ( <>
setEffects(true)}>
{variables.getMessage('settings:sections.background.effects.title')} {variables.getMessage('settings:sections.background.effects.subtitle')}
{' '}
) : null} ) : null} {backgroundSettingsSection !== true && effects !== true && (backgroundType === 'api' || backgroundType === 'custom' || marketplaceEnabled) ? ( ) : null} {backgroundSettingsSection && ( <> setBackgroundType(value)} category="background" items={getBackgroundOptionItems(marketplaceEnabled)} /> {/* todo: ideally refactor all of this file, but we need interval to appear on marketplace too */} {backgroundSettings} )} {(backgroundType === 'api' || backgroundType === 'custom' || marketplaceEnabled) && effects ? ( setBackgroundFilter(value)} category="backgroundeffect" element="#backgroundImage" items={backgroundImageEffects} /> {backgroundFilter !== 'none' && ( )} ) : null} ); } export { BackgroundOptions as default, BackgroundOptions };