From 069e595bea97313484ef71f26e99e752c4cfacf4 Mon Sep 17 00:00:00 2001 From: Mamalizz Date: Fri, 28 Feb 2025 23:12:23 +0330 Subject: [PATCH] added usePersianTimeAgo --- .../composables/global/usePersianTimeAgo.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 frontend/composables/global/usePersianTimeAgo.ts diff --git a/frontend/composables/global/usePersianTimeAgo.ts b/frontend/composables/global/usePersianTimeAgo.ts new file mode 100644 index 0000000..f22405b --- /dev/null +++ b/frontend/composables/global/usePersianTimeAgo.ts @@ -0,0 +1,25 @@ +// composables/usePersianTimeAgo.ts +import { ref, onMounted, onUnmounted } from "vue"; +import { formatDistance, toDate } from "date-fns-jalali"; +import { faIR } from "date-fns-jalali/locale"; + +export function usePersianTimeAgo(date: Date) { + const timeAgo = ref(""); + + const updateTimeAgo = () => { + timeAgo.value = formatDistance(toDate(date), new Date(), { + addSuffix: true, + locale: faIR, + }); + }; + + onMounted(() => { + updateTimeAgo(); + + const interval = setInterval(updateTimeAgo, 60000); + + onUnmounted(() => clearInterval(interval)); + }); + + return timeAgo; +}