Files
bobinoo/src/components/library/SelectableCard.tsx
T
Claude 94c27739b2 Bibliothèque : tags perso, actions groupées et « Annuler »
- Tags personnels libres sur chaque titre (fiche → Mon suivi), affichés discrètement
  dans la fiche, filtre par tag dans le catalogue, suggestions = tags déjà utilisés.
  Synchronisés avec la fiche (JSON), inclus dans l'export/import (et repris des tags
  de l'ancienne appli). Privés : retirés par public_item (migration) et côté client.
- Actions groupées dans le catalogue : appui long (mobile), case au survol (ordinateur)
  ou bouton « Sélectionner » ; tout sélectionner, compteur ; statut, listes (ajout /
  retrait), tag (ajout / retrait), suppression avec confirmation. Échap / retour quittent.
- Toast global accessible (role=status) avec « Annuler » (~6 s) après suppression,
  changement de statut, retrait d'une liste, actions groupées. La restauration remet la
  fiche à l'identique avec un updatedAt plus récent que la suppression : la synchro LWW
  recrée bien la fiche côté serveur.
- Store : updateMany, removeMany, restore.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-10-09 15:16:32 +00:00

102 lines
3.5 KiB
TypeScript

import { t } from '../../i18n'
import { Check } from 'lucide-react'
import { useRef, type ReactNode } from 'react'
import { cx } from '../../lib/utils'
interface Props {
title: string
selecting: boolean
selected: boolean
onToggle: () => void
/** Appui long (téléphone) ou case à cocher (ordinateur) : entre en mode sélection avec ce titre. */
onStartSelect: () => void
children: ReactNode
}
const LONG_PRESS_MS = 450
/**
* Enveloppe d'une carte du catalogue pour la sélection multiple :
* - téléphone : appui long sur la carte ;
* - ordinateur : case à cocher au survol ;
* - en mode sélection, toucher la carte la coche / décoche (au lieu d'ouvrir la fiche).
*/
export default function SelectableCard({ title, selecting, selected, onToggle, onStartSelect, children }: Props) {
const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
const origin = useRef<{ x: number; y: number }>(undefined)
const fired = useRef(false)
const cancel = () => {
clearTimeout(timer.current)
timer.current = undefined
origin.current = undefined
}
return (
<div
className="group relative select-none [-webkit-touch-callout:none]"
onPointerDown={(e) => {
fired.current = false
if (selecting || e.pointerType === 'mouse') return
origin.current = { x: e.clientX, y: e.clientY }
timer.current = setTimeout(() => {
fired.current = true
timer.current = undefined
navigator.vibrate?.(12)
onStartSelect()
}, LONG_PRESS_MS)
}}
onPointerMove={(e) => {
// Le doigt glisse (défilement) : ce n'est pas un appui long
if (origin.current && Math.hypot(e.clientX - origin.current.x, e.clientY - origin.current.y) > 10) cancel()
}}
onPointerUp={cancel}
onPointerCancel={cancel}
onPointerLeave={cancel}
onContextMenu={(e) => {
// Pas de menu « Enregistrer l'image » pendant l'appui long
if (fired.current || timer.current) e.preventDefault()
}}
onClickCapture={(e) => {
// Le « clic » qui suit un appui long ne doit pas ouvrir la fiche
if (fired.current) {
e.preventDefault()
e.stopPropagation()
fired.current = false
}
}}
>
{children}
{selecting ? (
<button
type="button"
onClick={onToggle}
aria-pressed={selected}
aria-label={t('select.toggle', { title })}
className={cx('absolute -inset-1 z-10 rounded-2xl transition-colors', selected ? 'bg-accent-fill/15 ring-2 ring-accent' : 'active:bg-ink/5')}
>
<span
className={cx(
'absolute end-3 top-3 grid size-7 place-items-center rounded-full border-2 transition-colors',
selected ? 'border-accent-fill bg-accent-fill text-on-accent' : 'border-ink/70 bg-bg/70 text-transparent',
)}
>
<Check size={15} strokeWidth={3} />
</span>
</button>
) : (
// Ordinateur : case à cocher au survol (ou au clavier) pour commencer une sélection
<button
type="button"
onClick={onStartSelect}
aria-label={t('select.toggle', { title })}
className="absolute end-2 top-2 z-10 hidden size-7 place-items-center rounded-full border-2 border-ink/70 bg-bg/85 text-transparent opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100 pointer-fine:grid"
>
<Check size={15} strokeWidth={3} />
</button>
)}
</div>
)
}