Skip to content

Commit ea392be

Browse files
authored
Replace moment date formatting in episode list (#148)
1 parent 0b4c333 commit ea392be

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

src/ui/PodcastView/EpisodeListItem.svelte

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
export let showEpisodeImage: boolean = false;
99
1010
const dispatch = createEventDispatcher();
11+
const dateFormatter = new Intl.DateTimeFormat("en-GB", {
12+
day: "2-digit",
13+
month: "long",
14+
year: "numeric"
15+
});
16+
const formattedDateCache = new Map<string, string>();
1117
1218
function onClickEpisode() {
1319
dispatch("clickEpisode", { episode });
@@ -17,13 +23,33 @@
1723
dispatch("contextMenu", { episode, event });
1824
}
1925
20-
let _date: Date;
21-
let date: string;
26+
function parseEpisodeDate(rawDate?: Date): Date | null {
27+
if (!rawDate) return null;
28+
const parsedDate = new Date(rawDate);
29+
return Number.isNaN(parsedDate.getTime()) ? null : parsedDate;
30+
}
2231
23-
$: {
24-
_date = new Date(episode.episodeDate || "");
25-
date = window.moment(_date).format("DD MMMM YYYY");
32+
function getCacheKey(ep: Episode, parsedDate: Date): string {
33+
const identifier = ep.url ?? ep.streamUrl ?? ep.title ?? "episode";
34+
return `${identifier}|${parsedDate.getTime()}`;
2635
}
36+
37+
function formatEpisodeDate(ep: Episode): string {
38+
const parsedDate = parseEpisodeDate(ep?.episodeDate);
39+
if (!parsedDate) return "";
40+
41+
const cacheKey = getCacheKey(ep, parsedDate);
42+
const cachedDate = formattedDateCache.get(cacheKey);
43+
if (cachedDate) return cachedDate;
44+
45+
const formattedDate = dateFormatter.format(parsedDate);
46+
formattedDateCache.set(cacheKey, formattedDate);
47+
return formattedDate;
48+
}
49+
50+
let date: string = "";
51+
52+
$: date = formatEpisodeDate(episode);
2753
</script>
2854

2955
<button

0 commit comments

Comments
 (0)