diff --git a/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView.tsx b/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView.tsx index 15f9b955..00f2574b 100644 --- a/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView.tsx +++ b/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView.tsx @@ -6,6 +6,7 @@ import { LatLng } from "leaflet"; import { Item } from "../../../../types"; import { toast } from "react-toastify"; import { useHasUserPermission } from "../../hooks/usePermissions"; +import { timeAgo } from "../../../../Utils/TimeAgo"; @@ -60,11 +61,12 @@ export function HeaderView({ item, title, avatar, setItemFormPopup }: { : "" } - {title ? title : item.name} + {title ? title : item.name} +
- {(item.layer?.api?.deleteItem || item.layer?.api?.updateItem) && (hasUserPermission(item.layer.api?.collectionName!, "delete") || hasUserPermission(item.layer.api?.collectionName!, "update") ) && + {(item.layer?.api?.deleteItem || item.layer?.api?.updateItem) && (hasUserPermission(item.layer.api?.collectionName!, "delete") || hasUserPermission(item.layer.api?.collectionName!, "update")) &&
diff --git a/src/Utils/TimeAgo.ts b/src/Utils/TimeAgo.ts new file mode 100644 index 00000000..6b3325ec --- /dev/null +++ b/src/Utils/TimeAgo.ts @@ -0,0 +1,36 @@ +// in miliseconds +const units = [ + { label: 'year', seconds: 31536000 }, + { label: 'month', seconds: 2592000 }, + { label: 'week', seconds: 604800 }, + { label: 'day', seconds: 86400 }, + { label: 'hour', seconds: 3600 }, + { label: 'minute', seconds: 60 }, + { label: 'second', seconds: 1 } +]; + + +export const timeAgo = (date: string | number | Date) => { + const time = Math.floor( + (new Date().valueOf() - new Date(date).valueOf()) / 1000 + ); + const { interval, unit } = calculateTimeDifference(time); + const suffix = interval === 1 ? '' : 's'; + return `${interval} ${unit}${suffix} ago`; +}; + +const calculateTimeDifference = (time: number) => { + for (let { label, seconds } of units) { + const interval = Math.floor(time / seconds); + if (interval >= 1) { + return { + interval: interval, + unit: label + }; + } + } + return { + interval: 0, + unit: '' + }; +}; diff --git a/tsconfig.json b/tsconfig.json index 18a8d60c..3fa2173c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "outDir": "dist", "module": "esnext", "target": "es5", - "lib": ["es6", "dom", "es2016", "es2017"], + "lib": ["es6", "dom", "es2016", "es2017", "es2020"], "sourceMap": true, "allowJs": false, "jsx": "react",