-
Notifications
You must be signed in to change notification settings - Fork 52
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
fix: nextBlockTimestamp refactor #2405
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { configManager } from "@/dojo/setup"; | ||
import { TickIds } from "@bibliothecadao/eternum"; | ||
import { startTransition, useEffect, useState } from "react"; | ||
|
||
const useNextBlockTimestamp = () => { | ||
const [nextBlockTimestamp, setNextBlockTimestamp] = useState<number>(Math.floor(Date.now() / 1000)); | ||
const [currentDefaultTick, setCurrentDefaultTick] = useState<number>(0); | ||
const [currentArmiesTick, setCurrentArmiesTick] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const tickConfigArmies = configManager.getTick(TickIds.Armies); | ||
const tickConfigDefault = configManager.getTick(TickIds.Default); | ||
|
||
const updateTimestamp = () => { | ||
const timestamp = Math.floor(Date.now() / 1000); | ||
startTransition(() => { | ||
setNextBlockTimestamp(timestamp); | ||
setCurrentDefaultTick(Math.floor(timestamp / Number(tickConfigDefault))); | ||
setCurrentArmiesTick(Math.floor(timestamp / Number(tickConfigArmies))); | ||
}); | ||
}; | ||
|
||
// Create a unique interval for each hook instance | ||
const intervalId = setInterval(updateTimestamp, 10000); | ||
|
||
return () => { | ||
clearInterval(intervalId); | ||
}; | ||
}, []); | ||
|
||
return { nextBlockTimestamp, currentDefaultTick, currentArmiesTick }; | ||
}; | ||
|
||
export default useNextBlockTimestamp; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
useStructureImmunityTimer, | ||
useStructures, | ||
} from "@/hooks/helpers/useStructures"; | ||
import useNextBlockTimestamp from "@/hooks/useNextBlockTimestamp"; | ||
import { ArmyCapacity } from "@/ui/elements/ArmyCapacity"; | ||
import { BaseThreeTooltip, Position } from "@/ui/elements/BaseThreeTooltip"; | ||
import { Headline } from "@/ui/elements/Headline"; | ||
|
@@ -56,7 +57,7 @@ const RaiderInfo = ({ army }: ArmyInfoLabelProps) => { | |
} | ||
}, [entityOwner.entity_owner_id]); | ||
|
||
const nextBlockTimestamp = useUIStore((state) => state.nextBlockTimestamp); | ||
const { nextBlockTimestamp } = useNextBlockTimestamp(); | ||
|
||
const isImmune = useIsStructureImmune(structure, nextBlockTimestamp || 0); | ||
const timer = useStructureImmunityTimer(structure as Structure, nextBlockTimestamp || 0); | ||
Comment on lines
+60
to
63
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. Consider handling undefined nextBlockTimestamp more gracefully Using 0 as a fallback for Here's a suggested improvement: - const { nextBlockTimestamp } = useNextBlockTimestamp();
+ const { nextBlockTimestamp } = useNextBlockTimestamp();
+
+ if (!nextBlockTimestamp) {
+ return (
+ <BaseThreeTooltip position={Position.CLEAN} className="pointer-events-none w-[250px]">
+ <div>Loading...</div>
+ </BaseThreeTooltip>
+ );
+ }
- const isImmune = useIsStructureImmune(structure, nextBlockTimestamp || 0);
- const timer = useStructureImmunityTimer(structure as Structure, nextBlockTimestamp || 0);
+ const isImmune = useIsStructureImmune(structure, nextBlockTimestamp);
+ const timer = useStructureImmunityTimer(structure as Structure, nextBlockTimestamp);
|
||
|
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.
Inconsistent timestamp retrieval methods detected
Several functions still use
useUIStore.getState().currentDefaultTick
while others have been updated to useuseNextBlockTimestamp
. This inconsistency could lead to synchronization issues in timestamp values.Consider updating these instances to use the new hook:
Affected functions:
Also applies to: 49-49, 89-89, 126-126, 147-147