diff --git a/src/routes/heatmap/+page.svelte b/src/routes/heatmap/+page.svelte
index 33345d36..eee590f9 100644
--- a/src/routes/heatmap/+page.svelte
+++ b/src/routes/heatmap/+page.svelte
@@ -4,11 +4,13 @@
import SEO from "$lib/components/SEO.svelte";
import { setCache, getCache } from "$lib/store";
import { onDestroy } from "svelte";
+ import toast from "svelte-french-toast";
export let data;
+ let isLoaded = false;
+
let rawData = data?.getData;
let iframe: HTMLIFrameElement;
- let iframeLoaded = false;
let selectedFormat: "png" | "jpeg" | "svg" = "png";
let selectedTimePeriod = "1D";
let iframeUrl: string;
@@ -36,47 +38,51 @@
}
async function downloadPlot(item) {
- if (item === "PNG") {
- selectedFormat = "png";
- } else if (item === "JPG") {
- selectedFormat = "jpeg";
- } else {
- selectedFormat = "svg";
- }
+ return toast.promise(
+ (async () => {
+ let selectedFormat;
- if (!iframe || !iframeLoaded) return;
+ if (item === "PNG") {
+ selectedFormat = "png";
+ } else if (item === "JPG") {
+ selectedFormat = "jpeg";
+ } else {
+ selectedFormat = "svg";
+ }
- try {
- const iframeWindow = iframe.contentWindow;
- if (!iframeWindow) return;
+ if (!iframe || !isLoaded) throw new Error("Iframe not ready");
- const Plotly = (iframeWindow as any).Plotly;
- if (!Plotly) throw new Error("Plotly not found in iframe");
+ const iframeWindow = iframe.contentWindow;
+ if (!iframeWindow) throw new Error("Iframe window not available");
- const plotDiv =
- iframe.contentDocument?.querySelector(".plotly-graph-div");
- if (!plotDiv) throw new Error("Plotly div not found");
+ const Plotly = iframeWindow.Plotly;
+ if (!Plotly) throw new Error("Plotly not found in iframe");
- // Configure image options based on format
- const options = {
- format: selectedFormat,
- width: 1200,
- height: 800,
- };
+ const plotDiv =
+ iframe.contentDocument?.querySelector(".plotly-graph-div");
+ if (!plotDiv) throw new Error("Plotly div not found");
- // Get image data URL
- const imageData = await Plotly.toImage(plotDiv, options);
+ const options = {
+ format: selectedFormat,
+ width: 1200,
+ height: 800,
+ };
- // Create download link
- const link = document.createElement("a");
- link.href = imageData;
- link.download = `sp500-heatmap-${selectedTimePeriod}`;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- } catch (error) {
- console.error("Download failed:", error);
- }
+ const imageData = await Plotly.toImage(plotDiv, options);
+
+ const link = document.createElement("a");
+ link.href = imageData;
+ link.download = `sp500-heatmap-${selectedTimePeriod}`;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ })(),
+ {
+ loading: "Downloading heatmap...",
+ success: "Heatmap downloaded!",
+ error: "Download failed. Try again.",
+ },
+ );
}
onDestroy(() => {
@@ -86,16 +92,14 @@
$: {
if (selectedTimePeriod && typeof window !== "undefined") {
(async () => {
- await getHeatMap();
+ isLoaded = false;
+ getHeatMap();
+ isLoaded = true;
})();
}
}
-
-
-
-