github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/timeFormatters.ts (about)

     1  import { Formatter, Suffix, Unit } from "react-timeago"
     2  // @ts-ignore
     3  import buildFormatter from "react-timeago/lib/formatters/buildFormatter"
     4  // @ts-ignore
     5  import enStrings from "react-timeago/lib/language-strings/en-short.js"
     6  
     7  const minutePlusFormatter = buildFormatter(enStrings)
     8  
     9  const timeAgoFormatter = (
    10    value: number,
    11    unit: Unit,
    12    suffix: Suffix,
    13    epochMilliseconds: Number,
    14    _nextFormatter?: Formatter,
    15    now?: any
    16  ) => {
    17    let str = ""
    18    if (unit === "second") {
    19      for (let threshold of [5, 15, 30, 45]) {
    20        if (value < threshold) {
    21          str = `<${threshold}s`
    22          break
    23        } else {
    24          str = "<1m"
    25        }
    26      }
    27    } else {
    28      str = minutePlusFormatter(
    29        value,
    30        unit,
    31        suffix,
    32        epochMilliseconds,
    33        _nextFormatter,
    34        now
    35      )
    36    }
    37    return `${str} ago`
    38  }
    39  
    40  export { timeAgoFormatter }