Closed aymericdelab closed 2 weeks ago
The latest updates on your projects. Learn more about Vercel for Git โ๏ธ
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
eternum | โ Ready (Inspect) | Visit Preview | ๐ฌ Add feedback | Jun 21, 2024 11:06am |
โฑ๏ธ Estimated effort to review [1-5] | 4 |
๐งช Relevant tests | No |
๐ Security concerns | No |
โก Key issues to review |
Possible Bug: The refactoring in useStructures.tsx introduces hooks (useFormattedRealmAtPosition , useFormattedStructureAtPosition , hasStructuresAtPosition ) that are defined within another hook (useStructuresPosition ). This pattern can lead to unexpected behavior as hooks should not be defined conditionally or nested within other hooks unless they are custom hooks themselves and are used as such. This needs to be addressed to avoid potential bugs and React errors. |
Performance Concern: The findShortestPathBFS function now includes a stamina-based step limit, which is a good enhancement. However, the implementation should ensure that this does not negatively impact the performance, especially in scenarios with large maps or numerous entities. It would be beneficial to see performance benchmarks or tests to validate the impact of this change. |
Category | Suggestion | Score |
Best practice |
Refactor hooks to be defined outside and independently from other hooks___ **Avoid defining hooks inside other hooks or functions. This can lead to unexpected behaviorand violates React's rules of hooks. Instead, define useFormattedRealmAtPosition and useFormattedStructureAtPosition outside of useStructuresPosition and pass necessary parameters.** [client/src/hooks/helpers/useStructures.tsx [44-74]](https://github.com/BibliothecaDAO/eternum/pull/976/files#diff-396eaa9679c616f55b4078861124f09fe1125e62921747c6e600b2d803948261R44-R74) ```diff -const useFormattedRealmAtPosition = () => { +export const useFormattedRealmAtPosition = (position) => { const realmsAtPosition = useEntityQuery([HasValue(Position, position), HasValue(Structure, { category: "Realm" })]); ... }; -const useFormattedStructureAtPosition = () => { +export const useFormattedStructureAtPosition = (position) => { const structuresAtPosition = useEntityQuery([HasValue(Position, position), Has(Structure)]); ... }; ``` Suggestion importance[1-10]: 9Why: This suggestion addresses a significant issue with React's rules of hooks, which can lead to unexpected behavior and bugs. Refactoring the hooks to be defined outside and independently is crucial for maintaining proper hook behavior and ensuring code stability. | 9 |
Performance |
Consolidate duplicate entity queries into a single reusable object___ **Refactor the repeated calls touseEntityQuery with the same parameters into a single call and reuse the result. This will improve performance by reducing the number of re-renders and duplicate queries.** [client/src/hooks/helpers/useStructures.tsx [44-74]](https://github.com/BibliothecaDAO/eternum/pull/976/files#diff-396eaa9679c616f55b4078861124f09fe1125e62921747c6e600b2d803948261R44-R74) ```diff -const realmsAtPosition = useEntityQuery([HasValue(Position, position), HasValue(Structure, { category: "Realm" })]); -const structuresAtPosition = useEntityQuery([HasValue(Position, position), Has(Structure)]); +const entityQueries = { + realms: useEntityQuery([HasValue(Position, position), HasValue(Structure, { category: "Realm" })]), + structures: useEntityQuery([HasValue(Position, position), Has(Structure)]) +}; +const { realmsAtPosition, structuresAtPosition } = entityQueries; ``` Suggestion importance[1-10]: 8Why: This suggestion improves performance by reducing redundant queries and re-renders, which is beneficial for the application's efficiency. It addresses a significant but not critical issue, hence the high score. | 8 |
Cache the result of
___
**Optimize the calculation of | 6 | |
Possible issue |
Improve error handling for cases where no path is found in army animation___ **Consider handling the case wherefindShortestPathBFS might return an empty path more robustly. Instead of directly assigning a fallback path, it might be beneficial to handle this scenario explicitly, perhaps by notifying the user or logging an error.** [client/src/ui/components/worldmap/armies/useArmyAnimation.tsx [32-37]](https://github.com/BibliothecaDAO/eternum/pull/976/files#diff-ce247ef03061209e44162c6a4f2a687d3ad45f5b3843f3f16253cd738cf64335R32-R37) ```diff let uiPath = findShortestPathBFS(startPos, endPos, exploredHexes).map((pos) => getUIPositionFromColRow(pos.x, pos.y), ); if (uiPath.length === 0) { - uiPath = [getUIPositionFromColRow(startPos.x, startPos.y), getUIPositionFromColRow(endPos.x, endPos.y)]; + console.error("No path found from start to end position"); + // Optionally handle the scenario more gracefully } ``` Suggestion importance[1-10]: 7Why: This suggestion improves error handling, which is important for debugging and user experience. However, it is not as critical as the first suggestion since it deals with a specific edge case rather than a fundamental issue. | 7 |
User description
PR Type
Bug fix, Enhancement
Description
useStructuresPosition
to split logic into separate hooks (useFormattedRealmAtPosition
,useFormattedStructureAtPosition
,hasStructuresAtPosition
).StructureCard
andHexagonInformationPanel
components to use the new hooks.useArmyAnimation
for cases where no path is found.findShortestPathBFS
by introducing a stamina-based step limit.Changes walkthrough ๐
useStructures.tsx
Refactor and enhance structure position hooks
client/src/hooks/helpers/useStructures.tsx
useStructuresPosition
to split logic into separate hooks.useFormattedRealmAtPosition
anduseFormattedStructureAtPosition
hooks.
hasStructuresAtPosition
function.StructureCard.tsx
Integrate new structure position hooks in StructureCard
client/src/ui/components/hyperstructures/StructureCard.tsx
StructureCard
to use new hooks fromuseStructuresPosition
.HexagonInformationPanel.tsx
Integrate new structure position hooks in HexagonInformationPanel
client/src/ui/components/worldmap/hexagon/HexagonInformationPanel.tsx
HexagonInformationPanel
to use new hooks fromuseStructuresPosition
.utils.tsx
Enhance pathfinding with stamina-based step limit
client/src/ui/components/worldmap/hexagon/utils.tsx
getMaxSteps
function to calculate maximum steps based onstamina.
findShortestPathBFS
to usegetMaxSteps
and limit pathfinding.useArmyAnimation.tsx
Add fallback path logic in army animation
client/src/ui/components/worldmap/armies/useArmyAnimation.tsx
useArmyAnimation
for cases with no path.