Persist live strategy restart choice

This commit is contained in:
Thigazhezhilan J 2026-04-17 22:01:31 +05:30
parent 02c48ae91b
commit 46cf062fe2

View File

@ -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() { function usePrefersReducedMotion() {
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false); const [prefersReducedMotion, setPrefersReducedMotion] = useState(false);
@ -845,6 +878,10 @@ export default function PortfolioSection() {
normalizedStrategyStatus === "RUNNING" || normalizedStrategyStatus === "RUNNING" ||
normalizedStrategyStatus === "WAITING"; normalizedStrategyStatus === "WAITING";
const canResumeSavedStrategy = !!strategyDetails?.can_resume; const canResumeSavedStrategy = !!strategyDetails?.can_resume;
const freshStartPreferenceKey = getFreshStartPreferenceKey(
sessionUser?.id,
strategyDetails?.run_id,
);
const showResumeStrategy = const showResumeStrategy =
!isStrategyActive && canResumeSavedStrategy && !freshStartRequested; !isStrategyActive && canResumeSavedStrategy && !freshStartRequested;
const showRestartStrategy = const showRestartStrategy =
@ -902,6 +939,30 @@ export default function PortfolioSection() {
const marketState = marketStatus?.status ?? "UNKNOWN"; const marketState = marketStatus?.status ?? "UNKNOWN";
const canArmStrategy = liveness === "ACTIVE" || liveness === "STOPPED"; 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 const nextEligibleTs = engineStatus?.next_eligible_ts
? new Date(engineStatus.next_eligible_ts) ? new Date(engineStatus.next_eligible_ts)
: null; : null;
@ -1022,7 +1083,7 @@ export default function PortfolioSection() {
description: "The engine is already active.", description: "The engine is already active.",
}); });
} else if (result?.status === "started" || result?.status === "restarted") { } else if (result?.status === "started" || result?.status === "restarted") {
setFreshStartRequested(false); clearFreshStartPreference();
toast({ toast({
title: "Live strategy started", title: "Live strategy started",
description: `Golden Nifty is now armed for live ${brokerLabel} execution.`, description: `Golden Nifty is now armed for live ${brokerLabel} execution.`,
@ -1086,7 +1147,7 @@ export default function PortfolioSection() {
description: "The engine is already active.", description: "The engine is already active.",
}); });
} else if (result?.status === "resumed") { } else if (result?.status === "resumed") {
setFreshStartRequested(false); clearFreshStartPreference();
toast({ toast({
title: "Live strategy resumed", title: "Live strategy resumed",
description: "The previous live run has been resumed with its saved schedule.", description: "The previous live run has been resumed with its saved schedule.",
@ -1128,7 +1189,7 @@ export default function PortfolioSection() {
try { try {
const result = await stopStrategy(); const result = await stopStrategy();
if (result?.status === "stopped") { if (result?.status === "stopped") {
setFreshStartRequested(false); clearFreshStartPreference();
if (result?.warning) { if (result?.warning) {
toast({ toast({
title: "Strategy stopped", title: "Strategy stopped",
@ -1136,7 +1197,7 @@ export default function PortfolioSection() {
}); });
} }
} else if (result?.status === "already_stopped") { } else if (result?.status === "already_stopped") {
setFreshStartRequested(false); clearFreshStartPreference();
toast({ toast({
title: "Strategy already stopped", title: "Strategy already stopped",
description: "The live strategy was already inactive.", description: "The live strategy was already inactive.",
@ -1592,7 +1653,7 @@ export default function PortfolioSection() {
</MotionButton> </MotionButton>
) : null} ) : null}
{showRestartStrategy ? ( {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 Restart Strategy
</Button> </Button>
) : null} ) : null}