-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpodcasts.js
More file actions
131 lines (109 loc) · 3.74 KB
/
podcasts.js
File metadata and controls
131 lines (109 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
```javascript
const state = {
episodes: [],
query: "",
year: ""
};
const $ = (s) => document.querySelector(s);
async function init() {
const container = $("#podcasts-root");
const searchInput = $("#search");
const yearFilter = $("#yearFilter");
if (!container || !searchInput || !yearFilter) return;
const isPreRendered = container.children.length > 0;
try {
const res = await fetch("podcasts.json", { cache: "no-store" });
state.episodes = await res.json();
// Populate Year Filter
const years = [...new Set(state.episodes.map(e => (e.publication_date||"").slice(0, 4)).filter(Boolean))].sort().reverse();
yearFilter.innerHTML = `<option value="">All Years</option>`;
years.forEach(y => {
const opt = document.createElement("option");
opt.value = y;
opt.textContent = y;
yearFilter.appendChild(opt);
});
if (isPreRendered) {
console.log("Hydrated static content.");
} else {
render(state.year, state.query);
}
// Bind
searchInput.addEventListener("input", (e) => {
state.query = e.target.value.toLowerCase();
render(state.year, state.query);
});
yearFilter.addEventListener("change", (e) => {
state.year = e.target.value;
render(state.year, state.query);
});
} catch (err) {
console.error(err);
if (!isPreRendered) container.innerHTML = "<p>Error loading podcasts.</p>";
}
}
function render(filterYear, filterQuery) {
const container = $("#podcasts-root");
container.innerHTML = "";
const items = state.episodes.map(ep => ({
...ep,
year: (ep.publication_date||"").slice(0, 4),
_search: [ep.episode_title, ep.podcast_title, ep.publication_date].join(" ").toLowerCase()
}));
const filtered = items.filter(ep => {
const okYear = !filterYear || ep.year === filterYear;
const okSearch = !filterQuery || ep._search.includes(filterQuery);
return okYear && okSearch;
});
const groups = {};
filtered.forEach(ep => {
const y = ep.year || "Unknown";
if (!groups[y]) groups[y] = [];
groups[y].push(ep);
});
const years = Object.keys(groups).sort().reverse();
if (!years.length) {
container.innerHTML = `<div style="padding:1rem;">No episodes match.</div>`;
return;
}
years.forEach(y => {
const section = document.createElement("section");
section.className = "year-section";
section.innerHTML = `
<div class="year-header">
<h2 class="year-title">${y}</h2>
<div class="year-count">${groups[y].length} episodes</div>
</div>
<div class="links-grid"></div>
`;
const grid = section.querySelector(".links-grid");
groups[y].forEach(ep => grid.appendChild(createCard(ep)));
container.appendChild(section);
});
}
function createCard(ep) {
const card = document.createElement("article");
card.className = "card reveal is-visible";
const titleHtml = ep.url
? `<a href="${ep.url}" target="_blank" rel="noopener">${escapeHtml(ep.episode_title)}</a>`
: escapeHtml(ep.episode_title);
const dateStr = ep.publication_date
? new Date(ep.publication_date).toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric" })
: "";
card.innerHTML = `
<div class="card__meta">
<time datetime="${ep.publication_date}">${dateStr}</time> · <span>${escapeHtml(ep.podcast_title)}</span>
</div>
<h3 class="card__title">${titleHtml}</h3>
<div class="card__footer">
<span class="source">Podcast</span>
<span class="badges"></span>
</div>
`;
return card;
}
function escapeHtml(s) {
return String(s || "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
document.addEventListener("DOMContentLoaded", init);
```