Skip to content

Commit 463406b

Browse files
committed
Refactor timezone handling in state management
- Removed the backend timezone cache clearing functionality, simplifying the process when users change their timezone settings. - Introduced a new method in the frontend to reload state management displays directly, enhancing user experience by automatically reflecting timezone changes. - Updated related functions to ensure accurate state management data is fetched and displayed for each application instance.
1 parent 64e966c commit 463406b

File tree

3 files changed

+65
-70
lines changed

3 files changed

+65
-70
lines changed

frontend/static/js/new-main.js

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,51 +3763,75 @@ let huntarrUI = {
37633763
console.log('[huntarrUI] Refreshing state management timezone displays due to settings change');
37643764

37653765
try {
3766-
// First, tell the backend to clear its timezone cache
3767-
HuntarrUtils.fetchWithTimeout('./api/stateful/refresh-timezone', {
3768-
method: 'POST',
3769-
headers: { 'Content-Type': 'application/json' }
3770-
})
3771-
.then(response => response.json())
3772-
.then(data => {
3773-
if (data.success) {
3774-
console.log('[huntarrUI] Backend timezone cache cleared for state management');
3775-
} else {
3776-
console.warn('[huntarrUI] Failed to clear backend timezone cache:', data.message);
3777-
}
3778-
})
3779-
.catch(error => {
3780-
console.warn('[huntarrUI] Error clearing backend timezone cache:', error);
3781-
});
3782-
3783-
// Refresh all visible state management displays
3784-
const supportedApps = ['sonarr', 'radarr', 'lidarr', 'readarr', 'whisparr', 'eros'];
3766+
// Simply reload the displays - the backend will use the new timezone automatically
3767+
this.reloadStateManagementDisplays();
37853768

3786-
supportedApps.forEach(appType => {
3787-
// Find all instance containers for this app
3788-
const appPanel = document.getElementById(`${appType}-panel`);
3789-
if (appPanel && appPanel.style.display !== 'none') {
3790-
// Look for instance containers
3791-
const instanceContainers = appPanel.querySelectorAll(`[id^="${appType}-instance-"]`);
3792-
3793-
instanceContainers.forEach((container, index) => {
3794-
// Check if state management is enabled for this instance
3795-
const stateStatusElement = document.getElementById(`${appType}-state-status-${index}`);
3796-
if (stateStatusElement && stateStatusElement.style.display !== 'none') {
3797-
console.log(`[huntarrUI] Refreshing state management for ${appType} instance ${index}`);
3798-
// Reload state info for this instance to get updated timezone
3799-
this.loadInstanceStateInfo(appType, index);
3800-
}
3801-
});
3802-
}
3803-
});
3804-
3805-
console.log('[huntarrUI] State management timezone refresh completed');
38063769
} catch (error) {
38073770
console.error('[huntarrUI] Error refreshing state management timezone:', error);
38083771
}
38093772
},
38103773

