useSyncWallet.ts raw

   1  import React from "react";
   2  
   3  import { request } from "src/utils/request";
   4  
   5  export function useSyncWallet() {
   6    const REQUEST_WALLET_SYNC_INTERVAL = 30_000; // request a wallet sync every 30s (NOTE: it won't actually sync this often)
   7  
   8    React.useEffect(() => {
   9      const intervalId = setInterval(async () => {
  10        try {
  11          await request("/api/wallet/sync", {
  12            method: "POST",
  13            headers: {
  14              "Content-Type": "application/json",
  15            },
  16          });
  17        } catch (error) {
  18          console.error("failed to request wallet sync", error);
  19        }
  20      }, REQUEST_WALLET_SYNC_INTERVAL);
  21  
  22      return () => {
  23        clearInterval(intervalId);
  24      };
  25    }, []);
  26  
  27    return null;
  28  }
  29