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
This commit is contained in:
35 files changed
+1795
-22
No files matched your search
@@ -0,0 +1,101 @@
|
||||
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>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
import { t } from '../../i18n'
|
||||
import { CircleDot, ListPlus, Minus, Plus, Tag, Trash2, X } from 'lucide-react'
|
||||
import { useMemo, useState, type ReactNode } from 'react'
|
||||
import { useBackToClose } from '../../lib/backNav'
|
||||
import { allTags, cleanTag, hasTag } from '../../lib/bulk'
|
||||
import { STATUSES } from '../../lib/constants'
|
||||
import { useEscape } from '../../lib/escape'
|
||||
import { cx, normalizeText } from '../../lib/utils'
|
||||
import { useMedia } from '../../store'
|
||||
import type { MediaItem, WatchStatus } from '../../types'
|
||||
import { StatusDot } from '../Badges'
|
||||
import ConfirmDialog from '../ConfirmDialog'
|
||||
import { NewListForm } from '../ListsView'
|
||||
import { useLibraryActions } from './useLibraryActions'
|
||||
|
||||
type Panel = null | 'status' | 'list' | 'tag' | 'delete'
|
||||
|
||||
interface Props {
|
||||
/** Fiches sélectionnées */
|
||||
selected: MediaItem[]
|
||||
/** Action terminée : on quitte le mode sélection */
|
||||
onDone: () => void
|
||||
}
|
||||
|
||||
/** Barre d'actions groupées (en bas, à la place de la barre de navigation). */
|
||||
export default function SelectionBar({ selected, onDone }: Props) {
|
||||
const actions = useLibraryActions()
|
||||
const [panel, setPanel] = useState<Panel>(null)
|
||||
const ids = selected.map((i) => i.id)
|
||||
const none = selected.length === 0
|
||||
|
||||
const run = async (fn: () => Promise<void>) => {
|
||||
setPanel(null)
|
||||
await fn()
|
||||
onDone()
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="safe-bottom fixed inset-x-0 bottom-0 z-40 border-t border-line bg-bg/95 backdrop-blur-md lg:start-60 lg:bottom-6 lg:mx-auto lg:max-w-xl lg:rounded-2xl lg:border lg:pb-0">
|
||||
<div className="mx-auto grid max-w-2xl grid-cols-4 px-2" role="toolbar" aria-label={t('select.actions')}>
|
||||
<BarButton icon={<CircleDot size={21} />} label={t('select.status')} onClick={() => setPanel('status')} disabled={none} />
|
||||
<BarButton icon={<ListPlus size={21} />} label={t('select.list')} onClick={() => setPanel('list')} disabled={none} />
|
||||
<BarButton icon={<Tag size={21} />} label={t('select.tag')} onClick={() => setPanel('tag')} disabled={none} />
|
||||
<BarButton icon={<Trash2 size={21} />} label={t('common.delete')} onClick={() => setPanel('delete')} disabled={none} danger />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{panel === 'status' && (
|
||||
<PickerSheet title={t('select.statusTitle', { count: selected.length })} onClose={() => setPanel(null)}>
|
||||
{STATUSES.map((s) => {
|
||||
const already = selected.every((i) => i.status === s.value)
|
||||
return (
|
||||
<button
|
||||
key={s.value}
|
||||
onClick={() => void run(() => actions.setStatus(ids, s.value as WatchStatus))}
|
||||
disabled={already}
|
||||
className="flex w-full items-center gap-3 rounded-xl px-3 py-3.5 text-start text-[15px] text-ink active:bg-surface-2 disabled:opacity-40"
|
||||
>
|
||||
<StatusDot status={s.value} className="size-2.5" />
|
||||
{s.label}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</PickerSheet>
|
||||
)}
|
||||
|
||||
{panel === 'list' && <ListPicker selected={selected} onClose={() => setPanel(null)} run={run} />}
|
||||
{panel === 'tag' && <TagPicker selected={selected} onClose={() => setPanel(null)} run={run} />}
|
||||
|
||||
<ConfirmDialog
|
||||
open={panel === 'delete'}
|
||||
title={t('confirm.title')}
|
||||
message={t('select.deleteConfirm', { count: selected.length })}
|
||||
confirmLabel={t('common.delete')}
|
||||
onConfirm={() => void run(() => actions.deleteItems(ids))}
|
||||
onCancel={() => setPanel(null)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function BarButton({ icon, label, onClick, disabled, danger }: { icon: ReactNode; label: string; onClick: () => void; disabled?: boolean; danger?: boolean }) {
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
className={cx('flex flex-col items-center gap-1 pb-2 pt-2.5 text-[11px] font-medium transition-colors disabled:opacity-35', danger ? 'text-accent' : 'text-ink')}
|
||||
>
|
||||
{icon}
|
||||
{label}
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
/** Petite feuille en bas de l'écran ; Échap ou le geste retour la ferment sans quitter la sélection. */
|
||||
function PickerSheet({ title, onClose, children }: { title: string; onClose: () => void; children: ReactNode }) {
|
||||
useEscape(onClose)
|
||||
useBackToClose(onClose)
|
||||
return (
|
||||
<div className="fixed inset-0 z-[60] flex items-end justify-center bg-black/70 sm:items-center" onClick={onClose}>
|
||||
<div
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={title}
|
||||
className="sheet-in safe-bottom max-h-[85dvh] w-full max-w-md overflow-y-auto overscroll-contain rounded-t-3xl border border-line-strong bg-surface p-3 sm:rounded-3xl"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div className="mb-1 flex items-center justify-between gap-2 ps-3">
|
||||
<h2 className="text-base font-semibold">{title}</h2>
|
||||
<button onClick={onClose} className="grid size-10 place-items-center rounded-full text-ink-3" aria-label={t('common.close')}>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
type Run = (fn: () => Promise<void>) => Promise<void>
|
||||
|
||||
function ListPicker({ selected, onClose, run }: { selected: MediaItem[]; onClose: () => void; run: Run }) {
|
||||
const { lists } = useMedia()
|
||||
const actions = useLibraryActions()
|
||||
const ids = selected.map((i) => i.id)
|
||||
return (
|
||||
<PickerSheet title={t('select.listTitle', { count: selected.length })} onClose={onClose}>
|
||||
{lists.length === 0 && <p className="px-3 pb-3 text-sm text-ink-3">{t('select.noLists')}</p>}
|
||||
<ul className="divide-y divide-line">
|
||||
{lists.map((l) => {
|
||||
const inside = selected.filter((i) => i.listIds?.includes(l.id)).length
|
||||
return (
|
||||
<li key={l.id} className="flex items-center gap-2 px-3 py-2.5">
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-[15px]">{l.name}</p>
|
||||
<p className="text-xs text-ink-3">{t('select.inList', { n: inside, total: selected.length })}</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => void run(() => actions.removeFromList(ids, l.id))}
|
||||
disabled={inside === 0}
|
||||
className="grid size-9 place-items-center rounded-full border border-line text-ink-2 disabled:opacity-30"
|
||||
aria-label={t('select.removeFrom', { list: l.name })}
|
||||
>
|
||||
<Minus size={16} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => void run(() => actions.addToList(ids, l.id))}
|
||||
disabled={inside === selected.length}
|
||||
className="grid size-9 place-items-center rounded-full bg-accent-fill text-on-accent disabled:opacity-30"
|
||||
aria-label={t('select.addTo', { list: l.name })}
|
||||
>
|
||||
<Plus size={16} />
|
||||
</button>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
<div className="mt-2 px-1 pb-1">
|
||||
<NewListForm onCreated={(l) => void run(() => actions.addToList(ids, l.id, l.name))} />
|
||||
</div>
|
||||
</PickerSheet>
|
||||
)
|
||||
}
|
||||
|
||||
function TagPicker({ selected, onClose, run }: { selected: MediaItem[]; onClose: () => void; run: Run }) {
|
||||
const { items } = useMedia()
|
||||
const actions = useLibraryActions()
|
||||
const ids = selected.map((i) => i.id)
|
||||
const existing = useMemo(() => allTags(items), [items])
|
||||
// Tags déjà portés par au moins une fiche sélectionnée (pour les retirer)
|
||||
const present = useMemo(() => allTags(selected), [selected])
|
||||
const [text, setText] = useState('')
|
||||
const q = normalizeText(text)
|
||||
const suggestions = existing.filter((tag) => !selected.every((i) => hasTag(i, tag)) && (!q || normalizeText(tag).includes(q))).slice(0, 16)
|
||||
const add = (raw: string) => {
|
||||
const tag = cleanTag(raw, existing)
|
||||
if (tag) void run(() => actions.addTag(ids, tag))
|
||||
}
|
||||
|
||||
return (
|
||||
<PickerSheet title={t('select.tagTitle', { count: selected.length })} onClose={onClose}>
|
||||
<form
|
||||
className="flex gap-2 px-1"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
add(text)
|
||||
}}
|
||||
>
|
||||
<input className="field" value={text} onChange={(e) => setText(e.target.value)} placeholder={t('tags.placeholder')} maxLength={40} autoFocus enterKeyHint="done" aria-label={t('tags.label')} />
|
||||
<button disabled={!cleanTag(text)} className="btn btn-light shrink-0 px-4 text-sm">
|
||||
{t('common.add')}
|
||||
</button>
|
||||
</form>
|
||||
{suggestions.length > 0 && (
|
||||
<div className="mt-3 flex flex-wrap gap-1.5 px-1">
|
||||
{suggestions.map((tag) => (
|
||||
<button key={tag} onClick={() => add(tag)} className="chip py-1! text-xs">
|
||||
+ {tag}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{present.length > 0 && (
|
||||
<div className="mt-5 border-t border-line px-1 pt-3">
|
||||
<span className="label">{t('select.removeTag')}</span>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{present.map((tag) => (
|
||||
<button key={tag} onClick={() => void run(() => actions.removeTag(ids, tag))} className="chip py-1! text-xs" aria-label={t('common.removeX', { name: tag })}>
|
||||
#{tag} <X size={13} />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<p className="mt-4 px-1 pb-1 text-xs text-ink-3">{t('tags.private')}</p>
|
||||
</PickerSheet>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { t } from '../../i18n'
|
||||
import { useCallback, useMemo } from 'react'
|
||||
import { STATUS_BY_VALUE } from '../../lib/constants'
|
||||
import { hasTag, statusPatch } from '../../lib/bulk'
|
||||
import { LIMITS } from '../../lib/security'
|
||||
import { normalizeText } from '../../lib/utils'
|
||||
import { useMedia } from '../../store'
|
||||
import type { MediaItem, WatchStatus } from '../../types'
|
||||
import { useToast } from '../Toast'
|
||||
|
||||
/**
|
||||
* Actions sur la bibliothèque qui proposent « Annuler » (suppression, statut, listes, tags).
|
||||
* L'annulation remet les fiches exactement comme avant (voir `restore` dans le store).
|
||||
*/
|
||||
export function useLibraryActions() {
|
||||
const { items, lists, updateMany, removeMany, restore } = useMedia()
|
||||
const toast = useToast()
|
||||
|
||||
const pick = useCallback((ids: Iterable<string>) => {
|
||||
const wanted = new Set(ids)
|
||||
return items.filter((i) => wanted.has(i.id))
|
||||
}, [items])
|
||||
|
||||
const undoable = useCallback(
|
||||
(message: string, snapshots: MediaItem[]) => toast.show(message, { undo: () => restore(snapshots) }),
|
||||
[toast, restore],
|
||||
)
|
||||
|
||||
const listName = useCallback((id: string) => lists.find((l) => l.id === id)?.name ?? '', [lists])
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
/** Supprime des fiches (avec « Annuler »). */
|
||||
async deleteItems(ids: Iterable<string>) {
|
||||
const snapshots = pick(ids)
|
||||
if (!snapshots.length) return
|
||||
await removeMany(snapshots.map((i) => i.id))
|
||||
undoable(
|
||||
snapshots.length === 1 ? t('toast.deleted', { title: snapshots[0].title }) : t('toast.deletedMany', { count: snapshots.length }),
|
||||
snapshots,
|
||||
)
|
||||
},
|
||||
|
||||
/** Change le statut (dates et épisodes suivent, comme sur la fiche). */
|
||||
async setStatus(ids: Iterable<string>, status: WatchStatus) {
|
||||
const snapshots = pick(ids).filter((i) => i.status !== status)
|
||||
if (!snapshots.length) return
|
||||
await updateMany(snapshots.map((i) => ({ id: i.id, patch: statusPatch(i, status) })))
|
||||
const label = STATUS_BY_VALUE[status].label
|
||||
undoable(
|
||||
snapshots.length === 1 ? t('toast.status', { title: snapshots[0].title, status: label }) : t('toast.statusMany', { count: snapshots.length, status: label }),
|
||||
snapshots,
|
||||
)
|
||||
},
|
||||
|
||||
async addToList(ids: Iterable<string>, listId: string, name?: string) {
|
||||
const snapshots = pick(ids).filter((i) => !i.listIds?.includes(listId))
|
||||
if (!snapshots.length) return
|
||||
await updateMany(snapshots.map((i) => ({ id: i.id, patch: { listIds: [...(i.listIds ?? []), listId] } })))
|
||||
undoable(t('toast.addedToList', { count: snapshots.length, list: name ?? listName(listId) }), snapshots)
|
||||
},
|
||||
|
||||
async removeFromList(ids: Iterable<string>, listId: string) {
|
||||
const snapshots = pick(ids).filter((i) => i.listIds?.includes(listId))
|
||||
if (!snapshots.length) return
|
||||
await updateMany(snapshots.map((i) => ({ id: i.id, patch: { listIds: i.listIds!.filter((x) => x !== listId) } })))
|
||||
undoable(
|
||||
snapshots.length === 1 ? t('toast.removedFromList', { title: snapshots[0].title, list: listName(listId) }) : t('toast.removedFromListMany', { count: snapshots.length, list: listName(listId) }),
|
||||
snapshots,
|
||||
)
|
||||
},
|
||||
|
||||
async addTag(ids: Iterable<string>, tag: string) {
|
||||
const snapshots = pick(ids).filter((i) => !hasTag(i, tag) && (i.tags?.length ?? 0) < LIMITS.tags)
|
||||
if (!snapshots.length || !tag) return
|
||||
await updateMany(snapshots.map((i) => ({ id: i.id, patch: { tags: [...(i.tags ?? []), tag] } })))
|
||||
undoable(t('toast.tagged', { count: snapshots.length, tag }), snapshots)
|
||||
},
|
||||
|
||||
async removeTag(ids: Iterable<string>, tag: string) {
|
||||
const key = normalizeText(tag)
|
||||
const snapshots = pick(ids).filter((i) => hasTag(i, tag))
|
||||
if (!snapshots.length) return
|
||||
await updateMany(
|
||||
snapshots.map((i) => {
|
||||
const tags = i.tags!.filter((x) => normalizeText(x) !== key)
|
||||
return { id: i.id, patch: { tags: tags.length ? tags : undefined } }
|
||||
}),
|
||||
)
|
||||
undoable(t('toast.untagged', { count: snapshots.length, tag }), snapshots)
|
||||
},
|
||||
|
||||
/** « Annuler » pour un changement fait ailleurs (fiche) : remet ces copies. */
|
||||
offerUndo: undoable,
|
||||
}),
|
||||
[pick, removeMany, updateMany, undoable, listName],
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user