Files
FitOS/admin/src/components/GymRolePicker.tsx

107 lines
3.7 KiB
TypeScript

import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Check } from 'lucide-react';
import type { GymRole } from '../api/types';
import { GYM_ROLE_META, GYM_ROLE_ORDER, GYM_ROLE_PILL_ACTIVE } from '../lib/roles';
import { cn } from '../lib/utils';
const PILL_INACTIVE = 'bg-transparent text-dim border-line hover:text-ink hover:border-dim';
interface GymRolePickerProps {
value: GymRole[];
onChange: (roles: GymRole[]) => void;
/** Przedrostek aria-label pigułek (np. nazwa siłowni lub „Dodaj”), do testów i A11y. */
labelPrefix: string;
/** Uniemożliwia odznaczenie, gdy została tylko `minRoles` (domyślnie 1). */
minRoles?: number;
/** Tytuł blokady ostatniej roli (title na zablokowanej pigułce). */
lockTitle?: string;
/** Pod pigułkami pokazuje wyjaśnienie wskazanej/ostatnio wybranej roli. Domyślnie true. */
showExplain?: boolean;
className?: string;
}
/**
* Wybór ról siłowni (Właściciel / Trener / Ćwiczący) z widocznym wyjaśnieniem,
* co daje każda z nich — żeby przydzielanie nie było zgadywanką.
*/
export function GymRolePicker({
value,
onChange,
labelPrefix,
minRoles = 1,
lockTitle,
showExplain = true,
className,
}: GymRolePickerProps) {
const { t } = useTranslation();
const [explained, setExplained] = useState<GymRole | null>(null);
const locked = value.length <= minRoles;
const toggle = (role: GymRole) => {
setExplained(role);
if (value.includes(role)) {
if (locked) return;
onChange(value.filter((r) => r !== role));
} else {
onChange([...value, role]);
}
};
const shown =
explained ??
(value.length > 0 ? (GYM_ROLE_ORDER.find((r) => value.includes(r)) ?? null) : null);
const meta = shown ? GYM_ROLE_META[shown] : null;
return (
<div className={className}>
<div className="flex flex-wrap gap-2">
{GYM_ROLE_ORDER.map((role) => {
const active = value.includes(role);
const disabled = active && locked;
return (
<button
key={role}
type="button"
role="checkbox"
aria-checked={active}
aria-label={`${t(GYM_ROLE_META[role].labelKey)} — ${labelPrefix}`}
aria-description={t(GYM_ROLE_META[role].summaryKey)}
title={disabled ? lockTitle : t(GYM_ROLE_META[role].summaryKey)}
disabled={disabled}
onClick={() => toggle(role)}
className={cn(
'inline-flex flex-1 cursor-pointer items-center justify-center gap-2 rounded-xl border px-3 py-2 text-[0.95rem] font-semibold transition-colors disabled:cursor-not-allowed disabled:opacity-40',
active ? GYM_ROLE_PILL_ACTIVE[role] : PILL_INACTIVE,
)}
>
<Check className={cn('h-4 w-4', active ? '' : 'opacity-0')} />
{t(GYM_ROLE_META[role].labelKey)}
</button>
);
})}
</div>
{showExplain && (
<div className="mt-2.5 min-h-[2.5rem] rounded-xl border border-line bg-input/40 px-3 py-2.5 text-[0.875rem]">
{meta ? (
<>
<span className="font-semibold text-ink">{t(meta.labelKey)}: </span>
<span className="text-dim">{t(meta.summaryKey)}</span>
<ul className="mt-1.5 space-y-0.5 text-dim">
{meta.grantKeys.map((g) => (
<li key={g} className="flex gap-1.5">
<span className="text-accent-text">•</span>
<span>{t(g)}</span>
</li>
))}
</ul>
</>
) : (
<span className="text-dim">{t('roles.picker.hint')}</span>
)}
</div>
)}
</div>
);
}