Files
FitOS/app/src/lib/equipment.ts

146 lines
5.3 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 i18n from '../i18n';
import type { Equipment, EquipmentKind, EquipmentSpec, GymEquipment } from '../api/types';
import { localizedName } from './localizedName';
function fmtKg(value: number): string {
return Number.isInteger(value) ? String(value) : value.toFixed(1).replace(/\.0$/, '');
}
/**
* Human-readable summary of a type-specific spec payload:
* DUMBBELL/KETTLEBELL/CABLE/MACHINE → "2.5–25 kg" ("5 kg" dla stałego ciężaru)
* BARBELL/BENCH → "do 100 kg"
* CARDIO → "do 16 km/h" (+ "nachylenie do 15%")
* BODYWEIGHT/OTHER or no spec → null
*/
export function formatEquipmentSpec(spec: EquipmentSpec | null | undefined): string | null {
if (!spec) return null;
const parts: string[] = [];
if (spec.minKg != null || spec.maxKg != null) {
if (spec.minKg != null && spec.maxKg != null) {
parts.push(
spec.minKg === spec.maxKg
? `${fmtKg(spec.minKg)} kg`
: `${fmtKg(spec.minKg)}–${fmtKg(spec.maxKg)} kg`,
);
} else if (spec.maxKg != null) parts.push(i18n.t('gyms:spec.upToKg', { v: fmtKg(spec.maxKg) }));
else parts.push(i18n.t('gyms:spec.fromKg', { v: fmtKg(spec.minKg!) }));
}
if (spec.maxLoadKg != null) parts.push(i18n.t('gyms:spec.upToKg', { v: fmtKg(spec.maxLoadKg) }));
if (spec.maxSpeedKmh != null)
parts.push(i18n.t('gyms:spec.upToKmh', { v: fmtKg(spec.maxSpeedKmh) }));
if (spec.maxInclinePct != null)
parts.push(i18n.t('gyms:spec.inclineUpTo', { v: fmtKg(spec.maxInclinePct) }));
return parts.length ? parts.join(', ') : null;
}
/** Kinds z zakresem obciążeń { minKg, maxKg }. */
export const LOAD_RANGE_KINDS: EquipmentKind[] = ['DUMBBELL', 'KETTLEBELL', 'CABLE', 'MACHINE'];
/** Kinds z maksymalnym obciążeniem { maxLoadKg }. */
export const MAX_LOAD_KINDS: EquipmentKind[] = ['BARBELL', 'BENCH'];
/** Pola formularza spec (stringi = wartość inputa). */
export interface SpecFields {
minKg: string;
maxKg: string;
maxLoadKg: string;
maxSpeedKmh: string;
maxInclinePct: string;
}
export const EMPTY_SPEC_FIELDS: SpecFields = {
minKg: '',
maxKg: '',
maxLoadKg: '',
maxSpeedKmh: '',
maxInclinePct: '',
};
export function specFieldsFrom(spec: EquipmentSpec | null | undefined): SpecFields {
const s = spec ?? {};
return {
minKg: s.minKg != null ? String(s.minKg) : '',
maxKg: s.maxKg != null ? String(s.maxKg) : '',
maxLoadKg: s.maxLoadKg != null ? String(s.maxLoadKg) : '',
maxSpeedKmh: s.maxSpeedKmh != null ? String(s.maxSpeedKmh) : '',
maxInclinePct: s.maxInclinePct != null ? String(s.maxInclinePct) : '',
};
}
function parseNum(value: string): number | undefined {
if (value.trim() === '') return undefined;
const n = Number(value);
return Number.isFinite(n) ? n : undefined;
}
/** Buduje payload spec z pól formularza wg rodzaju urządzenia; błąd = komunikat PL. */
export function specFromFields(
kind: EquipmentKind,
f: SpecFields,
): { spec: EquipmentSpec | null } | { error: string } {
if (LOAD_RANGE_KINDS.includes(kind)) {
const minKg = parseNum(f.minKg);
const maxKg = parseNum(f.maxKg);
if (minKg == null || maxKg == null) return { error: i18n.t('gyms:equipment.specRangeError') };
return { spec: { minKg, maxKg } };
}
if (MAX_LOAD_KINDS.includes(kind)) {
const maxLoadKg = parseNum(f.maxLoadKg);
if (maxLoadKg == null) return { error: i18n.t('gyms:equipment.specMaxLoadError') };
return { spec: { maxLoadKg } };
}
if (kind === 'CARDIO') {
const maxSpeedKmh = parseNum(f.maxSpeedKmh);
const maxInclinePct = parseNum(f.maxInclinePct);
if (maxSpeedKmh == null && maxInclinePct == null) return { spec: null };
return {
spec: {
...(maxSpeedKmh != null ? { maxSpeedKmh } : {}),
...(maxInclinePct != null ? { maxInclinePct } : {}),
},
};
}
return { spec: null };
}
/** Czy dla danego rodzaju formularz pokazuje jakiekolwiek pola spec. */
export function hasSpecFields(kind: EquipmentKind): boolean {
return LOAD_RANGE_KINDS.includes(kind) || MAX_LOAD_KINDS.includes(kind) || kind === 'CARDIO';
}
/** Efektywny spec instancji: nadpisanie siłowni, inaczej spec z katalogu. */
export function effectiveSpec(item: {
spec?: EquipmentSpec | null;
equipment: { spec: EquipmentSpec | null };
}): EquipmentSpec | null {
return item.spec ?? item.equipment.spec ?? null;
}
/** Chip label for a gym equipment instance: "Hantle ×2 · 2.5–25 kg". */
export function formatGymEquipment(
item: {
quantity: number;
spec?: EquipmentSpec | null;
equipment: Equipment;
},
lang = 'pl',
): string {
const spec = formatEquipmentSpec(effectiveSpec(item));
const qty = item.quantity > 1 ? ` ×${item.quantity}` : '';
const name = localizedName(item.equipment, lang);
return spec ? `${name}${qty} · ${spec}` : `${name}${qty}`;
}
/**
* Czy ćwiczenie można wykonać w danej siłowni: każde wymagane urządzenie
* musi być w wyposażeniu siłowni. BODYWEIGHT (masa ciała) = bez sprzętu,
* zawsze dostępne. Pusta lista urządzeń = ćwiczenie zawsze dostępne.
*/
export function exerciseAvailableAtGym(
exercise: { equipment?: Equipment[] },
gym: { equipment?: GymEquipment[] },
): boolean {
const available = new Set((gym.equipment ?? []).map((ge) => ge.equipmentId));
return (exercise.equipment ?? []).every((eq) => eq.kind === 'BODYWEIGHT' || available.has(eq.id));
}