/
index.tsx
189 lines (171 loc) · 6.69 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { GetStaticPropsContext, GetStaticPropsResult } from "next"
import Head from "next/head"
import { useState } from "react"
import { ExclusiveButton, ToggleAllButton, ToggleButton } from "../../components/Filters"
import FormattedLink from "../../components/FormattedLink"
import Icon from "../../components/Icon"
import Main from "../../components/Main"
import { getMaterials } from "../../utils/data-cache"
import { getStarColor, urlify } from "../../utils/utils"
interface SmallMaterial {
name: string
stars?: number
icon?: string
category?: string
type?: string
}
interface Props {
materials: SmallMaterial[]
}
export default function Materials(props: Props & { location: string }) {
const [filter, setFilter] = useState(false)
const defaultCategories = props.materials.map(m => m.category).filter((c, i, a) => c && a.indexOf(c) == i) as string[]
const defaultTypes = [
"Local Specialty (Mondstadt)",
"Local Specialty (Liyue)",
"Local Specialty (Inazuma)",
"Material",
"Forging Ore",
"Bait",
"Cooking Ingredient",
"Food",
"Potion",
"Character Level-Up Material",
"Weapon Ascension Material",
"Talent Level-Up Material",
"Quest Item",
"Adventure Item",
"Consumable",
"Gadget",
...props.materials.map(m => m.type)
].filter((c, i, a) => c && a.indexOf(c) == i) as string[]
const [starFilter, setStarFilter] = useState(0)
const [categoryFilter, setCategoryFilter] = useState(defaultCategories)
const [typeFilter, setTypeFilter] = useState(defaultTypes)
const desc = `View uses, sources and description of the ${props.materials.length} different materials in Genshin Impact.`
return (
<Main>
<Head>
<title>Materials | Hu Tao</title>
<meta name="twitter:card" content="summary" />
<meta property="og:title" content="Materials | Hu Tao" />
<meta property="og:description" content={desc} />
<meta property="description" content={desc} />
</Head>
<h1 className="text-5xl font-bold pb-2">
Materials
</h1>
{filter ? <div className="bg-slate-100 dark:bg-slate-600 flex flex-col p-2 rounded-2xl font-semibold gap-2">
<div className="pb-2">
<div className="flex flex-row font-semibold float-right">
<ExclusiveButton type={filter} value={false} setter={setFilter}>
Hide filters
</ExclusiveButton>
</div>
<div>
Rarity filter
</div>
<div className="flex flex-row flex-wrap gap-2 pt-2">
<ExclusiveButton type={starFilter} value={0} setter={setStarFilter}>
All
</ExclusiveButton>
<ExclusiveButton type={starFilter} value={5} setter={setStarFilter}>
5★ Only
</ExclusiveButton>
<ExclusiveButton type={starFilter} value={4} setter={setStarFilter}>
4★ Only
</ExclusiveButton>
<ExclusiveButton type={starFilter} value={3} setter={setStarFilter}>
3★ Only
</ExclusiveButton>
<ExclusiveButton type={starFilter} value={2} setter={setStarFilter}>
2★ Only
</ExclusiveButton>
<ExclusiveButton type={starFilter} value={1} setter={setStarFilter}>
1★ Only
</ExclusiveButton>
</div>
<div className="py-1">
<div className="flex flex-row gap-4">
Category filter
<ToggleAllButton type={categoryFilter} value={defaultCategories} setter={setCategoryFilter}>
All
</ToggleAllButton>
</div>
<div className="flex flex-row flex-wrap gap-2 pt-2">
{defaultCategories.map(e => (
<ToggleButton key={e} type={categoryFilter} value={e} setter={setCategoryFilter}>
{e}
</ToggleButton>
))}
</div>
</div>
<div className="py-1">
<div className="flex flex-row gap-4">
Type filter
<ToggleAllButton type={typeFilter} value={defaultTypes} setter={setTypeFilter}>
All
</ToggleAllButton>
</div>
<div className="flex flex-row flex-wrap gap-2 pt-2">
{defaultTypes.map(e => (
<ToggleButton key={e} type={typeFilter} value={e} setter={setTypeFilter}>
{e}
</ToggleButton>
))}
</div>
</div>
</div>
</div> : <div className="flex flex-row font-semibold justify-end">
<ExclusiveButton type={filter} value={true} setter={setFilter}>
Show filters
</ExclusiveButton>
</div>
}
<div className="flex flex-wrap justify-evenly text-center pt-2">
{props.materials
.filter(m => starFilter == 0 || starFilter == (m.stars ?? 1))
.filter(m => categoryFilter.some(e => m.category?.includes(e)) || m.category == undefined)
.filter(m => typeFilter.some(e => m.type?.includes(e)) || m.type == undefined)
.map((mat, i) => {
const color = getStarColor(mat.stars ?? 1)
return <FormattedLink key={mat.name} prefetch={i < 21 ? undefined : false} location={props.location} href={`/materials/${urlify(mat.name, false)}`} className="bg-slate-300 dark:bg-slate-800 w-24 sm:w-28 lg:w-32 m-1 relative rounded-xl transition-all duration-100 hover:outline outline-slate-800 dark:outline-slate-300 text-bold text-sm" >
<div className={`${color} rounded-t-xl h-24 sm:h-28 lg:h-32`}>
<Icon icon={mat} loading={i < 21 ? "eager" : "lazy"} className="rounded-t-xl m-0 p-0" />
</div>
<div
className={`flex justify-center items-center m-0 px-1 duration-200 md:text-base ${mat.name.length > 27 ? "text-xs md:text-xs" : mat.name.length > 20 ? "text-sm md:text-sm" : ""}`}
style={({ minHeight: "3rem" })}
>
{mat.name}
</div>
</FormattedLink>
})}
</div>
</Main>
)
}
export async function getStaticProps(context: GetStaticPropsContext): Promise<GetStaticPropsResult<Props>> {
const data = await getMaterials()
if (!data) {
return {
notFound: true,
revalidate: 15 * 60
}
}
return {
props: {
materials: Object
.values(data)
.map(m => {
const mat: SmallMaterial = { name: m.name }
if (m.stars) mat.stars = m.stars
if (m.icon) mat.icon = m.icon
if (m.category) mat.category = m.category
if (m.type) mat.type = m.type
return mat
})
},
revalidate: 60 * 60 * 2
}
}