|
| 1 | +import { GetStaticPropsContext, GetStaticPropsResult } from "next" |
| 2 | +import Head from "next/head" |
| 3 | +import Image from "next/image" |
| 4 | +import FormattedLink from "../../components/FormattedLink" |
| 5 | +import Main from "../../components/Main" |
| 6 | +import { getCharacters, urlify } from "../../utils/data-cache" |
| 7 | +import { Character, CharacterFull } from "../../utils/types" |
| 8 | + |
| 9 | +import Cryo from "../../public/img/element/Cryo.png" |
| 10 | +import Anemo from "../../public/img/element/Anemo.png" |
| 11 | +import Dendro from "../../public/img/element/Dendro.png" |
| 12 | +import Geo from "../../public/img/element/Geo.png" |
| 13 | +import Electro from "../../public/img/element/Electro.png" |
| 14 | +import Hydro from "../../public/img/element/Hydro.png" |
| 15 | +import Pyro from "../../public/img/element/Pyro.png" |
| 16 | + |
| 17 | +const elements = { |
| 18 | + Anemo, Cryo, Dendro, Geo, Electro, Hydro, Pyro |
| 19 | +} |
| 20 | + |
| 21 | +type Element = "Anemo" |
| 22 | + |
| 23 | +interface SmallChar { |
| 24 | + name: string |
| 25 | + stars?: number |
| 26 | + element?: (keyof (typeof elements))[] |
| 27 | + weapon?: string |
| 28 | + icon?: string |
| 29 | +} |
| 30 | + |
| 31 | +interface Props { |
| 32 | + characters: SmallChar[] |
| 33 | +} |
| 34 | + |
| 35 | +export default function Characters(props: Props & { location: string }) { |
| 36 | + return ( |
| 37 | + <Main> |
| 38 | + <Head> |
| 39 | + <title>Characters | Hu Tao</title> |
| 40 | + <meta name="twitter:card" content="summary" /> |
| 41 | + <meta property="og:title" content="Characters | Hu Tao" /> |
| 42 | + <meta property="og:description" content="View information about different Genshin Impact characters!" /> |
| 43 | + </Head> |
| 44 | + |
| 45 | + <h1 className="text-5xl font-bold pb-2"> |
| 46 | + Characters |
| 47 | + </h1> |
| 48 | + |
| 49 | + <div className="flex flex-wrap justify-around text-center"> |
| 50 | + {props.characters.map(char => ( |
| 51 | + <FormattedLink key={char.name} font="semibold" size="xl" location={props.location} href={`/characters/${urlify(char.name, false)}`} > |
| 52 | + <div className="bg-slate-600 w-24 h-24 m-2"> |
| 53 | + <div className="absolute w-6 m-1"> |
| 54 | + {char.element && char.element.map(e => <Image src={elements[e]} key={e} alt={`${e} Element`}/>)} |
| 55 | + <Image alt={char.name} src={char.icon ?? "/img/unknown.png"} className="w-24" width={256} height={256} onError={(e) => (e.target as HTMLImageElement).src = "/img/unknown.png"}/> |
| 56 | + </div> |
| 57 | + {char.name} |
| 58 | + </div> |
| 59 | + </FormattedLink> |
| 60 | + ))} |
| 61 | + </div> |
| 62 | + </Main> |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +function isFullCharacter(char: Character): char is CharacterFull { |
| 67 | + return typeof (char as CharacterFull).releasedOn == "string" |
| 68 | +} |
| 69 | + |
| 70 | +export async function getStaticProps(context: GetStaticPropsContext): Promise<GetStaticPropsResult<Props>> { |
| 71 | + const data = await getCharacters() |
| 72 | + |
| 73 | + if (!data) { |
| 74 | + return { |
| 75 | + notFound: true, |
| 76 | + revalidate: 5 * 60 |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + props: { |
| 82 | + characters: Object |
| 83 | + .values(data) |
| 84 | + .sort((a, b) => { |
| 85 | + if (isFullCharacter(a) && isFullCharacter(b)) |
| 86 | + return b.releasedOn.localeCompare(a.releasedOn) || b.star - a.star || a.name.localeCompare(b.name) |
| 87 | + else if (!isFullCharacter(b)) |
| 88 | + return 1 |
| 89 | + else if (!isFullCharacter(a)) |
| 90 | + return -1 |
| 91 | + else return a.name.localeCompare(b.name) |
| 92 | + }) |
| 93 | + .map(c => { |
| 94 | + const char: SmallChar = { name: c.name } |
| 95 | + if (c.star) char.stars = c.star |
| 96 | + if (c.skills) char.element = c.skills.map(skill => skill.ult?.type).filter(x => x) as Element[] |
| 97 | + if (c.weaponType) char.weapon = c.weaponType |
| 98 | + if (c.icon) char.icon = c.icon |
| 99 | + return char |
| 100 | + }) |
| 101 | + }, |
| 102 | + revalidate: 60 * 60 |
| 103 | + } |
| 104 | +} |
0 commit comments