Skip to content

Commit 0be1dba

Browse files
Merge pull request #179 from StackOverflowIsBetterThanAnyAI/164-cache-profile-pictures-thumbnails-for-faster-access-to-images
store profile pictures in session storage, grab them if available
2 parents 51b211f + e371c09 commit 0be1dba

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/hooks/useFetchImageUrl.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useEffect } from 'react'
2+
import { getItemFromStorage } from '../helper/getItemFromStorage'
23
import { getProfilePicture } from '../helper/getProfilePicture'
4+
import { setItemInStorage } from '../helper/setItemInStorage'
35

46
export const useFetchImageUrl = (
57
setImageUrl: (value: React.SetStateAction<string>) => void,
@@ -8,11 +10,22 @@ export const useFetchImageUrl = (
810
useEffect(() => {
911
const fetchImageUrl = async () => {
1012
try {
11-
const data = await getProfilePicture(user_id || '')
12-
if (!data) {
13-
throw new Error()
13+
const parsedData = getItemFromStorage()
14+
const profilePictures = parsedData.profile_pictures || {}
15+
16+
if (profilePictures[user_id]) {
17+
setImageUrl(profilePictures[user_id])
1418
} else {
15-
setImageUrl(data)
19+
const url = await getProfilePicture(user_id)
20+
if (!url) {
21+
throw new Error()
22+
} else {
23+
setImageUrl(url)
24+
const parsed = getItemFromStorage()
25+
const pictures = parsed.profile_pictures || {}
26+
pictures[user_id] = url
27+
setItemInStorage('profile_pictures', pictures)
28+
}
1629
}
1730
} catch (error: any) {}
1831
}

0 commit comments

Comments
 (0)