fix(collections): review follow-ups on the B–E work (#1081)

- Block the list-cover upload in demo mode (mirror the trips cover endpoint).
- Restrict list/place links to http(s) (schema) and normalise scheme-less URLs
  to https:// on save, so a bare "booking.com" no longer resolves as a relative
  SPA route (and javascript:/data: hrefs are rejected).
- Place detail: surface save errors with a toast instead of silently swallowing
  a 400 and leaving the sheet stuck in edit mode.
- List editor: don't create a duplicate list when a retry follows a cover-upload
  failure (reuse the created id); revoke the cover preview object URL.
- Map controls: one top bar (left toggle/select, right add/search) so they can't
  overlap on a narrow split map — the search shrinks instead.
- Dashboard list badge: full-opacity colour wash so the name stays legible over
  bright covers.
This commit is contained in:
Maurice
2026-07-01 11:24:36 +02:00
committed by Maurice
parent 68e101ed37
commit de4077d461
8 changed files with 69 additions and 21 deletions
@@ -46,8 +46,8 @@ export default function CollectionMapPanel({
dark={dark}
/>
{overlay && (
<>
<div className="col-map-controls">
<div className="col-map-topbar">
<div className="col-map-group">
<button
type="button"
onClick={onToggleView}
@@ -69,7 +69,7 @@ export default function CollectionMapPanel({
</button>
)}
</div>
<div className="col-map-topright">
<div className="col-map-group right">
{canAddPlace && (
<button type="button" onClick={onAddPlace} className="col-map-btn" aria-label={t('collections.addPlace')} title={t('collections.addPlace')}>
<Plus size={17} />
@@ -80,7 +80,7 @@ export default function CollectionMapPanel({
<input value={search} onChange={e => onSearch(e.target.value)} placeholder={t('collections.search')} />
</div>
</div>
</>
</div>
)}
</div>
)
@@ -8,6 +8,9 @@ import type { TranslationFn } from '../../types'
import MarkdownToolbar from '../Journey/MarkdownToolbar'
import StatusBadge from './StatusBadge'
import { entityGradient } from '../../utils/gradients'
import { normalizeLinkUrl } from '../../pages/collections/collectionsModel'
import { useToast } from '../shared/Toast'
import { getApiErrorMessage } from '../../types'
function linkHost(url: string): string {
try { return new URL(url).hostname.replace(/^www\./, '') } catch { return url }
@@ -34,6 +37,7 @@ interface CollectionPlaceDetailProps {
export default function CollectionPlaceDetail({
place, canEdit, onClose, onSetStatus, onSave, onCopyToTrip, onRemove, t,
}: CollectionPlaceDetailProps): React.ReactElement {
const toast = useToast()
const [editing, setEditing] = useState(false)
const [name, setName] = useState(place.name)
const [description, setDescription] = useState(place.description ?? '')
@@ -58,11 +62,13 @@ export default function CollectionPlaceDetail({
setLinks(links.map((l, idx) => (idx === i ? { ...l, ...patch } : l)))
const save = async () => {
const cleanLinks = links.map(l => ({ label: l.label?.trim() || undefined, url: l.url.trim() })).filter(l => l.url)
const cleanLinks = links.map(l => ({ label: l.label?.trim() || undefined, url: normalizeLinkUrl(l.url) })).filter(l => l.url)
setSaving(true)
try {
await onSave({ name: name.trim() || place.name, description: description.trim() || null, links: cleanLinks })
setEditing(false)
} catch (err) {
toast.error(getApiErrorMessage(err, t('common.error')))
} finally {
setSaving(false)
}
@@ -4,6 +4,7 @@ import Modal from '../shared/Modal'
import { useCollectionStore } from '../../store/collectionStore'
import { useToast } from '../shared/Toast'
import { getApiErrorMessage } from '../../types'
import { normalizeLinkUrl } from '../../pages/collections/collectionsModel'
import type { TranslationFn } from '../../types'
import type { Collection, CollectionLink } from '@trek/shared'
@@ -37,6 +38,12 @@ export default function ListEditorModal({ target, onClose, onCreated, t }: ListE
const [coverFile, setCoverFile] = useState<File | null>(null)
const [coverPreview, setCoverPreview] = useState<string | null>(null)
const [saving, setSaving] = useState(false)
// Remembers a freshly created list id so a retry after a cover-upload failure
// updates it instead of creating a duplicate list.
const [createdId, setCreatedId] = useState<number | null>(null)
const objectUrl = useRef<string | null>(null)
const dropObjectUrl = () => { if (objectUrl.current) { URL.revokeObjectURL(objectUrl.current); objectUrl.current = null } }
// (Re)seed the form whenever the target changes.
useEffect(() => {
@@ -46,16 +53,24 @@ export default function ListEditorModal({ target, onClose, onCreated, t }: ListE
setDescription(editing?.description ?? '')
setLinks(editing?.links ?? [])
setCoverFile(null)
dropObjectUrl()
setCoverPreview(editing?.cover_image ?? null)
setCreatedId(null)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target])
// Revoke the last preview blob on unmount.
useEffect(() => () => dropObjectUrl(), [])
if (!target) return null
const pickCover = (file: File | undefined) => {
if (!file) return
dropObjectUrl()
const url = URL.createObjectURL(file)
objectUrl.current = url
setCoverFile(file)
setCoverPreview(URL.createObjectURL(file))
setCoverPreview(url)
}
const setLink = (i: number, patch: Partial<CollectionLink>) =>
@@ -64,18 +79,22 @@ export default function ListEditorModal({ target, onClose, onCreated, t }: ListE
const save = async () => {
const trimmed = name.trim()
if (!trimmed) return
// Keep only links with a url; drop blank rows.
// Normalise + keep only links with a url; drop blank rows.
const cleanLinks = links
.map(l => ({ label: l.label?.trim() || undefined, url: l.url.trim() }))
.map(l => ({ label: l.label?.trim() || undefined, url: normalizeLinkUrl(l.url) }))
.filter(l => l.url)
const payload = { name: trimmed, color, description: description.trim() || null, links: cleanLinks }
setSaving(true)
try {
let id = editing?.id
if (editing) {
await updateCollection(editing.id, { name: trimmed, color, description: description.trim() || null, links: cleanLinks })
// A prior create that failed at the cover step left `createdId` set — reuse
// it so a retry updates that list instead of creating a duplicate.
let id = editing?.id ?? createdId
if (id != null) {
await updateCollection(id, payload)
} else {
const created = await createCollection({ name: trimmed, color, description: description.trim() || null, links: cleanLinks })
id = created?.id
const created = await createCollection(payload)
id = created?.id ?? null
setCreatedId(id)
}
if (id != null && coverFile) await uploadCover(id, coverFile)
if (!editing && id != null) onCreated(id)