Persist live strategy restart choice
This commit is contained in:
parent
02c48ae91b
commit
46cf062fe2
@ -310,6 +310,39 @@ function getAvailableFundsValue(funds?: FundsResponse["funds"] | null) {
|
||||
);
|
||||
}
|
||||
|
||||
function getFreshStartPreferenceKey(userId?: string | null, runId?: string | null) {
|
||||
if (!userId || !runId) {
|
||||
return null;
|
||||
}
|
||||
return `quantfortune:fresh-start:live:${userId}:${runId}`;
|
||||
}
|
||||
|
||||
function readFreshStartPreference(key: string | null) {
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return window.localStorage.getItem(key) === "1";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function writeFreshStartPreference(key: string | null, value: boolean) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (value) {
|
||||
window.localStorage.setItem(key, "1");
|
||||
} else {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function usePrefersReducedMotion() {
|
||||
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false);
|
||||
|
||||
@ -845,6 +878,10 @@ export default function PortfolioSection() {
|
||||
normalizedStrategyStatus === "RUNNING" ||
|
||||
normalizedStrategyStatus === "WAITING";
|
||||
const canResumeSavedStrategy = !!strategyDetails?.can_resume;
|
||||
const freshStartPreferenceKey = getFreshStartPreferenceKey(
|
||||
sessionUser?.id,
|
||||
strategyDetails?.run_id,
|
||||
);
|
||||
const showResumeStrategy =
|
||||
!isStrategyActive && canResumeSavedStrategy && !freshStartRequested;
|
||||
const showRestartStrategy =
|
||||
@ -902,6 +939,30 @@ export default function PortfolioSection() {
|
||||
const marketState = marketStatus?.status ?? "UNKNOWN";
|
||||
const canArmStrategy = liveness === "ACTIVE" || liveness === "STOPPED";
|
||||
|
||||
const requestFreshStart = useCallback(() => {
|
||||
setFreshStartRequested(true);
|
||||
writeFreshStartPreference(freshStartPreferenceKey, true);
|
||||
}, [freshStartPreferenceKey]);
|
||||
|
||||
const clearFreshStartPreference = useCallback(() => {
|
||||
setFreshStartRequested(false);
|
||||
writeFreshStartPreference(freshStartPreferenceKey, false);
|
||||
}, [freshStartPreferenceKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!freshStartPreferenceKey) {
|
||||
return;
|
||||
}
|
||||
if (isStrategyActive || !strategyDetails?.can_restart) {
|
||||
writeFreshStartPreference(freshStartPreferenceKey, false);
|
||||
setFreshStartRequested(false);
|
||||
return;
|
||||
}
|
||||
if (readFreshStartPreference(freshStartPreferenceKey)) {
|
||||
setFreshStartRequested(true);
|
||||
}
|
||||
}, [freshStartPreferenceKey, isStrategyActive, strategyDetails?.can_restart]);
|
||||
|
||||
const nextEligibleTs = engineStatus?.next_eligible_ts
|
||||
? new Date(engineStatus.next_eligible_ts)
|
||||
: null;
|
||||
@ -1022,7 +1083,7 @@ export default function PortfolioSection() {
|
||||
description: "The engine is already active.",
|
||||
});
|
||||
} else if (result?.status === "started" || result?.status === "restarted") {
|
||||
setFreshStartRequested(false);
|
||||
clearFreshStartPreference();
|
||||
toast({
|
||||
title: "Live strategy started",
|
||||
description: `Golden Nifty is now armed for live ${brokerLabel} execution.`,
|
||||
@ -1086,7 +1147,7 @@ export default function PortfolioSection() {
|
||||
description: "The engine is already active.",
|
||||
});
|
||||
} else if (result?.status === "resumed") {
|
||||
setFreshStartRequested(false);
|
||||
clearFreshStartPreference();
|
||||
toast({
|
||||
title: "Live strategy resumed",
|
||||
description: "The previous live run has been resumed with its saved schedule.",
|
||||
@ -1128,7 +1189,7 @@ export default function PortfolioSection() {
|
||||
try {
|
||||
const result = await stopStrategy();
|
||||
if (result?.status === "stopped") {
|
||||
setFreshStartRequested(false);
|
||||
clearFreshStartPreference();
|
||||
if (result?.warning) {
|
||||
toast({
|
||||
title: "Strategy stopped",
|
||||
@ -1136,7 +1197,7 @@ export default function PortfolioSection() {
|
||||
});
|
||||
}
|
||||
} else if (result?.status === "already_stopped") {
|
||||
setFreshStartRequested(false);
|
||||
clearFreshStartPreference();
|
||||
toast({
|
||||
title: "Strategy already stopped",
|
||||
description: "The live strategy was already inactive.",
|
||||
@ -1592,7 +1653,7 @@ export default function PortfolioSection() {
|
||||
</MotionButton>
|
||||
) : null}
|
||||
{showRestartStrategy ? (
|
||||
<Button variant="outline" onClick={() => setFreshStartRequested(true)} className="w-full sm:w-auto">
|
||||
<Button variant="outline" onClick={requestFreshStart} className="w-full sm:w-auto">
|
||||
Restart Strategy
|
||||
</Button>
|
||||
) : null}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user