import { useMutation } from '@tanstack/react-query' import { ChevronDown } from 'lucide-react' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { cssColor } from '@/features/admin/channels/lib/format' import type { BumperFont, BumperTemplateDto } from '@/shared/api/types' import { Badge } from '@/shared/ui/badge' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { Label } from '@/shared/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/shared/ui/select' import { toast } from '@/shared/ui/toast-store' import { addBumperVariant, deleteBumperTemplate, updateBumperTemplate } from '../api' import { BumperAudioField, BumperBackgroundField } from './BumperFileFields' import { BumperPreviewPlayer } from './BumperPreviewPlayer' import { BumperVariantEditor } from './BumperVariantEditor' export function BumperTemplateEditor({ template, channelId, onChanged, onError, }: Readonly<{ template: BumperTemplateDto /** Канал, чьими глазами смотрим на образцы подстановки; null — общие заглушки. */ channelId: string | null onChanged: () => void onError: (e: unknown) => void }>) { const { t } = useTranslation() const [open, setOpen] = useState(false) const [style, setStyle] = useState({ name: template.name, font: template.font, backgroundColor: template.backgroundColor, backgroundColor2: template.backgroundColor2, accentColor: template.accentColor, textColor: template.textColor, }) useEffect(() => { setStyle({ name: template.name, font: template.font, backgroundColor: template.backgroundColor, backgroundColor2: template.backgroundColor2, accentColor: template.accentColor, textColor: template.textColor, }) }, [template]) const save = useMutation({ mutationFn: () => updateBumperTemplate(template.id, { ...style, name: style.name.trim() }), onSuccess: () => { toast.success(t('settings.saved')) onChanged() }, onError, }) const remove = useMutation({ mutationFn: () => deleteBumperTemplate(template.id), onSuccess: onChanged, onError, }) const addVariant = useMutation({ mutationFn: () => addBumperVariant(template.id, t('admin.bumpers.newVariantName')), onSuccess: onChanged, onError, }) const colorFields = [ { key: 'backgroundColor', label: t('admin.bumpers.colorBg') }, { key: 'backgroundColor2', label: t('admin.bumpers.colorBg2') }, { key: 'accentColor', label: t('admin.bumpers.colorAccent') }, { key: 'textColor', label: t('admin.bumpers.colorText') }, ] as const return (
{open && ( <>
setStyle((s) => ({ ...s, name: e.target.value }))} />
{colorFields.map(({ key, label }) => (
setStyle((s) => ({ ...s, [key]: e.target.value }))} />
))}

{t('admin.bumpers.variants')}

{t('admin.bumpers.variantsHint')}

{[...template.variants] .sort((a, b) => a.position - b.position) .map((variant) => ( 1} onChanged={onChanged} onError={onError} /> ))}
)}
) }