|
1 | | -import { useState, useEffect } from 'react'; |
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
2 | 2 | import type { ChangelogEntry } from '../lib/types'; |
3 | 3 | import { placeholderWords } from '../lib/constants'; |
4 | 4 |
|
| 5 | +const CACHE_KEY = 'changelogData'; |
| 6 | +const CACHE_DURATION = 6 * 60 * 60 * 1000; // 6 hours |
| 7 | + |
5 | 8 | export const useAppEffects = (query: string) => { |
6 | 9 | const [showScrollButton, setShowScrollButton] = useState(false); |
7 | 10 | const [isChangelogOpen, setIsChangelogOpen] = useState(false); |
8 | 11 | const [changelogData, setChangelogData] = useState<ChangelogEntry[]>([]); |
9 | 12 | const [placeholderIndex, setPlaceholderIndex] = useState(0); |
10 | 13 |
|
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) { |
16 | 19 | setChangelogData(data); |
17 | | - } catch (error) { |
18 | | - console.error("Error fetching changelog:", error); |
| 20 | + return; |
19 | 21 | } |
20 | | - }; |
| 22 | + } |
21 | 23 |
|
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 | + }, []); |
23 | 33 |
|
| 34 | + useEffect(() => { |
| 35 | + if (isChangelogOpen && changelogData.length === 0) { |
| 36 | + fetchChangelog(); |
| 37 | + } |
| 38 | + }, [isChangelogOpen, changelogData, fetchChangelog]); |
| 39 | + |
| 40 | + useEffect(() => { |
24 | 41 | const handleScroll = () => { |
25 | 42 | setShowScrollButton(window.scrollY > 300); |
26 | 43 | }; |
|
0 commit comments