-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3344 from tloncorp/james/land-1691-groups-group-i…
…mage-broken-if-src-fails-to-load web/tlon: proper fallbacks for broken-src image avatars (groups, ships, clubs)
- Loading branch information
Showing
4 changed files
with
63 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,44 @@ | ||
import produce from 'immer'; | ||
import { useCallback } from 'react'; | ||
import create from 'zustand'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
interface AvatarStore { | ||
status: Record<string, boolean>; | ||
loaded: (src: string) => void; | ||
} | ||
export function useAvatar(src: string) { | ||
// This hook is used to load an image and determine if it has loaded | ||
const [hasLoaded, setHasLoaded] = useState(false); | ||
|
||
const useAvatarStore = create<AvatarStore>((set, get) => ({ | ||
status: {}, | ||
loaded: (src) => { | ||
set( | ||
produce((draft) => { | ||
draft.status[src] = true; | ||
}) | ||
); | ||
}, | ||
})); | ||
const load = useCallback(() => { | ||
// Create a new image object | ||
const img = new Image(); | ||
|
||
export function useAvatar(src: string) { | ||
return useAvatarStore( | ||
useCallback( | ||
(store: AvatarStore) => ({ | ||
hasLoaded: store.status[src] || false, | ||
load: () => store.loaded(src), | ||
}), | ||
[src] | ||
) | ||
); | ||
// Set the hasLoaded state based on the image load | ||
img.onload = () => { | ||
setHasLoaded(true); | ||
}; | ||
|
||
// Set the hasLoaded state to false if the image fails to load | ||
img.onerror = () => { | ||
setHasLoaded(false); | ||
}; | ||
|
||
// Set the src of the image to the src passed to the hook | ||
img.src = src; | ||
|
||
// If the src is not defined, set hasLoaded to false | ||
if (!src) { | ||
setHasLoaded(false); | ||
} | ||
}, [src]); | ||
|
||
// Load the image when the component mounts | ||
useEffect(() => { | ||
load(); | ||
}, [load]); | ||
|
||
// Reset the hasLoaded state when the src changes | ||
useEffect(() => { | ||
setHasLoaded(false); | ||
}, [src]); | ||
|
||
// Return the hasLoaded state and the load function | ||
return { hasLoaded, load }; | ||
} | ||
|
||
export default useAvatarStore; | ||
export default useAvatar; |