355 lines
11 KiB
TypeScript
355 lines
11 KiB
TypeScript
import type { ReactNode } from 'react';
|
|
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import { ArrowLeft, ChevronLeft, ChevronRight, X } from 'lucide-react';
|
|
import { buttonVariants } from './button';
|
|
import { H1, 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 className="mb-0">{title}</H1>
|
|
{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 = 'Wstecz',
|
|
className = '',
|
|
}: {
|
|
to?: string;
|
|
onClick?: () => void;
|
|
children?: ReactNode;
|
|
className?: string;
|
|
}) {
|
|
const cls = cn(buttonVariants({ variant: 'ghost', size: 'sm' }), className);
|
|
if (to) {
|
|
return (
|
|
<Link to={to} className={cls}>
|
|
<ArrowLeft className="w-4 h-4" />
|
|
{children}
|
|
</Link>
|
|
);
|
|
}
|
|
return (
|
|
<button type="button" onClick={onClick} className={cls}>
|
|
<ArrowLeft className="w-4 h-4" />
|
|
{children}
|
|
</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-lg 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>
|
|
);
|
|
}
|
|
|
|
/* 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 = 'Usuń',
|
|
className = '',
|
|
}: {
|
|
children: ReactNode;
|
|
onRemove?: () => void;
|
|
removeLabel?: string;
|
|
className?: string;
|
|
}) {
|
|
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} 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 — paginowane: zakładki dzielone na strony mieszczące się w całości
|
|
(brak uciętych słów), płynny slide między stronami, strzałki wewnątrz
|
|
kontenera (fade, miejsce zarezerwowane — bez skoków layoutu).
|
|
Strona z aktywną zakładką wybierana automatycznie. */
|
|
export function Tabs({
|
|
tabs,
|
|
active,
|
|
onChange,
|
|
}: {
|
|
tabs: { id: string; label: string }[];
|
|
active: string;
|
|
onChange: (id: string) => void;
|
|
}) {
|
|
const wrapRef = useRef<HTMLDivElement>(null);
|
|
const measureRef = useRef<HTMLDivElement>(null);
|
|
const [pages, setPages] = useState<number[][]>(() => [tabs.map((_, i) => i)]);
|
|
const [page, setPage] = useState(0);
|
|
|
|
const recompute = () => {
|
|
const wrap = wrapRef.current;
|
|
const meas = measureRef.current;
|
|
if (!wrap || !meas || meas.children.length === 0) return;
|
|
const GAP = 4;
|
|
const PAD = 8; // p-1 kontenera
|
|
const ARROW = 28 + GAP; // strzałka + odstęp po jednej stronie
|
|
const widths = Array.from(meas.children).map((c) => (c as HTMLElement).offsetWidth);
|
|
const build = (avail: number) => {
|
|
const out: number[][] = [];
|
|
let cur: number[] = [];
|
|
let w = 0;
|
|
widths.forEach((tw, i) => {
|
|
const add = tw + (cur.length ? GAP : 0);
|
|
if (cur.length && w + add > avail) {
|
|
out.push(cur);
|
|
cur = [];
|
|
w = 0;
|
|
}
|
|
cur.push(i);
|
|
w += tw + (cur.length > 1 ? GAP : 0);
|
|
});
|
|
if (cur.length) out.push(cur);
|
|
return out;
|
|
};
|
|
let p = build(wrap.clientWidth - PAD);
|
|
if (p.length > 1) p = build(wrap.clientWidth - PAD - 2 * ARROW);
|
|
setPages(p);
|
|
};
|
|
|
|
useLayoutEffect(() => {
|
|
recompute();
|
|
}, [tabs]);
|
|
|
|
useEffect(() => {
|
|
const onResize = () => recompute();
|
|
window.addEventListener('resize', onResize);
|
|
return () => window.removeEventListener('resize', onResize);
|
|
}, [tabs]);
|
|
|
|
// strona zawierająca aktywną zakładkę (np. przywróconą z pamięci)
|
|
useEffect(() => {
|
|
const idx = tabs.findIndex((t) => t.id === active);
|
|
const p = pages.findIndex((pg) => pg.includes(idx));
|
|
if (p >= 0) setPage(p);
|
|
}, [active, pages, tabs]);
|
|
|
|
const multi = pages.length > 1;
|
|
const safePage = Math.min(page, pages.length - 1);
|
|
|
|
const tabButton = (i: number) => {
|
|
const t = tabs[i];
|
|
return (
|
|
<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-lg 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>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<div
|
|
ref={wrapRef}
|
|
data-slot="tabs"
|
|
className="relative flex items-center gap-1 p-1 bg-input rounded-[10px] border border-line"
|
|
>
|
|
{/* niewidoczny wiersz pomiarowy — te same klasy co docelowe przyciski */}
|
|
<div
|
|
ref={measureRef}
|
|
aria-hidden="true"
|
|
className="pointer-events-none invisible absolute flex gap-1"
|
|
>
|
|
{tabs.map((t) => (
|
|
<span
|
|
key={t.id}
|
|
className="px-3.5 py-2 rounded-lg text-[0.9rem] font-semibold whitespace-nowrap border border-transparent"
|
|
>
|
|
{t.label}
|
|
</span>
|
|
))}
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
aria-label="Poprzednie zakładki"
|
|
onClick={() => setPage((p) => Math.max(0, p - 1))}
|
|
className={`grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-all duration-150 hover:text-ink ${
|
|
multi && safePage > 0 ? 'opacity-100' : 'pointer-events-none opacity-0'
|
|
}`}
|
|
>
|
|
<ChevronLeft className="h-4 w-4" />
|
|
</button>
|
|
|
|
<div className="min-w-0 flex-1 overflow-hidden">
|
|
<div
|
|
className="flex transition-transform duration-200 ease-out"
|
|
style={{
|
|
width: `${pages.length * 100}%`,
|
|
transform: `translateX(-${safePage * (100 / pages.length)}%)`,
|
|
}}
|
|
>
|
|
{pages.map((pg, pi) => (
|
|
<div key={pi} className="flex gap-1" style={{ width: `${100 / pages.length}%` }}>
|
|
{pg.map(tabButton)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
aria-label="Następne zakładki"
|
|
onClick={() => setPage((p) => Math.min(pages.length - 1, p + 1))}
|
|
className={`grid h-7 w-7 shrink-0 cursor-pointer place-items-center rounded-full border border-line bg-card text-dim shadow-card transition-all duration-150 hover:text-ink ${
|
|
multi && safePage < pages.length - 1 ? 'opacity-100' : 'pointer-events-none opacity-0'
|
|
}`}
|
|
>
|
|
<ChevronRight className="h-4 w-4" />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|