File size: 634 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React, { useEffect, useState } from "react";
import { Text } from "ink";

export function HeaderSwr({ fetcher, interval = 5000, render, initial = null }) {
  const [data, setData] = useState(initial);

  useEffect(() => {
    let cancelled = false;
    async function tick() {
      try {
        const next = await fetcher();
        if (!cancelled) setData(next);
      } catch {}
    }
    tick();
    const id = setInterval(tick, interval);
    return () => {
      cancelled = true;
      clearInterval(id);
    };
  }, [fetcher, interval]);

  if (!data) return <Text dimColor>Loading…</Text>;
  return render(data);
}