better schedule page
This commit is contained in:
@@ -1,76 +1,373 @@
|
||||
const ANILIST_API = 'https://graphql.anilist.co';
|
||||
const CACHE_NAME = 'waifuboard-schedule-v1';
|
||||
const CACHE_DURATION = 5 * 60 * 1000;
|
||||
const CACHE_NAME = 'waifuboard-schedule-v5';
|
||||
|
||||
const CACHE_DURATION = 6 * 60 * 60 * 1000;
|
||||
|
||||
const state = {
|
||||
currentDate: new Date(),
|
||||
viewType: 'MONTH',
|
||||
mode: 'SUB',
|
||||
filter: 'ALL',
|
||||
loading: false,
|
||||
abortController: null,
|
||||
refreshInterval: null
|
||||
userListIds: new Set(),
|
||||
scheduleData: [],
|
||||
selectedWeekDayIndex: 0
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
await fetchUserList();
|
||||
renderHeader();
|
||||
fetchSchedule();
|
||||
|
||||
state.refreshInterval = setInterval(() => {
|
||||
console.log("Auto-refreshing schedule...");
|
||||
fetchSchedule(true);
|
||||
}, CACHE_DURATION);
|
||||
if (state.userListIds.size > 0) {
|
||||
const filterGroup = document.getElementById('filter-group');
|
||||
if (filterGroup) filterGroup.style.display = 'flex';
|
||||
}
|
||||
});
|
||||
|
||||
async function getCache(key) {
|
||||
async function fetchUserList() {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) return;
|
||||
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const res = await fetch('http://localhost:54322/api/list', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const response = await cache.match(`/schedule-cache/${key}`);
|
||||
if (res.ok) {
|
||||
const json = await res.json();
|
||||
if (json.results && Array.isArray(json.results)) {
|
||||
state.userListIds.clear();
|
||||
json.results.forEach(item => {
|
||||
|
||||
if (!response) return null;
|
||||
|
||||
const cached = await response.json();
|
||||
const age = Date.now() - cached.timestamp;
|
||||
|
||||
if (age < CACHE_DURATION) {
|
||||
console.log(`[Cache Hit] Loaded ${key} (Age: ${Math.round(age / 1000)}s)`);
|
||||
return cached.data;
|
||||
if (item.source === 'anilist') {
|
||||
state.userListIds.add(item.entry_id);
|
||||
}
|
||||
});
|
||||
console.log(`[UserList] Loaded ${state.userListIds.size} entries.`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[Cache Stale] ${key} expired.`);
|
||||
|
||||
cache.delete(`/schedule-cache/${key}`);
|
||||
return null;
|
||||
} catch (e) {
|
||||
console.error("Cache read failed", e);
|
||||
return null;
|
||||
console.warn("[UserList] Could not fetch user list:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function setCache(key, data) {
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const payload = JSON.stringify({
|
||||
timestamp: Date.now(),
|
||||
data: data
|
||||
});
|
||||
async function fetchSchedule(forceRefresh = false) {
|
||||
const key = getCacheKey();
|
||||
|
||||
const response = new Response(payload, {
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
await cache.put(`/schedule-cache/${key}`, response);
|
||||
} catch (e) {
|
||||
console.warn("Cache write failed", e);
|
||||
if (!forceRefresh) {
|
||||
const cachedData = await getCache(key);
|
||||
if (cachedData) {
|
||||
console.log(`[Schedule] Using cached data for key: ${key}`);
|
||||
state.scheduleData = cachedData;
|
||||
renderContent();
|
||||
updateAmbient(cachedData);
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getCacheKey() {
|
||||
if (state.abortController) state.abortController.abort();
|
||||
state.abortController = new AbortController();
|
||||
const signal = state.abortController.signal;
|
||||
|
||||
setLoading(true);
|
||||
|
||||
let startObj, endObj;
|
||||
|
||||
if (state.viewType === 'MONTH') {
|
||||
return `M_${state.currentDate.getFullYear()}_${state.currentDate.getMonth()}`;
|
||||
const year = state.currentDate.getFullYear();
|
||||
const month = state.currentDate.getMonth();
|
||||
startObj = new Date(year, month, 1, 0, 0, 0, 0);
|
||||
endObj = new Date(year, month + 1, 0, 23, 59, 59, 999);
|
||||
} else {
|
||||
const start = getWeekStart(state.currentDate);
|
||||
return `W_${start.toISOString().split('T')[0]}`;
|
||||
|
||||
startObj = new Date(start);
|
||||
|
||||
endObj = new Date(startObj);
|
||||
endObj.setDate(endObj.getDate() + 7);
|
||||
endObj.setHours(23, 59, 59, 999);
|
||||
}
|
||||
|
||||
const startTs = Math.floor(startObj.getTime() / 1000);
|
||||
const endTs = Math.floor(endObj.getTime() / 1000);
|
||||
|
||||
const query = `
|
||||
query ($start: Int, $end: Int, $page: Int) {
|
||||
Page(page: $page, perPage: 50) {
|
||||
pageInfo { hasNextPage }
|
||||
airingSchedules(airingAt_greater: $start, airingAt_lesser: $end, sort: TIME) {
|
||||
airingAt
|
||||
episode
|
||||
media {
|
||||
id
|
||||
title { userPreferred english }
|
||||
coverImage { large extraLarge }
|
||||
bannerImage
|
||||
isAdult
|
||||
countryOfOrigin
|
||||
format
|
||||
duration
|
||||
popularity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
let allData = [];
|
||||
let page = 1;
|
||||
let hasNext = true;
|
||||
|
||||
try {
|
||||
while (hasNext && page <= 10) {
|
||||
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
|
||||
const res = await fetch(ANILIST_API, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
||||
body: JSON.stringify({ query, variables: { start: startTs, end: endTs, page } }),
|
||||
signal: signal
|
||||
});
|
||||
|
||||
if (res.status === 429) {
|
||||
await delay(2000);
|
||||
continue;
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
if (json.errors) throw new Error("API Error");
|
||||
|
||||
const data = json.data.Page;
|
||||
allData = [...allData, ...data.airingSchedules];
|
||||
hasNext = data.pageInfo.hasNextPage;
|
||||
page++;
|
||||
await delay(200);
|
||||
}
|
||||
|
||||
if (!signal.aborted) {
|
||||
state.scheduleData = allData;
|
||||
await setCache(key, allData);
|
||||
|
||||
renderContent();
|
||||
updateAmbient(allData);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
if (e.name !== 'AbortError') console.error("Fetch failed:", e);
|
||||
} finally {
|
||||
if (!signal.aborted) {
|
||||
setLoading(false);
|
||||
state.abortController = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function renderContent() {
|
||||
|
||||
let items = state.scheduleData.filter(i =>
|
||||
!i.media.isAdult &&
|
||||
i.media.countryOfOrigin === 'JP'
|
||||
|
||||
);
|
||||
|
||||
if (state.mode === 'DUB') {
|
||||
items = items.filter(i => i.media.popularity > 20000);
|
||||
}
|
||||
|
||||
if (state.filter === 'MY_LIST') {
|
||||
items = items.filter(i => state.userListIds.has(i.media.id));
|
||||
}
|
||||
|
||||
const container = document.getElementById('schedule-content');
|
||||
if (!container) return;
|
||||
|
||||
container.innerHTML = '';
|
||||
|
||||
if (state.viewType === 'MONTH') {
|
||||
renderMonthView(container, items);
|
||||
} else {
|
||||
renderWeekView(container, items);
|
||||
}
|
||||
}
|
||||
|
||||
function renderMonthView(container, items) {
|
||||
const grid = document.createElement('div');
|
||||
grid.className = 'calendar-grid';
|
||||
|
||||
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||
days.forEach(d => {
|
||||
const h = document.createElement('div');
|
||||
h.className = 'weekday-header';
|
||||
h.textContent = d;
|
||||
grid.appendChild(h);
|
||||
});
|
||||
|
||||
const year = state.currentDate.getFullYear();
|
||||
const month = state.currentDate.getMonth();
|
||||
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
||||
|
||||
let firstDayIndex = new Date(year, month, 1).getDay() - 1;
|
||||
if (firstDayIndex === -1) firstDayIndex = 6;
|
||||
|
||||
for (let i = 0; i < firstDayIndex; i++) {
|
||||
const empty = document.createElement('div');
|
||||
empty.className = 'day-cell empty';
|
||||
grid.appendChild(empty);
|
||||
}
|
||||
|
||||
for (let day = 1; day <= daysInMonth; day++) {
|
||||
const cell = document.createElement('div');
|
||||
cell.className = 'day-cell';
|
||||
|
||||
const currentCellDate = new Date(year, month, day);
|
||||
const now = new Date();
|
||||
|
||||
if (isSameDay(currentCellDate, now)) {
|
||||
cell.classList.add('today');
|
||||
}
|
||||
|
||||
cell.innerHTML = `<span class="day-number">${day}</span>`;
|
||||
|
||||
const dayEvents = items.filter(i => {
|
||||
const releaseDate = new Date(i.airingAt * 1000);
|
||||
return isSameDay(currentCellDate, releaseDate);
|
||||
});
|
||||
|
||||
dayEvents.sort((a, b) => b.media.popularity - a.media.popularity);
|
||||
|
||||
dayEvents.forEach(evt => {
|
||||
const title = evt.media.title.english || evt.media.title.userPreferred;
|
||||
const ep = evt.episode;
|
||||
const time = new Date(evt.airingAt * 1000).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
|
||||
const isMine = state.userListIds.has(evt.media.id);
|
||||
|
||||
const el = document.createElement('a');
|
||||
el.className = `anime-item-month ${isMine ? 'is-mine' : ''}`;
|
||||
el.href = `/anime/${evt.media.id}`;
|
||||
el.innerHTML = `
|
||||
<span class="item-time">${time}</span>
|
||||
<span class="item-title">
|
||||
${title} <span class="ep-badge">EP ${ep}</span>
|
||||
</span>
|
||||
`;
|
||||
|
||||
cell.appendChild(el);
|
||||
});
|
||||
|
||||
grid.appendChild(cell);
|
||||
}
|
||||
|
||||
container.appendChild(grid);
|
||||
}
|
||||
|
||||
function renderWeekView(container, items) {
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.className = 'week-container';
|
||||
|
||||
const nav = document.createElement('div');
|
||||
nav.className = 'week-nav';
|
||||
|
||||
const startOfWeek = getWeekStart(state.currentDate);
|
||||
const dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
||||
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const d = new Date(startOfWeek);
|
||||
d.setDate(startOfWeek.getDate() + i);
|
||||
|
||||
const btn = document.createElement('button');
|
||||
btn.className = `day-btn ${i === state.selectedWeekDayIndex ? 'active' : ''}`;
|
||||
|
||||
btn.onclick = () => {
|
||||
state.selectedWeekDayIndex = i;
|
||||
renderWeekCards(d, items);
|
||||
|
||||
document.querySelectorAll('.day-btn').forEach(b => b.classList.remove('active'));
|
||||
btn.classList.add('active');
|
||||
};
|
||||
|
||||
btn.innerHTML = `
|
||||
<span class="name">${dayNames[i]}</span>
|
||||
<span class="date">${d.getDate()}</span>
|
||||
`;
|
||||
nav.appendChild(btn);
|
||||
}
|
||||
wrapper.appendChild(nav);
|
||||
|
||||
const grid = document.createElement('div');
|
||||
grid.className = 'week-grid';
|
||||
grid.id = 'weekGrid';
|
||||
wrapper.appendChild(grid);
|
||||
|
||||
container.appendChild(wrapper);
|
||||
|
||||
const selectedDate = new Date(startOfWeek);
|
||||
selectedDate.setDate(selectedDate.getDate() + state.selectedWeekDayIndex);
|
||||
renderWeekCards(selectedDate, items);
|
||||
}
|
||||
|
||||
function renderWeekCards(targetDate, allItems) {
|
||||
const grid = document.getElementById('weekGrid');
|
||||
if (!grid) return;
|
||||
grid.innerHTML = '';
|
||||
|
||||
const dayItems = allItems.filter(i => {
|
||||
const releaseDate = new Date(i.airingAt * 1000);
|
||||
return isSameDay(targetDate, releaseDate);
|
||||
});
|
||||
|
||||
dayItems.sort((a, b) => b.media.popularity - a.media.popularity);
|
||||
|
||||
if (dayItems.length === 0) {
|
||||
grid.innerHTML = `
|
||||
<div style="grid-column: 1/-1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 4rem; opacity: 0.5;">
|
||||
<p style="margin-top: 1rem; font-size: 1.2rem; font-weight: 500;">No releases found for this day</p>
|
||||
</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
dayItems.forEach(evt => {
|
||||
const m = evt.media;
|
||||
const title = m.title.english || m.title.userPreferred;
|
||||
const time = new Date(evt.airingAt * 1000).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
|
||||
|
||||
const card = document.createElement('div');
|
||||
card.className = `card`;
|
||||
card.onclick = () => window.location.href = `/anime/${m.id}`;
|
||||
|
||||
card.innerHTML = `
|
||||
<div class="card-img-wrap">
|
||||
<img src="${m.coverImage.large}" alt="${title}" loading="lazy">
|
||||
<div class="card-ep-badge">EP ${evt.episode} • ${time}</div>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
<h3>${title}</h3>
|
||||
<p>${m.format || 'TV'}</p>
|
||||
</div>
|
||||
`;
|
||||
grid.appendChild(card);
|
||||
});
|
||||
}
|
||||
|
||||
function isSameDay(d1, d2) {
|
||||
return d1.getFullYear() === d2.getFullYear() &&
|
||||
d1.getMonth() === d2.getMonth() &&
|
||||
d1.getDate() === d2.getDate();
|
||||
}
|
||||
|
||||
function getWeekStart(d) {
|
||||
const date = new Date(d);
|
||||
date.setHours(0, 0, 0, 0);
|
||||
|
||||
const day = date.getDay();
|
||||
const diff = date.getDate() - day + (day === 0 ? -6 : 1);
|
||||
date.setDate(diff);
|
||||
return date;
|
||||
}
|
||||
|
||||
function navigate(delta) {
|
||||
@@ -93,10 +390,19 @@ function setViewType(type) {
|
||||
document.getElementById('btnViewMonth').classList.toggle('active', type === 'MONTH');
|
||||
document.getElementById('btnViewWeek').classList.toggle('active', type === 'WEEK');
|
||||
|
||||
if (state.abortController) state.abortController.abort();
|
||||
state.selectedWeekDayIndex = new Date().getDay() - 1;
|
||||
if (state.selectedWeekDayIndex === -1) state.selectedWeekDayIndex = 6;
|
||||
|
||||
renderHeader();
|
||||
fetchSchedule();
|
||||
|
||||
if (state.scheduleData.length) {
|
||||
renderContent();
|
||||
|
||||
} else {
|
||||
fetchSchedule();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function setMode(mode) {
|
||||
@@ -104,8 +410,18 @@ function setMode(mode) {
|
||||
state.mode = mode;
|
||||
document.getElementById('btnSub').classList.toggle('active', mode === 'SUB');
|
||||
document.getElementById('btnDub').classList.toggle('active', mode === 'DUB');
|
||||
renderContent();
|
||||
|
||||
fetchSchedule();
|
||||
}
|
||||
|
||||
function setFilter(filterType) {
|
||||
if (state.filter === filterType) return;
|
||||
state.filter = filterType;
|
||||
|
||||
document.getElementById('btnAll').classList.toggle('active', filterType === 'ALL');
|
||||
document.getElementById('btnMyList').classList.toggle('active', filterType === 'MY_LIST');
|
||||
|
||||
renderContent();
|
||||
}
|
||||
|
||||
function renderHeader() {
|
||||
@@ -119,242 +435,86 @@ function renderHeader() {
|
||||
|
||||
const startStr = start.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
const endStr = end.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
|
||||
title = `Week of ${startStr} - ${endStr}`;
|
||||
title = `${startStr} - ${endStr}`;
|
||||
}
|
||||
|
||||
document.getElementById('monthTitle').textContent = title;
|
||||
}
|
||||
|
||||
function getWeekStart(date) {
|
||||
const d = new Date(date);
|
||||
const day = d.getDay();
|
||||
const diff = d.getDate() - day + (day === 0 ? -6 : 1);
|
||||
return new Date(d.setDate(diff));
|
||||
}
|
||||
|
||||
async function fetchSchedule(forceRefresh = false) {
|
||||
const key = getCacheKey();
|
||||
|
||||
if (!forceRefresh) {
|
||||
const cachedData = await getCache(key);
|
||||
if (cachedData) {
|
||||
renderGrid(cachedData);
|
||||
updateAmbient(cachedData);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (state.abortController) state.abortController.abort();
|
||||
state.abortController = new AbortController();
|
||||
const signal = state.abortController.signal;
|
||||
|
||||
if (!forceRefresh) setLoading(true);
|
||||
|
||||
let startTs, endTs;
|
||||
if (state.viewType === 'MONTH') {
|
||||
const year = state.currentDate.getFullYear();
|
||||
const month = state.currentDate.getMonth();
|
||||
startTs = Math.floor(new Date(year, month, 1).getTime() / 1000);
|
||||
endTs = Math.floor(new Date(year, month + 1, 0, 23, 59, 59).getTime() / 1000);
|
||||
} else {
|
||||
const start = getWeekStart(state.currentDate);
|
||||
start.setHours(0, 0, 0, 0);
|
||||
const end = new Date(start);
|
||||
end.setDate(end.getDate() + 7);
|
||||
startTs = Math.floor(start.getTime() / 1000);
|
||||
endTs = Math.floor(end.getTime() / 1000);
|
||||
}
|
||||
|
||||
const query = `
|
||||
query ($start: Int, $end: Int, $page: Int) {
|
||||
Page(page: $page, perPage: 50) {
|
||||
pageInfo { hasNextPage }
|
||||
airingSchedules(airingAt_greater: $start, airingAt_lesser: $end, sort: TIME) {
|
||||
airingAt
|
||||
episode
|
||||
media {
|
||||
id
|
||||
title { userPreferred english }
|
||||
coverImage { large }
|
||||
bannerImage
|
||||
isAdult
|
||||
countryOfOrigin
|
||||
popularity
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
let allData = [];
|
||||
let page = 1;
|
||||
let hasNext = true;
|
||||
let retries = 0;
|
||||
|
||||
try {
|
||||
while (hasNext && page <= 6) {
|
||||
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
|
||||
try {
|
||||
const res = await fetch(ANILIST_API, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
variables: { start: startTs, end: endTs, page }
|
||||
}),
|
||||
signal: signal
|
||||
});
|
||||
|
||||
if (res.status === 429) {
|
||||
if (retries > 2) throw new Error("Rate Limited");
|
||||
console.warn("429 Hit. Waiting...");
|
||||
await delay(4000);
|
||||
retries++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const json = await res.json();
|
||||
if (json.errors) throw new Error("API Error");
|
||||
|
||||
const data = json.data.Page;
|
||||
allData = [...allData, ...data.airingSchedules];
|
||||
hasNext = data.pageInfo.hasNextPage;
|
||||
page++;
|
||||
|
||||
await delay(600);
|
||||
|
||||
} catch (e) {
|
||||
if (e.name === 'AbortError') throw e;
|
||||
console.error(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!signal.aborted) {
|
||||
|
||||
await setCache(key, allData);
|
||||
renderGrid(allData);
|
||||
updateAmbient(allData);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
if (e.name !== 'AbortError') console.error("Fetch failed:", e);
|
||||
} finally {
|
||||
if (!signal.aborted) {
|
||||
setLoading(false);
|
||||
state.abortController = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function renderGrid(data) {
|
||||
const grid = document.getElementById('daysGrid');
|
||||
grid.innerHTML = '';
|
||||
|
||||
let items = data.filter(i => !i.media.isAdult && i.media.countryOfOrigin === 'JP');
|
||||
if (state.mode === 'DUB') {
|
||||
items = items.filter(i => i.media.popularity > 20000);
|
||||
}
|
||||
|
||||
if (state.viewType === 'MONTH') {
|
||||
const year = state.currentDate.getFullYear();
|
||||
const month = state.currentDate.getMonth();
|
||||
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
||||
let firstDayIndex = new Date(year, month, 1).getDay() - 1;
|
||||
if (firstDayIndex === -1) firstDayIndex = 6;
|
||||
|
||||
for (let i = 0; i < firstDayIndex; i++) {
|
||||
const empty = document.createElement('div');
|
||||
empty.className = 'day-cell empty';
|
||||
grid.appendChild(empty);
|
||||
}
|
||||
|
||||
for (let day = 1; day <= daysInMonth; day++) {
|
||||
const dateObj = new Date(year, month, day);
|
||||
renderDayCell(dateObj, items, grid);
|
||||
}
|
||||
} else {
|
||||
const start = getWeekStart(state.currentDate);
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const dateObj = new Date(start);
|
||||
dateObj.setDate(start.getDate() + i);
|
||||
renderDayCell(dateObj, items, grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function renderDayCell(dateObj, items, grid) {
|
||||
const cell = document.createElement('div');
|
||||
cell.className = 'day-cell';
|
||||
|
||||
if (state.viewType === 'WEEK') cell.style.minHeight = '300px';
|
||||
|
||||
const day = dateObj.getDate();
|
||||
const month = dateObj.getMonth();
|
||||
const year = dateObj.getFullYear();
|
||||
|
||||
const now = new Date();
|
||||
if (day === now.getDate() && month === now.getMonth() && year === now.getFullYear()) {
|
||||
cell.classList.add('today');
|
||||
}
|
||||
|
||||
const dayEvents = items.filter(i => {
|
||||
const eventDate = new Date(i.airingAt * 1000);
|
||||
return eventDate.getDate() === day && eventDate.getMonth() === month && eventDate.getFullYear() === year;
|
||||
});
|
||||
|
||||
dayEvents.sort((a, b) => b.media.popularity - a.media.popularity);
|
||||
|
||||
if (dayEvents.length > 0) {
|
||||
const top = dayEvents[0].media;
|
||||
const bg = document.createElement('div');
|
||||
bg.className = 'cell-backdrop';
|
||||
bg.style.backgroundImage = `url('${top.coverImage.large}')`;
|
||||
cell.appendChild(bg);
|
||||
}
|
||||
|
||||
const header = document.createElement('div');
|
||||
header.className = 'day-header';
|
||||
header.innerHTML = `
|
||||
<span class="day-number">${day}</span>
|
||||
<span class="today-label">Today</span>
|
||||
`;
|
||||
cell.appendChild(header);
|
||||
|
||||
const list = document.createElement('div');
|
||||
list.className = 'events-list';
|
||||
|
||||
dayEvents.forEach(evt => {
|
||||
const title = evt.media.title.english || evt.media.title.userPreferred;
|
||||
const link = `/anime/${evt.media.id}`;
|
||||
|
||||
const chip = document.createElement('a');
|
||||
chip.className = 'anime-chip';
|
||||
chip.href = link;
|
||||
chip.innerHTML = `
|
||||
<span class="chip-title">${title}</span>
|
||||
<span class="chip-ep">Ep ${evt.episode}</span>
|
||||
`;
|
||||
list.appendChild(chip);
|
||||
});
|
||||
|
||||
cell.appendChild(list);
|
||||
grid.appendChild(cell);
|
||||
const titleEl = document.getElementById('monthTitle');
|
||||
if (titleEl) titleEl.textContent = title;
|
||||
}
|
||||
|
||||
function setLoading(bool) {
|
||||
state.loading = bool;
|
||||
const loader = document.getElementById('loader');
|
||||
if (bool) loader.classList.add('active');
|
||||
else loader.classList.remove('active');
|
||||
if (loader) {
|
||||
if (bool) loader.classList.add('active');
|
||||
else loader.classList.remove('active');
|
||||
}
|
||||
}
|
||||
|
||||
function updateAmbient(data) {
|
||||
if (!data || !data.length) return;
|
||||
|
||||
const top = data.reduce((prev, curr) => (prev.media.popularity > curr.media.popularity) ? prev : curr, data[0]);
|
||||
|
||||
const img = top.media.bannerImage || top.media.coverImage.extraLarge;
|
||||
const bgEl = document.getElementById('ambientBg');
|
||||
if (bgEl && img) {
|
||||
bgEl.style.backgroundImage = `url('${img}')`;
|
||||
}
|
||||
}
|
||||
|
||||
function delay(ms) { return new Promise(r => setTimeout(r, ms)); }
|
||||
|
||||
function updateAmbient(data) {
|
||||
if (!data || !data.length) return;
|
||||
const top = data.reduce((prev, curr) => (prev.media.popularity > curr.media.popularity) ? prev : curr);
|
||||
const img = top.media.bannerImage || top.media.coverImage.large;
|
||||
if (img) document.getElementById('ambientBg').style.backgroundImage = `url('${img}')`;
|
||||
}
|
||||
async function getCache(key) {
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const response = await cache.match(`/schedule-cache/${key}`);
|
||||
if (!response) return null;
|
||||
|
||||
const cached = await response.json();
|
||||
const age = Date.now() - cached.timestamp;
|
||||
|
||||
if (age < CACHE_DURATION) {
|
||||
console.log(`[Cache Hit] ${key}`);
|
||||
return cached.data;
|
||||
}
|
||||
cache.delete(`/schedule-cache/${key}`);
|
||||
return null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getCacheKey() {
|
||||
|
||||
let d;
|
||||
|
||||
if (state.viewType === 'MONTH') {
|
||||
d = new Date(state.currentDate.getFullYear(), state.currentDate.getMonth(), 1);
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||
return `M_${year}_${month}`;
|
||||
} else {
|
||||
d = getWeekStart(state.currentDate);
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(d.getDate()).padStart(2, '0');
|
||||
return `W_${year}_${month}_${day}`;
|
||||
}
|
||||
}
|
||||
|
||||
async function setCache(key, data) {
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const payload = JSON.stringify({ timestamp: Date.now(), data: data });
|
||||
const response = new Response(payload, { headers: { 'Content-Type': 'application/json' } });
|
||||
await cache.put(`/schedule-cache/${key}`, response);
|
||||
} catch (e) {
|
||||
console.warn("Cache write failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
window.navigate = navigate;
|
||||
window.setViewType = setViewType;
|
||||
window.setMode = setMode;
|
||||
window.setFilter = setFilter;
|
||||
Reference in New Issue
Block a user