|
| 1 | +import { GetStaticPathsResult, GetStaticPropsContext, GetStaticPropsResult } from "next" |
| 2 | +import Head from "next/head" |
| 3 | +import Image from "next/image" |
| 4 | +import ReactMarkdown from "react-markdown" |
| 5 | +import FormattedLink from "../../components/FormattedLink" |
| 6 | +import Guides from "../../components/Guides" |
| 7 | +import Icon from "../../components/Icon" |
| 8 | +import Main from "../../components/Main" |
| 9 | +import { getEnemies } from "../../utils/data-cache" |
| 10 | +import { Enemy } from "../../utils/types" |
| 11 | +import { elements, getGuidesFor, getLinkToGuide, getStarColor, urlify } from "../../utils/utils" |
| 12 | +import styles from "../style.module.css" |
| 13 | + |
| 14 | +interface Props { |
| 15 | + enemy: Enemy, |
| 16 | + guides?: string[][], |
| 17 | +} |
| 18 | + |
| 19 | +export default function EnemyWebpage({ enemy, location, guides }: Props & { location: string }) { |
| 20 | + const color = getStarColor(1) |
| 21 | + |
| 22 | + return ( |
| 23 | + <Main> |
| 24 | + <Head> |
| 25 | + <title>{enemy.name} | Hu Tao</title> |
| 26 | + <meta name="twitter:card" content="summary" /> |
| 27 | + <meta property="og:title" content={`${enemy.name} | Hu Tao`} /> |
| 28 | + <meta property="og:description" content={`View ${enemy.name} information`} /> |
| 29 | + </Head> |
| 30 | + <h2 className="font-semibold"> |
| 31 | + <FormattedLink href="/enemies/" location={location} className="font-semibold text-lg"> |
| 32 | + Enemies |
| 33 | + </FormattedLink> |
| 34 | + </h2> |
| 35 | + |
| 36 | + <h1 className="text-4xl font-bold pb-2 sm:sr-only not-sr-only"> |
| 37 | + <Icon icon={enemy} className={`${color} rounded-xl sm:w-0 mr-2 w-12 inline-block`} /> |
| 38 | + {enemy.name} |
| 39 | + </h1> |
| 40 | + |
| 41 | + <div className="grid grid-flow-col justify-start"> |
| 42 | + <div className="sm:w-36 mr-2 w-0 "> |
| 43 | + <Icon icon={enemy} className={`${color} rounded-xl`} /> |
| 44 | + </div> |
| 45 | + |
| 46 | + <div id="description" className="w-full"> |
| 47 | + <h1 className="text-4xl font-bold pb-2 sm:not-sr-only sr-only"> |
| 48 | + {enemy.name} |
| 49 | + </h1> |
| 50 | + |
| 51 | + {enemy.type && <div className="inline-block pr-2 font-semibold"> |
| 52 | + {enemy.type} |
| 53 | + </div>} |
| 54 | + |
| 55 | + {enemy.kind && <div className="inline-block pr-2 "> |
| 56 | + {enemy.kind} |
| 57 | + </div>} |
| 58 | + |
| 59 | + <blockquote className="pl-5 mt-3 mb-2 border-l-2"> |
| 60 | + <ReactMarkdown>{(enemy.desc?.replace(/ ?\$\{.*?\}/g, "").replace(/\n/g, "\n\n") ?? "")}</ReactMarkdown> |
| 61 | + </blockquote> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + |
| 65 | + <div id="details"> |
| 66 | + {guides && guides.length > 0 && <Guides guides={guides} />} |
| 67 | + {enemy.notes && <ReactMarkdown>{(enemy.notes?.replace(/ ?\$\{.*?\}/g, "").replace(/\n/g, "\n\n") ?? "")}</ReactMarkdown>} |
| 68 | + {enemy.resistance && <Resistances resistance={enemy.resistance} />} |
| 69 | + </div> |
| 70 | + </Main> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +function Resistances({ resistance }: { resistance: string[][] }) { |
| 75 | + function getStyles(r: string) { |
| 76 | + |
| 77 | + if (r.match(/\d+%/)) { |
| 78 | + const amount = parseInt(r.replace("%", "")) |
| 79 | + if (amount == 10) return "opacity-60" |
| 80 | + else if (amount > 50) return "font-bold" |
| 81 | + else if (amount > 100) return "font-bold text-red-400" |
| 82 | + else if (amount < -50) return "font-semibold text-green-400" |
| 83 | + else if (amount < 0) return "text-green-400" |
| 84 | + } |
| 85 | + |
| 86 | + return "" |
| 87 | + } |
| 88 | + return <> |
| 89 | + <h3 className="text-lg font-bold pt-1" id="resistance">Resistances:</h3> |
| 90 | + <table className={`table-auto w-full ${styles.table} mb-2 sm:text-base text-sm`}> |
| 91 | + <thead className="font-semibold divide-x divide-gray-200 dark:divide-gray-500"> |
| 92 | + <td> |
| 93 | + <div className="flex flex-row items-center content-center"> |
| 94 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Pyro} alt={"Pyro Element"} /></div> |
| 95 | + <div>Pyro</div> |
| 96 | + </div> |
| 97 | + </td> |
| 98 | + <td> |
| 99 | + <div className="flex flex-row items-center content-center"> |
| 100 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Electro} alt={"Electro Element"} /></div> |
| 101 | + <div>Electro</div> |
| 102 | + </div> |
| 103 | + </td> |
| 104 | + <td> |
| 105 | + <div className="flex flex-row items-center content-center"> |
| 106 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Cryo} alt={"Cryo Element"} /></div> |
| 107 | + <div>Cryo</div> |
| 108 | + </div> |
| 109 | + </td> |
| 110 | + <td> |
| 111 | + <div className="flex flex-row items-center content-center"> |
| 112 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Hydro} alt={"Hydro Element"} /></div> |
| 113 | + <div>Hydro</div> |
| 114 | + </div> |
| 115 | + </td> |
| 116 | + <td> |
| 117 | + <div className="flex flex-row items-center content-center"> |
| 118 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Anemo} alt={"Anemo Element"} /></div> |
| 119 | + <div>Anemo</div> |
| 120 | + </div> |
| 121 | + </td> |
| 122 | + <td> |
| 123 | + <div className="flex flex-row items-center content-center"> |
| 124 | + <div className="absolute invisible inline-block w-5 h-5 sm:relative sm:pr-1 sm:visible"><Image src={elements.Geo} alt={"Geo Element"} /></div> |
| 125 | + <div>Geo</div> |
| 126 | + </div> |
| 127 | + </td> |
| 128 | + <td>Physical</td> |
| 129 | + {resistance[0].length > 7 && <td>Note</td>} |
| 130 | + </thead> |
| 131 | + <tbody className="divide-y divide-gray-200 dark:divide-gray-500"> |
| 132 | + {resistance |
| 133 | + .map((row, i) => <tr className="pr-1 divide-x divide-gray-200 dark:divide-gray-500" key={i}> |
| 134 | + {row.map((r, j) => <td key={i + "," + j} className={getStyles(r)}><ReactMarkdown>{r}</ReactMarkdown></td>)} |
| 135 | + </tr>)} |
| 136 | + </tbody> |
| 137 | + </table> |
| 138 | + </> |
| 139 | +} |
| 140 | + |
| 141 | +export async function getStaticProps(context: GetStaticPropsContext): Promise<GetStaticPropsResult<Props>> { |
| 142 | + const enemyName = context.params?.enemy |
| 143 | + const data = await getEnemies() |
| 144 | + |
| 145 | + const enemy = Object.values(data ?? {}).find(e => urlify(e.name, false) == enemyName) |
| 146 | + if (!data || !enemy) { |
| 147 | + return { |
| 148 | + notFound: true, |
| 149 | + revalidate: 5 * 60 |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const guides = (await getGuidesFor("enemy", enemy.name))?.map(({ guide, page }) => [page.name, getLinkToGuide(guide, page)]) |
| 154 | + |
| 155 | + return { |
| 156 | + props: { |
| 157 | + enemy, |
| 158 | + guides, |
| 159 | + }, |
| 160 | + revalidate: 60 * 60 |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +export async function getStaticPaths(): Promise<GetStaticPathsResult> { |
| 165 | + const data = await getEnemies() |
| 166 | + return { |
| 167 | + paths: Object.values(data ?? {}).map(e => ({ |
| 168 | + params: { enemy: urlify(e.name, false) } |
| 169 | + })) ?? [], |
| 170 | + fallback: "blocking" |
| 171 | + } |
| 172 | +} |
0 commit comments