Files
chocoadmin/lib/utils.ts
T

136 lines
3.4 KiB
TypeScript

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import {
ACCOUNT_TYPE_LABELS,
ACCOUNT_TYPE_PLAYER_COUNTS,
ACCOUNT_TYPE_PRICES,
ACTIVATE_STATUS_LABELS,
REQUEST_STATUS_LABELS,
TRIAL_ACCOUNT_TYPE_LABELS,
type AccountType,
type ActivateStatus,
type PaidAccountType,
type RequestStatus,
} from "@/lib/constants";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function getAccountTypeLabel(accountType: number): string {
return (
ACCOUNT_TYPE_LABELS[accountType as AccountType] ?? `${accountType}? (N/A)`
);
}
export function getTrialAccountTypeLabel(accountType: number): string {
return (
TRIAL_ACCOUNT_TYPE_LABELS[accountType as PaidAccountType] ??
`${accountType}? (N/A)`
);
}
export function getAccountTypePlayerCount(accountType: number): number {
return ACCOUNT_TYPE_PLAYER_COUNTS[accountType as PaidAccountType] ?? 0;
}
export function getAccountTypePrice(accountType: number): number {
return ACCOUNT_TYPE_PRICES[accountType as PaidAccountType] ?? 0;
}
export function getActivateStatusLabel(activateStatus: number): string {
return (
ACTIVATE_STATUS_LABELS[activateStatus as ActivateStatus] ??
`${activateStatus}? (N/A)`
);
}
export function getRequestStatusLabel(status: number): string {
return REQUEST_STATUS_LABELS[status as RequestStatus] ?? `${status}? (N/A)`;
}
export function calculateUpgradePrice(
requestedAccountType: number,
registeredAccountType: number
): number {
return (
getAccountTypePrice(requestedAccountType) -
getAccountTypePrice(registeredAccountType)
);
}
export function formatDate(value: Date | string | null | undefined): string {
const date = toDate(value);
if (!date) {
return "";
}
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
export function formatDateTime(
value: Date | string | null | undefined
): string {
const date = toDate(value);
if (!date) {
return "";
}
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
return `${formatDate(date)} ${hours}:${minutes}:${seconds}`;
}
export function calculateExtendedAvailableDate(
availableDate: Date,
now = new Date()
): Date {
const baseDate =
availableDate.getTime() < now.getTime() ? now : availableDate;
const extendedDate = new Date(baseDate);
extendedDate.setFullYear(extendedDate.getFullYear() + 1);
extendedDate.setHours(0, 0, 0, 0);
return extendedDate;
}
export function toDatetimeLocal(value: Date | string | null | undefined): string {
const date = toDate(value);
if (!date) {
return "";
}
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day}T${hours}:${minutes}`;
}
function toDate(value: Date | string | null | undefined): Date | null {
if (!value) {
return null;
}
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) {
return null;
}
return date;
}