Add card for item information

This commit is contained in:
KingRainbow44 2023-04-09 17:46:48 -04:00
parent 219d9f28e1
commit bc3310ae29
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
7 changed files with 306 additions and 23 deletions

View File

@ -1,4 +1,6 @@
export type Page = "Home" | "Commands" | "Avatars" | "Items"; export type Page = "Home" | "Commands" | "Avatars" | "Items";
export type Days = "Sunday" | "Monday" | "Tuesday"
| "Wednesday" | "Thursday" | "Friday" | "Saturday";
export type Command = { export type Command = {
name: string[]; name: string[];
@ -22,6 +24,26 @@ export type Item = {
icon: string; icon: string;
}; };
// Exported from Project Amber.
export type ItemInfo = {
response: number | 200 | 404;
data: {
name: string;
description: string;
type: string;
recipe: boolean;
mapMark: boolean;
source: {
name: string;
type: string | "domain";
days: Days;
}[];
icon: string;
rank: 1 | 2 | 3 | 4 | 5;
route: string;
};
};
export enum Target { export enum Target {
None = "NONE", None = "NONE",
Offline = "OFFLINE", Offline = "OFFLINE",
@ -66,3 +88,21 @@ export enum ItemCategory {
export function isPage(page: string): page is Page { export function isPage(page: string): page is Page {
return ["Home", "Commands"].includes(page); return ["Home", "Commands"].includes(page);
} }
/**
* Converts an item type to a string.
*
* @param type The item type to convert.
*/
export function itemTypeToString(type: ItemType): string {
switch (type) {
default: return "Unknown";
case ItemType.None: return "None";
case ItemType.Virtual: return "Virtual";
case ItemType.Material: return "Material";
case ItemType.Reliquary: return "Reliquary";
case ItemType.Weapon: return "Weapon";
case ItemType.Display: return "Display";
case ItemType.Furniture: return "Furniture";
}
}

View File

@ -3,12 +3,20 @@
height: 100%; height: 100%;
width: 100%; width: 100%;
flex-direction: row;
justify-content: space-between;
background-color: var(--background-color); background-color: var(--background-color);
flex-direction: column;
padding: 24px; padding: 24px;
} }
.ItemsPage_Content {
display: flex;
flex-direction: column;
width: 80%;
}
.ItemsPage_Header { .ItemsPage_Header {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
@ -72,3 +80,14 @@
margin-bottom: 28px; margin-bottom: 28px;
overflow-y: scroll; overflow-y: scroll;
} }
.ItemsPage_Card {
display: flex;
width: 100%;
max-width: 300px;
min-height: 300px;
max-height: 700px;
align-self: center;
}

View File

@ -0,0 +1,71 @@
.ItemCard {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
height: 100%;
max-width: 300px;
min-height: 300px;
max-height: 700px;
padding: 20px;
box-sizing: border-box;
border-radius: 10px;
background-color: var(--accent-color);
}
.ItemCard_Content {
display: flex;
gap: 10px;
flex-direction: column;
}
.ItemCard_Header {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.ItemCard_Info {
display: flex;
flex-direction: column;
gap: 10px;
:nth-child(1) {
font-weight: bold;
font-size: 20px;
max-width: 170px;
max-height: 40px;
color: var(--text-primary-color);
}
:nth-child(2) {
font-size: 16px;
color: var(--text-primary-color);
}
}
.ItemCard_Icon {
width: 64px;
height: 64px
}
.ItemCard_Description {
display: flex;
flex-direction: column;
max-width: 250px;
max-height: 460px;
p {
font-size: 14px;
color: var(--text-primary-color);
}
}

View File

@ -1,17 +1,22 @@
import React, { ChangeEvent } from "react"; import React, { ChangeEvent } from "react";
import Item from "@widgets/Item"; import Item from "@widgets/Item";
import ItemCard from "@widgets/ItemCard";
import VirtualizedGrid from "@components/VirtualizedGrid"; import VirtualizedGrid from "@components/VirtualizedGrid";
import { ItemCategory } from "@backend/types"; import { ItemCategory } from "@backend/types";
import type { Item as ItemType } from "@backend/types"; import type { Item as ItemType, ItemInfo } from "@backend/types";
import { getItems, sortedItems } from "@backend/data"; import { getItems, sortedItems } from "@backend/data";
import { fetchItemData } from "@app/utils";
import "@css/pages/ItemsPage.scss"; import "@css/pages/ItemsPage.scss";
interface IState { interface IState {
filters: ItemCategory[]; filters: ItemCategory[];
search: string; search: string;
selected: ItemType | null;
selectedInfo: ItemInfo | null;
} }
class ItemsPage extends React.Component<{}, IState> { class ItemsPage extends React.Component<{}, IState> {
@ -20,7 +25,10 @@ class ItemsPage extends React.Component<{}, IState> {
this.state = { this.state = {
filters: [], filters: [],
search: "" search: "",
selected: null,
selectedInfo: null
}; };
} }
@ -82,11 +90,29 @@ class ItemsPage extends React.Component<{}, IState> {
return item.id > 0; return item.id > 0;
} }
/**
* Sets the selected item.
*
* @param item The item.
* @private
*/
private async setSelectedItem(item: ItemType): Promise<void> {
let data: ItemInfo | null = null; try {
data = await fetchItemData(item);
} catch { }
this.setState({
selected: item,
selectedInfo: data
});
}
render() { render() {
const items = this.getItems(); const items = this.getItems();
return ( return (
<div className={"ItemsPage"}> <div className={"ItemsPage"}>
<div className={"ItemsPage_Content"}>
<div className={"ItemsPage_Header"}> <div className={"ItemsPage_Header"}>
<h1 className={"ItemsPage_Title"}>Items</h1> <h1 className={"ItemsPage_Title"}>Items</h1>
@ -104,13 +130,24 @@ class ItemsPage extends React.Component<{}, IState> {
<VirtualizedGrid <VirtualizedGrid
list={items.filter((item) => this.showItem(item))} list={items.filter((item) => this.showItem(item))}
itemHeight={64} itemHeight={64}
itemsPerRow={20} itemsPerRow={18}
gap={5} gap={5}
itemGap={5} itemGap={5}
render={(item) => <Item key={item.id} data={item} />} render={(item) => <Item
key={item.id} data={item}
onClick={() => this.setSelectedItem(item)}
/>}
/> />
) : undefined} ) : undefined}
</div> </div>
<div className={"ItemsPage_Card"}>
<ItemCard
item={this.state.selected}
info={this.state.selectedInfo}
/>
</div>
</div>
); );
} }
} }

