Files
collabry-components/src/components/Group/Group.tsx
T
2026-11-04 22:31:12 +01:00

189 lines
7.0 KiB
TypeScript

import type { ReactNode } from 'react';
import { CalendarClock, ChevronRight, MoreHorizontal, Plus, Users } from 'lucide-react';
import { Avatar, AvatarStack, type Person } from '../Avatar/Avatar';
import { RoleBadge } from '../Badge/Badge';
import { Button } from '../Button/Button';
import { Card } from '../Card/Card';
import { Menu } from '../Menu/Menu';
import { ProgressBar } from '../Progress/Progress';
export interface GroupData {
id: string;
name: string;
color: string;
members: Person[];
done: number;
total: number;
description?: string;
role?: 'admin' | 'member';
/** Next deadline, e.g. "Nächste: Fr, 27. Nov". */
next?: string;
}
const pct = (g: Pick<GroupData, 'done' | 'total'>) => (g.total ? Math.round((g.done / g.total) * 100) : 0);
export interface GroupRowProps {
group: GroupData;
href?: string;
}
/** Compact group entry with members and progress, used on the dashboard. */
export function GroupRow({ group: g, href = '#' }: GroupRowProps) {
const p = pct(g);
return (
<li className="list-none">
<a href={href} className="-mx-2.5 block rounded-lg px-2.5 py-4 text-fg hover:bg-hover-row hover:text-fg">
<div className="flex items-center gap-2.5">
<span className="h-2.5 w-2.5 flex-none rounded-[3px]" style={{ background: g.color }} />
<span className="min-w-0 flex-auto truncate font-bold">{g.name}</span>
<AvatarStack people={g.members} size={22} />
<ChevronRight size={16} strokeWidth={1.75} aria-hidden="true" className="text-fg-faint" />
</div>
<div className="mt-3 flex items-center gap-3 pl-5">
<ProgressBar value={p} color={g.color} label={`${g.name}: ${p} % erledigt`} className="flex-auto" />
<div className="w-[84px] flex-none text-right text-xs text-fg-muted tabular-nums">
<strong className="text-fg">{p}%</strong> · {g.done}/{g.total}
</div>
</div>
</a>
</li>
);
}
export interface GroupCardProps {
group: GroupData;
href?: string;
/** Items of the "…" menu at the top right (`MenuItem`, `MenuLink`, `MenuSeparator`). */
menu?: ReactNode;
}
/** Group overview card on the Gruppen page. */
export function GroupCard({ group: g, href = '#', menu }: GroupCardProps) {
const p = pct(g);
const headingId = `gc-${g.id}`;
return (
<Card as="article" aria-labelledby={headingId} padding="sm">
<div className="flex items-start gap-3">
<span
className="box-border flex h-10 w-10 flex-none items-center justify-center rounded-[11px] border"
style={{
borderColor: `color-mix(in srgb, ${g.color} 45%, transparent)`,
background: `color-mix(in srgb, ${g.color} 18%, transparent)`,
color: g.color,
}}
>
<Users size={18} strokeWidth={1.75} aria-hidden="true" />
</span>
<div className="min-w-0 flex-auto">
<h2 id={headingId} className="m-0 truncate text-[16px] leading-tight font-bold">
<a href={href} className="text-inherit hover:text-inherit">
{g.name}
</a>
</h2>
<RoleBadge role={g.role} className="mt-[5px]" />
</div>
{menu && (
<Menu
label={`Aktionen für ${g.name}`}
trigger={
<Button
variant="ghost"
size="sm"
iconOnly
aria-label={`Aktionen für ${g.name}`}
className="text-fg-subtle data-[state=open]:bg-hover"
>
<MoreHorizontal size={16} strokeWidth={2.5} />
</Button>
}
>
{menu}
</Menu>
)}
</div>
{g.description !== undefined && (
<p className="m-0 min-h-[39px] text-sm leading-normal text-fg-muted">{g.description}</p>
)}
<div>
<div className="flex items-baseline justify-between gap-2 text-xs text-fg-subtle">
<span>Fortschritt</span>
<span>
<strong className="text-sm text-fg">{p}%</strong> · {g.done}/{g.total} Aufgaben
</span>
</div>
<ProgressBar value={p} color={g.color} label={`${p} % erledigt`} className="mt-2" />
</div>
<div className="mt-auto flex items-center justify-between gap-3 border-t border-line-subtle pt-3.5">
<span className="flex items-center gap-2 text-xs text-fg-subtle">
<AvatarStack people={g.members} size={26} max={4} />
{g.members.length} Mitglieder
</span>
{g.next && (
<span className="flex min-w-0 items-center gap-1.5 text-xs text-fg-subtle">
<CalendarClock size={14} strokeWidth={1.75} aria-hidden="true" className="flex-none" />
<span className="truncate">{g.next}</span>
</span>
)}
</div>
</Card>
);
}
export interface NewGroupTileProps {
onClick?: () => void;
title?: string;
description?: string;
}
/** Dashed tile that starts creating a group. */
export function NewGroupTile({
onClick,
title = 'Neue Gruppe erstellen',
description = 'Lade dein Team ein und behaltet Aufgaben und Deadlines gemeinsam im Blick.',
}: NewGroupTileProps) {
return (
<button
type="button"
onClick={onClick}
className="flex min-h-[260px] flex-col items-center justify-center gap-2.5 rounded-[18px] border-[1.5px] border-dashed border-line-heavy bg-transparent p-6 text-center text-fg-muted hover:border-line-hover hover:bg-hover-row"
>
<span className="flex h-11 w-11 items-center justify-center rounded-xl border border-line-strong bg-surface text-fg">
<Plus size={20} strokeWidth={1.75} aria-hidden="true" />
</span>
<span className="font-bold text-fg">{title}</span>
<span className="max-w-[240px] text-sm leading-normal">{description}</span>
</button>
);
}
export interface InviteBannerProps {
from: Person;
groupName: string;
onAccept?: () => void;
onDecline?: () => void;
acceptState?: 'idle' | 'loading' | 'success';
declineState?: 'idle' | 'loading' | 'success';
}
/** Pending group invitation, shown above the group grid. */
export function InviteBanner({ from, groupName, onAccept, onDecline, acceptState, declineState }: InviteBannerProps) {
const busy = acceptState === 'loading' || declineState === 'loading';
return (
<Card as="div" padding="none" innerClassName="flex-row flex-wrap items-center gap-3.5 px-4 py-3.5">
<Avatar person={from} size={36} aria-hidden="true" />
<div className="min-w-0 flex-[1_1_280px] text-base text-fg-muted">
<strong className="text-fg">{from.name}</strong> hat dich in die Gruppe <strong className="text-fg">„{groupName}“</strong>{' '}
eingeladen.
</div>
<div className="flex gap-2">
<Button variant="secondary" onClick={onDecline} state={declineState} disabled={busy}>
Ablehnen
</Button>
<Button variant="primary" onClick={onAccept} state={acceptState} disabled={busy}>
Beitreten
</Button>
</div>
</Card>
);
}