Files
FitOS/app/src/components/ui/dialogs.tsx

360 lines
11 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 type { ReactNode } from 'react';
import { useEffect, useState } from 'react';
import { X } from 'lucide-react';
import { Button } from './button';
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from './dialog';
/* Modal — Radix Dialog: focus trap, Esc, scroll-lock, aria; bottom-sheet na mobile.
`wide` = szerszy panel (sm:max-w-2xl) dla rozbudowanych formularzy. */
export function Modal({
open,
onClose,
title,
children,
wide,
handle,
}: {
open: boolean;
onClose: () => void;
title: string;
children: ReactNode;
wide?: boolean;
/** Uchwyt bottom sheeta (mobile) — wizualna wskazówka przyciągania. */
handle?: boolean;
}) {
return (
<Dialog open={open} onOpenChange={(o) => !o && onClose()}>
<DialogContent className={wide ? 'sm:max-w-2xl' : undefined}>
{handle && (
<div
aria-hidden="true"
className="mx-auto mb-3 h-1 w-10 rounded-full bg-line sm:hidden"
/>
)}
<DialogHeader>
<DialogTitle asChild>
<h3 className="text-[1.05rem] font-semibold">{title}</h3>
</DialogTitle>
<DialogClose asChild>
<button
aria-label="Zamknij"
className="p-1.5 -m-1.5 text-dim hover:text-ink cursor-pointer"
>
<X className="w-5 h-5" />
</button>
</DialogClose>
</DialogHeader>
<DialogDescription asChild>
<span className="sr-only" />
</DialogDescription>
{children}
</DialogContent>
</Dialog>
);
}
/* Confirm dialog */
export function ConfirmDialog({
open,
onClose,
onConfirm,
title,
description,
confirmLabel = 'Potwierdź',
danger,
busy,
}: {
open: boolean;
onClose: () => void;
onConfirm: () => void;
title: string;
description: string;
confirmLabel?: string;
danger?: boolean;
busy?: boolean;
}) {
return (
<Modal open={open} onClose={onClose} title={title}>
<p className="text-dim text-[0.95rem]">{description}</p>
<div className="flex flex-col-reverse sm:flex-row sm:items-center sm:justify-end gap-3 mt-5">
<Button variant="secondary" onClick={onClose} className="w-full sm:w-auto">
Anuluj
</Button>
<Button
variant={danger ? 'danger' : 'primary'}
onClick={onConfirm}
disabled={busy}
className="w-full sm:w-auto"
>
{confirmLabel}
</Button>
</div>
</Modal>
);
}
/* Number pad dialog — mobile-friendly numeric entry (keypad + Wpisz/Anuluj). */
export function NumberPadDialog({
open,
title,
value,
hint,
onConfirm,
onClose,
}: {
open: boolean;
title: string;
value: string;
hint?: string;
onConfirm: (value: string) => void;
onClose: () => void;
}) {
const [draft, setDraft] = useState(value);
useEffect(() => {
if (open) setDraft(value);
}, [open, value]);
const press = (d: string) => {
setDraft((cur) => {
if (d === 'C') return '';
if (d === '⌫') return cur.slice(0, -1);
if (cur.length >= 3) return cur;
return cur + d;
});
};
const KEYS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'C', '0', '⌫'];
return (
<Modal open={open} onClose={onClose} title={title}>
<div className="mb-4">
<input
value={draft}
onChange={(e) => setDraft(e.target.value.replace(/\D/g, '').slice(0, 3))}
inputMode="numeric"
placeholder="—"
aria-label={title}
className="w-full text-center text-5xl font-bold font-display tabular-nums text-ink bg-input border border-line rounded-[10px] py-2 shadow-(--inset-shadow) focus:outline-none focus:border-accent-text focus:ring-[3px] focus:ring-accent/18"
/>
{hint && <div className="text-center text-dim text-[0.85rem] mt-1.5">{hint}</div>}
</div>
<div className="grid grid-cols-3 gap-2 mb-4">
{KEYS.map((d) => (
<button
key={d}
type="button"
onClick={() => press(d)}
aria-label={d === 'C' ? 'Wyczyść' : d === '⌫' ? 'Usuń cyfrę' : d}
className="h-14 rounded-[10px] bg-input border border-line text-xl font-semibold text-ink shadow-(--inset-shadow) cursor-pointer hover:border-accent/55 hover:text-accent-text active:bg-line/40 transition-all duration-150"
>
{d}
</button>
))}
</div>
<div className="flex flex-col-reverse sm:flex-row sm:items-center sm:justify-end gap-3">
<Button variant="secondary" onClick={onClose} className="w-full sm:w-auto">
Anuluj
</Button>
<Button onClick={() => onConfirm(draft)} className="w-full sm:w-auto">
Wpisz
</Button>
</div>
</Modal>
);
}
/* Difficulty slider dialog — 1 (łatwo) … 5 (trudno). */
export function DifficultyDialog({
open,
value,
onConfirm,
onClose,
}: {
open: boolean;
value: string;
onConfirm: (value: string) => void;
onClose: () => void;
}) {
const normalize = (v: string) => {
const parsed = v === '' ? NaN : parseInt(v);
return String(isNaN(parsed) ? 3 : Math.max(1, Math.min(5, parsed)));
};
const [draft, setDraft] = useState(() => normalize(value));
useEffect(() => {
if (open) setDraft(normalize(value));
}, [open, value]);
return (
<Modal open={open} onClose={onClose} title="Trudność">
<div className="text-center mb-5">
<div className="text-5xl font-bold tabular-nums text-ink">{draft}</div>
</div>
<div className="mb-5">
<input
type="range"
min={1}
max={5}
step={1}
value={parseInt(draft)}
onChange={(e) => setDraft(e.target.value)}
aria-label="Trudność (suwak)"
className="w-full accent-accent cursor-pointer"
/>
<div className="flex justify-between text-dim text-[0.85rem] mt-2">
<span>łatwo</span>
<span>trudno</span>
</div>
</div>
<div className="flex flex-col-reverse sm:flex-row sm:items-center sm:justify-end gap-3">
<Button variant="secondary" onClick={onClose} className="w-full sm:w-auto">
Anuluj
</Button>
<Button onClick={() => onConfirm(draft)} className="w-full sm:w-auto">
Wpisz
</Button>
</div>
</Modal>
);
}
/* Metadata for the set fields edited in SetEditDialog. */
const SET_FIELD_META: { key: 'reps' | 'rir' | 'difficulty'; label: string; hint: string }[] = [
{ key: 'reps', label: 'Powtórzenia', hint: 'liczba powtórzeń' },
{ key: 'rir', label: 'RIR', hint: '0–10 (reps in reserve)' },
{ key: 'difficulty', label: 'Trudność', hint: '1–5' },
];
export type SetFieldValues = { reps: string; rir: string; difficulty: string };
export type PlannedValues = { reps: number | null; rir: number | null; difficulty: number | null };
/* Trwająca przerwa pokazana w oknie serii (timer + informacja, że zapis zrestartuje odliczanie). */
export type SetEditRestInfo = { remaining: number; total: number; resetsOnSave: boolean };
const fmtRestSec = (sec: number) => `${Math.floor(sec / 60)}:${String(sec % 60).padStart(2, '0')}`;
/* Bulk set editor — fill in all set fields at once (each via the number pad). */
export function SetEditDialog({
open,
setIndex,
values,
planned,
title,
rest,
onSave,
onClose,
}: {
open: boolean;
setIndex: number;
values: SetFieldValues;
planned: PlannedValues;
/** Tytuł okna — domyślnie „Uzupełnij serię N” (przekazuj „Edytuj serię N” dla uzupełnionych serii). */
title?: string;
/** Trwająca przerwa — timer odświeżany co sekundę przez rodzica. */
rest?: SetEditRestInfo | null;
onSave: (values: SetFieldValues) => void;
onClose: () => void;
}) {
const [drafts, setDrafts] = useState<SetFieldValues>(values);
const [editingField, setEditingField] = useState<'reps' | 'rir' | 'difficulty' | null>(null);
useEffect(() => {
if (open) setDrafts(values);
}, [open, values]);
const editing = editingField ? SET_FIELD_META.find((f) => f.key === editingField) : undefined;
const hasNorm = planned.reps != null;
const fillNorm = () => {
setDrafts({
reps: planned.reps?.toString() ?? '',
rir: planned.rir?.toString() ?? '',
difficulty: planned.difficulty?.toString() ?? '',
});
};
return (
<>
<Modal open={open} onClose={onClose} title={title ?? `Uzupełnij serię ${setIndex + 1}`}>
{rest && rest.remaining > 0 && (
<div className="mb-3 rounded-[10px] border border-line bg-input px-3.5 py-3">
<div className="flex items-center gap-2">
<span className="live-dot" aria-hidden="true" />
<span className="text-[0.8125rem] text-dim">przerwa</span>
<b className="font-display text-[1rem] font-bold tabular-nums text-ink">
{fmtRestSec(rest.remaining)}
</b>
<span className="text-[0.8125rem] text-dim">/ {fmtRestSec(rest.total)}</span>
</div>
{rest.resetsOnSave && (
<p className="text-dim text-[0.8125rem] mt-1.5">
Zapisanie serii zrestartuje odliczanie przerwy.
</p>
)}
</div>
)}
{hasNorm && (
<Button variant="secondary" size="sm" onClick={fillNorm} className="mb-3 w-full">
Norma spełniona
</Button>
)}
<div className="space-y-2 mb-4">
{SET_FIELD_META.map((f) => (
<button
key={f.key}
type="button"
onClick={() => setEditingField(f.key)}
className="w-full flex items-center justify-between rounded-[10px] border border-line bg-input px-3.5 py-3 shadow-(--inset-shadow) cursor-pointer hover:border-accent/55 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/60"
>
<span className="text-[0.9rem] font-semibold text-ink">{f.label}</span>
<span
className={`text-[1.1rem] font-bold ${drafts[f.key] ? 'text-ink' : 'text-dim'}`}
>
{drafts[f.key] || '—'}
</span>
</button>
))}
</div>
<div className="flex flex-col-reverse sm:flex-row sm:items-center sm:justify-end gap-3">
<Button variant="secondary" onClick={onClose} className="w-full sm:w-auto">
Anuluj
</Button>
<Button onClick={() => onSave(drafts)} className="w-full sm:w-auto">
Zapisz
</Button>
</div>
</Modal>
{editing &&
(editing.key === 'difficulty' ? (
<DifficultyDialog
open
value={drafts[editing.key]}
onConfirm={(v) => {
setDrafts((d) => ({ ...d, [editing.key]: v }));
setEditingField(null);
}}
onClose={() => setEditingField(null)}
/>
) : (
<NumberPadDialog
open
title={editing.label}
value={drafts[editing.key]}
hint={editing.hint}
onConfirm={(v) => {
setDrafts((d) => ({ ...d, [editing.key]: v }));
setEditingField(null);
}}
onClose={() => setEditingField(null)}
/>
))}
</>
);
}