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:
alexsparkes
2026-01-31 17:44:29 +00:00
parent 7415b9cd5c
commit f7a799fe34
42 changed files with 786 additions and 70 deletions

View File

@@ -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') {

View File

@@ -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);
}

View File

@@ -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>
)}