update frontend

This commit is contained in:
Parsa Nazer
2026-05-28 10:32:03 +03:30
parent 481452eea7
commit 35864e61dd
14 changed files with 473 additions and 35 deletions
+15 -2
View File
@@ -1,11 +1,24 @@
// composables/usePersianDate.ts
import { format, toDate } from "date-fns-jalali";
import { format } from "date-fns-jalali";
import { faIR } from "date-fns-jalali/locale";
import { Jalali } from "jalali-ts";
export default function usePersianDate() {
const formatToPersian = (isoDate: string): string => {
try {
const date = toDate(new Date(isoDate));
const yearStr = isoDate.slice(0, 4);
const year = Number(yearStr);
// jDateTimeField from django_jalali sends Jalali years (13XX/14XX).
// Normal DateTimeField sends Gregorian (19XX/20XX). Detect by year
// and use jalali-ts to convert when needed — date-fns-jalali's
// parseISO doesn't actually do calendar conversion.
const date =
!Number.isNaN(year) && year < 1700
? Jalali.parse(isoDate).date
: new Date(isoDate);
if (isNaN(date.getTime())) return "Invalid date";
const persianDate = format(date, "yyyy/MM/dd", { locale: faIR });
@@ -1,12 +1,44 @@
// composables/usePersianTimeAgo.ts
import { formatDistance, toDate } from "date-fns-jalali";
import { formatDistance } from "date-fns-jalali";
import { faIR } from "date-fns-jalali/locale";
import { Jalali } from "jalali-ts";
export function usePersianTimeAgo(date: Date) {
const toGregorianDate = (input: Date | string): Date | null => {
if (input instanceof Date) {
return isNaN(input.getTime()) ? null : input;
}
if (typeof input !== "string" || !input) return null;
const yearStr = input.slice(0, 4);
const year = Number(yearStr);
// jDateTimeField from django_jalali serializes with the Jalali year
// (typically 13XX or 14XX). Anything below ~1700 is treated as Jalali
// and converted to the equivalent Gregorian moment via jalali-ts.
// date-fns-jalali's parseISO can't be used here — it parses the year
// numerically without calendar conversion.
if (!Number.isNaN(year) && year < 1700) {
try {
return Jalali.parse(input).date;
} catch {
return null;
}
}
const native = new Date(input);
return isNaN(native.getTime()) ? null : native;
};
export function usePersianTimeAgo(date: Date | string) {
const timeAgo = ref("");
const updateTimeAgo = () => {
timeAgo.value = formatDistance(toDate(date), new Date(), {
const parsed = toGregorianDate(date);
if (!parsed) {
timeAgo.value = "";
return;
}
timeAgo.value = formatDistance(parsed, new Date(), {
addSuffix: true,
locale: faIR,
});