From 31736ccc783ab5c27dae2861091dce3327bfb08b Mon Sep 17 00:00:00 2001 From: RKWojs Date: Thu, 2 Oct 2025 10:01:54 +0200 Subject: [PATCH] feat: Enhance FieldWithHistory component with improved tooltip display and time formatting --- src/components/FieldWithHistory.js | 129 +++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 26 deletions(-) diff --git a/src/components/FieldWithHistory.js b/src/components/FieldWithHistory.js index 20b77d9..4d688da 100644 --- a/src/components/FieldWithHistory.js +++ b/src/components/FieldWithHistory.js @@ -4,6 +4,8 @@ import { useState, useEffect } from "react"; import Tooltip from "@/components/ui/Tooltip"; import { formatDate } from "@/lib/utils"; import { useTranslation } from "@/lib/i18n"; +import { formatDistanceToNow } from "date-fns"; +import { pl, enUS } from "date-fns/locale"; export default function FieldWithHistory({ tableName, @@ -14,11 +16,14 @@ export default function FieldWithHistory({ label = null, className = "", }) { - const { t } = useTranslation(); + const { t, language } = useTranslation(); const [hasHistory, setHasHistory] = useState(false); const [history, setHistory] = useState([]); const [loading, setLoading] = useState(true); + // Get locale for date-fns + const locale = language === 'pl' ? pl : enUS; + useEffect(() => { const fetchHistory = async () => { try { @@ -59,33 +64,95 @@ export default function FieldWithHistory({ // Create tooltip content const tooltipContent = history.length > 0 && ( -
-
{t("common.changeHistory")}:
+
+
+ + + + {t("common.changeHistory")} ({history.length}) +
{history.map((change, index) => ( -
-
+
+
-
- {t("common.changedTo")}: {getDisplayValue(change.new_value)} + {/* New Value */} +
+ + + +
+ + {t("common.changedTo")} + + + {getDisplayValue(change.new_value)} + +
+ + {/* Old Value */} {change.old_value && ( -
- {t("common.from")}: {getDisplayValue(change.old_value)} +
+ + + +
+ + {t("common.from")} + + + {getDisplayValue(change.old_value)} + +
)} + + {/* Changed By */} {change.changed_by_name && ( -
- {t("common.by")} {change.changed_by_name} +
+ + + + + {t("common.by")} {change.changed_by_name} +
)}
-
- {formatDate(change.changed_at)} + + {/* Timestamp */} +
+
+ {formatDistanceToNow(new Date(change.changed_at), { + addSuffix: true, + locale: locale + })} +
+
+ {formatDate(change.changed_at)} +
+ + {/* Change Reason */} {change.change_reason && ( -
- {t("common.reason")}: {change.change_reason} +
+
+ + + +
+ + {t("common.reason")} + + + {change.change_reason} + +
+
)}
@@ -109,17 +176,27 @@ export default function FieldWithHistory({

{getDisplayValue(currentValue)}

{hasHistory && ( - - - +
+ {/* History Icon with Badge */} +
+ + + + + {history.length} + +
+
)}