feat(admin): per-row badge preview with unconfigured tooltip, Excel-style column filters and search in Contacts/Registrations
- New shared BadgeRender module (resolveText/resolveQr/BadgeElementBody/BadgePagePreview/fitScale) reused by the badge editor and the new read-only preview modal - Registrations: Badge button on every row opens a live preview of that attendee's badge; grayed out with a Uiverse-style APPE-red tooltip when the event has no badge configured - Contacts + Registrations: per-column multi-select filters (LinkedIn/Visibility/Admin; Type/Ticket/Checked in) with Excel-style live counts, plus general search (contacts: first/last/email; registrations: +type) - New Tooltip and ColumnFilter primitives in ui.tsx (portal-based, escape/outside-click close) - e2e: registration-badge-preview.spec.ts (13) + table-filters.spec.ts (13), all green; docs synced Co-authored-by: Stephan Charles <[email protected]> Co-authored-by: Amelia Hernandez <[email protected]> Co-authored-by: Markus Franz <[email protected]> Co-authored-by: Sabrina Hota <[email protected]> Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
1 parent
30ed4388c0
commit
8e38052f4c
10 files changed
+988
-121
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import { QRCodeSVG } from 'qrcode.react';
|
||||
import type { BadgeElement, BadgeQrElement, BadgeSample, BadgeTextElement } from '../lib/types';
|
||||
|
||||
// CSS 96 dpi: 1mm = 3.7795px. Shared by the editor canvas and read-only previews.
|
||||
export const MM = 3.7795275591;
|
||||
|
||||
export const CONTENT_LABEL: Record<BadgeTextElement['content'], string> = {
|
||||
attendee_name: 'Attendee name',
|
||||
full_name: 'Full name',
|
||||
email: 'Email address',
|
||||
ticket_type: 'Ticket type',
|
||||
event_name: 'Event name',
|
||||
company: 'Company / organizer',
|
||||
qr_token: 'QR / badge token',
|
||||
static: 'Static text',
|
||||
};
|
||||
export const QR_LABEL: Record<BadgeQrElement['value'], string> = {
|
||||
qr_token: 'QR / badge token',
|
||||
checkin_url: 'Check-in URL',
|
||||
email: 'Email address',
|
||||
attendee_name: 'Attendee name',
|
||||
static: 'Static value',
|
||||
};
|
||||
|
||||
/** Resolve the bound content of a text element against a sample attendee. */
|
||||
export function resolveText(el: BadgeTextElement, sample: BadgeSample): string {
|
||||
let v: string;
|
||||
switch (el.content) {
|
||||
case 'attendee_name':
|
||||
case 'full_name': v = sample.full_name; break;
|
||||
case 'email': v = sample.email; break;
|
||||
case 'ticket_type': v = sample.ticket_type; break;
|
||||
case 'event_name': v = sample.event_name; break;
|
||||
case 'company': v = sample.company; break;
|
||||
case 'qr_token': v = sample.qr_token; break;
|
||||
default: v = el.text;
|
||||
}
|
||||
if (el.transform === 'uppercase') return v.toUpperCase();
|
||||
if (el.transform === 'lowercase') return v.toLowerCase();
|
||||
if (el.transform === 'capitalize') return v.replace(/\b\w/g, (m) => m.toUpperCase());
|
||||
return v;
|
||||
}
|
||||
|
||||
/** Resolve the encoded value of a QR element against a sample attendee. */
|
||||
export function resolveQr(el: BadgeQrElement, sample: BadgeSample): string {
|
||||
switch (el.value) {
|
||||
case 'qr_token': return sample.qr_token || 'CONVIVO-QR';
|
||||
case 'checkin_url': return `${window.location.origin}/checkin/${sample.qr_token || 'TOKEN'}`;
|
||||
case 'email': return sample.email || '[email protected]';
|
||||
case 'attendee_name': return sample.full_name || 'Attendee';
|
||||
default: return el.static_value || 'https://example.com';
|
||||
}
|
||||
}
|
||||
|
||||
/** The visual body of one badge element (text / QR / image), no editing chrome. */
|
||||
export function BadgeElementBody({ el, sample, pxPerMm }: { el: BadgeElement; sample: BadgeSample; pxPerMm: number }) {
|
||||
if (el.type === 'text') {
|
||||
const text = resolveText(el, sample);
|
||||
return (
|
||||
<div
|
||||
className="badge-text-body"
|
||||
style={{
|
||||
fontFamily: `"${el.font}", sans-serif`,
|
||||
fontSize: el.font_size * (pxPerMm / MM), // pt→mm→px so print scale matches
|
||||
lineHeight: el.line_height,
|
||||
color: el.color,
|
||||
fontWeight: el.bold ? 700 : 400,
|
||||
fontStyle: el.italic ? 'italic' : 'normal',
|
||||
justifyContent: el.align_h === 'left' ? 'flex-start' : el.align_h === 'right' ? 'flex-end' : 'center',
|
||||
alignItems: el.align_v === 'top' ? 'flex-start' : el.align_v === 'bottom' ? 'flex-end' : 'center',
|
||||
textAlign: el.align_h,
|
||||
}}
|
||||
>
|
||||
<span>{text || <span className="badge-el-placeholder">{CONTENT_LABEL[el.content]}</span>}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (el.type === 'qr') {
|
||||
const size = Math.round(Math.min(el.width, el.height) * pxPerMm);
|
||||
return (
|
||||
<div className="badge-qr-body">
|
||||
<QRCodeSVG value={resolveQr(el, sample)} size={Math.max(16, size)} level="M" marginSize={0} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
// image
|
||||
if (!el.src) {
|
||||
return <div className="badge-image-body badge-image-empty">Image</div>;
|
||||
}
|
||||
return <img className="badge-image-body" src={el.src} alt="" style={{ objectFit: el.fit }} />;
|
||||
}
|
||||
|
||||
export interface BadgePageData {
|
||||
page_width_mm: number;
|
||||
page_height_mm: number;
|
||||
elements: BadgeElement[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-only rendering of a badge layout for a specific attendee — the exact
|
||||
* same markup the editor canvas produces, minus the editing chrome.
|
||||
*/
|
||||
export function BadgePagePreview({ layout, sample, pxPerMm, className = '' }: {
|
||||
layout: BadgePageData; sample: BadgeSample; pxPerMm: number; className?: string;
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
className={`badge-page badge-page-static ${className}`}
|
||||
style={{ width: layout.page_width_mm * pxPerMm, height: layout.page_height_mm * pxPerMm }}
|
||||
>
|
||||
{layout.elements.map((el) => (
|
||||
<div
|
||||
key={el.id}
|
||||
className={`badge-el badge-el-${el.type}`}
|
||||
style={{
|
||||
left: el.x * pxPerMm, top: el.y * pxPerMm,
|
||||
width: el.width * pxPerMm, height: el.height * pxPerMm,
|
||||
transform: `rotate(${el.rotation}deg)`,
|
||||
cursor: 'default',
|
||||
}}
|
||||
>
|
||||
<BadgeElementBody el={el} sample={sample} pxPerMm={pxPerMm} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Fit a layout inside a box of `maxW` × `maxH` px, returning the px-per-mm scale. */
|
||||
export function fitScale(layout: BadgePageData, maxW: number, maxH: number): number {
|
||||
return Math.min(maxW / layout.page_width_mm, maxH / layout.page_height_mm);
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { type ReactNode } from 'react';
|
||||
import { cloneElement, isValidElement, useEffect, useRef, useState, useId, type ReactElement, type ReactNode } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { Filter } from 'lucide-react';
|
||||
import { QRCodeSVG } from 'qrcode.react';
|
||||
|
||||
const AV_COLORS = ['#d50000', '#20b2aa', '#e11d48', '#0d9488', '#f59e0b', '#0ea5e9', '#dc2626', '#7c3aed', '#16a34a', '#ea580c', '#0891b2', '#db2777'];
|
||||
@@ -108,3 +110,139 @@ export function Toggle({
|
||||
</label>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hover/focus tooltip rendered through a portal, so it is never clipped by
|
||||
* scrollable or `overflow: hidden` ancestors (e.g. data tables). The card
|
||||
* visual follows the Uiverse dark-glass tooltip already used in Interfaces,
|
||||
* re-themed to the APPE red accent. Wrap any element — including a disabled
|
||||
* button, whose own mouse events browsers suppress.
|
||||
*/
|
||||
export function Tooltip({ content, title, children }: {
|
||||
content: ReactNode;
|
||||
title?: string;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const anchor = useRef<HTMLSpanElement>(null);
|
||||
const id = useId();
|
||||
const [pos, setPos] = useState<{ x: number; y: number } | null>(null);
|
||||
const show = () => {
|
||||
const r = anchor.current?.getBoundingClientRect();
|
||||
if (r) setPos({ x: r.left + r.width / 2, y: r.top });
|
||||
};
|
||||
const hide = () => setPos(null);
|
||||
// Point the control at the card so screen readers announce the explanation
|
||||
// when it is focused (focus also reveals it visually).
|
||||
const child = isValidElement(children)
|
||||
? cloneElement(children as ReactElement<{ 'aria-describedby'?: string }>, { 'aria-describedby': id })
|
||||
: children;
|
||||
return (
|
||||
<span className="tip-anchor" ref={anchor} onMouseEnter={show} onMouseLeave={hide} onFocus={show} onBlur={hide}>
|
||||
{child}
|
||||
{pos && createPortal(
|
||||
<div className="tip-card" role="tooltip" id={id} style={{ left: pos.x, top: pos.y - 10 }}>
|
||||
{title && (
|
||||
<div className="tip-head">
|
||||
<span className="tip-ico" aria-hidden="true">
|
||||
<svg viewBox="0 0 20 20" fill="currentColor" width="15" height="15">
|
||||
<path clipRule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" fillRule="evenodd" />
|
||||
</svg>
|
||||
</span>
|
||||
<strong>{title}</strong>
|
||||
</div>
|
||||
)}
|
||||
<p>{content}</p>
|
||||
<span className="tip-arrow" aria-hidden="true" />
|
||||
</div>,
|
||||
document.body,
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/** One selectable value inside a `ColumnFilter`, with a live result count. */
|
||||
export interface ColumnFilterOption {
|
||||
value: string;
|
||||
label: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excel-style multi-select filter for a table column. Rendered through a portal
|
||||
* so the dropdown is never clipped by the table's `overflow: hidden`. An empty
|
||||
* selection means "no filter" (everything matches).
|
||||
*/
|
||||
export function ColumnFilter({ label, options, selected, onChange }: {
|
||||
label: string;
|
||||
options: ColumnFilterOption[];
|
||||
selected: string[];
|
||||
onChange: (next: string[]) => void;
|
||||
}) {
|
||||
const btnRef = useRef<HTMLButtonElement>(null);
|
||||
const popRef = useRef<HTMLDivElement>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [pos, setPos] = useState<{ left: number; top: number } | null>(null);
|
||||
const active = selected.length > 0;
|
||||
|
||||
const toggleOpen = () => {
|
||||
if (!open) {
|
||||
const r = btnRef.current?.getBoundingClientRect();
|
||||
if (r) setPos({ left: Math.min(Math.max(8, r.left), window.innerWidth - 240), top: r.bottom + 6 });
|
||||
}
|
||||
setOpen((o) => !o);
|
||||
};
|
||||
const toggleValue = (value: string) =>
|
||||
onChange(selected.includes(value) ? selected.filter((v) => v !== value) : [...selected, value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const onDown = (e: MouseEvent) => {
|
||||
const t = e.target as Node;
|
||||
if (popRef.current?.contains(t) || btnRef.current?.contains(t)) return;
|
||||
setOpen(false);
|
||||
};
|
||||
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); };
|
||||
const onScroll = () => setOpen(false);
|
||||
window.addEventListener('mousedown', onDown);
|
||||
window.addEventListener('keydown', onKey);
|
||||
window.addEventListener('scroll', onScroll, true);
|
||||
return () => {
|
||||
window.removeEventListener('mousedown', onDown);
|
||||
window.removeEventListener('keydown', onKey);
|
||||
window.removeEventListener('scroll', onScroll, true);
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
return (
|
||||
<span className="colfilter">
|
||||
<button
|
||||
ref={btnRef}
|
||||
type="button"
|
||||
className={`colfilter-btn ${active ? 'on' : ''}`}
|
||||
onClick={toggleOpen}
|
||||
aria-label={`Filter ${label}`}
|
||||
aria-expanded={open}
|
||||
title={`Filter ${label}`}
|
||||
>
|
||||
<Filter size={13} aria-hidden="true" />
|
||||
{active && <span className="colfilter-count">{selected.length}</span>}
|
||||
</button>
|
||||
{open && pos && createPortal(
|
||||
<div className="colfilter-pop" ref={popRef} role="group" aria-label={`Filter ${label}`} style={{ left: pos.left, top: pos.top }}>
|
||||
<div className="colfilter-head">
|
||||
<strong>{label}</strong>
|
||||
{active && <button type="button" className="colfilter-clear" onClick={() => onChange([])}>Clear</button>}
|
||||
</div>
|
||||
{options.map((o) => (
|
||||
<label key={o.value} className="colfilter-opt">
|
||||
<input type="checkbox" checked={selected.includes(o.value)} onChange={() => toggleValue(o.value)} />
|
||||
<span className="lbl">{o.label}</span>
|
||||
<span className="cnt">{o.count}</span>
|
||||
</label>
|
||||
))}
|
||||
</div>,
|
||||
document.body,
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user