All files / presentation/ui/src TimeDisplay.tsx

87.23% Statements 41/47
91.42% Branches 32/35
75% Functions 12/16
87.23% Lines 41/47

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                  525x 5x   520x         525x 105x   520x 34x 486x 241x   520x               258x     258x         258x 1x 257x 257x   258x               7x     7x         7x 1x 6x 1x   7x                                   67x             471x   256x   5x     67x             5x 5x 5x   5x 5x         1x 1x     1x 1x     36x                 5x               604x    
import React, { createContext, useContext, useMemo, useState } from "react";
import {
  fetchStoredTimeFormat,
  fetchStoredTimezone,
  storeTimeFormat,
  storeTimezone,
  TimeFormat,
  TimezonePreference
} from "./timePreferences";
 
export function formatDisplayTime(
  timestamp: number,
  format: TimeFormat,
  timezone: TimezonePreference,
  defaultTimeZone?: string,
  seconds = false
): string {
  if (!Number.isFinite(timestamp) || timestamp <= 0) {
    return "Time unavailable";
  }
  const options: Intl.DateTimeFormatOptions = {
    hour: "numeric",
    minute: "2-digit",
    ...(format === "12h" ? { hour12: true } : { hourCycle: "h23" })
  };
  if (seconds) {
    options.second = "2-digit";
  }
  if (timezone === "utc") {
    options.timeZone = "UTC";
  } else if (timezone === "default" && defaultTimeZone) {
    options.timeZone = defaultTimeZone;
  }
  return new Intl.DateTimeFormat("en-US", options).format(timestamp);
}
 
export function formatDisplayDay(
  timestamp: number,
  timezone: TimezonePreference,
  defaultTimeZone?: string
): string {
  Iif (!Number.isFinite(timestamp) || timestamp <= 0) {
    return "Date unavailable";
  }
  const options: Intl.DateTimeFormatOptions = {
    weekday: "short",
    month: "short",
    day: "numeric"
  };
  if (timezone === "utc") {
    options.timeZone = "UTC";
  } else Eif (timezone === "default" && defaultTimeZone) {
    options.timeZone = defaultTimeZone;
  }
  return new Intl.DateTimeFormat("en-US", options).format(timestamp);
}
 
export function formatDisplayDate(
  timestamp: number,
  timezone: TimezonePreference,
  defaultTimeZone?: string
): string {
  Iif (!Number.isFinite(timestamp) || timestamp <= 0) {
    return "Date unavailable";
  }
  const options: Intl.DateTimeFormatOptions = {
    month: "2-digit",
    day: "2-digit",
    year: "2-digit"
  };
  if (timezone === "utc") {
    options.timeZone = "UTC";
  } else if (timezone === "default" && defaultTimeZone) {
    options.timeZone = defaultTimeZone;
  }
  return new Intl.DateTimeFormat("en-US", options).format(timestamp);
}
 
export interface TimeDisplayContextValue {
  format: TimeFormat;
  timezone: TimezonePreference;
  storageError: boolean;
  setFormat: (format: TimeFormat) => void;
  setTimezone: (timezone: TimezonePreference) => void;
  formatTime: (
    timestamp: number,
    defaultTimeZone?: string,
    seconds?: boolean
  ) => string;
  formatDay: (timestamp: number, defaultTimeZone?: string) => string;
  formatDate: (timestamp: number, defaultTimeZone?: string) => string;
}
 
const defaultValue: TimeDisplayContextValue = {
  format: "12h",
  timezone: "default",
  storageError: false,
  setFormat: () => undefined,
  setTimezone: () => undefined,
  formatTime: (timestamp, defaultTimeZone, seconds) =>
    formatDisplayTime(timestamp, "12h", "default", defaultTimeZone, seconds),
  formatDay: (timestamp, defaultTimeZone) =>
    formatDisplayDay(timestamp, "default", defaultTimeZone),
  formatDate: (timestamp, defaultTimeZone) =>
    formatDisplayDate(timestamp, "default", defaultTimeZone)
};
 
const TimeDisplayContext = createContext(defaultValue);
 
export function TimeDisplayProvider({
  children
}: {
  children: React.ReactNode;
}) {
  const [format, setFormatState] = useState(fetchStoredTimeFormat);
  const [timezone, setTimezoneState] = useState(fetchStoredTimezone);
  const [storageError, setStorageError] = useState(false);
 
  const value = useMemo<TimeDisplayContextValue>(
    () => ({
      format,
      timezone,
      storageError,
      setFormat: next => {
        setFormatState(next);
        setStorageError(!storeTimeFormat(next));
      },
      setTimezone: next => {
        setTimezoneState(next);
        setStorageError(!storeTimezone(next));
      },
      formatTime: (timestamp, defaultTimeZone, seconds) =>
        formatDisplayTime(timestamp, format, timezone, defaultTimeZone, seconds),
      formatDay: (timestamp, defaultTimeZone) =>
        formatDisplayDay(timestamp, timezone, defaultTimeZone),
      formatDate: (timestamp, defaultTimeZone) =>
        formatDisplayDate(timestamp, timezone, defaultTimeZone)
    }),
    [format, timezone, storageError]
  );
 
  return (
    <TimeDisplayContext.Provider value={value}>
      {children}
    </TimeDisplayContext.Provider>
  );
}
 
export function useTimeDisplay(): TimeDisplayContextValue {
  return useContext(TimeDisplayContext);
}