3774+
// Reload state management displays after timezone change
3775+
reloadStateManagementDisplays: function() {
3776+
console.log('[huntarrUI] Reloading state management displays after timezone change');
3777+
3778+
// Refresh all visible state management displays
3779+
const supportedApps = ['sonarr', 'radarr', 'lidarr', 'readarr', 'whisparr', 'eros'];
3780+
3781+
supportedApps.forEach(appType => {
3782+
// Find all instance containers for this app
3783+
const appPanel = document.getElementById(`${appType}-panel`);
3784+
if (appPanel && appPanel.style.display !== 'none') {
3785+
// Look for state reset time elements
3786+
const stateElements = appPanel.querySelectorAll(`[id*="${appType}-state-reset-time-"]`);
3787+
3788+
stateElements.forEach(element => {
3789+
// Extract instance index from element ID
3790+
const match = element.id.match(/(\w+)-state-reset-time-(\d+)/);
3791+
if (match) {
3792+
const instanceIndex = parseInt(match[2]);
3793+
3794+
// Get instance name from the form
3795+
const instanceNameElement = document.querySelector(`#${appType}-instance-name-${instanceIndex}`);
3796+
if (instanceNameElement) {
3797+
const instanceName = instanceNameElement.value || 'Default';
3798+
3799+
console.log(`[huntarrUI] Reloading state management for ${appType} instance ${instanceIndex} (${instanceName})`);
3800+
3801+
// Fetch fresh state management data
3802+
this.loadStateManagementForInstance(appType, instanceIndex, instanceName);
3803+
}
3804+
}
3805+
});
3806+
}
3807+
});
3808+
3809+
console.log('[huntarrUI] State management timezone refresh completed');
3810+
},
3811+
3812+
// Load state management data for a specific instance
3813+
loadStateManagementForInstance: function(appType, instanceIndex, instanceName) {
3814+
const url = `./api/stateful/summary?app_type=${encodeURIComponent(appType)}&instance_name=${encodeURIComponent(instanceName)}`;
3815+
3816+
HuntarrUtils.fetchWithTimeout(url, {
3817+
method: 'GET'
3818+
})
3819+
.then(response => response.json())
3820+
.then(data => {
3821+
if (data.success) {
3822+
console.log(`[huntarrUI] Received updated state management data for ${appType}/${instanceName}:`, data);
3823+
3824+
// Update the display with the new timezone-converted data
3825+
this.updateInstanceStateDisplay(appType, instanceIndex, data, instanceName, data.expiration_hours);
3826+
} else {
3827+
console.warn(`[huntarrUI] Failed to get state management data for ${appType}/${instanceName}:`, data.message || 'Unknown error');
3828+
}
3829+
})
3830+
.catch(error => {
3831+
console.error(`[huntarrUI] Error loading state management data for ${appType}/${instanceName}:`, error);
3832+
});
3833+
},
3834+
38113835
showRequestarrSidebar: function() {
38123836
// Hide main sidebar and settings sidebar
38133837
const mainSidebar = document.getElementById('sidebar');

src/primary/stateful_manager.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,7 @@ def _get_user_timezone():
397397
import pytz
398398
return pytz.UTC
399399

400-
def clear_timezone_cache():
401-
"""
402-
Clear timezone cache and force fresh timezone lookup for state management.
403-
This should be called when user changes timezone settings.
404-
"""
405-
try:
406-
from src.primary.utils.timezone_utils import clear_timezone_cache as clear_tz_cache
407-
clear_tz_cache()
408-
stateful_logger.debug("Cleared timezone cache for state management")
409-
except Exception as e:
410-
stateful_logger.warning(f"Error clearing timezone cache: {e}")
400+
411401

412402
def get_next_reset_time() -> Optional[str]:
413403
"""

src/primary/stateful_routes.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,4 @@ def get_summary():
222222
response.headers['Access-Control-Allow-Origin'] = '*'
223223
return response
224224

225-
@stateful_api.route('/refresh-timezone', methods=['POST'])
226-
def refresh_timezone():
227-
"""Refresh timezone cache for state management when user changes timezone settings."""
228-
try:
229-
from src.primary.stateful_manager import clear_timezone_cache
230-
clear_timezone_cache()
231-
232-
response_data = {"success": True, "message": "State management timezone cache refreshed"}
233-
response = Response(json.dumps(response_data))
234-
response.headers['Content-Type'] = 'application/json'
235-
response.headers['Access-Control-Allow-Origin'] = '*'
236-
return response
237-
238-
except Exception as e:
239-
stateful_logger.error(f"Error refreshing state management timezone: {e}")
240-
error_data = {"success": False, "message": f"Error refreshing timezone: {str(e)}"}
241-
response = Response(json.dumps(error_data), status=500)
242-
response.headers['Content-Type'] = 'application/json'
243-
response.headers['Access-Control-Allow-Origin'] = '*'
244-
return response
225+

0 commit comments

Comments
 (0)