on d760cbf help sign in
bridge/src/components/Blinky.js
32 lines 820 B history raw
1import React, { useState, useRef } from 'react';
3import useInterval from 'lib/useInterval';
5// TODO: make these characters display as the same width
6export const LOADING_CHARACTER = '▓';
7export const INTERSTITIAL_CHARACTER = '░';
8const BLINK_AFTER_MS = 2500; // ms
10export const matchBlinky = obj =>
11 obj.matchWith({
12 Nothing: () => <Blinky delayed />,
13 Just: p => p.value,
14 });
16export default function Blinky({
17 a = LOADING_CHARACTER,
18 b = INTERSTITIAL_CHARACTER,
19 delayed = false,
20}) {
21 const [value, setValue] = useState(true);
22 const now = useRef(new Date());
24 useInterval(() => {
25 // only start blinking if we've elapsed enough time and want to delay
26 if (!delayed || new Date() - now.current > BLINK_AFTER_MS) {
27 setValue(val => !val);
28 }
29 }, 1000);
31 return value ? a : b;