74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
/**
|
|
* Utility Functions
|
|
*
|
|
* Common helper functions for the application including:
|
|
* - UI styling utilities (shadcn)
|
|
* - Date formatting for notices
|
|
* - PocketBase file URL resolution
|
|
*/
|
|
|
|
/* Shadcn */
|
|
import { twMerge } from "tailwind-merge";
|
|
import { clsx, type ClassValue } from "clsx";
|
|
import type { RecordModel } from "pocketbase";
|
|
import { pb } from "./pocketbase";
|
|
|
|
/* Combines Tailwind classes with proper conflict resolution */
|
|
const cn = (...inputs: ClassValue[]) => twMerge(clsx(inputs));
|
|
export { cn };
|
|
|
|
/**
|
|
* Formats PocketBase created timestamp to human-readable Bosnian format
|
|
* Examples: "Danas, 9:30", "Juče, 15:15", "18. Okt, 10:45"
|
|
*
|
|
* @param dateStr ISO 8601 datetime string from PocketBase
|
|
* @returns Formatted date string in Bosnian
|
|
*/
|
|
export function formatPostedDate(dateStr: string): string {
|
|
const date = new Date(dateStr);
|
|
const now = new Date();
|
|
const diffMs = now.getTime() - date.getTime();
|
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
|
|
const timeStr = date.toLocaleTimeString("bs-BA", {
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
});
|
|
|
|
if (diffDays === 0) {
|
|
return `Danas, ${timeStr}`;
|
|
}
|
|
|
|
if (diffDays === 1) {
|
|
return `Juče, ${timeStr}`;
|
|
}
|
|
|
|
const dateStr2 = date.toLocaleDateString("bs-BA", {
|
|
day: "numeric",
|
|
month: "short",
|
|
});
|
|
|
|
return `${dateStr2}, ${timeStr}`;
|
|
}
|
|
|
|
/**
|
|
* Checks if a notice has expired
|
|
*
|
|
* @param expiresAt ISO 8601 datetime string
|
|
* @returns true if the expiry date has passed, false otherwise
|
|
*/
|
|
export function isExpired(expiresAt: string): boolean {
|
|
return new Date() > new Date(expiresAt);
|
|
}
|
|
|
|
/**
|
|
* Generates PocketBase file URL for a record's file field
|
|
*
|
|
* @param record PocketBase record containing the file
|
|
* @param filename Name of the file field
|
|
* @returns Full URL to the file, or empty string if no file
|
|
*/
|
|
export function getFileUrl(record: RecordModel, filename: string): string {
|
|
return filename ? pb.files.getURL(record, filename) : "";
|
|
}
|