import * as React from 'react' import styled from 'styled-components' export type GuildPicProps = { id: string, icon: string, name: string } export type GuildPicState = { src: string | undefined, ok: boolean } const Fallback = styled.div` display: flex; justify-content: center; align-items: center; background-color: var(--fallback-color); ` export default class DiscordGuildPic extends React.Component { state: GuildPicState = { src: undefined, ok: true } static getDerivedStateFromProps (nextProps: GuildPicProps, prevState: GuildPicState): GuildPicState { return { ...prevState, src: `https://cdn.discordapp.com/icons/${nextProps.id}/${nextProps.icon}.png` } } renderFallback () { const { name, id, icon, ...rest } = this.props // @ts-ignore return {name[0]} } onError = () => { // console.log('onError') this.setState({ ok: false }) } onLoad = () => { this.setState({ ok: true }) } renderImg () { const { name, id, icon, ...rest } = this.props return } render () { return (this.state.ok === false) ? this.renderFallback() : this.renderImg() } }