Fix showing undefined in chips if there's no value
[arvados-workbench2.git] / src / common / formatters.ts
index 49e0690515e868a4b1145c79ccbd2ede9d813314..5383c66e949f59ee1b2d1258f0d2d1402f485bdb 100644 (file)
@@ -2,10 +2,15 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export const formatDate = (isoDate: string) => {
-    const date = new Date(isoDate);
-    const text = date.toLocaleString();
-    return text === 'Invalid Date' ? "" : text;
+import { PropertyValue } from "~/models/search-bar";
+
+export const formatDate = (isoDate?: string) => {
+    if (isoDate) {
+        const date = new Date(isoDate);
+        const text = date.toLocaleString();
+        return text === 'Invalid Date' ? "" : text;
+    }
+    return "";
 };
 
 export const formatFileSize = (size?: number) => {
@@ -19,6 +24,17 @@ export const formatFileSize = (size?: number) => {
     return "";
 };
 
+export const formatTime = (time: number) => {
+    const minutes = Math.floor(time / (1000 * 60) % 60).toFixed(0);
+    const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
+
+    return hours + "h " + minutes + "m";
+};
+
+export const getTimeDiff = (endTime: string, startTime: string) => {
+    return new Date(endTime).getTime() - new Date(startTime).getTime();
+};
+
 export const formatProgress = (loaded: number, total: number) => {
     const progress = loaded >= 0 && total > 0 ? loaded * 100 / total : 0;
     return `${progress.toFixed(2)}%`;
@@ -53,3 +69,12 @@ const FILE_SIZES = [
         unit: "B"
     }
 ];
+
+export const formatPropertyValue = (pv: PropertyValue) => {
+    if (pv.key) {
+        return pv.value
+            ? `${pv.key}: ${pv.value}`
+            : pv.key;
+    }
+    return "";
+};