diff --git a/src/features/background/options/BackgroundOptions.jsx b/src/features/background/options/BackgroundOptions.jsx index 8b08ae21..32fdb6a4 100644 --- a/src/features/background/options/BackgroundOptions.jsx +++ b/src/features/background/options/BackgroundOptions.jsx @@ -1,5 +1,5 @@ import variables from 'config/variables'; -import { PureComponent } from 'react'; +import { useState, useEffect } from 'react'; import { MdSource, MdOutlineKeyboardArrowRight, MdOutlineAutoAwesome } from 'react-icons/md'; import { Header, PreferencesWrapper } from 'components/Layout/Settings'; @@ -15,344 +15,355 @@ import { APIQualityOptions, backgroundImageEffects, getBackgroundOptionItems } f import defaults from './default'; -class BackgroundOptions extends PureComponent { - constructor() { - super(); - this.state = { - backgroundType: localStorage.getItem('backgroundType') || defaults.backgroundType, - backgroundFilter: localStorage.getItem('backgroundFilter') || 'none', - backgroundCategories: [variables.getMessage('modals.main.loading')], - backgroundAPI: localStorage.getItem('backgroundAPI') || defaults.backgroundAPI, - marketplaceEnabled: localStorage.getItem('photo_packs'), - effects: false, - backgroundSettingsSection: false, - }; - this.controller = new AbortController(); - } +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); - async getBackgroundCategories() { + const controller = new AbortController(); + + async function getBackgroundCategories() { const data = await ( await fetch(variables.constants.API_URL + '/images/categories', { - signal: this.controller.signal, + signal: controller.signal, }) ).json(); - if (this.controller.signal.aborted === true) { + if (controller.signal.aborted === true) { return; } - if (this.state.backgroundAPI !== 'mue') { + if (backgroundAPI !== 'mue') { // remove counts from unsplash categories data.forEach((category) => { delete category.count; }); } - this.setState({ - backgroundCategories: data, - backgroundCategoriesOG: data, - }); + setBackgroundCategories(data); } - updateAPI(e) { + function updateAPI(e) { localStorage.setItem('nextImage', null); if (e === 'mue') { - this.setState({ - backgroundCategories: this.state.backgroundCategoriesOG, - backgroundAPI: 'mue', - }); + setBackgroundCategories(backgroundCategories); + setBackgroundAPI('mue'); } else { - const data = this.state.backgroundCategories; + const data = backgroundCategories; data.forEach((category) => { delete category.count; }); - this.setState({ - backgroundAPI: 'unsplash', - backgroundCategories: data, - }); + setBackgroundAPI('unsplash'); + setBackgroundCategories(data); } } - componentDidMount() { + useEffect(() => { if (navigator.onLine === false || localStorage.getItem('offlineMode') === 'true') { - return this.setState({ - backgroundCategories: [variables.getMessage('modals.update.offline.title')], - }); + setBackgroundCategories([variables.getMessage('modals.update.offline.title')]); + return; } - this.getBackgroundCategories(); - } + getBackgroundCategories(); - componentWillUnmount() { - // stop making requests - this.controller.abort(); - } + return () => { + controller.abort(); + }; + }, []); - render() { - const APISettings = ( - <> - + const APISettings = ( + <> + + + + {backgroundCategories[0] === variables.getMessage('modals.main.loading') ? ( + <> + + + ) : ( + + )} + + updateAPI(e)} + /> + + + {backgroundAPI === 'unsplash' && ( + - {this.state.backgroundCategories[0] === variables.getMessage('modals.main.loading') ? ( - <> - - - ) : ( - - )} - - this.updateAPI(e)} /> - {this.state.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) ? ( + - + + - )} - - ); - - let backgroundSettings = APISettings; - switch (this.state.backgroundType) { - case 'custom': - backgroundSettings = ; - break; - case 'colour': - backgroundSettings = ; - break; - case 'random_colour': - case 'random_gradient': - backgroundSettings = <>; - break; - default: - break; - } - - if ( - localStorage.getItem('photo_packs') && - this.state.backgroundType !== 'custom' && - this.state.backgroundType !== 'colour' && - this.state.backgroundType !== 'api' - ) { - backgroundSettings = null; - } - - const usingImage = - this.state.backgroundType !== 'colour' && - this.state.backgroundType !== 'random_colour' && - this.state.backgroundType !== 'random_gradient'; - - let header; - if (this.state.effects === true) { - header = ( -
this.setState({ effects: false })} - /> - ); - } else if (this.state.backgroundSettingsSection === true) { - header = ( -
this.setState({ backgroundSettingsSection: false })} - /> - ); - } else { - header = ( -
- ); - } - - return ( - <> - {header} - {this.state.backgroundSettingsSection !== true && this.state.effects !== true ? ( - <> -
this.setState({ backgroundSettingsSection: true })} - > -
- -
- - {variables.getMessage('settings:sections.background.source.title')} - - - {variables.getMessage('settings:sections.background.source.subtitle')} - -
-
-
- this.setState({ backgroundType: value })} - category="background" - items={getBackgroundOptionItems(this.state.marketplaceEnabled)} - /> -
-
- {this.state.backgroundType === 'api' || - this.state.backgroundType === 'custom' || - this.state.marketplaceEnabled ? ( - <> -
this.setState({ effects: true })}> -
- -
- - {variables.getMessage('settings:sections.background.effects.title')} - - - {variables.getMessage('settings:sections.background.effects.subtitle')} - -
-
-
- {' '} - -
-
- - ) : null} - - ) : null} - {this.state.backgroundSettingsSection !== true && - this.state.effects !== true && - (this.state.backgroundType === 'api' || - this.state.backgroundType === 'custom' || - this.state.marketplaceEnabled) ? ( - - - - - - - - - - - ) : null} - {this.state.backgroundSettingsSection && ( - <> - - - - this.setState({ backgroundType: value })} - category="background" - items={getBackgroundOptionItems(this.state.marketplaceEnabled)} - /> - - - {/* todo: ideally refactor all of this file, but we need interval to appear on marketplace too */} - {backgroundSettings} - - )} - {(this.state.backgroundType === 'api' || - this.state.backgroundType === 'custom' || - this.state.marketplaceEnabled) && - this.state.effects ? ( - + + ) : 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' && ( - - this.setState({ backgroundFilter: value })} - category="backgroundeffect" - element="#backgroundImage" - items={backgroundImageEffects} - /> - {this.state.backgroundFilter !== 'none' && ( - - )} - - - ) : null} - - ); - } + )} + + + ) : null} + + ); } export { BackgroundOptions as default, BackgroundOptions };