Import « Coller une liste » : depuis l'app Notes ou n'importe quel texte
deploy / deploy (push) Successful in 52s
deploy / deploy (push) Successful in 52s
- Une ligne par titre : note (4,5/5, 8/10, ★★★★), année, statut (vu, en cours, à voir), progression S3E12 et avis reconnus - Sections « Films », « Séries à voir »… appliquées aux lignes suivantes - Recherche TMDB + AniList, écran de vérification : décocher une ligne ou choisir un autre titre - Textes dans les 18 langues Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
This commit is contained in:
23 files changed
+884
-17
No files matched your search
@@ -0,0 +1,195 @@
|
||||
import { Check, Loader2, Search } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { t, type TKey } from '../i18n'
|
||||
import { searchAll, type SearchResult } from '../lib/catalogApi'
|
||||
import { matchFor, type Match } from '../lib/import/resolve'
|
||||
import type { TextEntry } from '../lib/import/textList'
|
||||
import { cx } from '../lib/utils'
|
||||
import Poster from './Poster'
|
||||
import { RatingBadge } from './Rating'
|
||||
|
||||
/**
|
||||
* Vérification d'une liste collée : chaque ligne avec le titre trouvé.
|
||||
* On peut décocher une ligne, ou choisir un autre titre (recherche intégrée).
|
||||
*/
|
||||
export default function ImportTextReview({
|
||||
entries,
|
||||
matches,
|
||||
unsure,
|
||||
excluded,
|
||||
tmdbKey,
|
||||
onToggle,
|
||||
onChange,
|
||||
onContinue,
|
||||
}: {
|
||||
entries: TextEntry[]
|
||||
matches: Map<string, Match>
|
||||
unsure: Set<string>
|
||||
excluded: Set<string>
|
||||
tmdbKey?: string
|
||||
onToggle: (key: string) => void
|
||||
onChange: (key: string, match: Match) => void
|
||||
onContinue: () => void
|
||||
}) {
|
||||
const [editing, setEditing] = useState<string>()
|
||||
const kept = entries.filter((e) => !excluded.has(e.key) && matches.has(e.key)).length
|
||||
const toCheck = entries.filter((e) => !excluded.has(e.key) && (unsure.has(e.key) || !matches.has(e.key))).length
|
||||
// Les lignes à vérifier d'abord
|
||||
const ordered = [...entries].sort((a, b) => rank(a) - rank(b))
|
||||
function rank(e: TextEntry) {
|
||||
return !matches.has(e.key) ? 0 : unsure.has(e.key) ? 1 : 2
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<p className="text-sm text-ink-2">{t('import.text.reviewHint')}</p>
|
||||
{toCheck > 0 && <p className="mt-2 text-sm font-medium text-ink">{t('import.text.toCheck', { n: toCheck })}</p>}
|
||||
<ul className="mt-4 divide-y divide-line border-y border-line">
|
||||
{ordered.map((e) => {
|
||||
const m = matches.get(e.key)
|
||||
const off = excluded.has(e.key)
|
||||
const doubt = !m || unsure.has(e.key)
|
||||
return (
|
||||
<li key={e.key} className="py-3">
|
||||
<div className={cx('flex items-start gap-3', off && 'opacity-45')}>
|
||||
<button
|
||||
type="button"
|
||||
role="checkbox"
|
||||
aria-checked={!off && !!m}
|
||||
disabled={!m}
|
||||
onClick={() => onToggle(e.key)}
|
||||
aria-label={e.line}
|
||||
className={cx(
|
||||
'mt-1 grid size-6 shrink-0 place-items-center rounded-[2px] border',
|
||||
!off && m ? 'border-accent bg-accent-fill text-on-accent' : 'border-line-strong bg-surface',
|
||||
)}
|
||||
>
|
||||
{!off && m && <Check size={15} strokeWidth={3} />}
|
||||
</button>
|
||||
<div className="w-11 shrink-0">
|
||||
<Poster src={m?.result.thumb ?? m?.result.posterUrl} title={m?.result.title ?? e.title} className="rounded-[2px]" />
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-xs text-ink-3">{e.line}</p>
|
||||
{m ? (
|
||||
<p className="mt-0.5 font-semibold leading-snug">
|
||||
{m.result.title}
|
||||
<span className="font-normal text-ink-3">
|
||||
{' · '}
|
||||
{t(`type.${m.meta.type ?? m.result.typeGuess}` as TKey)}
|
||||
{m.result.year ? ` · ${m.result.year}` : ''}
|
||||
</span>
|
||||
</p>
|
||||
) : (
|
||||
<p className="mt-0.5 font-semibold text-ink-2">{t('import.text.none')}</p>
|
||||
)}
|
||||
<div className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-ink-2">
|
||||
<span>{t(`status.${e.status}` as TKey)}</span>
|
||||
<RatingBadge value={e.rating} />
|
||||
{e.notes && <span className="min-w-0 max-w-full truncate italic">« {e.notes} »</span>}
|
||||
</div>
|
||||
<div className="mt-1.5 flex items-center gap-3">
|
||||
{doubt && m && !off && <span className="text-xs font-semibold text-danger">{t('import.text.check')}</span>}
|
||||
<button type="button" onClick={() => setEditing(editing === e.key ? undefined : e.key)} className="text-xs font-semibold text-accent underline underline-offset-2">
|
||||
{m ? t('import.text.change') : t('import.text.search')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{editing === e.key && (
|
||||
<Picker
|
||||
initial={e.fullTitle ?? e.title}
|
||||
tmdbKey={tmdbKey}
|
||||
onPick={(match) => {
|
||||
onChange(e.key, match)
|
||||
setEditing(undefined)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
<button onClick={onContinue} disabled={kept === 0} className="btn btn-primary mt-6 w-full">
|
||||
{t('import.continue')} ({kept})
|
||||
</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
/** Recherche d'un autre titre pour une ligne. */
|
||||
function Picker({ initial, tmdbKey, onPick }: { initial: string; tmdbKey?: string; onPick: (m: Match) => void }) {
|
||||
const [q, setQ] = useState(initial)
|
||||
const [results, setResults] = useState<SearchResult[]>()
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [error, setError] = useState<string>()
|
||||
|
||||
const run = async () => {
|
||||
const query = q.trim()
|
||||
if (!query || busy) return
|
||||
setBusy(true)
|
||||
setError(undefined)
|
||||
try {
|
||||
setResults((await searchAll(query, tmdbKey?.trim() || undefined)).slice(0, 8))
|
||||
} catch (err) {
|
||||
setError((err as Error)?.message || t('search.offline'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const pick = async (r: SearchResult) => {
|
||||
setBusy(true)
|
||||
try {
|
||||
onPick(await matchFor(r, tmdbKey?.trim() || undefined))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-3 rounded-[2px] border border-line bg-surface-2 p-3">
|
||||
<form
|
||||
className="flex gap-2"
|
||||
onSubmit={(ev) => {
|
||||
ev.preventDefault()
|
||||
void run()
|
||||
}}
|
||||
>
|
||||
<input
|
||||
className="field min-w-0 flex-1"
|
||||
value={q}
|
||||
onChange={(ev) => setQ(ev.target.value.slice(0, 200))}
|
||||
aria-label={t('import.text.search')}
|
||||
enterKeyHint="search"
|
||||
autoFocus
|
||||
/>
|
||||
<button type="submit" className="btn btn-primary px-4" disabled={busy || !q.trim()} aria-label={t('import.text.search')}>
|
||||
{busy ? <Loader2 size={18} className="animate-spin" /> : <Search size={18} />}
|
||||
</button>
|
||||
</form>
|
||||
{error && <p className="mt-2 text-sm text-danger">{error}</p>}
|
||||
{results && !results.length && <p className="mt-2 text-sm text-ink-2">{t('import.text.none')}</p>}
|
||||
{results && results.length > 0 && (
|
||||
<ul className="mt-2 divide-y divide-line">
|
||||
{results.map((r) => (
|
||||
<li key={r.externalId}>
|
||||
<button type="button" disabled={busy} onClick={() => void pick(r)} className="flex w-full items-center gap-3 py-2 text-start">
|
||||
<div className="w-9 shrink-0">
|
||||
<Poster src={r.thumb ?? r.posterUrl} title={r.title} className="rounded-[2px]" />
|
||||
</div>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm font-semibold">{r.title}</span>
|
||||
<span className="block text-xs text-ink-3">
|
||||
{t(`type.${r.typeGuess}` as TKey)}
|
||||
{r.year ? ` · ${r.year}` : ''}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+115
-15
@@ -2,10 +2,12 @@ import { ArrowLeft, Check, ChevronDown, FileUp, Loader2, X } from 'lucide-react'
|
||||
import { useEffect, useMemo, useRef, useState, type DragEvent } from 'react'
|
||||
import { t, type TKey } from '../i18n'
|
||||
import { useBackToClose } from '../lib/backNav'
|
||||
import ImportTextReview from './ImportTextReview'
|
||||
import { useEscape } from '../lib/escape'
|
||||
import { ImportFileError, readImportFile, type TextFile } from '../lib/import/files'
|
||||
import { buildImportPlan, countByKind, type ImportPlan } from '../lib/import/merge'
|
||||
import { CancelledError, fetchAniListUser, isAniListUserName, resolveEntries, type CancelToken, type Match, type Progress } from '../lib/import/resolve'
|
||||
import { CancelledError, fetchAniListUser, isAniListUserName, resolveEntries, resolveTextEntries, withProgress, type CancelToken, type Match, type Progress } from '../lib/import/resolve'
|
||||
import { parseTextList, TEXT_MAX_CHARS, type TextEntry } from '../lib/import/textList'
|
||||
import { letterboxdRole, parseLetterboxd, parseMal, parseTvTime, type ImportEntry, type ImportSource } from '../lib/import/sources'
|
||||
import { useScrollLock } from '../lib/scrollLock'
|
||||
import { LIMITS } from '../lib/security'
|
||||
@@ -18,16 +20,18 @@ import { useMedia } from '../store'
|
||||
* Tout se passe sur l'appareil ; la synchro existante enverra ensuite les fiches au compte.
|
||||
*/
|
||||
|
||||
type Step = 'source' | 'input' | 'working' | 'preview' | 'done'
|
||||
type Step = 'source' | 'input' | 'working' | 'review' | 'preview' | 'done'
|
||||
|
||||
const SOURCES: { id: ImportSource; name: string; mark: string; accept?: string; multiple?: boolean }[] = [
|
||||
{ id: 'text', name: '', mark: 'Aa' },
|
||||
{ id: 'letterboxd', name: 'Letterboxd', mark: 'Lb', accept: '.zip,.csv,application/zip,text/csv', multiple: true },
|
||||
{ id: 'tvtime', name: 'TV Time', mark: 'TV', accept: '.zip,.csv,application/zip,text/csv', multiple: true },
|
||||
{ id: 'mal', name: 'MyAnimeList', mark: 'MAL', accept: '.xml,.gz,application/xml,text/xml,application/gzip' },
|
||||
{ id: 'anilist', name: 'AniList', mark: 'AL' },
|
||||
]
|
||||
|
||||
const NEEDS_TMDB: ImportSource[] = ['letterboxd', 'tvtime']
|
||||
const NEEDS_TMDB: ImportSource[] = ['text', 'letterboxd', 'tvtime']
|
||||
const sourceName = (s: { id: ImportSource; name: string }) => (s.id === 'text' ? t('import.text.name') : s.name)
|
||||
|
||||
/** Message lisible pour une erreur de lecture / de réseau. */
|
||||
function errorText(e: unknown): string {
|
||||
@@ -69,6 +73,11 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
const [showMissing, setShowMissing] = useState(false)
|
||||
const [result, setResult] = useState<{ added: number; updated: number; unchanged: number; notFound: number }>()
|
||||
const [dragging, setDragging] = useState(false)
|
||||
// Liste collée
|
||||
const [pasted, setPasted] = useState('')
|
||||
const [defaultStatus, setDefaultStatus] = useState<'termine' | 'a_voir'>('termine')
|
||||
const [unsure, setUnsure] = useState<Set<string>>(new Set())
|
||||
const [excluded, setExcluded] = useState<Set<string>>(new Set())
|
||||
const [now, setNow] = useState(Date.now())
|
||||
const token = useRef<CancelToken>({ cancelled: false })
|
||||
const fileRef = useRef<HTMLInputElement>(null)
|
||||
@@ -93,10 +102,15 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
const info = SOURCES.find((s) => s.id === source)
|
||||
const hasTmdb = !!settings.tmdbKey?.trim()
|
||||
|
||||
const plan: ImportPlan | undefined = useMemo(
|
||||
() => (step === 'preview' ? buildImportPlan(entries, matches, items, { includeUnmatched }) : undefined),
|
||||
[step, entries, matches, items, includeUnmatched],
|
||||
)
|
||||
const plan: ImportPlan | undefined = useMemo(() => {
|
||||
if (step !== 'preview') return undefined
|
||||
// Liste collée : seulement les lignes gardées, avec la progression « S3E12 » convertie en épisodes
|
||||
const list =
|
||||
source === 'text'
|
||||
? (entries as TextEntry[]).filter((e) => !excluded.has(e.key) && matches.has(e.key)).map((e) => withProgress(e, matches.get(e.key)))
|
||||
: entries
|
||||
return buildImportPlan(list, matches, items, { includeUnmatched: source === 'text' ? false : includeUnmatched })
|
||||
}, [step, source, entries, matches, items, includeUnmatched, excluded])
|
||||
|
||||
const run = async (load: () => Promise<ImportEntry[]>) => {
|
||||
const tk = { cancelled: false }
|
||||
@@ -127,10 +141,36 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
const onFiles = (list: FileList | null | undefined) => {
|
||||
const files = Array.from(list ?? [])
|
||||
if (fileRef.current) fileRef.current.value = ''
|
||||
if (!files.length || !source || source === 'anilist') return
|
||||
if (!files.length || !source || source === 'anilist' || source === 'text') return
|
||||
void run(() => readEntries(source, files))
|
||||
}
|
||||
|
||||
const onText = () => {
|
||||
const list = parseTextList(pasted, { defaultStatus })
|
||||
if (!list.length) return setError(t('import.err.empty'))
|
||||
const tk = { cancelled: false }
|
||||
token.current = tk
|
||||
setError(undefined)
|
||||
setEntries(list)
|
||||
setStep('working')
|
||||
setPhase('resolving')
|
||||
setProgress({ done: 0, total: list.length })
|
||||
resolveTextEntries(list, { tmdbKey: settings.tmdbKey, token: tk, onProgress: (p) => !tk.cancelled && setProgress(p) })
|
||||
.then(({ found, unsure }) => {
|
||||
if (tk.cancelled) return
|
||||
setEntries([...list])
|
||||
setMatches(found)
|
||||
setUnsure(unsure)
|
||||
setExcluded(new Set())
|
||||
setStep('review')
|
||||
})
|
||||
.catch((e) => {
|
||||
if (e instanceof CancelledError || tk.cancelled) return
|
||||
setError(errorText(e))
|
||||
setStep('input')
|
||||
})
|
||||
}
|
||||
|
||||
const onAniList = () => {
|
||||
const name = userName.trim()
|
||||
if (!isAniListUserName(name)) return setError(t('import.err.user'))
|
||||
@@ -148,7 +188,7 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
setPhase('saving')
|
||||
try {
|
||||
await importItems([...plan.added, ...plan.updated], 'merge')
|
||||
setResult({ added: plan.added.length, updated: plan.updated.length, unchanged: plan.unchanged, notFound: includeUnmatched ? 0 : plan.notFound.length })
|
||||
setResult({ added: plan.added.length, updated: plan.updated.length, unchanged: plan.unchanged, notFound: includeUnmatched || source === 'text' ? 0 : plan.notFound.length })
|
||||
setStep('done')
|
||||
} catch (e) {
|
||||
setError(errorText(e))
|
||||
@@ -164,7 +204,7 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
|
||||
const back = () => {
|
||||
setError(undefined)
|
||||
setStep(step === 'preview' ? 'input' : 'source')
|
||||
setStep(step === 'preview' ? (source === 'text' ? 'review' : 'input') : step === 'review' ? 'input' : 'source')
|
||||
}
|
||||
|
||||
const pct = progress.total ? Math.round((progress.done / progress.total) * 100) : 0
|
||||
@@ -175,14 +215,14 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
<div className="sheet sheet-in z-[60]" role="dialog" aria-modal="true" aria-label={t('import.title')}>
|
||||
<header className="safe-top border-b border-line">
|
||||
<div className="mx-auto flex max-w-2xl items-center gap-2 px-3 py-2.5 lg:max-w-none lg:px-10">
|
||||
{step === 'input' || step === 'preview' ? (
|
||||
{step === 'input' || step === 'review' || step === 'preview' ? (
|
||||
<button onClick={back} className="grid size-10 shrink-0 place-items-center rounded-full text-ink-2 rtl:-scale-x-100" aria-label={t('import.back')}>
|
||||
<ArrowLeft size={21} />
|
||||
</button>
|
||||
) : (
|
||||
<span className="size-10 shrink-0" />
|
||||
)}
|
||||
<h2 className="min-w-0 flex-1 truncate text-center text-base font-semibold">{info && step !== 'source' ? info.name : t('import.title')}</h2>
|
||||
<h2 className="min-w-0 flex-1 truncate text-center text-base font-semibold">{info && step !== 'source' ? sourceName(info) : t('import.title')}</h2>
|
||||
<button onClick={close} className="grid size-10 shrink-0 place-items-center rounded-full text-ink-2" aria-label={t('common.close')}>
|
||||
<X size={22} />
|
||||
</button>
|
||||
@@ -207,7 +247,7 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
>
|
||||
<span className="grid size-12 shrink-0 place-items-center rounded-xl border border-line-strong bg-surface-2 text-sm font-bold tracking-tight text-ink">{s.mark}</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block font-semibold">{s.name}</span>
|
||||
<span className="block font-semibold">{sourceName(s)}</span>
|
||||
<span className="mt-0.5 block text-xs text-ink-3">{t(`import.src.${s.id}` as TKey)}</span>
|
||||
</span>
|
||||
<span className="text-ink-3 rtl:-scale-x-100">→</span>
|
||||
@@ -236,7 +276,36 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
<p className="mt-5 rounded-2xl border border-dashed border-line-strong p-3.5 text-xs leading-relaxed text-ink-2">{t('import.noTmdb')}</p>
|
||||
)}
|
||||
|
||||
{info.id === 'anilist' ? (
|
||||
{info.id === 'text' ? (
|
||||
<div className="mt-6">
|
||||
<textarea
|
||||
className="field min-h-56 w-full resize-y py-3 leading-relaxed"
|
||||
value={pasted}
|
||||
onChange={(e) => setPasted(e.target.value.slice(0, TEXT_MAX_CHARS))}
|
||||
placeholder={t('import.text.placeholder')}
|
||||
aria-label={t('import.text.name')}
|
||||
spellCheck={false}
|
||||
/>
|
||||
<p className="mt-4 text-sm font-medium">{t('import.text.default')}</p>
|
||||
<div className="mt-2 grid grid-cols-2 gap-2" role="radiogroup" aria-label={t('import.text.default')}>
|
||||
{(['termine', 'a_voir'] as const).map((st) => (
|
||||
<button
|
||||
key={st}
|
||||
type="button"
|
||||
role="radio"
|
||||
aria-checked={defaultStatus === st}
|
||||
onClick={() => setDefaultStatus(st)}
|
||||
className={cx('btn', defaultStatus === st ? 'btn-primary' : 'btn-ghost')}
|
||||
>
|
||||
{st === 'termine' ? t('import.text.seen') : t('status.a_voir')}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<button type="button" onClick={onText} disabled={!pasted.trim()} className="btn btn-primary mt-5 w-full">
|
||||
{t('import.text.analyze')}
|
||||
</button>
|
||||
</div>
|
||||
) : info.id === 'anilist' ? (
|
||||
<form
|
||||
className="mt-6 flex gap-2"
|
||||
onSubmit={(e) => {
|
||||
@@ -312,6 +381,37 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{step === 'review' && source === 'text' && (
|
||||
<ImportTextReview
|
||||
entries={entries as TextEntry[]}
|
||||
matches={matches}
|
||||
unsure={unsure}
|
||||
excluded={excluded}
|
||||
tmdbKey={settings.tmdbKey}
|
||||
onToggle={(key) =>
|
||||
setExcluded((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (!next.delete(key)) next.add(key)
|
||||
return next
|
||||
})
|
||||
}
|
||||
onChange={(key, match) => {
|
||||
setMatches((prev) => new Map(prev).set(key, match))
|
||||
setUnsure((prev) => {
|
||||
const next = new Set(prev)
|
||||
next.delete(key)
|
||||
return next
|
||||
})
|
||||
setExcluded((prev) => {
|
||||
const next = new Set(prev)
|
||||
next.delete(key)
|
||||
return next
|
||||
})
|
||||
}}
|
||||
onContinue={() => setStep('preview')}
|
||||
/>
|
||||
)}
|
||||
|
||||
{step === 'preview' && plan && kinds && (
|
||||
<>
|
||||
<p className="text-sm text-ink-2">{t('import.found', { n: entries.length })}</p>
|
||||
@@ -336,7 +436,7 @@ export default function ImportWizard({ onClose }: { onClose: () => void }) {
|
||||
<Line label={t('import.notFound')} value={plan.notFound.length} accent={plan.notFound.length > 0} />
|
||||
</div>
|
||||
|
||||
{plan.notFound.length > 0 && (
|
||||
{source !== 'text' && plan.notFound.length > 0 && (
|
||||
<div className="mt-3">
|
||||
<button onClick={() => setShowMissing((v) => !v)} className="flex items-center gap-1.5 text-sm font-medium text-ink-2" aria-expanded={showMissing}>
|
||||
<ChevronDown size={16} className={cx('transition-transform', showMissing && 'rotate-180')} />
|
||||
|
||||
Reference in new issue
Block a user