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

378 lines
12 KiB
TypeScript

import type { ReactNode } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
import { ArrowLeft, ChevronLeft, ChevronRight, MessageSquare, X } from 'lucide-react';
import { buttonVariants } from './button';
import { H2, H3, Muted } from './typography';
import { cn } from '../../lib/utils';
/* Page header: opcjonalny element przed tytułem (np. awatar), tytuł + badge,
akcje zawsze w tej samej linii po prawej (nie stackują się na mobile). */
export function PageHeader({
title,
leading,
badge,
actions,
}: {
title: string;
/** Element przed tytułem (np. awatar). */
leading?: ReactNode;
badge?: ReactNode;
actions?: ReactNode;
}) {
return (
<div className="flex items-center justify-between gap-3 mb-5">
<div className="flex min-w-0 items-center gap-3 flex-wrap">
{leading}
{/* `<h1>` należy do topbaru (tytuł strony) — nagłówek treści to `<h2>` */}
<H2 className="mb-0">{title}</H2>
{badge}
</div>
{actions && <div className="shrink-0">{actions}</div>}
</div>
);
}
/* Back button — ghost, small; `to` renders a Link, `onClick` a plain button */
export function BackButton({
to,
onClick,
children,
className = '',
}: {
to?: string;
onClick?: () => void;
children?: ReactNode;
className?: string;
}) {
const { t } = useTranslation();
// na telefonie „Wstecz” siedzi w topmenu („<”) — tu tylko md+
const cls = cn(buttonVariants({ variant: 'ghost', size: 'sm' }), 'max-md:hidden', className);
if (to) {
return (
<Link to={to} className={cls}>
<ArrowLeft className="w-4 h-4" />
{children ?? t('back')}
</Link>
);
}
return (
<button type="button" onClick={onClick} className={cls}>
<ArrowLeft className="w-4 h-4" />
{children ?? t('back')}
</button>
);
}
/* Section header: H2 + muted description + optional actions (right) */
export function SectionHeader({
title,
description,
actions,
className = '',
}: {
title: ReactNode;
description?: ReactNode;
actions?: ReactNode;
className?: string;
}) {
return (
<div className={`flex flex-wrap items-start justify-between gap-3 mb-3 ${className}`.trim()}>
<div className="min-w-0">
<H2 className="mb-0.5">{title}</H2>
{description && <Muted className="text-[0.875rem]">{description}</Muted>}
</div>
{actions && <div className="flex items-center gap-2 shrink-0">{actions}</div>}
</div>
);
}
/* Stat tile: bez boxa — mini etykieta (uppercase) + duża wartość */
export function StatTile({
label,
value,
valueClassName = 'truncate',
className = '',
}: {
label: ReactNode;
value: ReactNode;
/** Styl wartości (domyślnie `truncate`; np. `break-words` dla długich tekstów). */
valueClassName?: string;
className?: string;
}) {
return (
<div data-slot="stat-tile" className={`min-w-0 ${className}`.trim()}>
<div className="text-[11px] uppercase tracking-[0.1em] text-dim font-bold">{label}</div>
<div
className={`font-display text-[1.5rem] leading-tight font-bold tabular-nums tracking-[-0.02em] ${valueClassName}`.trim()}
>
{value}
</div>
</div>
);
}
/* List row: płaski wiersz (tytuł + meta lewo, akcje prawo); w listach w karcie
rodzic używa `divide-y divide-line` (dłuższe) albo `space-y-1` (krótkie). */
export function ListRow({
title,
meta,
actions,
children,
className = '',
}: {
title: ReactNode;
meta?: ReactNode;
actions?: ReactNode;
children?: ReactNode;
className?: string;
}) {
return (
<div
data-slot="list-row"
className={`flex flex-wrap items-center justify-between gap-3 px-2 py-3 rounded-[10px] transition-colors hover:bg-line/20 ${className}`.trim()}
>
<div className="min-w-0 flex-1">
<div className="text-[0.95rem] font-semibold min-w-0 truncate">{title}</div>
{meta && <Muted className="text-[0.875rem]">{meta}</Muted>}
</div>
{actions && <div className="flex items-center gap-2 shrink-0">{actions}</div>}
{children}
</div>
);
}
/* Callout komentarza serii (SessionSet.note): akcentowa ramka + normalna czcionka —
trener od razu widzi, że seria ma komentarz (ten sam styl w historii i u trenera). */
export function NoteCallout({
label,
text,
className = '',
}: {
label: string;
text: string;
className?: string;
}) {
return (
<div
data-slot="note-callout"
className={cn(
'flex items-start gap-2 rounded-[10px] border border-accent/40 bg-accent/10 px-2.5 py-2 text-[0.8125rem] text-ink',
className,
)}
>
<MessageSquare className="mt-0.5 h-3.5 w-3.5 shrink-0 text-accent-text" aria-hidden="true" />
<span className="min-w-0">
<strong className="font-semibold">{label}</strong> {text}
</span>
</div>
);
}
/* Meta item: small icon + muted text */
export function MetaItem({
icon: Icon,
children,
className = '',
}: {
icon: React.ComponentType<{ className?: string }>;
children: ReactNode;
className?: string;
}) {
return (
<span
className={`inline-flex items-center gap-1.5 text-dim text-[0.875rem] ${className}`.trim()}
>
<Icon className="w-4 h-4 shrink-0" />
<span className="min-w-0 truncate">{children}</span>
</span>
);
}
/* Chip: small pill (+ optional remove button) */
export function Chip({
children,
onRemove,
removeLabel,
className = '',
}: {
children: ReactNode;
onRemove?: () => void;
removeLabel?: string;
className?: string;
}) {
const { t } = useTranslation();
return (
<span
data-slot="chip"
className={`inline-flex items-center gap-1.5 bg-card border border-line rounded-full px-3 py-1 text-[0.8125rem] font-semibold text-dim ${className}`.trim()}
>
{children}
{onRemove && (
<button
type="button"
onClick={onRemove}
aria-label={removeLabel ?? t('remove')}
className="text-dim hover:text-danger cursor-pointer"
>
<X className="w-3.5 h-3.5" />
</button>
)}
</span>
);
}
/* Empty state */
export function EmptyState({
icon: Icon,
title,
description,
action,
}: {
icon: React.ComponentType<{ className?: string }>;
title: string;
description?: string;
action?: ReactNode;
}) {
return (
<div
data-slot="empty-state"
className="flex flex-col items-center justify-center text-center py-10 px-4"
>
<div className="w-14 h-14 rounded-full bg-accent/10 border border-accent/25 text-accent-text flex items-center justify-center mb-4">
<Icon className="w-7 h-7" />
</div>
<H3 className="mb-1">{title}</H3>
{description && <p className="text-dim text-[0.95rem] max-w-sm">{description}</p>}
{action && <div className="mt-4">{action}</div>}
</div>
);
}
/* Tabs — scroll bez scrollbarów ze snapem do całych zakładek (w spoczynku brak
uciętych słów, brak pomiarów/przeskalowań JS — nie zacina na słabych
urządzeniach). Strzałki wewnątrz kontenera montowane tylko gdy naprawdę jest
co przewijać: miejsce istnieje dokładnie wtedy, gdy istnieje przycisk. */
export function Tabs({
tabs,
active,
onChange,
}: {
tabs: { id: string; label: ReactNode }[];
active: string;
onChange: (id: string) => void;
}) {
const { t } = useTranslation();
const scrollRef = useRef<HTMLDivElement>(null);
const [canPrev, setCanPrev] = useState(false);
const [canNext, setCanNext] = useState(false);
const updateArrows = () => {
const el = scrollRef.current;
if (!el) return;
setCanPrev(el.scrollLeft > 2);
setCanNext(el.scrollLeft + el.clientWidth < el.scrollWidth - 2);
};
useEffect(() => {
updateArrows();
const onResize = () => updateArrows();
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
}, [tabs.length]);
// aktywna zakładka zawsze wycentrowana w pasku (prosty, przewidywalny
// efekt kierunkowy bez snap-kombinacji — działa też na Safari/iOS)
useEffect(() => {
const el = scrollRef.current;
const btn = el?.querySelector<HTMLElement>(`[data-tab-id="${CSS.escape(active)}"]`);
if (!el || !btn) return;
const br = btn.getBoundingClientRect();
const er = el.getBoundingClientRect();
const target = el.scrollLeft + (br.left - er.left) + br.width / 2 - er.width / 2;
const clamped = Math.max(0, Math.min(el.scrollWidth - el.clientWidth, target));
if (Math.abs(clamped - el.scrollLeft) > 2) el.scrollTo({ left: clamped, behavior: 'smooth' });
}, [active]);
const scrollByGroup = (dir: 1 | -1) => {
const el = scrollRef.current;
if (el) el.scrollBy({ left: dir * el.clientWidth * 0.9, behavior: 'smooth' });
};
return (
<div
data-slot="tabs"
className="flex items-center gap-1 p-1 bg-input rounded-[10px] border border-line"
>
{/* strzałki zawsze w DOM — szerokość i widoczność animowane, więc miejsce
pojawia się płynnie razem z przyciskiem (bez „żabkowania” layoutu) */}
<div
className={`grid shrink-0 place-items-center overflow-hidden transition-all duration-200 ${
canPrev ? 'w-7 opacity-100' : 'w-0 opacity-0 pointer-events-none'
}`}
>
<button
type="button"
aria-label={t('tabs.prev')}
tabIndex={canPrev ? 0 : -1}
onClick={() => scrollByGroup(-1)}
className="grid h-7 w-7 shrink-0 cursor-pointer place-items-center text-dim transition-colors hover:text-ink"
>
<ChevronLeft className="h-4 w-4" />
</button>
</div>
<div className="relative min-w-0 flex-1">
<div
ref={scrollRef}
onScroll={updateArrows}
className="flex gap-1 overflow-x-auto overscroll-contain [scrollbar-width:none] [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden"
>
{tabs.map((t) => (
<button
key={t.id}
type="button"
data-tab-id={t.id}
data-active={active === t.id || undefined}
onClick={() => onChange(t.id)}
className={`px-3.5 py-2 rounded-[10px] text-[0.9rem] font-semibold whitespace-nowrap cursor-pointer transition-all duration-150 ${
active === t.id
? 'bg-card text-ink border border-line shadow-card'
: 'border border-transparent text-dim hover:text-ink'
}`}
>
{t.label}
</button>
))}
</div>
{/* miękkie wygaszenie krawędzi — ucięty tekst znika zanim dojdzie do strzałki */}
<div
aria-hidden="true"
className={`pointer-events-none absolute inset-y-0 left-0 w-6 transition-opacity duration-200 ${canPrev ? 'opacity-100' : 'opacity-0'}`}
style={{ background: 'linear-gradient(to right, var(--color-input), transparent)' }}
/>
<div
aria-hidden="true"
className={`pointer-events-none absolute inset-y-0 right-0 w-6 transition-opacity duration-200 ${canNext ? 'opacity-100' : 'opacity-0'}`}
style={{ background: 'linear-gradient(to left, var(--color-input), transparent)' }}
/>
</div>
<div
className={`grid shrink-0 place-items-center overflow-hidden transition-all duration-200 ${
canNext ? 'w-7 opacity-100' : 'w-0 opacity-0 pointer-events-none'
}`}
>
<button
type="button"
aria-label={t('tabs.next')}
tabIndex={canNext ? 0 : -1}
onClick={() => scrollByGroup(1)}
className="grid h-7 w-7 shrink-0 cursor-pointer place-items-center text-dim transition-colors hover:text-ink"
>
<ChevronRight className="h-4 w-4" />
</button>
</div>
</div>
);
}