From 2b76e32d5714fc410085b5a5ab5c1f4333190fe6 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 7 Mar 2024 09:34:06 -0300 Subject: time component --- .../demobank-ui/src/components/Cashouts/views.tsx | 60 +++++++++--------- packages/demobank-ui/src/components/Time.tsx | 71 ++++++++++++++++++++++ .../src/components/Transactions/views.tsx | 15 ++--- 3 files changed, 109 insertions(+), 37 deletions(-) create mode 100644 packages/demobank-ui/src/components/Time.tsx (limited to 'packages/demobank-ui/src/components') diff --git a/packages/demobank-ui/src/components/Cashouts/views.tsx b/packages/demobank-ui/src/components/Cashouts/views.tsx index 90ee6bc2f..09e986dd4 100644 --- a/packages/demobank-ui/src/components/Cashouts/views.tsx +++ b/packages/demobank-ui/src/components/Cashouts/views.tsx @@ -15,7 +15,9 @@ */ import { + AbsoluteTime, Amounts, + Duration, HttpStatusCode, TalerError, assertUnreachable, @@ -31,6 +33,7 @@ import { useConversionInfo } from "../../hooks/circuit.js"; import { RenderAmount } from "../../pages/PaytoWireTransferForm.js"; import { ErrorLoadingWithDebug } from "../ErrorLoadingWithDebug.js"; import { State } from "./index.js"; +import { Time } from "../Time.js"; export function FailedView({ error }: State.Failed) { const { i18n } = useTranslationContext(); @@ -141,12 +144,6 @@ export function ReadyView({ {txs.map((item) => { - const creationTime = - item.creation_time.t_s === "never" - ? "" - : format(item.creation_time.t_s * 1000, "HH:mm:ss", { - locale: dateLocale, - }); return ( - -
- {creationTime} -
- { - //FIXME: implement responsive view - } - {/*
+ +
+
+ { + //FIXME: implement responsive view + } + {/*
Amount
{item.negative ? i18n.str`sent` : i18n.str`received`} {item.amount ? ( @@ -185,24 +185,24 @@ export function ReadyView({
*/} - - - - - - - + + + + + + + - + - {item.subject} - + {item.subject} +
); })} diff --git a/packages/demobank-ui/src/components/Time.tsx b/packages/demobank-ui/src/components/Time.tsx new file mode 100644 index 000000000..39ce33f60 --- /dev/null +++ b/packages/demobank-ui/src/components/Time.tsx @@ -0,0 +1,71 @@ +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +import { AbsoluteTime, Duration } from "@gnu-taler/taler-util"; +import { useTranslationContext } from "@gnu-taler/web-util/browser"; +import { formatISO, format, formatDuration, intervalToDuration } from "date-fns"; +import { Fragment, h, VNode } from "preact"; + +/** + * + * @param timestamp time to be formatted + * @param relative duration threshold, if the difference is lower + * the timestamp will be formatted as relative time from "now" + * + * @returns + */ +export function Time({ + timestamp, + relative, + format: formatString, +}: { + timestamp: AbsoluteTime | undefined; + relative?: Duration, + format: string; +}): VNode { + const { i18n, dateLocale } = useTranslationContext() + if (!timestamp) return + + if (timestamp.t_ms === "never") { + return + } + + const now = AbsoluteTime.now(); + const diff = AbsoluteTime.difference(now, timestamp) + if (relative && now.t_ms !== "never" && Duration.cmp(diff, relative) === -1) { + const d = intervalToDuration({ + start: now.t_ms, + end: timestamp.t_ms + }) + d.seconds = 0 + const duration = formatDuration(d, { locale: dateLocale }) + const isFuture = AbsoluteTime.cmp(now, timestamp) < 0 + if (isFuture) { + return + } else { + return + } + } + return ( + + ); +} diff --git a/packages/demobank-ui/src/components/Transactions/views.tsx b/packages/demobank-ui/src/components/Transactions/views.tsx index cdf134b2f..7da9fc5a9 100644 --- a/packages/demobank-ui/src/components/Transactions/views.tsx +++ b/packages/demobank-ui/src/components/Transactions/views.tsx @@ -20,6 +20,8 @@ import { Fragment, VNode, h } from "preact"; import { useBankCoreApiContext } from "../../context/config.js"; import { RenderAmount } from "../../pages/PaytoWireTransferForm.js"; import { State } from "./index.js"; +import { Duration } from "@gnu-taler/taler-util"; +import { Time } from "../Time.js"; export function ReadyView({ transactions, @@ -107,19 +109,18 @@ export function ReadyView({ {txs.map((item) => { - const time = - item.when.t_ms === "never" - ? "" - : format(item.when.t_ms, "HH:mm:ss", { - locale: dateLocale, - }); return ( -
{time}
+
+
Amount -- cgit v1.2.3