import { useT } from 'contexts'; import variables from 'config/variables'; import React, { useState, useEffect } from 'react'; import EventBus from 'utils/eventbus'; import { MdCancel, MdAdd, MdSource, MdOutlineFormatQuote, MdExplore, MdPalette, MdRefresh, } from 'react-icons/md'; import googleFonts from 'config/googleFonts.json'; import { toast } from 'react-toastify'; import { Header, Row, Content, Action, Section, PreferencesWrapper, } from 'components/Layout/Settings'; import { Checkbox, Dropdown, Textarea } from 'components/Form/Settings'; import { Button } from 'components/Elements'; import { FREQUENCY_OPTIONS } from 'utils/frequencyManager'; import Items from 'features/marketplace/components/Items/Items'; import { uninstall } from 'utils/marketplace'; import { updateHash } from 'utils/deepLinking'; import './QuoteOptions.scss'; const QuoteOptions = ({ currentSubSection, onSubSectionChange, sectionName }) => { const t = useT(); const getCustom = () => { let data = JSON.parse(localStorage.getItem('customQuote')); if (data === null) { data = []; } return data; }; const [quoteType, setQuoteType] = useState(() => { let type = localStorage.getItem('quoteType') || 'quote_pack'; if (type === 'api') { type = 'quote_pack'; localStorage.setItem('quoteType', 'quote_pack'); } return type; }); useState(() => { if (localStorage.getItem('authorDetails') === null) { localStorage.setItem('authorDetails', 'true'); } }); const [customQuote, setCustomQuote] = useState(getCustom()); useEffect(() => { localStorage.removeItem('quoteQueue'); localStorage.removeItem('currentQuote'); }, [quoteType]); const handleCustomQuote = (e, text, index, type) => { const result = text === true ? e.target.value : e.target.result; const updatedCustomQuote = [...customQuote]; updatedCustomQuote[index][type] = result; setCustomQuote(updatedCustomQuote); localStorage.setItem('customQuote', JSON.stringify(updatedCustomQuote)); document.querySelector('.reminder-info').style.display = 'flex'; localStorage.setItem('showReminder', true); }; const modifyCustomQuote = (type, index) => { const updatedCustomQuote = [...customQuote]; if (type === 'add') { updatedCustomQuote.push({ quote: '', author: '' }); } else { updatedCustomQuote.splice(index, 1); } setCustomQuote(updatedCustomQuote); localStorage.setItem('customQuote', JSON.stringify(updatedCustomQuote)); localStorage.removeItem('quoteQueue'); localStorage.removeItem('currentQuote'); }; const QUOTE_SECTION = 'modals.main.settings.sections.quote'; const ButtonOptions = () => { return ( ); }; const SourceDropdown = () => { return ( setQuoteType(value)} category="quote" items={[ localStorage.getItem('quote_packs') && { value: 'quote_pack', text: t('modals.main.marketplace.title'), }, { value: 'custom', text: t(`${QUOTE_SECTION}.custom`) }, ]} /> ); }; const FrequencyOptions = () => { return ( { localStorage.setItem('quoteStartTime', Date.now()); const oldValue = localStorage.getItem('quoteFrequency'); if (oldValue === 'refresh' && value !== 'refresh') { localStorage.removeItem('quoteQueue'); } window.dispatchEvent( new CustomEvent('frequencyChanged', { detail: { type: 'quote' }, }), ); }} items={FREQUENCY_OPTIONS.map((opt) => ({ value: opt.value, text: t(opt.text), }))} /> ); }; const AdditionalOptions = () => { return ( ); }; const AppearanceSection = () => { const [quoteColor, setQuoteColor] = useState(localStorage.getItem('quoteColor') || '#ffffff'); const fontWeight = `${QUOTE_SECTION}.appearance.font_weight`; const updateColor = (event) => { const color = event.target.value; setQuoteColor(color); localStorage.setItem('quoteColor', color); EventBus.emit('refresh', 'quote'); }; return ( <> ({ value: font, text: font, }))} /> {quoteColor} { localStorage.setItem('quoteColor', '#ffffff'); setQuoteColor('#ffffff'); }} > {t('modals.main.settings.buttons.reset')} > ); }; const isSourceSection = currentSubSection === 'source'; const isAppearanceSection = currentSubSection === 'appearance'; const getInstalledQuotePacks = () => { try { const installed = JSON.parse(localStorage.getItem('installed')) || []; return installed.filter((item) => item.type === 'quotes'); } catch (e) { return []; } }; const [installedQuotePacks, setInstalledQuotePacks] = useState(getInstalledQuotePacks()); useEffect(() => { const handleInstalledAddonsChanged = () => { setInstalledQuotePacks(getInstalledQuotePacks()); const currentType = localStorage.getItem('quoteType') || 'api'; if (currentType !== quoteType) { setQuoteType(currentType); } }; window.addEventListener('installedAddonsChanged', handleInstalledAddonsChanged); return () => { window.removeEventListener('installedAddonsChanged', handleInstalledAddonsChanged); }; }, [quoteType]); const handleUninstall = (type, name) => { const DEFAULT_PACK_ID = '0c8a5bdebd13'; if (installedQuotePacks.length === 1) { const remainingPack = installedQuotePacks[0]; if (remainingPack.id === DEFAULT_PACK_ID || remainingPack.name === name) { toast(t('toasts.quote_pack_only_one')); return; } } uninstall(type, name); toast(t('toasts.uninstalled')); variables.stats.postEvent('marketplace-item', `${name} uninstalled`); variables.stats.postEvent('marketplace', 'Uninstall'); setInstalledQuotePacks(getInstalledQuotePacks()); window.dispatchEvent(new window.Event('installedAddonsChanged')); }; const goToQuotePacks = () => { updateHash('#discover/quote_packs'); const event = new window.Event('popstate'); window.dispatchEvent(event); }; const handleToggle = (pack) => { const itemId = pack.name; updateHash(`#discover/all?item=${itemId}`); const event = new window.Event('popstate'); window.dispatchEvent(event); variables.stats.postEvent('marketplace', 'ItemPage viewed'); }; const getTotalQuoteCount = () => { return installedQuotePacks.reduce((total, pack) => { return total + (pack.quotes?.length || 0); }, 0); }; let customSettings; if (quoteType === 'custom' && isSourceSection) { customSettings = ( <> modifyCustomQuote('add')} icon={} label={t(`${QUOTE_SECTION}.add`)} /> {customQuote.length !== 0 ? ( {customQuote.map((quote, index) => ( {t('modals.main.settings.sections.quote.title')} modifyCustomQuote('remove', index)} aria-label={t('modals.main.marketplace.product.buttons.remove')} > handleCustomQuote(e, true, index, 'quote')} minRows={3} className="quoteTextarea" /> {t('modals.main.settings.sections.quote.author')} handleCustomQuote(e, true, index, 'author')} /> ))} ) : ( {t(`${QUOTE_SECTION}.no_quotes`)} {t('modals.main.settings.sections.message.add_some')} modifyCustomQuote('add')} icon={} label={t(`${QUOTE_SECTION}.add`)} /> )} > ); } else if (quoteType === 'quote_pack' && isSourceSection && installedQuotePacks.length > 0) { const totalQuotes = getTotalQuoteCount(); customSettings = ( <> } label={t('modals.main.settings.sections.quote.get_more')} /> {}} viewType="grid" showChips={false} /> > ); } else { // api customSettings = <>>; } let header; if (isSourceSection) { header = ( onSubSectionChange(null, sectionName)} report={false} /> ); } else if (isAppearanceSection) { header = ( onSubSectionChange(null, sectionName)} report={false} > { localStorage.removeItem('quoteFont'); localStorage.removeItem('quoteFontWeight'); localStorage.removeItem('quoteFontStyle'); localStorage.setItem('quoteColor', '#ffffff'); EventBus.emit('refresh', 'quote'); toast(t('toasts.reset')); }} icon={} label={t('modals.main.settings.buttons.reset')} /> ); } else { header = ( ); } return ( <> {header} {isSourceSection && ( <> {customSettings} > )} {isAppearanceSection && } {!isSourceSection && !isAppearanceSection && ( } title={t(`${QUOTE_SECTION}.appearance.title`)} subtitle={t(`${QUOTE_SECTION}.appearance.description`)} onClick={() => onSubSectionChange('appearance', sectionName)} > { e.stopPropagation(); localStorage.removeItem('quoteFont'); localStorage.removeItem('quoteFontWeight'); localStorage.removeItem('quoteFontStyle'); localStorage.setItem('quoteColor', '#ffffff'); EventBus.emit('refresh', 'quote'); toast(t('toasts.reset')); }} icon={} label={t('modals.main.settings.buttons.reset')} /> } title={t('modals.main.settings.sections.background.source.title')} subtitle={t(`${QUOTE_SECTION}.source_subtitle`)} onClick={() => onSubSectionChange('source', sectionName)} > )} > ); }; export { QuoteOptions as default, QuoteOptions };