<script lang="ts">
  import { onMount } from "svelte";
  import goalStore, { fpGoalStore } from "../stores/goal";
  import logStore from "../stores/logs";
  import projectStore, { fpProjectStore } from "../stores/project";

  let lastFocus = new Date();

  function handleFocus() {
    if (Date.now() - lastFocus.getTime() < 60000) {
      return;
    }

    lastFocus = new Date();

    goalStore.markStale();
    fpGoalStore.markStale();
    projectStore.markStale();
    fpProjectStore.markStale();
    logStore.markStale();
  }

  onMount(() => {
    window.addEventListener("focus", handleFocus);

    return () => {
      window.removeEventListener("focus", handleFocus);
    }
  })
</script>