mirror of
https://github.com/mue/mue.git
synced 2026-07-19 23:14:10 +02:00
feat: add update frequency settings for quotes and backgrounds
- Introduced frequency settings for quotes and background updates in multiple languages. - Added new localization keys for "Update Frequency" and its options. - Implemented a new hook `useFrequencyInterval` to manage update intervals based on user settings. - Created a `frequencyManager` utility to handle frequency intervals and update logic. - Updated default settings to include frequency preferences for both quotes and backgrounds.
This commit is contained in:
@@ -5,6 +5,7 @@ import { ShareModal } from 'components/Elements';
|
||||
import { useQuoteState, useQuoteLoader, useQuoteActions } from './hooks';
|
||||
import { AuthorInfo, AuthorInfoLegacy } from './components';
|
||||
import EventBus from 'utils/eventbus';
|
||||
import { useFrequencyInterval } from '../../hooks/useFrequencyInterval';
|
||||
|
||||
import './scss/index.scss';
|
||||
|
||||
@@ -38,6 +39,9 @@ export default function Quote() {
|
||||
setLocalIsFavourited(!localIsFavourited);
|
||||
};
|
||||
|
||||
// Set up frequency-based interval for automatic quote updates
|
||||
useFrequencyInterval('quote', getQuote);
|
||||
|
||||
useEffect(() => {
|
||||
const handleRefresh = (data) => {
|
||||
if (data === 'quote') {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useCallback } from 'react';
|
||||
import variables from 'config/variables';
|
||||
import offline_quotes from '../offline_quotes.json';
|
||||
import { shouldUpdateByFrequency, resetStartTime } from 'utils/frequencyManager';
|
||||
|
||||
/**
|
||||
* Custom hook for loading quote data from various sources
|
||||
@@ -278,6 +279,21 @@ export function useQuoteLoader(updateQuote) {
|
||||
localStorage.setItem('quotePrefetchEnabled', 'true');
|
||||
}
|
||||
|
||||
// Check if we should update based on frequency
|
||||
if (!shouldUpdateByFrequency('quote')) {
|
||||
// Load cached quote without fetching new one
|
||||
const cached = localStorage.getItem('currentQuote');
|
||||
if (cached) {
|
||||
try {
|
||||
const cachedQuote = JSON.parse(cached);
|
||||
updateQuote(cachedQuote);
|
||||
return;
|
||||
} catch {
|
||||
// If cache invalid, continue to fetch new
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SPECIAL CASE: Favourite quote (highest priority, no queue)
|
||||
const favouriteQuote = localStorage.getItem('favouriteQuote');
|
||||
if (favouriteQuote) {
|
||||
@@ -353,9 +369,10 @@ export function useQuoteLoader(updateQuote) {
|
||||
updateQuote(quoteData);
|
||||
}
|
||||
|
||||
// Step 4: Store current quote
|
||||
// Step 4: Store current quote and reset timestamp
|
||||
try {
|
||||
localStorage.setItem('currentQuote', JSON.stringify(quoteData));
|
||||
resetStartTime('quote'); // Reset timestamp after successfully updating quote
|
||||
} catch (e) {
|
||||
console.warn('Could not save currentQuote to localStorage:', e);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from 'components/Layout/Settings';
|
||||
import { Checkbox, Dropdown, Textarea } from 'components/Form/Settings';
|
||||
import { Button } from 'components/Elements';
|
||||
import { FREQUENCY_OPTIONS } from 'utils/frequencyManager';
|
||||
|
||||
const QuoteOptions = ({ currentSubSection, onSubSectionChange, sectionName }) => {
|
||||
const getCustom = () => {
|
||||
@@ -123,6 +124,39 @@ const QuoteOptions = ({ currentSubSection, onSubSectionChange, sectionName }) =>
|
||||
);
|
||||
};
|
||||
|
||||
const FrequencyOptions = () => {
|
||||
return (
|
||||
<Row>
|
||||
<Content
|
||||
title={variables.getMessage(`${QUOTE_SECTION}.frequency.title`)}
|
||||
subtitle={variables.getMessage(`${QUOTE_SECTION}.frequency.subtitle`)}
|
||||
/>
|
||||
<Action>
|
||||
<Dropdown
|
||||
name="quoteFrequency"
|
||||
label={variables.getMessage(`${QUOTE_SECTION}.frequency.title`)}
|
||||
onChange={(value) => {
|
||||
localStorage.setItem('quoteStartTime', Date.now());
|
||||
// Clear queue if switching from refresh to time-based frequency
|
||||
const oldValue = localStorage.getItem('quoteFrequency');
|
||||
if (oldValue === 'refresh' && value !== 'refresh') {
|
||||
localStorage.removeItem('quoteQueue');
|
||||
}
|
||||
// Notify the frequency interval hook that the frequency changed
|
||||
window.dispatchEvent(new CustomEvent('frequencyChanged', {
|
||||
detail: { type: 'quote' }
|
||||
}));
|
||||
}}
|
||||
items={FREQUENCY_OPTIONS.map((opt) => ({
|
||||
value: opt.value,
|
||||
text: variables.getMessage(opt.text),
|
||||
}))}
|
||||
/>
|
||||
</Action>
|
||||
</Row>
|
||||
);
|
||||
};
|
||||
|
||||
const AdditionalOptions = () => {
|
||||
return (
|
||||
<Row final={true}>
|
||||
@@ -282,6 +316,7 @@ const QuoteOptions = ({ currentSubSection, onSubSectionChange, sectionName }) =>
|
||||
<SourceDropdown />
|
||||
</Section>
|
||||
<ButtonOptions />
|
||||
<FrequencyOptions />
|
||||
<AdditionalOptions />
|
||||
</PreferencesWrapper>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user