155 lines
5.7 KiB
TypeScript
155 lines
5.7 KiB
TypeScript
import i18n from '../i18n';
|
|
import { resolveLocaleTag } from '../i18n/language';
|
|
|
|
const t = (key: string, options?: Record<string, unknown>) => i18n.t(key, options);
|
|
|
|
function currentTag(): Intl.LocalesArgument {
|
|
return resolveLocaleTag(i18n.language) as Intl.LocalesArgument;
|
|
}
|
|
|
|
function capitalize(s: string): string {
|
|
return s ? s.charAt(0).toUpperCase() + s.slice(1) : s;
|
|
}
|
|
|
|
/** Nickname jako główna nazwa, z fallbackiem na imię + nazwisko. */
|
|
export function displayName(
|
|
u: { nickname?: string | null; firstName?: string | null; lastName?: string | null },
|
|
fallback = '—',
|
|
): string {
|
|
return u.nickname || [u.firstName, u.lastName].filter(Boolean).join(' ') || fallback;
|
|
}
|
|
|
|
export function fmtDate(iso: string | null | undefined): string {
|
|
if (!iso) return '—';
|
|
return new Date(iso).toLocaleDateString(currentTag(), {
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
year: 'numeric',
|
|
});
|
|
}
|
|
|
|
export function fmtDateTime(iso: string | null | undefined): string {
|
|
if (!iso) return '—';
|
|
return new Date(iso).toLocaleString(currentTag(), {
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
year: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
/** Sama godzina „HH:MM" (Intl wg języka; bytność: „Jesteś od 18:03"). */
|
|
export function fmtTime(iso: string | null | undefined): string {
|
|
if (!iso) return '—';
|
|
return new Date(iso).toLocaleTimeString(currentTag(), {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
/** Czas względny: „przed chwilą”, „5 min temu”, „wczoraj”, potem data. */
|
|
export function timeAgo(iso: string | null | undefined): string {
|
|
if (!iso) return '—';
|
|
const diffMin = Math.floor((Date.now() - new Date(iso).getTime()) / 60000);
|
|
if (diffMin < 1) return t('fmt.justNow');
|
|
if (diffMin < 60) return t('fmt.minAgo', { count: diffMin });
|
|
const h = Math.floor(diffMin / 60);
|
|
if (h < 24) return t('fmt.hourAgo', { count: h });
|
|
const d = Math.floor(h / 24);
|
|
if (d === 1) return t('fmt.yesterday');
|
|
if (d < 7) return t('fmt.daysAgo', { count: d });
|
|
return fmtDate(iso);
|
|
}
|
|
|
|
/** Polska odmiana: „została 1 seria”, „zostały 2 serie”, „zostało 5 serii”. */
|
|
export function setsPlural(n: number): string {
|
|
return t('fmt.remainingSets', { count: n });
|
|
}
|
|
|
|
/** Odmiana dni: 1 → „1 dzień”, 2 → „2 dni”, 5 → „5 dni”. */
|
|
export function daysPlural(n: number): string {
|
|
return t('counters.daysCount', { count: n });
|
|
}
|
|
|
|
/** Różnica w dniach kalendarzowych względem dziś: przyszłość → dodatnia, przeszłość → ujemna. */
|
|
export function daysFromToday(iso: string): number {
|
|
const d = new Date(iso);
|
|
const a = new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
|
const tNow = new Date();
|
|
const b = new Date(tNow.getFullYear(), tNow.getMonth(), tNow.getDate()).getTime();
|
|
return Math.round((a - b) / 86_400_000);
|
|
}
|
|
|
|
/** Licznik dni względem dziś: „dziś”, „jutro”, „za 3 dni”, „wczoraj”, „3 dni temu”. */
|
|
export function daysCountdown(n: number): string {
|
|
if (n === 0) return t('fmt.today');
|
|
if (n === 1) return t('fmt.tomorrow');
|
|
if (n === -1) return t('fmt.yesterday');
|
|
if (n > 1) return t('fmt.inDays', { count: n });
|
|
return t('fmt.daysAgo', { count: -n });
|
|
}
|
|
|
|
/** Dni w plecy od ostatniego treningu w motywującej formie (pusty stan „Następny trening”). */
|
|
export function daysBehindMessage(n: number): string {
|
|
if (n <= 0) return t('fmt.behindTrainedToday');
|
|
if (n === 1) return t('fmt.behindOneDay');
|
|
return t('fmt.behindDays', { count: n });
|
|
}
|
|
|
|
/** Dni kalendarzowe od danego dnia włącznie (tego samego dnia = 1). */
|
|
export function daysSince(iso: string): number {
|
|
const s = new Date(iso);
|
|
const startMid = new Date(s.getFullYear(), s.getMonth(), s.getDate()).getTime();
|
|
const now = new Date();
|
|
const todayMid = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
|
|
return Math.round((todayMid - startMid) / 86_400_000) + 1;
|
|
}
|
|
|
|
/** Objętość w kg → „t” powyżej tysiąca (12 400 kg → „12,4 t”). */
|
|
export function fmtVolume(kg: number): string {
|
|
if (kg >= 1000)
|
|
return `${(kg / 1000).toLocaleString(currentTag(), { maximumFractionDigits: 1 })} t`;
|
|
return `${Math.round(kg).toLocaleString(currentTag())} kg`;
|
|
}
|
|
|
|
/** Czas trwania sesji: „45 min”, „1 h 30 min”. */
|
|
export function fmtDuration(startIso: string, endIso: string): string {
|
|
const min = Math.round((new Date(endIso).getTime() - new Date(startIso).getTime()) / 60000);
|
|
if (min < 1) return t('fmt.lt1m');
|
|
if (min < 60) return t('fmt.mins', { count: min });
|
|
const h = Math.floor(min / 60);
|
|
const m = min % 60;
|
|
return m > 0 ? t('fmt.hoursMins', { h, m }) : t('fmt.hours', { h });
|
|
}
|
|
|
|
export function todayStr(): string {
|
|
const d = new Date();
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
}
|
|
|
|
export function monthStr(d: Date): string {
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`;
|
|
}
|
|
|
|
/** Krótkie nazwy do siatki miesięcy w panelu selektora (Intl, bez kropki). */
|
|
export function monthShort(i: number): string {
|
|
const s = new Date(2024, i, 1).toLocaleDateString(currentTag(), { month: 'short' });
|
|
return capitalize(s.replace(/\.$/, ''));
|
|
}
|
|
|
|
/** `2026-08` → `Sierpień 2026` (Intl, wielka litera). */
|
|
export function formatMonth(month: string): string {
|
|
const [y, m] = month.split('-').map(Number);
|
|
const name = capitalize(
|
|
new Date(y, m - 1, 1).toLocaleDateString(currentTag(), { month: 'long' }),
|
|
);
|
|
return `${name} ${y}`;
|
|
}
|
|
|
|
/** Pełna nazwa dnia tygodnia wg konwencji API (1 = poniedziałek … 7 = niedziela). */
|
|
export function dayName(dow: number): string {
|
|
const d = new Date(2024, 0, 1 + (dow - 1)); // 2024-01-01 = poniedziałek
|
|
return capitalize(d.toLocaleDateString(currentTag(), { weekday: 'long' }));
|
|
}
|