dressed_for_succes_store/frontend/lib/utils.ts

68 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import { PUBLIC_BASE_URL } from "./api"
import { normalizeProductImage } from "./catalog"
import { apiStatus } from "./api"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
// Функции для форматирования
// Форматировать дату
export function formatDate(dateString: string): string {
if (!dateString) return 'Не указано';
try {
const date = new Date(dateString);
// Проверка на валидность даты
if (isNaN(date.getTime())) {
return 'Некорректная дата';
}
return new Intl.DateTimeFormat('ru-RU', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
}).format(date);
} catch (error) {
console.error('Ошибка при форматировании даты:', error);
return 'Ошибка форматирования';
}
}
// Форматировать цену
export function formatPrice(price: number | null | undefined): string {
if (price === null || price === undefined) {
return '0 ₽';
}
try {
return price.toLocaleString('ru-RU', {
style: 'currency',
currency: 'RUB',
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
} catch (error) {
console.error('Ошибка при форматировании цены:', error);
return `${price}`;
}
}
// Алиас для обратной совместимости
export const formatCurrency = formatPrice;
// Получить правильный URL изображения с журналированием
export const getProperImageUrl = (imageUrl: string | null | undefined): string => {
const normalizedUrl = normalizeProductImage(imageUrl);
if (apiStatus.debugMode) {
// console.log(`Обработка URL изображения: ${imageUrl} -> ${normalizedUrl}`);
}
return normalizedUrl;
};