Echodelta06 commited on
Commit
6a672fe
·
1 Parent(s): 41c8461

Update: amelioration du refresh

Browse files
frontend/app/providers.tsx CHANGED
@@ -2,11 +2,13 @@
2
 
3
  import { AnalysisProvider } from "@/lib/analysis-context";
4
  import { LanguageProvider } from "@/lib/language-context";
 
5
 
6
  export function Providers({ children }: { children: React.ReactNode }) {
7
  return (
8
  <LanguageProvider>
9
  <AnalysisProvider>
 
10
  {children}
11
  </AnalysisProvider>
12
  </LanguageProvider>
 
2
 
3
  import { AnalysisProvider } from "@/lib/analysis-context";
4
  import { LanguageProvider } from "@/lib/language-context";
5
+ import { RefreshResetGuard } from "@/components/refresh-reset-guard";
6
 
7
  export function Providers({ children }: { children: React.ReactNode }) {
8
  return (
9
  <LanguageProvider>
10
  <AnalysisProvider>
11
+ <RefreshResetGuard />
12
  {children}
13
  </AnalysisProvider>
14
  </LanguageProvider>
frontend/components/refresh-reset-guard.tsx ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use client";
2
+
3
+ import { useEffect, useRef } from "react";
4
+
5
+ import { useAnalysis } from "@/lib/analysis-context";
6
+ import { clearValuationSnapshot } from "@/lib/valuation-cache";
7
+
8
+ function isHardReload(): boolean {
9
+ if (typeof window === "undefined") return false;
10
+ const navEntry = performance.getEntriesByType("navigation")[0] as PerformanceNavigationTiming | undefined;
11
+ if (navEntry?.type === "reload") return true;
12
+ // Fallback legacy browsers.
13
+ // eslint-disable-next-line deprecation/deprecation
14
+ return typeof performance !== "undefined" && typeof performance.navigation !== "undefined" && performance.navigation.type === 1;
15
+ }
16
+
17
+ export function RefreshResetGuard() {
18
+ const { clearAnalysis } = useAnalysis();
19
+ const handledRef = useRef(false);
20
+
21
+ useEffect(() => {
22
+ if (handledRef.current) return;
23
+ handledRef.current = true;
24
+ if (!isHardReload()) return;
25
+
26
+ clearAnalysis();
27
+ clearValuationSnapshot();
28
+
29
+ // Au refresh, retour systematique a la page d'entree.
30
+ if (window.location.pathname !== "/") {
31
+ window.location.replace("/");
32
+ }
33
+ }, [clearAnalysis]);
34
+
35
+ return null;
36
+ }
frontend/lib/valuation-cache.ts CHANGED
@@ -47,3 +47,12 @@ export function readValuationSnapshot(): ValuationSnapshot | null {
47
  return null;
48
  }
49
  }
 
 
 
 
 
 
 
 
 
 
47
  return null;
48
  }
49
  }
50
+
51
+ export function clearValuationSnapshot(): void {
52
+ if (typeof window === "undefined") return;
53
+ try {
54
+ window.localStorage.removeItem(STORAGE_KEY);
55
+ } catch {
56
+ // no-op
57
+ }
58
+ }