Refactor ShowMetadataCard and API: remove obsolete poster upload functionality, streamline metadata saving process into a single mutation, and enhance UI for original name input. Update related state management and error handling for improved user experience.

This commit is contained in:
Leonid Pershin
2026-07-25 12:03:25 +03:00
parent 7efd616c29
commit afe6cc24aa
2 changed files with 22 additions and 87 deletions
@@ -1,5 +1,5 @@
import { useMutation, useQuery } from '@tanstack/react-query'
import { useRef, useState } from 'react'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { HttpError } from '@/shared/api/client'
import type { MetadataCandidate, ShowDto } from '@/shared/api/types'
@@ -20,12 +20,10 @@ import {
setShowOriginalName,
setShowPoster,
updateMetadata,
uploadPoster,
} from './api'
export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged: () => void }) {
const { t } = useTranslation()
const posterInput = useRef<HTMLInputElement>(null)
const [galleryOpen, setGalleryOpen] = useState(false)
const [provider, setProvider] = useState('')
const [originalName, setOriginalName] = useState(show.originalName ?? '')
@@ -63,15 +61,6 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
},
onError,
})
const saveOriginal = useMutation({
mutationFn: () => setShowOriginalName(show.id, originalName.trim() || null),
onSuccess: () => {
toast.success(t('settings.saved'))
setQuery(originalName.trim() || show.name)
onChanged()
},
onError,
})
const apply = useMutation({
mutationFn: (externalId: string) => applyMetadata(show.id, effectiveProvider, externalId),
onSuccess: () => {
@@ -82,14 +71,21 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
},
onError,
})
const saveManual = useMutation({
mutationFn: () =>
updateMetadata(show.id, {
description: description.trim() || null,
year: year.trim() ? Number(year) : null,
}),
// Одна кнопка «Сохранить»: пишем только изменённое. Оригинальное название идёт своим эндпоинтом
// (не трогает привязку к источнику), описание/год — ручной правкой метаданных.
const save = useMutation({
mutationFn: async () => {
const yearNum = year.trim() ? Number(year) : null
const nameChanged = originalName.trim() !== (show.originalName ?? '')
const infoChanged =
(description.trim() || null) !== (show.description ?? null) || yearNum !== (show.year ?? null)
if (nameChanged) await setShowOriginalName(show.id, originalName.trim() || null)
if (infoChanged)
await updateMetadata(show.id, { description: description.trim() || null, year: yearNum })
},
onSuccess: () => {
toast.success(t('settings.saved'))
setQuery(originalName.trim() || show.name)
changed()
},
onError,
@@ -103,11 +99,6 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
},
onError,
})
const posterUpload = useMutation({
mutationFn: (file: File) => uploadPoster(show.id, file),
onSuccess: changed,
onError,
})
const refreshEpisodes = useMutation({
mutationFn: () => refreshEpisodesMetadata(show.id),
onSuccess: (count) => {
@@ -139,25 +130,6 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
<span className="text-xs text-muted-foreground">{t('admin.metadata.noPoster')}</span>
)}
</div>
<input
ref={posterInput}
type="file"
accept="image/*"
className="hidden"
onChange={(e) => {
const file = e.target.files?.[0]
if (file) posterUpload.mutate(file)
e.target.value = ''
}}
/>
<Button
size="sm"
variant="outline"
disabled={posterUpload.isPending}
onClick={() => posterInput.current?.click()}
>
{t('admin.metadata.uploadPoster')}
</Button>
<Button size="sm" variant="outline" onClick={() => setGalleryOpen(true)}>
{t('admin.metadata.pickFromGallery')}
</Button>
@@ -173,23 +145,12 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
<div className="flex min-w-0 flex-1 flex-col gap-4">
<div className="flex flex-col gap-1.5">
<Label>{t('admin.metadata.originalName')}</Label>
<div className="flex items-end gap-2">
<Input
className="min-w-40 flex-1"
value={originalName}
maxLength={256}
placeholder={t('admin.metadata.originalNamePlaceholder')}
onChange={(e) => setOriginalName(e.target.value)}
/>
<Button
size="sm"
variant="outline"
disabled={saveOriginal.isPending || originalName === (show.originalName ?? '')}
onClick={() => saveOriginal.mutate()}
>
{t('common.save')}
</Button>
</div>
<Input
value={originalName}
maxLength={256}
placeholder={t('admin.metadata.originalNamePlaceholder')}
onChange={(e) => setOriginalName(e.target.value)}
/>
<p className="text-xs text-muted-foreground">{t('admin.metadata.originalNameHint')}</p>
</div>
@@ -284,7 +245,7 @@ export function ShowMetadataCard({ show, onChanged }: { show: ShowDto; onChanged
</div>
</div>
<div className="flex flex-wrap items-center gap-2">
<Button size="sm" disabled={saveManual.isPending} onClick={() => saveManual.mutate()}>
<Button size="sm" disabled={save.isPending} onClick={() => save.mutate()}>
{t('common.save')}
</Button>
{linked && (