import { useMutation } from '@tanstack/react-query' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { ChevronDown } from 'lucide-react' import type { 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 { toast } from '@/shared/ui/toast-store' import { addBumperVariant, clearBumperTemplateAudio, removeBumperTemplate, updateBumperTemplate, uploadBumperTemplateAudio, } from '../api' import { cssColor } from '../lib/format' import { BumperBackgroundField } from './BumperBackgroundField' import { BumperFileUpload } from './BumperFileUpload' import { BumperPreviewPlayer } from './BumperPreviewPlayer' import { BumperVariantEditor } from './BumperVariantEditor' export function BumperTemplateEditor({ channelId, template, onChanged, onError, }: Readonly<{ channelId: string template: BumperTemplateDto onChanged: () => void onError: (e: unknown) => void }>) { const { t } = useTranslation() const [open, setOpen] = useState(false) const [name, setName] = useState(template.name) const [colors, setColors] = useState({ backgroundColor: template.backgroundColor, backgroundColor2: template.backgroundColor2, accentColor: template.accentColor, textColor: template.textColor, }) useEffect(() => { setName(template.name) setColors({ backgroundColor: template.backgroundColor, backgroundColor2: template.backgroundColor2, accentColor: template.accentColor, textColor: template.textColor, }) }, [template]) const save = useMutation({ mutationFn: () => updateBumperTemplate(channelId, template.id, { name: name.trim(), ...colors }), onSuccess: () => { toast.success(t('settings.saved')) onChanged() }, onError, }) const remove = useMutation({ mutationFn: () => removeBumperTemplate(channelId, template.id), onSuccess: onChanged, onError, }) const addVariant = useMutation({ mutationFn: () => addBumperVariant(channelId, template.id, ''), onSuccess: onChanged, onError, }) const colorFields: { key: keyof typeof colors; label: string }[] = [ { key: 'backgroundColor', label: t('admin.channels.bumperBg') }, { key: 'backgroundColor2', label: t('admin.channels.bumperBg2') }, { key: 'accentColor', label: t('admin.channels.bumperAccent') }, { key: 'textColor', label: t('admin.channels.bumperText') }, ] return (
{t('admin.channels.bumperVariants')}
{t('admin.channels.bumperVariantsHint')}
{[...template.variants] .sort((a, b) => a.position - b.position) .map((variant) => (