# typed: false # frozen_string_literal: true module Ariadne module UI module TimeAgo # Formats a timestamp as a localized string or as relative text that auto-updates in the user's browser. class Component < Ariadne::BaseComponent include FetchOrFallbackHelper TENSE_DEFAULT = :auto TENSE_OPTIONS = [TENSE_DEFAULT, :past, :future].freeze FORMAT_DEFAULT = :auto FORMAT_OPTIONS = [FORMAT_DEFAULT, :micro, :elapsed].freeze FORMAT_STYLE_DEFAULT = nil FORMAT_STYLE_OPTIONS = [FORMAT_STYLE_DEFAULT, :long, :short, :narrow].freeze SECOND_DEFAULT = nil SECOND_MAPPINGS = { SECOND_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", }.freeze SECOND_OPTIONS = SECOND_MAPPINGS.keys MINUTE_DEFAULT = nil MINUTE_MAPPINGS = { MINUTE_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", }.freeze MINUTE_OPTIONS = MINUTE_MAPPINGS.keys HOUR_DEFAULT = nil HOUR_MAPPINGS = { HOUR_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", }.freeze HOUR_OPTIONS = HOUR_MAPPINGS.keys WEEKDAY_DEFAULT = nil WEEKDAY_OPTIONS = [WEEKDAY_DEFAULT, :long, :short, :narrow].freeze DAY_DEFAULT = nil DAY_MAPPINGS = { DAY_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", }.freeze DAY_OPTIONS = DAY_MAPPINGS.keys MONTH_DEFAULT = nil MONTH_MAPPINGS = { MONTH_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", :short => "short", :long => "long", :narrow => "narrow", }.freeze MONTH_OPTIONS = MONTH_MAPPINGS.keys YEAR_DEFAULT = nil YEAR_MAPPINGS = { YEAR_DEFAULT => nil, :numeric => "numeric", :two_digit => "2-digit", }.freeze YEAR_OPTIONS = YEAR_MAPPINGS.keys TIMEZONENAME_DEFAULT = nil TIMEZONE_MAPPINGS = { TIMEZONENAME_DEFAULT => nil, :long => "long", :short => "short", :short_offset => "shortOffset", :long_offset => "longOffset", :short_generic => "shortGeneric", :long_generic => "longGeneric", }.freeze TIMEZONENAME_OPTIONS = TIMEZONE_MAPPINGS.keys PRECISION_DEFAULT = nil PRECISION_OPTIONS = [PRECISION_DEFAULT, :second, :minute, :hour, :day, :month, :year].freeze # @param datetime [Time] The time to be formatted. option :datetime # @param tense [Symbol] Which tense to use. <%= one_of(Primer::Beta::RelativeTime::TENSE_OPTIONS) %> option :tense, default: -> { TENSE_DEFAULT } # @param prefix [sring] What to prefix the relative time display with. option :prefix, default: -> { nil } # @param second [Symbol] What format seconds should take. <%= one_of(Primer::Beta::RelativeTime::SECOND_OPTIONS) %> option :second, default: -> { SECOND_DEFAULT } # @param minute [Symbol] What format minues should take. <%= one_of(Primer::Beta::RelativeTime::MINUTE_OPTIONS) %> option :minute, default: -> { MINUTE_DEFAULT } # @param hour [Symbol] What format hours should take. <%= one_of(Primer::Beta::RelativeTime::HOUR_OPTIONS) %> option :hour, default: -> { HOUR_DEFAULT } # @param weekday [Symbol] What format weekdays should take. <%= one_of(Primer::Beta::RelativeTime::WEEKDAY_OPTIONS) %> option :weekday, default: -> { WEEKDAY_DEFAULT } # @param day [Symbol] What format days should take. <%= one_of(Primer::Beta::RelativeTime::DAY_OPTIONS) %> option :day, default: -> { DAY_DEFAULT } # @param month [Symbol] What format months should take. <%= one_of(Primer::Beta::RelativeTime::MONTH_OPTIONS) %> option :month, default: -> { MONTH_DEFAULT } # @param year [Symbol] What format years should take. <%= one_of(Primer::Beta::RelativeTime::YEAR_OPTIONS) %> option :year, default: -> { YEAR_DEFAULT } # @param time_zone_name [Symbol] What format the time zone should take. <%= one_of(Primer::Beta::RelativeTime::TIMEZONENAME_OPTIONS) %> option :time_zone_name, default: -> { TIMEZONENAME_DEFAULT } # @param threshold [String] The threshold, in ISO-8601 'durations' format, at which relative time displays become absolute. option :threshold, default: -> { nil } # @param precision [Symbol] The precision elapsed time should display. <%= one_of(Primer::Beta::RelativeTime::PRECISION_OPTIONS) %> option :precision, default: -> { PRECISION_DEFAULT } # @param format [Symbol] The format the display should take. <%= one_of(Primer::Beta::RelativeTime::FORMAT_OPTIONS) %> option :format, default: -> { FORMAT_DEFAULT } # @param format_style [Symbol] The format the display should take. <%= one_of(Primer::Beta::RelativeTime::FORMAT_STYLE_OPTIONS) %> option :format_style, default: -> { FORMAT_STYLE_DEFAULT } # @param lang [String] The language to use. option :lang, default: -> { nil } # @param title [String] Provide a custom title to the element. option :title, default: -> { nil } # @param no_title [Boolean] Removes the `title` attribute provided on the element by default. option :no_title, default: -> { false } accepts_html_attributes do |html_attrs| html_attrs[:tense] = tense if tense.present? html_attrs[:prefix] = prefix if prefix.present? html_attrs[:second] = fetch_or_fallback(SECOND_OPTIONS, second, SECOND_DEFAULT) if second.present? html_attrs[:minute] = fetch_or_fallback(MINUTE_OPTIONS, minute, MINUTE_DEFAULT) if minute.present? html_attrs[:hour] = fetch_or_fallback(HOUR_OPTIONS, hour, HOUR_DEFAULT) if hour.present? html_attrs[:weekday] = fetch_or_fallback(WEEKDAY_OPTIONS, weekday, WEEKDAY_DEFAULT) if weekday.present? html_attrs[:day] = fetch_or_fallback(DAY_OPTIONS, day, DAY_DEFAULT) if day.present? html_attrs[:month] = fetch_or_fallback(MONTH_OPTIONS, month, MONTH_DEFAULT) if month.present? html_attrs[:year] = fetch_or_fallback(YEAR_OPTIONS, year, YEAR_DEFAULT) if year.present? html_attrs[:"time-zone-name"] = fetch_or_fallback(TIMEZONENAME_OPTIONS, time_zone_name, TIMEZONENAME_DEFAULT) if time_zone_name.present? html_attrs[:threshold] = threshold if threshold.present? html_attrs[:precision] = precision if precision.present? html_attrs[:title] = title if title.present? html_attrs[:"no-title"] = no_title if no_title html_attrs[:lang] = lang if lang.present? html_attrs[:format] = fetch_or_fallback(FORMAT_OPTIONS, format, FORMAT_DEFAULT) if format.present? html_attrs[:"format-style"] = format_style if format_style.present? if datetime.respond_to?(:iso8601) @datetime = datetime html_attrs[:datetime] = datetime.iso8601 elsif datetime.present? @datetime = Time.iso8601(datetime) html_attrs[:datetime] = @datetime end end end end end end