From d306c1ff29f6a903f464ae14e6d4753b08aa4b16 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Mon, 25 May 2026 18:01:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E6=8F=8F=E7=BB=88=E7=AB=AF=E5=8A=A0?= =?UTF-8?q?=20localStorage=20=E7=BC=93=E5=AD=98=EF=BC=8C=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E5=9F=8E=E5=B8=82=E5=88=97=E8=A1=A8=E7=9E=AC?= =?UTF-8?q?=E9=97=B4=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scan-terminal/use-scan-terminal-query.ts | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/frontend/components/dashboard/scan-terminal/use-scan-terminal-query.ts b/frontend/components/dashboard/scan-terminal/use-scan-terminal-query.ts index 71be087f..369aa605 100644 --- a/frontend/components/dashboard/scan-terminal/use-scan-terminal-query.ts +++ b/frontend/components/dashboard/scan-terminal/use-scan-terminal-query.ts @@ -1,6 +1,6 @@ "use client"; -import { useCallback, useEffect, useRef } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { scanTerminalQueryPolicy, scanTerminalClient, @@ -10,6 +10,25 @@ import { import { useRemoteDataQuery } from "@/components/dashboard/scan-terminal/use-remote-data-query"; import type { ScanTerminalResponse } from "@/lib/dashboard-types"; +const SCAN_CACHE_KEY = "polyweather_scan_v1"; +const SCAN_CACHE_TTL_MS = 2 * 60 * 1000; // 2 min — cities list instant on revisit + +function readScanCache(): ScanTerminalResponse | null { + try { + const raw = localStorage.getItem(SCAN_CACHE_KEY); + if (!raw) return null; + const cached = JSON.parse(raw); + if (cached.ts && Date.now() - cached.ts < SCAN_CACHE_TTL_MS && cached.data?.rows) { + return cached.data; + } + } catch { /* ignore */ } + return null; +} + +function writeScanCache(data: ScanTerminalResponse) { + try { localStorage.setItem(SCAN_CACHE_KEY, JSON.stringify({ ts: Date.now(), data })); } catch { /* ignore */ } +} + export function useScanTerminalQuery({ isPro, proAccessLoading, @@ -30,7 +49,12 @@ export function useScanTerminalQuery({ reset, run, } = useRemoteDataQuery(); + const lastForcedScanRefreshAtRef = useRef(0); + const [cachedRows, setCachedRows] = useState(() => { + if (typeof window !== "undefined") return readScanCache(); + return null; + }); const fetchScanTerminal = useCallback( async ({ @@ -56,6 +80,7 @@ export function useScanTerminalQuery({ tradingRegion, }), showLoading, + onSuccess: (data) => { writeScanCache(data); setCachedRows(data); }, }); }, [isPro, proAccessLoading, run, timezoneOffsetSeconds, tradingRegion], @@ -70,6 +95,8 @@ export function useScanTerminalQuery({ void fetchScanTerminal({ forceRefresh: false, showLoading: true }); }, [fetchScanTerminal, isPro, proAccessLoading, reset, timezoneOffsetSeconds, tradingRegion]); + const effectiveData = terminalData || cachedRows; + const refreshScanTerminalManually = useCallback(() => { if ( shouldSkipManualTerminalRefresh({ @@ -110,6 +137,6 @@ export function useScanTerminalQuery({ scanError, scanLoading, scanRemote, - terminalData, + terminalData: effectiveData, }; }