Last active
February 24, 2024 06:19
-
-
Save emekaorji/2cbd7d02b5a60fe8f32569a3c11f4c48 to your computer and use it in GitHub Desktop.
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
import { useCallback, useEffect, useState } from 'react'; | |
function testImageUrl(url: string): Promise<string> { | |
return new Promise(function (resolve, reject) { | |
var image = new Image(); | |
image.src = url; | |
image.addEventListener('load', () => resolve(url)); | |
image.addEventListener('error', reject); | |
}); | |
} | |
const DisplayImage = ({ url }: { url: string }) => { | |
const [actualUrl, setActualUrl] = useState<string | null>(null); | |
const [loading, setLoading] = useState(false); | |
const handleImage = useCallback(async () => { | |
try { | |
setLoading(true); | |
const _actualUrl = await testImageUrl(url); | |
setActualUrl(_actualUrl); | |
} catch (error) { | |
setActualUrl(null); | |
} | |
setLoading(false); | |
}, [url]); | |
useEffect(() => { | |
handleImage(); | |
}, [handleImage]); | |
return loading ? ( | |
'Loading...' | |
) : actualUrl === null ? ( | |
'Nullishonking' | |
) : ( | |
<img src={actualUrl} alt='' /> | |
); | |
}; |
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
import NextImage, { StaticImageData } from "next/image"; | |
import { useCallback, useEffect, useState } from "react"; | |
async function testImageUrl( | |
url: string | StaticImageData | |
): Promise<string | StaticImageData> { | |
if (typeof url !== "string") return url; | |
return new Promise(function (resolve, reject) { | |
var image = new Image(); | |
image.src = url; | |
image.addEventListener("load", () => resolve(url)); | |
image.addEventListener("error", reject); | |
}); | |
} | |
function getInitials(fullName: string) { | |
const [firstName = "", lastName = ""] = fullName.split(" "); | |
return `${firstName.slice(0, 1)}${lastName.slice(0, 1)}`; | |
} | |
const Avatar = ({ | |
name, | |
src, | |
}: { | |
name: string; | |
src: string | StaticImageData; | |
}) => { | |
const [actualUrl, setActualUrl] = useState<string | StaticImageData | null>( | |
null | |
); | |
const [loading, setLoading] = useState(false); | |
const handleImage = useCallback(async () => { | |
try { | |
setLoading(true); | |
const _actualUrl = await testImageUrl(src); | |
setActualUrl(_actualUrl); | |
} catch (error) { | |
setActualUrl(null); | |
} | |
setLoading(false); | |
}, [src]); | |
useEffect(() => { | |
handleImage(); | |
}, [handleImage]); | |
return loading ? ( | |
"..." | |
) : actualUrl === null ? ( | |
getInitials(name) | |
) : ( | |
<NextImage src={actualUrl} alt="" /> | |
); | |
}; | |
export default Avatar; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment