43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
"use client";
|
|
|
|
import type { AuditFilterKey } from "@/features/groups/components/group-settings.types";
|
|
|
|
export function formatInviteExpiry(expiresAt: string) {
|
|
const expiry = new Date(expiresAt).getTime();
|
|
if (Number.isNaN(expiry)) return "Unknown";
|
|
const diffMs = expiry - Date.now();
|
|
const absMs = Math.abs(diffMs);
|
|
const minute = 60 * 1000;
|
|
const hour = 60 * minute;
|
|
const day = 24 * hour;
|
|
const pick = (value: number, unit: string) => `${value} ${unit}${value === 1 ? "" : "s"}`;
|
|
let label = "";
|
|
|
|
if (absMs < hour) {
|
|
const value = Math.max(1, Math.round(absMs / minute));
|
|
label = pick(value, "minute");
|
|
} else if (absMs < day) {
|
|
const value = Math.max(1, Math.round(absMs / hour));
|
|
label = pick(value, "hour");
|
|
} else {
|
|
const value = Math.max(1, Math.round(absMs / day));
|
|
label = pick(value, "day");
|
|
}
|
|
|
|
return diffMs >= 0 ? `in ${label}` : `${label} ago`;
|
|
}
|
|
|
|
export function isInviteExpired(expiresAt: string) {
|
|
const expiry = new Date(expiresAt).getTime();
|
|
if (Number.isNaN(expiry)) return false;
|
|
return Date.now() > expiry;
|
|
}
|
|
|
|
export function eventCategory(eventType: string): AuditFilterKey {
|
|
const upper = eventType.toUpperCase();
|
|
if (upper.includes("SPENDING") || upper.includes("ENTRY")) return "entries";
|
|
if (upper.includes("TAG")) return "tags";
|
|
if (upper.includes("SETTING") || upper.includes("RENAMED")) return "settings";
|
|
return "members";
|
|
}
|