362 lines
12 KiB
TypeScript
362 lines
12 KiB
TypeScript
import { useEffect, type ReactNode } from 'react';
|
|
import { Link } from 'react-router';
|
|
import { useTranslation } from 'react-i18next';
|
|
import type { UseQueryResult } from '@tanstack/react-query';
|
|
import {
|
|
ArrowDownRight,
|
|
ArrowUpRight,
|
|
Bell,
|
|
Check,
|
|
Circle,
|
|
Clock3,
|
|
MoveUpRight,
|
|
} from 'lucide-react';
|
|
import type { Status, Merchant, Task, StageEvent } from '@merchantflow/contracts';
|
|
import { Badge } from '@merchantflow/ui/components/badge';
|
|
import { Button, buttonVariants } from '@merchantflow/ui/components/button';
|
|
import { LoadingState, EmptyState, ErrorState } from '@merchantflow/ui/components/states';
|
|
import { cn } from '@merchantflow/ui/lib/utils';
|
|
import { ApiError } from '../lib/api';
|
|
import { useFormat } from '../lib/format';
|
|
export function Brand({ compact = false }: { compact?: boolean }) {
|
|
return (
|
|
<Link
|
|
to="/"
|
|
aria-label="MerchantFlow"
|
|
className="inline-flex min-h-11 items-center gap-2.5 rounded-md"
|
|
>
|
|
<svg className="size-9 shrink-0" aria-hidden="true" viewBox="0 0 40 40">
|
|
<rect width="40" height="40" rx="12" fill="#d1ef7c" />
|
|
<path
|
|
d="M10 27V13l10 8 10-8v14M20 21v8"
|
|
fill="none"
|
|
stroke="#28321b"
|
|
strokeWidth="3.2"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
/>
|
|
</svg>
|
|
{!compact && (
|
|
<span className="text-xl font-semibold tracking-[-.05em]">
|
|
merchantflow<span className="text-[#6a8a34]">.</span>
|
|
</span>
|
|
)}
|
|
</Link>
|
|
);
|
|
}
|
|
export function LanguageSwitch() {
|
|
const { t, i18n } = useTranslation();
|
|
return (
|
|
<div
|
|
role="group"
|
|
aria-label={t('common.language')}
|
|
className="inline-flex shrink-0 rounded-lg border bg-card p-0.5"
|
|
>
|
|
{['en', 'ru'].map((l) => (
|
|
<button
|
|
key={l}
|
|
type="button"
|
|
aria-label={l === 'en' ? 'English' : 'Русский'}
|
|
aria-pressed={i18n.language === l}
|
|
onClick={() => void i18n.changeLanguage(l)}
|
|
className={cn(
|
|
'min-h-11 min-w-11 rounded-md px-2 text-xs font-semibold',
|
|
i18n.language === l ? 'bg-secondary text-foreground' : 'text-muted-foreground',
|
|
)}
|
|
>
|
|
{l.toUpperCase()}
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
export function LinkButton({
|
|
to,
|
|
children,
|
|
variant = 'default',
|
|
className,
|
|
state,
|
|
}: {
|
|
to: string;
|
|
state?: { returnTo: string };
|
|
children: ReactNode;
|
|
variant?: 'default' | 'outline' | 'ghost';
|
|
className?: string;
|
|
}) {
|
|
return (
|
|
<Link to={to} state={state} className={cn(buttonVariants({ variant }), className)}>
|
|
{children}
|
|
</Link>
|
|
);
|
|
}
|
|
export function PageHeader({
|
|
title,
|
|
description,
|
|
eyebrow,
|
|
action,
|
|
}: {
|
|
title: string;
|
|
description: string;
|
|
eyebrow?: string;
|
|
action?: ReactNode;
|
|
}) {
|
|
useEffect(() => {
|
|
document.title = `${title} · MerchantFlow`;
|
|
}, [title]);
|
|
return (
|
|
<div className="mb-7 flex flex-col gap-5 sm:flex-row sm:items-end sm:justify-between">
|
|
<div>
|
|
{eyebrow && <p className="section-label mb-2.5">{eyebrow}</p>}
|
|
<h1 className="page-title">{title}</h1>
|
|
<p className="mt-2 max-w-2xl text-sm leading-relaxed text-muted-foreground">
|
|
{description}
|
|
</p>
|
|
</div>
|
|
{action && <div className="shrink-0">{action}</div>}
|
|
</div>
|
|
);
|
|
}
|
|
export function SectionHeader({
|
|
title,
|
|
description,
|
|
action,
|
|
}: {
|
|
title: string;
|
|
description?: string;
|
|
action?: ReactNode;
|
|
}) {
|
|
return (
|
|
<div className="flex flex-wrap items-start justify-between gap-3 px-5 pt-5 sm:px-6">
|
|
<div>
|
|
<h2 className="text-base font-semibold tracking-tight">{title}</h2>
|
|
{description && (
|
|
<p className="mt-1 text-xs leading-relaxed text-muted-foreground">{description}</p>
|
|
)}
|
|
</div>
|
|
{action}
|
|
</div>
|
|
);
|
|
}
|
|
export const statusStyles: Record<Status, string> = {
|
|
NEW_LEAD: 'bg-slate-100 text-slate-700',
|
|
CONTACTED: 'bg-blue-50 text-blue-800',
|
|
QUALIFIED: 'bg-indigo-50 text-indigo-800',
|
|
KYB: 'bg-amber-50 text-amber-800',
|
|
COMPLIANCE_REVIEW: 'bg-orange-50 text-orange-800',
|
|
INTEGRATION: 'bg-violet-50 text-violet-800',
|
|
TESTING: 'bg-teal-50 text-teal-800',
|
|
LIVE: 'bg-[#edf5da] text-[#46641d]',
|
|
REJECTED: 'bg-rose-50 text-rose-800',
|
|
};
|
|
export const stageColors: Record<Status, string> = {
|
|
NEW_LEAD: '#aebbb0',
|
|
CONTACTED: '#8ba5ce',
|
|
QUALIFIED: '#9e9bc9',
|
|
KYB: '#ddbd78',
|
|
COMPLIANCE_REVIEW: '#dba879',
|
|
INTEGRATION: '#b49acf',
|
|
TESTING: '#8ebcb0',
|
|
LIVE: '#a6c65e',
|
|
REJECTED: '#cf969e',
|
|
};
|
|
export function StatusBadge({ status }: { status: Status }) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<Badge
|
|
className={cn('gap-1.5 border-0 px-2 py-1 text-[11px] font-medium', statusStyles[status])}
|
|
>
|
|
<span className="size-1.5 shrink-0 rounded-full bg-current" aria-hidden="true" />
|
|
{t(`statuses.${status}`)}
|
|
</Badge>
|
|
);
|
|
}
|
|
export function Avatar({ name, small = false }: { name: string; small?: boolean }) {
|
|
const sum = [...name].reduce((a, c) => a + c.charCodeAt(0), 0);
|
|
return (
|
|
<div
|
|
aria-hidden="true"
|
|
className={cn(
|
|
'flex shrink-0 items-center justify-center rounded-xl border border-black/5 font-semibold',
|
|
small ? 'size-9 text-xs' : 'size-11 text-sm',
|
|
[
|
|
'bg-[#ecf0e4] text-[#56693d]',
|
|
'bg-[#e9edf5] text-[#596a88]',
|
|
'bg-[#f3ebe4] text-[#8a6b52]',
|
|
'bg-[#ede9f2] text-[#78678b]',
|
|
][sum % 4],
|
|
)}
|
|
>
|
|
{name
|
|
.split(' ')
|
|
.map((n) => n[0])
|
|
.slice(0, 2)
|
|
.join('')}
|
|
</div>
|
|
);
|
|
}
|
|
export function QueryBoundary<T>({
|
|
query,
|
|
children,
|
|
}: {
|
|
query: UseQueryResult<T, Error>;
|
|
children: (data: T) => ReactNode;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
if (query.isPending) return <LoadingState label={t('common.loading')} />;
|
|
if (query.isError)
|
|
return (
|
|
<ErrorState
|
|
title={t('common.errorTitle')}
|
|
description={t(
|
|
`errors.${query.error instanceof ApiError ? query.error.code : 'INTERNAL_ERROR'}`,
|
|
{ defaultValue: t('errors.INTERNAL_ERROR') },
|
|
)}
|
|
retryLabel={t('common.retry')}
|
|
onRetry={() => void query.refetch()}
|
|
/>
|
|
);
|
|
return <>{children(query.data)}</>;
|
|
}
|
|
export function MerchantCard({ merchant, className }: { merchant: Merchant; className?: string }) {
|
|
const { t } = useTranslation();
|
|
const f = useFormat();
|
|
return (
|
|
<Link
|
|
to={`/app/merchants/${merchant.id}`}
|
|
className={cn('surface hover-surface block p-4', className)}
|
|
>
|
|
<div className="flex items-center gap-3">
|
|
<Avatar name={merchant.name} small />
|
|
<div className="min-w-0">
|
|
<h3 className="truncate text-sm font-semibold">{merchant.name}</h3>
|
|
<p className="mt-0.5 text-xs text-muted-foreground">
|
|
{merchant.industry ? t(`industry.${merchant.industry}`) : '—'}
|
|
</p>
|
|
</div>
|
|
<MoveUpRight
|
|
aria-hidden="true"
|
|
className="ml-auto size-3.5 shrink-0 text-muted-foreground"
|
|
/>
|
|
</div>
|
|
<div className="mt-5 flex items-center justify-between gap-2 text-xs text-muted-foreground">
|
|
<span>{merchant.owner?.name.split(' ')[0] ?? t('workflow.unassigned')}</span>
|
|
<span className="flex items-center gap-1">
|
|
<Clock3 className="size-3" aria-hidden="true" />
|
|
{f.relative(merchant.stageEnteredAt)}
|
|
</span>
|
|
</div>
|
|
</Link>
|
|
);
|
|
}
|
|
export function TaskRow({ task, compact = false }: { task: Task; compact?: boolean }) {
|
|
const { t } = useTranslation();
|
|
const f = useFormat();
|
|
const done = task.status === 'DONE';
|
|
const overdue = !done && new Date(task.dueAt).getTime() < Date.now();
|
|
return (
|
|
<div className={cn('flex gap-3 border-b px-5 py-4 last:border-0 sm:px-6', compact && 'py-4')}>
|
|
<div
|
|
className={cn(
|
|
'mt-0.5 flex size-6 shrink-0 items-center justify-center rounded-full border',
|
|
done ? 'border-[#ccdfab] bg-accent text-[#46641d]' : 'text-muted-foreground',
|
|
)}
|
|
>
|
|
{done ? (
|
|
<Check className="size-3.5" aria-label={t('tasks.completed')} />
|
|
) : (
|
|
<Circle className="size-2" aria-hidden="true" />
|
|
)}
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<p className={cn('text-sm leading-relaxed', done && 'text-muted-foreground')}>
|
|
{task.title}
|
|
</p>
|
|
<div className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs">
|
|
<Link to={`/app/merchants/${task.merchant.id}`} className="font-medium hover:underline">
|
|
{task.merchant.name}
|
|
</Link>
|
|
<span className="text-muted-foreground" aria-hidden="true">
|
|
·
|
|
</span>
|
|
<span className={overdue ? 'text-amber-800' : 'text-muted-foreground'}>
|
|
{done ? t('tasks.completed') : t('tasks.due', { date: f.shortDate(task.dueAt) })}
|
|
{overdue && ` · ${t('tasks.OVERDUE')}`}
|
|
</span>
|
|
</div>
|
|
{!compact && (
|
|
<div className="mt-2 flex flex-wrap gap-x-4 gap-y-1 text-xs text-muted-foreground">
|
|
<span>{t('tasks.assigned', { name: task.assignee.name })}</span>
|
|
{!done && task.remindAt && (
|
|
<span className="inline-flex items-center gap-1">
|
|
<Bell className="size-3" aria-hidden="true" />
|
|
{new Date(task.remindAt).getTime() <= Date.now()
|
|
? t('tasks.reminderDue')
|
|
: `${t('tasks.reminder')}: ${f.date(task.remindAt)}`}
|
|
</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
export function ActivityFeed({
|
|
events,
|
|
detail = false,
|
|
}: {
|
|
events: StageEvent[];
|
|
detail?: boolean;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const f = useFormat();
|
|
if (!events.length) return <EmptyState title={t('dashboard.emptyActivity')} />;
|
|
return (
|
|
<div className="px-5 py-2 sm:px-6">
|
|
{events.map((event, i) => (
|
|
<div key={event.id} className="relative flex gap-3 py-4">
|
|
{i < events.length - 1 && (
|
|
<div
|
|
className="absolute top-11 bottom-[-16px] left-[15px] w-px bg-border"
|
|
aria-hidden="true"
|
|
/>
|
|
)}
|
|
<div className="z-10 flex size-8 shrink-0 items-center justify-center rounded-full border bg-card">
|
|
{event.fromStatus === 'COMPLIANCE_REVIEW' && event.toStatus === 'KYB' ? (
|
|
<ArrowDownRight className="size-3.5 text-amber-700" aria-hidden="true" />
|
|
) : (
|
|
<ArrowUpRight className="size-3.5 text-muted-foreground" aria-hidden="true" />
|
|
)}
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
{!detail && (
|
|
<Link
|
|
to={`/app/merchants/${event.merchant.id}`}
|
|
className="text-sm font-medium hover:underline"
|
|
>
|
|
{event.merchant.name}
|
|
</Link>
|
|
)}
|
|
<p className="mt-0.5 text-xs leading-relaxed text-muted-foreground">
|
|
{t(event.fromStatus ? 'dashboard.moved' : 'dashboard.added')}{' '}
|
|
<span className="font-medium text-foreground">{t(`statuses.${event.toStatus}`)}</span>
|
|
<span className="mx-1.5">·</span>
|
|
{event.actor?.name ?? t('workflow.system')}
|
|
</p>
|
|
{event.note && (
|
|
<p className="mt-2 rounded-lg bg-muted p-3 text-xs leading-relaxed">{event.note}</p>
|
|
)}
|
|
<time
|
|
dateTime={event.occurredAt}
|
|
title={f.date(event.occurredAt)}
|
|
className="mt-1 block text-[11px] text-muted-foreground"
|
|
>
|
|
{f.relative(event.occurredAt)}
|
|
</time>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
export { Button, EmptyState };
|