mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
d008331e5f
Both the desktop sheet and the phone sheet of All places show the place's location as a badge, its notes per list, its visits from trips (hidden without any), linked tours and bookings, its change history and the reader's own tags with a tag filter. A trip map can show the places of linked lists and add one to the trip. All places reloads after a list is added or removed.
106 lines
4.1 KiB
TypeScript
106 lines
4.1 KiB
TypeScript
import { Check, Shapes } from 'lucide-react';
|
|
import React, { createElement, useEffect, useRef, useState } from 'react';
|
|
import type { Category, TranslationFn } from '../../types';
|
|
import { getCategoryIcon } from '../shared/categoryIcons';
|
|
import { Tooltip } from '../shared/Tooltip';
|
|
|
|
interface CategoryTileMenuProps {
|
|
categories: Category[];
|
|
value: number | null;
|
|
onChange: (id: number | null) => void;
|
|
/** The tile's side in pixels: 46 in a dialog's head band, 40 beside the sheet's name field. */
|
|
size?: number;
|
|
t: TranslationFn;
|
|
}
|
|
|
|
/**
|
|
* The place's category as the tile in front of its name: its icon in its colour,
|
|
* and a tap opens the list to pick another. It replaces the row of category chips
|
|
* the forms had, so the category reads as part of the place and the form loses a row.
|
|
*/
|
|
export default function CategoryTileMenu({
|
|
categories,
|
|
value,
|
|
onChange,
|
|
size = 46,
|
|
t,
|
|
}: CategoryTileMenuProps): React.ReactElement {
|
|
const [open, setOpen] = useState(false);
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const onDoc = (e: MouseEvent) => {
|
|
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
|
|
};
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') {
|
|
e.stopPropagation();
|
|
setOpen(false);
|
|
}
|
|
};
|
|
document.addEventListener('mousedown', onDoc);
|
|
document.addEventListener('keydown', onKey, true);
|
|
return () => {
|
|
document.removeEventListener('mousedown', onDoc);
|
|
document.removeEventListener('keydown', onKey, true);
|
|
};
|
|
}, [open]);
|
|
|
|
const current = value != null ? (categories.find((c) => c.id === value) ?? null) : null;
|
|
const Icon = current ? getCategoryIcon(current.icon ?? undefined) : Shapes;
|
|
const tone = current?.color || null;
|
|
const label = `${t('collections.category')}: ${current?.name ?? t('collections.noCategory')}`;
|
|
|
|
return (
|
|
<div className="relative flex-none" ref={ref}>
|
|
<Tooltip label={label} disabled={open}>
|
|
<button
|
|
type="button"
|
|
onClick={() => setOpen((o) => !o)}
|
|
aria-haspopup="listbox"
|
|
aria-expanded={open}
|
|
aria-label={label}
|
|
className={`grid place-items-center rounded-[14px] shadow-sm transition-transform hover:scale-[1.03] ${tone ? '' : 'bg-surface-card text-content-muted'}`}
|
|
// The category's own colour, the one data-driven value of the tile.
|
|
style={{
|
|
width: size,
|
|
height: size,
|
|
...(tone ? { background: `color-mix(in srgb, ${tone} 16%, var(--bg-card))`, color: tone } : {}),
|
|
}}
|
|
>
|
|
{createElement(Icon, { size: Math.round(size * 0.43), strokeWidth: 1.9 })}
|
|
</button>
|
|
</Tooltip>
|
|
{open && (
|
|
<div
|
|
role="listbox"
|
|
aria-label={t('collections.category')}
|
|
className="absolute start-0 top-[calc(100%+6px)] z-50 max-h-[280px] w-[220px] overflow-y-auto rounded-xl border border-edge-faint bg-surface-card p-1.5 shadow-lg"
|
|
>
|
|
{[null, ...categories].map((cat) => {
|
|
const on = (cat?.id ?? null) === value;
|
|
const OptIcon = cat ? getCategoryIcon(cat.icon ?? undefined) : Shapes;
|
|
return (
|
|
<button
|
|
key={cat?.id ?? 'none'}
|
|
type="button"
|
|
role="option"
|
|
aria-selected={on}
|
|
onClick={() => {
|
|
onChange(cat?.id ?? null);
|
|
setOpen(false);
|
|
}}
|
|
className={`flex w-full items-center gap-2.5 rounded-lg px-2.5 py-2 text-start text-body ${on ? 'bg-surface-secondary font-semibold text-content' : 'text-content-muted hover:bg-surface-secondary hover:text-content'}`}
|
|
>
|
|
<OptIcon size={15} className="flex-none" style={cat?.color ? { color: cat.color } : undefined} />
|
|
<span className="min-w-0 flex-1 truncate">{cat?.name ?? t('collections.noCategory')}</span>
|
|
{on && <Check size={14} className="flex-none" />}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|