Replace `catch (e: any)` with `catch (e)` and read the message through a new errorMessage() helper. ApiError.data gets a real type, and useDismissingState's setter keeps one identity across renders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { clsx, type ClassValue } from "clsx"
|
|
import { twMerge } from "tailwind-merge"
|
|
import { format, isValid } from "date-fns"
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
export function formatDate(date: Date): string {
|
|
if (!isValid(date)) {
|
|
return "Invalid date"
|
|
}
|
|
return format(date, "MMMM d, yyyy")
|
|
}
|
|
|
|
export function formatCurrency(amount: number): string {
|
|
return new Intl.NumberFormat("en-ZA", {
|
|
style: "currency",
|
|
currency: "ZAR",
|
|
minimumFractionDigits: 2,
|
|
}).format(amount)
|
|
}
|
|
|
|
export function truncateText(text: string, maxLength: number): string {
|
|
if (text.length <= maxLength) return text
|
|
return `${text.slice(0, maxLength)}...`
|
|
}
|
|
|
|
// Message from a caught value (an Error, an ApiError, or anything thrown), or undefined when
|
|
// it has none — so callers can write `errorMessage(e) || "Fallback text"`.
|
|
export function errorMessage(e: unknown): string | undefined {
|
|
if (e instanceof Error) return e.message || undefined
|
|
if (typeof e === "object" && e !== null && "message" in e && typeof e.message === "string") return e.message || undefined
|
|
return undefined
|
|
}
|