Skip to content

Commit b070900

Browse files
committed
Refactor Crafting tab to use shared components
Replaces local logic and markup in the Crafting tab with shared AchievementItem and ItemWithCounter components. Updates props to use a new itemsCraftedType structure, streamlining the display of crafted items and achievements. Improves code readability and maintainability by removing redundant state and effect logic.
1 parent 1bfaab0 commit b070900

1 file changed

Lines changed: 45 additions & 46 deletions

File tree

src/Components/AchieveTabs/Crafting/index.tsx

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,52 @@
1-
import React, { useState, useEffect } from 'react'
2-
3-
interface CraftingItem {
4-
times?: number;
5-
name: string;
6-
image: string;
7-
}
8-
9-
interface CraftingProps {
10-
itemsCrafted: CraftingItem[];
11-
}
12-
13-
const Crafting: React.FC<CraftingProps> = ({ itemsCrafted }) => {
14-
const [totalCrafted, setTotalCrafted] = useState(0);
15-
16-
const getCraftedItems = (items: CraftingItem[]) => {
17-
return items.map((num) => (num.times !== undefined && num.times > 0 && num.name !== "Wedding Ring") ? 1 : 0).reduce((n: number, next: number) => next + n, 0);
18-
};
19-
20-
useEffect(() => {
21-
setTotalCrafted(getCraftedItems(itemsCrafted));
22-
}, [itemsCrafted]);
23-
24-
const craftedCount = itemsCrafted.map((num) => (num.times !== undefined && num.times > 0) ? 1 : 0).reduce((n: number, next: number) => next + n, 0);
25-
const knownCount = itemsCrafted.map((num) => (num.times !== undefined && num.times >= 0) ? 1 : 0).reduce((n: number, next: number) => next + n, 0);
26-
1+
import { AchievementItem, ItemWithCounter } from "@components/common";
2+
import type { itemsCraftedType } from "types/displayDataTypes";
3+
4+
const Crafting = ({
5+
knownItems,
6+
alreadyCraftedItems,
7+
craftedItems,
8+
totalRecipes,
9+
achievements }: itemsCraftedType) => {
2710
return (
28-
<div className="progress-container">
29-
<span className="a-title"><h1>has crafted {craftedCount} and knows {knownCount} of {itemsCrafted.length} recipes.</h1></span>
11+
<div className="progress-container">
12+
<span className="a-title">
13+
<h2>
14+
has crafted {alreadyCraftedItems} and knows {knownItems} of {totalRecipes} recipes.
15+
</h2>
16+
</span>
3017
<br />
3118
<h2>Crafting Achievements</h2>
32-
<ul className="a-List">
33-
<li>D.I.Y.: {(totalCrafted >= 15) ? <span className="completed">You have this achievement</span> : <span className="pending">You need to craft {15 - totalCrafted} more items to get this</span> } </li>
34-
<li>Artisan: {(totalCrafted >= 30) ? <span className="completed">You have this achievement</span> : <span className="pending">You need to craft {30 - totalCrafted} more items to get this </span>}</li>
35-
<li>Craft Master: {(totalCrafted >= 104) ? <span className="completed">You have this achievement</span> : <span className="pending">You need to craft {104 - totalCrafted} more items to get this </span>}</li>
36-
</ul>
19+
<div className="section-achievements">
20+
{achievements && achievements.map((ach, i) => (
21+
<AchievementItem
22+
key={i}
23+
done={ach.done}
24+
image={ach.image}
25+
achievementName={ach.name}
26+
achievementDesc={ach.description}
27+
achievementHoverDesc={ach.hoverDesc}
28+
/>))}
29+
</div>
3730
<br />
38-
{itemsCrafted.map((item, i) => (
39-
<a href={`https://stardewvalleywiki.com/${item.image}`}
40-
target="_blank"
41-
rel="noreferrer"
42-
key={i}>
43-
<img
44-
key={i}
45-
src={`https://stardew-tracker.s3.amazonaws.com/Crafting/${item.image}.png`}
46-
alt={item.name}
47-
className={(item.times !== undefined) ? ((item.times > 0) ? "done" : "known") : "" }
48-
title={(item.times !== undefined) ? ((item.times > 0) ? `You have crafted "${item.name}" ${item.times} Times` : `You haven't crafted any ${item.name} yet`) : `You don't know how to craft ${item.name} yet`}>
49-
</img>
50-
</a>))}
31+
32+
<div className="item-grid">
33+
{ craftedItems ?
34+
craftedItems.map((d, i) =>
35+
<ItemWithCounter
36+
key={i}
37+
link={`https://stardewvalleywiki.com/${d.image}`}
38+
src={`https://stardew-tracker.s3.amazonaws.com/Crafting/${d.image}.png`}
39+
name={d.name}
40+
state={ (d.known) ? ((d.times && d.times > 0) ? "done" : "known" ): "unknown" }
41+
hoverDesc={(d.known) ?
42+
(d.times && d.times > 0) ? `Crafted ${d.name} ${d.times} times`
43+
: `You haven't crafted ${d.name}`
44+
: `You don't know how to craft ${d.name}`}
45+
times={d.known ? d.times : undefined}
46+
/>)
47+
: <div>No crafting data available.</div>
48+
}
49+
</div>
5150
</div>
5251
);
5352
};

0 commit comments

Comments
 (0)