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:
Claude committed 2026-10-09 15:16:32 +00:00
1 parent c246b16e7e
commit 94c27739b2
35 files changed
+1795 -22

No files matched your search

@@ -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],
)
}