View File

@ -7,6 +7,7 @@ import "@css/widgets/Item.scss";
interface IProps { interface IProps {
data: ItemData; data: ItemData;
onClick?: () => void;
} }
interface IState { interface IState {
@ -51,7 +52,9 @@ class Item extends React.Component<IProps, IState> {
render() { render() {
return ( return (
<div className={"Item"}> <div className={"Item"}
onClick={this.props.onClick}
>
<div className={"Item_Background"}> <div className={"Item_Background"}>
{this.state.icon && ( {this.state.icon && (
<img <img

View File

@ -0,0 +1,75 @@
import React from "react";
import type { Item as ItemType, ItemInfo } from "@backend/types";
import { itemTypeToString } from "@backend/types";
import { itemIcon } from "@app/utils";
import "@css/widgets/ItemCard.scss";
/**
* Converts a description string into a list of paragraphs.
*
* @param description The description to convert.
*/
function toDescription(description: string | undefined): JSX.Element[] {
if (!description) return [];
return description.split("\\n")
.map((line, index) => {
return <p key={index}>{line}</p>;
});
}
interface IProps {
item: ItemType | null;
info: ItemInfo | null;
}
interface IState {
icon: boolean;
}
class ItemCard extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
icon: true
};
}
render() {
const { item, info } = this.props;
const data = info?.data;
return item ? (
<div className={"ItemCard"}>
<div className={"ItemCard_Content"}>
<div className={"ItemCard_Header"}>
<div className={"ItemCard_Info"}>
<p>{data?.name ?? item.name}</p>
<p>{data?.type ?? itemTypeToString(item.type)}</p>
</div>
{ this.state.icon && <img
className={"ItemCard_Icon"}
alt={item.name}
src={itemIcon(item)}
onError={() => this.setState({ icon: false })}
/> }
</div>
<div className={"ItemCard_Description"}>
{toDescription(data?.description)}
</div>
</div>
<div>
</div>
</div>
) : undefined;
}
}
export default ItemCard;

View File

@ -1,5 +1,5 @@
import { ItemType, Quality } from "@backend/types";
import type { Item } from "@backend/types"; import type { Item } from "@backend/types";
import { ItemInfo, ItemType, Quality } from "@backend/types";
/** /**
* Fetches the name of the CSS variable for the quality. * Fetches the name of the CSS variable for the quality.
@ -73,3 +73,41 @@ const refSwitch: { [key: number]: string } = {
10000005: "traveler_anemo", 10000005: "traveler_anemo",
10000007: "traveler_geo" 10000007: "traveler_geo"
}; };
/**
* Gets the route for an item type.
*
* @param type The type of the item.
*/
export function typeToRoute(type: ItemType): string {
switch (type) {
default:
return "material";
case ItemType.Furniture:
return "furniture";
case ItemType.Reliquary:
return "reliquary";
case ItemType.Weapon:
return "weapon";
}
}
/**
* Fetches the data for an item.
* Uses the Project Amber API to get the data.
*
* @route GET https://api.ambr.top/v2/EN/{type}/{id}
* @param item The item to fetch the data for.
*/
export async function fetchItemData(item: Item): Promise<ItemInfo> {
let url = `https://api.ambr.top/v2/EN/(type)/(id)`;
// Replace the type and ID in the URL.
url = url.replace("(type)", typeToRoute(item.type));
url = url.replace("(id)", item.id.toString());
// Fetch the data.
return fetch(url)
.then((res) => res.json())
.catch(() => {});
}