marco.pms.web/src/utils/appUtils.js

152 lines
4.4 KiB
JavaScript

import { useEffect, useState } from "react";
import { format, parseISO } from "date-fns";
export const formatFileSize = (bytes) => {
if (bytes < 1024) return bytes + " B";
else if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + " KB";
else return (bytes / (1024 * 1024)).toFixed(2) + " MB";
};
export const AppColorconfig = {
colors: {
primary: "#696cff",
secondary: "#8592a3",
success: "#71dd37",
info: "#03c3ec",
warning: "#ffab00",
danger: "#ff3e1d",
dark: "#233446",
black: "#000",
white: "#fff",
cardColor: "#fff",
bodyBg: "#f5f5f9",
bodyColor: "#697a8d",
headingColor: "#566a7f",
textMuted: "#a1acb8",
borderColor: "#eceef1",
},
};
export const getColorNameFromHex = (hex) => {
const normalizedHex = hex?.replace(/'/g, "").toLowerCase();
const colors = AppColorconfig.colors;
for (const [name, value] of Object.entries(colors)) {
if (value.toLowerCase() === normalizedHex) {
return name;
}
}
return null; //
};
export const useDebounce = (value, delay = 500) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay);
return () => clearTimeout(timer);
}, [value, delay]);
return debouncedValue;
};
export const getIconByFileType = (type = "") => {
const lower = type.toLowerCase();
if (lower === "application/pdf") return "bxs-file-pdf";
if (lower.includes("word")) return "bxs-file-doc";
if (lower.includes("excel") || lower.includes("spreadsheet"))
return "bxs-file-xls";
if (lower === "image/png") return "bxs-file-png";
if (lower === "image/jpeg" || lower === "image/jpg") return "bxs-file-jpg";
if (lower.includes("zip") || lower.includes("rar")) return "bxs-file-archive";
return "bx bx-file";
};
export const normalizeAllowedContentTypes = (allowedContentType) => {
if (!allowedContentType) return [];
if (Array.isArray(allowedContentType)) return allowedContentType;
if (typeof allowedContentType === "string")
return allowedContentType.split(",");
return [];
};
export function localToUtc(dateString) {
if (!dateString || typeof dateString !== "string") return null;
const parts = dateString.trim().split("-");
if (parts.length !== 3) return null;
let day, month, year;
if (parts[0].length === 4) {
// Format: yyyy-mm-dd
[year, month, day] = parts;
} else {
// Format: dd-mm-yyyy
[day, month, year] = parts;
}
if (!day || !month || !year) return null;
const date = new Date(
Date.UTC(Number(year), Number(month) - 1, Number(day), 0, 0, 0)
);
return isNaN(date.getTime()) ? null : date.toISOString();
}
/**
* Flexible number formatter for currency, numbers, or percentages.
*
* @param {number} amount - The value to format.
* @param {Object} options - Formatting options.
* @param {"currency"|"number"|"percent"} [options.type="number"] - Type of format.
* @param {string} [options.currency="INR"] - Currency code (only used when type="currency").
* @param {string} [options.locale="en-US"] - Locale for formatting.
* @param {"short"|"long"|"standard"} [options.notation="compact"] - Display style for large numbers.
* @param {number} [options.minimumFractionDigits=0] - Minimum decimal places.
* @param {number} [options.maximumFractionDigits=2] - Maximum decimal places.
* @returns {string} Formatted number string.
*/
export const formatFigure = (
amount,
{
type = "number",
currency = "INR",
locale = "en-US",
notation = "standard", // standard or compact
compactDisplay = "short",
minimumFractionDigits = 0,
maximumFractionDigits = 2,
} = {}
) => {
if (amount == null || isNaN(amount)) return "-";
const formatterOptions = {
style: type === "currency" ? "currency" : type === "percent" ? "percent" : "decimal",
notation: notation,
compactDisplay,
minimumFractionDigits,
maximumFractionDigits,
};
if (type === "currency") {
formatterOptions.currency = currency;
}
return new Intl.NumberFormat(locale, formatterOptions).format(amount);
};
export const frequencyLabel = (freq, isLong = false) => {
const frequency = parseInt(freq, 10);
switch (frequency) {
case 0:
return isLong ? "1 Month" : "1 mo";
case 1:
return isLong ? "Quarterly (3 Months)" : "3 mo";
case 2:
return isLong ? "6 Months" : "6 mo";
case 3:
return isLong ? "1 Year" : "1 yr";
default:
return isLong ? "Unknown" : "N/A";
}
};