From 0e2c59a32b44a31587a3008e840e98bd5e34b1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Mon, 11 Jan 2021 13:02:19 +0900 Subject: [PATCH] feat: renamed component --- components/DiscordAvatar.tsx | 60 ++++++++++++++++++++++++++++++++++++ components/DiscordImage.tsx | 25 --------------- components/Owner.tsx | 10 ++---- pages/bots/[id].tsx | 4 +-- pages/users/[id].tsx | 4 +-- 5 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 components/DiscordAvatar.tsx delete mode 100644 components/DiscordImage.tsx diff --git a/components/DiscordAvatar.tsx b/components/DiscordAvatar.tsx new file mode 100644 index 0000000..ced185d --- /dev/null +++ b/components/DiscordAvatar.tsx @@ -0,0 +1,60 @@ +import { SyntheticEvent } from 'react' +import { ImageSize } from '../types' +import { DiscordEnpoints } from '../utils/Constants' + +const DiscordAvatar = (props: { + alt?: string + userID: string + avatarHash: string + tag: string + className?: string + size? : ImageSize +}) => { + const fallback ='/img/default.png' + const webp = localStorage.webp === 'true' + return ( + {props.alt)=> { + if(webp) { + (e.target as ImageTarget).onerror = (event) => { + // All Fails + (event.target as ImageTarget).onerror = ()=> { console.log('FALLBACK IMAGE LOAD FAIL') } + (event.target as ImageTarget).src = fallback + } + // Webp Load Fail + (e.target as ImageTarget).src = props.avatarHash + ? DiscordEnpoints.CDN.user(props.userID, props.avatarHash, { size: props.size ?? 256 }) + : DiscordEnpoints.CDN.default(props.tag, { size: props.size ?? 256}) + } + else (e.target as ImageTarget).onerror = (event) => { + // All Fails + (event.target as ImageTarget).onerror = ()=> { console.log('FALLBACK IMAGE LOAD FAIL') } + (event.target as ImageTarget).src = fallback + } + // Webp Load Fail + (e.target as ImageTarget).src = props.avatarHash + ? DiscordEnpoints.CDN.user(props.userID, props.avatarHash, { size: props.size ?? 256 }) + : DiscordEnpoints.CDN.default(props.tag, { size: props.size ?? 256}) + + }} + /> + ) +} + +export default DiscordAvatar + +interface ImageEvent extends Event { + target: ImageTarget +} + +interface ImageTarget extends EventTarget { + src: string + onerror: (event: SyntheticEvent) => void +} \ No newline at end of file diff --git a/components/DiscordImage.tsx b/components/DiscordImage.tsx deleted file mode 100644 index 0f96335..0000000 --- a/components/DiscordImage.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import NextImage from 'next/image' - -const DiscordImage = (props: { - size?: number - userID: string - avatarHash: string - tag: number | string - className?: string -}) => { - return ( - - ) -} - -export default DiscordImage diff --git a/components/Owner.tsx b/components/Owner.tsx index 0c763ba..7bd958c 100644 --- a/components/Owner.tsx +++ b/components/Owner.tsx @@ -1,23 +1,17 @@ import Link from 'next/link' -import DiscordImage from './DiscordImage' +import DiscordAvatar from './DiscordAvatar' const Owner = ({ id, username, tag, avatarHash }:OwnerProps):JSX.Element => { return
- +

{username}

#{tag}
- {/*
- - - {username} #{tag} - -
*/} } diff --git a/pages/bots/[id].tsx b/pages/bots/[id].tsx index 28eb239..27cdb15 100644 --- a/pages/bots/[id].tsx +++ b/pages/bots/[id].tsx @@ -5,7 +5,7 @@ import { ParsedUrlQuery } from 'querystring' import { Bot, User } from '../../types' import Container from '../../components/Container' -import DiscordImage from '../../components/DiscordImage' +import DiscordAvatar from '../../components/DiscordAvatar' import Divider from '../../components/Divider' import Tag from '../../components/Tag' import Owner from '../../components/Owner' @@ -34,7 +34,7 @@ const Bots: NextPage = ({ data, date }) => { />
- = ({ data }) => { />
-