Skip to content

Commit e792a89

Browse files
authored
chore: stop loading changelog on every character (#57)
1 parent e43b3ac commit e792a89

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

public/changelog.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Fix release date ordering button.",
88
"Add stamps for 1st edition and reverse holo cards.",
99
"Fix cards with non numeric card numbers not showing up in the grid.",
10-
"Fix cards not loading properly when there are too many on the screen."
10+
"Fix cards not loading properly when there are too many on the screen.",
11+
"Improve file loading performance."
1112
]
1213
},
1314
{

src/hooks/useAppEffects.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState, useEffect, useCallback } from 'react';
22
import type { ChangelogEntry } from '../lib/types';
33
import { placeholderWords } from '../lib/constants';
44

5+
const CACHE_KEY = 'changelogData';
6+
const CACHE_DURATION = 6 * 60 * 60 * 1000; // 6 hours
7+
58
export const useAppEffects = (query: string) => {
69
const [showScrollButton, setShowScrollButton] = useState(false);
710
const [isChangelogOpen, setIsChangelogOpen] = useState(false);
811
const [changelogData, setChangelogData] = useState<ChangelogEntry[]>([]);
912
const [placeholderIndex, setPlaceholderIndex] = useState(0);
1013

11-
useEffect(() => {
12-
const fetchChangelog = async () => {
13-
try {
14-
const response = await fetch('/changelog.json');
15-
const data = await response.json();
14+
const fetchChangelog = useCallback(async () => {
15+
const cachedData = localStorage.getItem(CACHE_KEY);
16+
if (cachedData) {
17+
const { timestamp, data } = JSON.parse(cachedData);
18+
if (Date.now() - timestamp < CACHE_DURATION) {
1619
setChangelogData(data);
17-
} catch (error) {
18-
console.error("Error fetching changelog:", error);
20+
return;
1921
}
20-
};
22+
}
2123

22-
fetchChangelog();
24+
try {
25+
const response = await fetch('/changelog.json');
26+
const data = await response.json();
27+
setChangelogData(data);
28+
localStorage.setItem(CACHE_KEY, JSON.stringify({ timestamp: Date.now(), data }));
29+
} catch (error) {
30+
console.error("Error fetching changelog:", error);
31+
}
32+
}, []);
2333

34+
useEffect(() => {
35+
if (isChangelogOpen && changelogData.length === 0) {
36+
fetchChangelog();
37+
}
38+
}, [isChangelogOpen, changelogData, fetchChangelog]);
39+
40+
useEffect(() => {
2441
const handleScroll = () => {
2542
setShowScrollButton(window.scrollY > 300);
2643
};

0 commit comments

Comments
 (0)