refactor: move supporters loading to separate API endpoint

- Add SupportersHandler in misc_handlers.py to serve /api/lm/supporters
- Register new endpoint in misc_route_registrar.py
- Remove supporters from page load template context in model_handlers.py
- Create supportersService.js for frontend data fetching
- Update Header.js to fetch supporters when support modal opens
- Modify support_modal.html to use client-side rendering

This change improves page load performance by loading supporters data
on-demand instead of during initial page render.
This commit is contained in:
Will Miao
2026-02-28 20:14:20 +08:00
parent 77a2215e62
commit 78b55d10ba
20 changed files with 1206 additions and 89 deletions

View File

@@ -5,6 +5,7 @@ import { FilterManager } from '../managers/FilterManager.js';
import { initPageState } from '../state/index.js';
import { getStorageItem } from '../utils/storageHelpers.js';
import { updateElementAttribute } from '../utils/i18nHelpers.js';
import { renderSupporters } from '../services/supportersService.js';
/**
* Header.js - Manages the application header behavior across different pages
@@ -85,9 +86,15 @@ export class HeaderManager {
// Handle support toggle
const supportToggle = document.getElementById('supportToggleBtn');
if (supportToggle) {
supportToggle.addEventListener('click', () => {
supportToggle.addEventListener('click', async () => {
if (window.modalManager) {
window.modalManager.toggleModal('supportModal');
// Load supporters data when modal opens
try {
await renderSupporters();
} catch (error) {
console.error('Error loading supporters:', error);
}
}
});
}

View File

@@ -0,0 +1,104 @@
/**
* Supporters service - Fetches and manages supporters data
*/
let supportersData = null;
let isLoading = false;
let loadPromise = null;
/**
* Fetch supporters data from the API
* @returns {Promise<Object>} Supporters data
*/
export async function fetchSupporters() {
// Return cached data if available
if (supportersData) {
return supportersData;
}
// Return existing promise if already loading
if (isLoading && loadPromise) {
return loadPromise;
}
isLoading = true;
loadPromise = fetch('/api/lm/supporters')
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch supporters: ${response.statusText}`);
}
return response.json();
})
.then(data => {
if (data.success && data.supporters) {
supportersData = data.supporters;
return supportersData;
}
throw new Error(data.error || 'Failed to load supporters data');
})
.catch(error => {
console.error('Error loading supporters:', error);
// Return empty data on error
return {
specialThanks: [],
allSupporters: [],
totalCount: 0
};
})
.finally(() => {
isLoading = false;
loadPromise = null;
});
return loadPromise;
}
/**
* Clear cached supporters data
*/
export function clearSupportersCache() {
supportersData = null;
}
/**
* Render supporters in the support modal
*/
export async function renderSupporters() {
const supporters = await fetchSupporters();
// Update subtitle with total count
const subtitleEl = document.getElementById('supportersSubtitle');
if (subtitleEl) {
// Get the translation key and replace count
const originalText = subtitleEl.textContent;
// Replace the count in the text (simple approach)
subtitleEl.textContent = originalText.replace(/\d+/, supporters.totalCount);
}
// Render special thanks
const specialThanksGrid = document.getElementById('specialThanksGrid');
if (specialThanksGrid && supporters.specialThanks) {
specialThanksGrid.innerHTML = supporters.specialThanks
.map(supporter => `
<div class="supporter-special-card" title="${supporter}">
<span class="supporter-special-name">${supporter}</span>
</div>
`)
.join('');
}
// Render all supporters
const supportersGrid = document.getElementById('supportersGrid');
if (supportersGrid && supporters.allSupporters) {
supportersGrid.innerHTML = supporters.allSupporters
.map((supporter, index, array) => {
const separator = index < array.length - 1
? '<span class="supporter-separator">·</span>'
: '';
return `
<span class="supporter-name-item" title="${supporter}">${supporter}</span>${separator}
`;
})
.join('');
}
}