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 (
{!compact && (
merchantflow.
)}
);
}
export function LanguageSwitch() {
const { t, i18n } = useTranslation();
return (
{['en', 'ru'].map((l) => (
))}
);
}
export function LinkButton({
to,
children,
variant = 'default',
className,
state,
}: {
to: string;
state?: { returnTo: string };
children: ReactNode;
variant?: 'default' | 'outline' | 'ghost';
className?: string;
}) {
return (
{children}
);
}
export function PageHeader({
title,
description,
eyebrow,
action,
}: {
title: string;
description: string;
eyebrow?: string;
action?: ReactNode;
}) {
useEffect(() => {
document.title = `${title} · MerchantFlow`;
}, [title]);
return (
{eyebrow &&
{eyebrow}
}
{title}
{description}
{action &&
{action}
}
);
}
export function SectionHeader({
title,
description,
action,
}: {
title: string;
description?: string;
action?: ReactNode;
}) {
return (
{title}
{description && (
{description}
)}
{action}
);
}
export const statusStyles: Record = {
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 = {
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 (
{t(`statuses.${status}`)}
);
}
export function Avatar({ name, small = false }: { name: string; small?: boolean }) {
const sum = [...name].reduce((a, c) => a + c.charCodeAt(0), 0);
return (
{name
.split(' ')
.map((n) => n[0])
.slice(0, 2)
.join('')}
);
}
export function QueryBoundary({
query,
children,
}: {
query: UseQueryResult;
children: (data: T) => ReactNode;
}) {
const { t } = useTranslation();
if (query.isPending) return ;
if (query.isError)
return (
void query.refetch()}
/>
);
return <>{children(query.data)}>;
}
export function MerchantCard({ merchant, className }: { merchant: Merchant; className?: string }) {
const { t } = useTranslation();
const f = useFormat();
return (
{merchant.name}
{merchant.industry ? t(`industry.${merchant.industry}`) : '—'}
{merchant.owner?.name.split(' ')[0] ?? t('workflow.unassigned')}
{f.relative(merchant.stageEnteredAt)}
);
}
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 (
{done ? (
) : (
)}
{task.title}
{task.merchant.name}
·
{done ? t('tasks.completed') : t('tasks.due', { date: f.shortDate(task.dueAt) })}
{overdue && ` · ${t('tasks.OVERDUE')}`}
{!compact && (
{t('tasks.assigned', { name: task.assignee.name })}
{!done && task.remindAt && (
{new Date(task.remindAt).getTime() <= Date.now()
? t('tasks.reminderDue')
: `${t('tasks.reminder')}: ${f.date(task.remindAt)}`}
)}
)}
);
}
export function ActivityFeed({
events,
detail = false,
}: {
events: StageEvent[];
detail?: boolean;
}) {
const { t } = useTranslation();
const f = useFormat();
if (!events.length) return ;
return (
{events.map((event, i) => (
{i < events.length - 1 && (
)}
{event.fromStatus === 'COMPLIANCE_REVIEW' && event.toStatus === 'KYB' ? (
) : (
)}
{!detail && (
{event.merchant.name}
)}
{t(event.fromStatus ? 'dashboard.moved' : 'dashboard.added')}{' '}
{t(`statuses.${event.toStatus}`)}
·
{event.actor?.name ?? t('workflow.system')}
{event.note && (
{event.note}
)}
))}
);
}
export { Button, EmptyState };