All files / applications/go-by-transit/src/component/time TimeDisplayMenu.tsx

90.47% Statements 19/21
50% Branches 3/6
88.88% Functions 8/9
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53            27x 27x 27x 27x 27x   27x 18x 18x     27x 19x 19x       27x   1x               18x 18x 18x     27x                            
import React, { useEffect, useRef, useState } from "react";
import FontAwesome from "react-fontawesome";
import { useLocation } from "react-router-dom";
import { TimeDisplaySettings, useTimeDisplay } from "@overworld/ui";
 
export default function TimeDisplayMenu() {
  const { formatTime } = useTimeDisplay();
  const { pathname } = useLocation();
  const [now, setNow] = useState(() => Date.now());
  const menuRef = useRef<HTMLDetailsElement>(null);
  const currentTime = formatTime(now);
 
  useEffect(() => {
    const timer = window.setInterval(() => setNow(Date.now()), 30_000);
    return () => window.clearInterval(timer);
  }, []);
 
  useEffect(() => {
    Eif (menuRef.current) {
      menuRef.current.open = false;
    }
  }, [pathname]);
 
  useEffect(() => {
    function closeOnOutsideClick(event: PointerEvent) {
      Iif (
        menuRef.current?.open &&
        !menuRef.current.contains(event.target as Node)
      ) {
        menuRef.current.open = false;
      }
    }
 
    document.addEventListener("pointerdown", closeOnOutsideClick);
    return () =>
      document.removeEventListener("pointerdown", closeOnOutsideClick);
  }, []);
 
  return (
    <details className="transit-time-menu" ref={menuRef}>
      <summary
        aria-label={`Time display settings, current time ${currentTime}`}
      >
        <FontAwesome name="clock-o" aria-hidden="true" />
        <time dateTime={new Date(now).toISOString()}>{currentTime}</time>
      </summary>
      <div className="transit-time-menu-popover">
        <TimeDisplaySettings />
      </div>
    </details>
  );
}