-
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 #3351 from tloncorp/james/revert-group-avatar
groups: revert avatar src-checking
- Loading branch information
Showing
4 changed files
with
59 additions
and
63 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,44 +1,33 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import produce from 'immer'; | ||
import { useCallback } from 'react'; | ||
import create from 'zustand'; | ||
|
||
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 load = useCallback(() => { | ||
// Create a new image object | ||
const img = new Image(); | ||
|
||
// 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]); | ||
interface AvatarStore { | ||
status: Record<string, boolean>; | ||
loaded: (src: string) => void; | ||
} | ||
|
||
// Reset the hasLoaded state when the src changes | ||
useEffect(() => { | ||
setHasLoaded(false); | ||
}, [src]); | ||
const useAvatarStore = create<AvatarStore>((set, get) => ({ | ||
status: {}, | ||
loaded: (src) => { | ||
set( | ||
produce((draft) => { | ||
draft.status[src] = true; | ||
}) | ||
); | ||
}, | ||
})); | ||
|
||
// Return the hasLoaded state and the load function | ||
return { hasLoaded, load }; | ||
export function useAvatar(src: string) { | ||
return useAvatarStore( | ||
useCallback( | ||
(store: AvatarStore) => ({ | ||
hasLoaded: store.status[src] || false, | ||
load: () => store.loaded(src), | ||
}), | ||
[src] | ||
) | ||
); | ||
} | ||
|
||
export default useAvatar; | ||
export default useAvatarStore; |