-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memo the market #2525
memo the market #2525
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,152 +5,154 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@ | |
import TextInput from "@/ui/elements/TextInput"; | ||
import { divideByPrecision, formatNumber } from "@/ui/utils/utils"; | ||
import { ID, Resources, ResourcesIds, findResourceById, findResourceIdByTrait } from "@bibliothecadao/eternum"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { memo, useEffect, useRef, useState } from "react"; | ||
import { HintSection } from "../hints/HintModal"; | ||
|
||
export const ResourceBar = ({ | ||
entityId, | ||
lordsFee, | ||
resources, | ||
resourceId, | ||
setResourceId, | ||
amount, | ||
setAmount, | ||
disableInput = false, | ||
onFocus, | ||
onBlur, | ||
max = Infinity, | ||
}: { | ||
entityId: ID; | ||
lordsFee: number; | ||
resources: Resources[]; | ||
resourceId: ResourcesIds; | ||
setResourceId: (resourceId: ResourcesIds) => void; | ||
amount: number; | ||
setAmount: (amount: number) => void; | ||
disableInput?: boolean; | ||
onFocus?: () => void; // New prop | ||
onBlur?: () => void; // New prop | ||
max?: number; | ||
}) => { | ||
const { getBalance } = useResourceBalance(); | ||
export const ResourceBar = memo( | ||
({ | ||
entityId, | ||
lordsFee, | ||
resources, | ||
resourceId, | ||
setResourceId, | ||
amount, | ||
setAmount, | ||
disableInput = false, | ||
onFocus, | ||
onBlur, | ||
max = Infinity, | ||
}: { | ||
entityId: ID; | ||
lordsFee: number; | ||
resources: Resources[]; | ||
resourceId: ResourcesIds; | ||
setResourceId: (resourceId: ResourcesIds) => void; | ||
amount: number; | ||
setAmount: (amount: number) => void; | ||
disableInput?: boolean; | ||
onFocus?: () => void; // New prop | ||
onBlur?: () => void; // New prop | ||
max?: number; | ||
}) => { | ||
Comment on lines
+11
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Verify memoization effectiveness While wrapping the component in Consider wrapping the callback props with // In parent component:
const handleSetResourceId = useCallback((resourceId: ResourcesIds) => {
setResourceId(resourceId);
}, []);
const handleSetAmount = useCallback((amount: number) => {
setAmount(amount);
}, []); |
||
const { getBalance } = useResourceBalance(); | ||
|
||
const [selectedResourceBalance, setSelectedResourceBalance] = useState(0); | ||
const [searchInput, setSearchInput] = useState(""); | ||
const [open, setOpen] = useState(false); | ||
const [selectedResourceBalance, setSelectedResourceBalance] = useState(0); | ||
const [searchInput, setSearchInput] = useState(""); | ||
const [open, setOpen] = useState(false); | ||
|
||
const inputRef = useRef<HTMLInputElement>(null); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
setSelectedResourceBalance(divideByPrecision(getBalance(entityId, Number(resourceId)).balance)); | ||
}, [resourceId, getBalance, entityId]); | ||
useEffect(() => { | ||
setSelectedResourceBalance(divideByPrecision(getBalance(entityId, Number(resourceId)).balance)); | ||
}, [resourceId, getBalance, entityId]); | ||
|
||
const handleResourceChange = (trait: string) => { | ||
const newResourceId = findResourceIdByTrait(trait); | ||
setResourceId(newResourceId); | ||
}; | ||
const handleResourceChange = (trait: string) => { | ||
const newResourceId = findResourceIdByTrait(trait); | ||
setResourceId(newResourceId); | ||
}; | ||
|
||
const handleAmountChange = (amount: number) => { | ||
setAmount(amount); | ||
}; | ||
const handleAmountChange = (amount: number) => { | ||
setAmount(amount); | ||
}; | ||
|
||
const hasLordsFees = lordsFee > 0 && resourceId === ResourcesIds.Lords; | ||
const finalResourceBalance = hasLordsFees ? selectedResourceBalance - lordsFee : selectedResourceBalance; | ||
const hasLordsFees = lordsFee > 0 && resourceId === ResourcesIds.Lords; | ||
const finalResourceBalance = hasLordsFees ? selectedResourceBalance - lordsFee : selectedResourceBalance; | ||
|
||
const filteredResources = resources.filter( | ||
(resource) => resource.trait.toLowerCase().startsWith(searchInput.toLowerCase()) || resource.id === resourceId, | ||
); | ||
const filteredResources = resources.filter( | ||
(resource) => resource.trait.toLowerCase().startsWith(searchInput.toLowerCase()) || resource.id === resourceId, | ||
); | ||
|
||
const handleOpenChange = (newOpen: boolean) => { | ||
setOpen(newOpen); | ||
if (newOpen && inputRef.current) { | ||
setResourceId(ResourcesIds.Wood); | ||
setSearchInput(""); | ||
setTimeout(() => { | ||
inputRef.current?.focus(); | ||
}, 0); | ||
} | ||
}; | ||
const handleOpenChange = (newOpen: boolean) => { | ||
setOpen(newOpen); | ||
if (newOpen && inputRef.current) { | ||
setResourceId(ResourcesIds.Wood); | ||
setSearchInput(""); | ||
setTimeout(() => { | ||
inputRef.current?.focus(); | ||
}, 0); | ||
} | ||
}; | ||
|
||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
if (filteredResources.length > 0) { | ||
const selectedResource = filteredResources.find((resource) => resource.id !== resourceId); | ||
if (selectedResource) { | ||
setResourceId(selectedResource.id); | ||
setOpen(false); | ||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
if (filteredResources.length > 0) { | ||
const selectedResource = filteredResources.find((resource) => resource.id !== resourceId); | ||
if (selectedResource) { | ||
setResourceId(selectedResource.id); | ||
setOpen(false); | ||
} | ||
} | ||
setSearchInput(""); | ||
} else { | ||
e.stopPropagation(); | ||
} | ||
setSearchInput(""); | ||
} else { | ||
e.stopPropagation(); | ||
} | ||
}; | ||
}; | ||
|
||
return ( | ||
<div className="resource-bar-selector w-full bg-gold/10 rounded-xl p-3 flex justify-between h-28 flex-wrap"> | ||
<div className="self-center"> | ||
<NumberInput | ||
className="text-2xl border-transparent" | ||
value={amount} | ||
onChange={handleAmountChange} | ||
max={max} | ||
arrows={false} | ||
allowDecimals | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
/> | ||
|
||
return ( | ||
<div className="resource-bar-selector w-full bg-gold/10 rounded-xl p-3 flex justify-between h-28 flex-wrap"> | ||
<div className="self-center"> | ||
<NumberInput | ||
className="text-2xl border-transparent" | ||
value={amount} | ||
onChange={handleAmountChange} | ||
max={max} | ||
arrows={false} | ||
allowDecimals | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
/> | ||
{!disableInput && ( | ||
<div | ||
className="flex text-xs text-gold/70 mt-1 justify-center items-center relative text-center self-center mx-auto w-full cursor-pointer" | ||
onClick={() => handleAmountChange(finalResourceBalance)} | ||
> | ||
Max: {isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace unsafe isNaN with Number.isNaN The global Apply this fix: - Max: {isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()}
+ Max: {Number.isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()}
{hasLordsFees && (
<div className="text-danger ml-2">
- <div>{`[+${isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div>
+ <div>{`[+${Number.isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div> Also applies to: 113-113 🧰 Tools🪛 Biome (1.9.4)[error] 110-110: isNaN is unsafe. It attempts a type coercion. Use Number.isNaN instead. See the MDN documentation for more details. (lint/suspicious/noGlobalIsNan) |
||
{hasLordsFees && ( | ||
<div className="text-danger ml-2"> | ||
<div>{`[+${isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div> | ||
</div> | ||
)} | ||
</div> | ||
)} | ||
</div> | ||
|
||
{!disableInput && ( | ||
<div | ||
className="flex text-xs text-gold/70 mt-1 justify-center items-center relative text-center self-center mx-auto w-full cursor-pointer" | ||
onClick={() => handleAmountChange(finalResourceBalance)} | ||
> | ||
Max: {isNaN(selectedResourceBalance) ? "0" : selectedResourceBalance.toLocaleString()} | ||
{hasLordsFees && ( | ||
<div className="text-danger ml-2"> | ||
<div>{`[+${isNaN(lordsFee) ? "0" : formatNumber(lordsFee, 2)}]`}</div> | ||
<Select | ||
open={open} | ||
onOpenChange={handleOpenChange} | ||
value={findResourceById(Number(resourceId))?.trait || ""} | ||
onValueChange={(trait) => { | ||
handleResourceChange(trait); | ||
setOpen(false); | ||
setSearchInput(""); | ||
}} | ||
> | ||
<SelectTrigger className="w-[140px]"> | ||
<SelectValue placeholder={HintSection.Resources} /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{resources.length > 1 && ( | ||
<div className="p-2"> | ||
<TextInput | ||
ref={inputRef} | ||
onChange={setSearchInput} | ||
placeholder="Filter resources..." | ||
onKeyDown={handleKeyDown} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
)} | ||
{filteredResources.map((resource) => ( | ||
<SelectItem key={resource.id} value={resource.trait} disabled={resource.id === resourceId}> | ||
<ResourceCost | ||
resourceId={resource.id} | ||
amount={divideByPrecision(getBalance(entityId, resource.id).balance)} | ||
className="border-0 bg-transparent" | ||
/> | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
|
||
<Select | ||
open={open} | ||
onOpenChange={handleOpenChange} | ||
value={findResourceById(Number(resourceId))?.trait || ""} | ||
onValueChange={(trait) => { | ||
handleResourceChange(trait); | ||
setOpen(false); | ||
setSearchInput(""); | ||
}} | ||
> | ||
<SelectTrigger className="w-[140px]"> | ||
<SelectValue placeholder={HintSection.Resources} /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{resources.length > 1 && ( | ||
<div className="p-2"> | ||
<TextInput | ||
ref={inputRef} | ||
onChange={setSearchInput} | ||
placeholder="Filter resources..." | ||
onKeyDown={handleKeyDown} | ||
/> | ||
</div> | ||
)} | ||
{filteredResources.map((resource) => ( | ||
<SelectItem key={resource.id} value={resource.trait} disabled={resource.id === resourceId}> | ||
<ResourceCost | ||
resourceId={resource.id} | ||
amount={divideByPrecision(getBalance(entityId, resource.id).balance)} | ||
className="border-0 bg-transparent" | ||
/> | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
</div> | ||
); | ||
}; | ||
); | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling with more context and recovery strategy.
While adding error handling is good, the current implementation could be improved to:
Consider this enhanced implementation:
💡 Codebase verification
Based on the collected evidence, I can now provide the final response:
Add error handling for animation mixer updates
The current error handling for
biome.updateAnimations()
is good practice, but there's an unprotected animation update inHexception.tsx
wheremixer.update(deltaTime)
is called without error handling. To maintain consistency and prevent potential runtime errors, similar error handling should be added there as well.client/src/three/scenes/Hexception.tsx
: Add try-catch block aroundmixer.update(deltaTime)
in the update method🔗 Analysis chain
Verify error handling coverage across animation-related code.
Let's verify if similar error handling is implemented in other animation-related methods.
Let me analyze the findings and generate additional verification scripts to get a clearer picture.
The initial results show multiple animation-related code paths, but we need to specifically check the error handling in animation update methods and mixer updates. These additional scripts will help us verify the coverage of error handling in critical animation-related operations.
Let me try one more search pattern to ensure we have a complete view of animation-related error handling.
The previous results show only one mixer update without error handling, but we need more context to understand the full scope of animation-related updates in the codebase.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 16752
Script:
Length of output: 208
Script:
Length of output: 686