mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
ba70ad28b8
Arabic runs the app right to left, but margins, paddings, insets, text alignment, borders and corner radii were written for the left side, so icons sat against the wrong edge of their labels, indents and badges pointed the wrong way and amount columns aligned left. They now use the logical forms (ms-, ps-, start-, text-start, marginInlineStart, insetInlineEnd and their CSS equivalents), which are the very same sides in a left to right language. Geometry stays physical on purpose: maps, positions measured from the pointer or a bounding box, time axes, sliders, transforms and print output. Amounts drawn as several spans keep their order in a right to left row, the time picker keeps hours before minutes, and two switches whose thumb slid out of its track in Arabic now move the right way. lint:rtl counts what is still physical per file against a baseline that may only shrink (1512 uses in 370 files before, 319 in 113 now), and CI runs it, so new code reaches for the logical forms.
193 lines
8.1 KiB
TypeScript
193 lines
8.1 KiB
TypeScript
import { createPortal } from 'react-dom'
|
|
import { useState, useEffect, useRef, useCallback } from 'react'
|
|
import { Pencil, Users, Check } from 'lucide-react'
|
|
import type { BudgetItemMember } from '../../types'
|
|
|
|
export interface TripMember {
|
|
id: number
|
|
username: string
|
|
avatar_url?: string | null
|
|
is_guest?: boolean
|
|
}
|
|
|
|
// ── Chip with custom tooltip ─────────────────────────────────────────────────
|
|
interface ChipWithTooltipProps {
|
|
label: string
|
|
avatarUrl: string | null
|
|
size?: number
|
|
paid?: boolean
|
|
onClick?: () => void
|
|
}
|
|
|
|
export function ChipWithTooltip({ label, avatarUrl, size = 20, paid, onClick }: ChipWithTooltipProps) {
|
|
const [hover, setHover] = useState(false)
|
|
const [pos, setPos] = useState({ top: 0, left: 0 })
|
|
const ref = useRef<HTMLElement | null>(null)
|
|
|
|
const onEnter = () => {
|
|
if (ref.current) {
|
|
const rect = ref.current.getBoundingClientRect()
|
|
setPos({ top: rect.top - 6, left: rect.left + rect.width / 2 })
|
|
}
|
|
setHover(true)
|
|
}
|
|
|
|
const borderColor = paid ? '#22c55e' : 'var(--border-primary)'
|
|
const bg = paid ? 'rgba(34,197,94,0.15)' : 'var(--bg-tertiary)'
|
|
|
|
// Identical for both shells — a clickable chip is a real button so it can be
|
|
// tabbed to and pressed, a decorative one stays a plain div.
|
|
const chipStyle = {
|
|
width: size, height: size, borderRadius: '50%', border: `2px solid ${borderColor}`,
|
|
background: bg, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
fontSize: size * 0.4, fontWeight: 700, color: paid ? '#16a34a' : 'var(--text-muted)',
|
|
overflow: 'hidden', flexShrink: 0, cursor: onClick ? 'pointer' : 'default',
|
|
transition: 'border-color 0.15s, background 0.15s',
|
|
padding: 0, fontFamily: 'inherit',
|
|
}
|
|
const inner = avatarUrl
|
|
? <img src={avatarUrl} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
|
|
: label?.[0]?.toUpperCase()
|
|
|
|
return (
|
|
<>
|
|
{onClick ? (
|
|
<button type="button" ref={el => { ref.current = el }} aria-label={label}
|
|
onMouseEnter={onEnter} onMouseLeave={() => setHover(false)}
|
|
onClick={onClick} style={chipStyle}>
|
|
{inner}
|
|
</button>
|
|
) : (
|
|
<div ref={el => { ref.current = el }} onMouseEnter={onEnter} onMouseLeave={() => setHover(false)}
|
|
style={chipStyle}>
|
|
{inner}
|
|
</div>
|
|
)}
|
|
{hover && createPortal(
|
|
<div style={{
|
|
position: 'fixed', top: pos.top, left: pos.left, transform: 'translate(-50%, -100%)',
|
|
pointerEvents: 'none', zIndex: 10000, whiteSpace: 'nowrap',
|
|
display: 'flex', alignItems: 'center', gap: 5,
|
|
background: 'var(--bg-card, white)', color: 'var(--text-primary, #111827)',
|
|
fontSize: 'calc(11px * var(--fs-scale-caption, 1))', fontWeight: 500, padding: '5px 10px', borderRadius: 8,
|
|
boxShadow: '0 4px 12px rgba(0,0,0,0.15)', border: '1px solid var(--border-faint, #e5e7eb)',
|
|
}}>
|
|
{label}
|
|
{paid && (
|
|
<span style={{
|
|
fontSize: 'calc(9px * var(--fs-scale-caption, 1))', fontWeight: 700, padding: '1px 5px', borderRadius: 4,
|
|
background: 'rgba(34,197,94,0.15)', color: '#16a34a',
|
|
textTransform: 'uppercase', letterSpacing: '0.03em',
|
|
}}>Paid</span>
|
|
)}
|
|
</div>,
|
|
document.body
|
|
)}
|
|
</>
|
|
)
|
|
}
|
|
|
|
// ── Budget Member Chips (for Persons column) ────────────────────────────────
|
|
interface BudgetMemberChipsProps {
|
|
members?: BudgetItemMember[]
|
|
tripMembers?: TripMember[]
|
|
onSetMembers: (memberIds: number[]) => void
|
|
onTogglePaid?: (userId: number, paid: boolean) => void
|
|
compact?: boolean
|
|
readOnly?: boolean
|
|
}
|
|
|
|
export default function BudgetMemberChips({ members = [], tripMembers = [], onSetMembers, onTogglePaid, compact = true, readOnly = false }: BudgetMemberChipsProps) {
|
|
const chipSize = compact ? 20 : 30
|
|
const btnSize = compact ? 18 : 28
|
|
const iconSize = compact ? (members.length > 0 ? 8 : 9) : (members.length > 0 ? 12 : 14)
|
|
const [showDropdown, setShowDropdown] = useState(false)
|
|
const [dropPos, setDropPos] = useState({ top: 0, left: 0 })
|
|
const btnRef = useRef<HTMLButtonElement>(null)
|
|
const dropRef = useRef<HTMLDivElement>(null)
|
|
|
|
const openDropdown = useCallback(() => {
|
|
if (btnRef.current) {
|
|
const rect = btnRef.current.getBoundingClientRect()
|
|
setDropPos({ top: rect.bottom + 4, left: rect.left + rect.width / 2 })
|
|
}
|
|
setShowDropdown(v => !v)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (!showDropdown) return
|
|
const close = (e: MouseEvent) => {
|
|
if (dropRef.current && dropRef.current.contains(e.target as Node)) return
|
|
if (btnRef.current && btnRef.current.contains(e.target as Node)) return
|
|
setShowDropdown(false)
|
|
}
|
|
document.addEventListener('mousedown', close)
|
|
return () => document.removeEventListener('mousedown', close)
|
|
}, [showDropdown])
|
|
|
|
const memberIds = members.map(m => m.user_id)
|
|
|
|
const toggleMember = (userId: number) => {
|
|
const newIds = memberIds.includes(userId)
|
|
? memberIds.filter(id => id !== userId)
|
|
: [...memberIds, userId]
|
|
onSetMembers(newIds)
|
|
}
|
|
|
|
return (
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 2, flexWrap: 'wrap' }}>
|
|
{members.map(m => (
|
|
<ChipWithTooltip key={m.user_id} label={m.username} avatarUrl={m.avatar_url} size={chipSize}
|
|
paid={!!m.paid}
|
|
onClick={!readOnly && onTogglePaid ? () => onTogglePaid(m.user_id, !m.paid) : undefined}
|
|
/>
|
|
))}
|
|
{!readOnly && (
|
|
<button type="button" ref={btnRef} onClick={openDropdown}
|
|
style={{
|
|
width: btnSize, height: btnSize, borderRadius: '50%', border: '1.5px dashed var(--border-primary)',
|
|
background: 'none', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
color: 'var(--text-faint)', padding: 0, flexShrink: 0,
|
|
}}>
|
|
{members.length > 0 ? <Pencil size={iconSize} /> : <Users size={iconSize} />}
|
|
</button>
|
|
)}
|
|
{showDropdown && createPortal(
|
|
<div ref={dropRef} style={{
|
|
position: 'fixed', top: dropPos.top, left: dropPos.left, transform: 'translateX(-50%)', zIndex: 10000,
|
|
background: 'var(--bg-card)', border: '1px solid var(--border-primary)', borderRadius: 10,
|
|
boxShadow: '0 4px 16px rgba(0,0,0,0.12)', padding: 4, minWidth: 150,
|
|
}}>
|
|
{tripMembers.map(tm => {
|
|
const isActive = memberIds.includes(tm.id)
|
|
return (
|
|
<button type="button" key={tm.id} onClick={() => toggleMember(tm.id)} style={{
|
|
display: 'flex', alignItems: 'center', gap: 6, width: '100%', padding: '5px 8px',
|
|
borderRadius: 6, border: 'none', background: isActive ? 'var(--bg-hover)' : 'none', cursor: 'pointer',
|
|
fontFamily: 'inherit', fontSize: 'calc(11px * var(--fs-scale-caption, 1))', color: 'var(--text-primary)', textAlign: 'start',
|
|
}}
|
|
onMouseEnter={e => { if (!isActive) e.currentTarget.style.background = 'var(--bg-hover)' }}
|
|
onMouseLeave={e => { if (!isActive) e.currentTarget.style.background = 'none' }}
|
|
>
|
|
<div style={{
|
|
width: 18, height: 18, borderRadius: '50%', background: 'var(--bg-tertiary)',
|
|
display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 'calc(8px * var(--fs-scale-caption, 1))', fontWeight: 700,
|
|
color: 'var(--text-muted)', overflow: 'hidden', flexShrink: 0,
|
|
}}>
|
|
{tm.avatar_url
|
|
? <img src={tm.avatar_url} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
|
|
: tm.username?.[0]?.toUpperCase()
|
|
}
|
|
</div>
|
|
<span style={{ flex: 1 }}>{tm.username}</span>
|
|
{isActive && <Check size={12} color="var(--text-primary)" />}
|
|
</button>
|
|
)
|
|
})}
|
|
</div>,
|
|
document.body
|
|
)}
|
|
</div>
|
|
)
|
|
}
|