import { useAppToast } from '@/composables/useAppToast'; import type { ColumnDef } from '@tanstack/vue-table'; import { useTranslation } from 'i18next-vue'; import { computed, defineComponent, type PropType } from 'vue'; import type { AdTemplate } from '../types'; // Components import CheckIcon from '@/components/icons/CheckIcon.vue'; import LinkIcon from '@/components/icons/LinkIcon.vue'; import PencilIcon from '@/components/icons/PencilIcon.vue'; import TrashIcon from '@/components/icons/TrashIcon.vue'; import AppButton from '@/components/ui/AppButton.vue'; import AppSwitch from '@/components/ui/AppSwitch.vue'; import BaseTable from '@/components/ui/BaseTable.vue'; import SettingsTableSkeleton from '@/routes/settings/components/SettingsTableSkeleton.vue'; import { formatDate } from '../../DomainsDns/helpers'; export default defineComponent({ name: 'AdTemplateTable', props: { templates: { type: Array as PropType, required: true }, isInitialLoading: { type: Boolean, default: false }, isReadOnly: { type: Boolean, default: false }, isMutating: { type: Boolean, default: false }, saving: { type: Boolean, default: false }, deletingId: { type: String as PropType, default: null }, togglingId: { type: String as PropType, default: null }, defaultingId: { type: String as PropType, default: null }, }, emits: { edit: (template: AdTemplate) => true, delete: (template: AdTemplate) => true, 'toggle-active': (payload: { template: AdTemplate; value: boolean }) => true, 'set-default': (template: AdTemplate) => true, }, setup(props, { emit }) { const toast = useAppToast(); const { t } = useTranslation(); const adFormatLabels = computed>(() => ({ 'pre-roll': t('settings.adsVast.formats.preRoll'), 'mid-roll': t('settings.adsVast.formats.midRoll'), 'post-roll': t('settings.adsVast.formats.postRoll'), })); const getAdFormatLabel = (format?: string) => adFormatLabels.value[format || ''] || format || '-'; const getAdFormatColor = (format?: string) => { const colors: Record = { 'pre-roll': 'bg-blue-500/10 text-blue-500', 'mid-roll': 'bg-yellow-500/10 text-yellow-500', 'post-roll': 'bg-purple-500/10 text-purple-500', }; return colors[format || ''] || 'bg-gray-500/10 text-gray-500'; }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); } catch { const textArea = document.createElement('textarea'); textArea.value = text; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); } toast.add({ severity: 'success', summary: t('settings.adsVast.toast.copiedSummary'), detail: t('settings.adsVast.toast.copiedDetail'), life: 2000, }); }; const columns = computed[]>(() => [ { id: 'template', header: t('settings.adsVast.table.template'), accessorFn: (row) => row.name || '', cell: ({ row }) => ( <>
{row.original.name || ''} {row.original.isDefault && ( {t('settings.adsVast.defaultBadge')} )}

{t('settings.adsVast.createdOn', { date: formatDate(row.original.createdAt) || '-' })}

), meta: { headerClass: 'px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-foreground/50', cellClass: 'px-6 py-3', }, }, { id: 'format', header: t('settings.adsVast.table.format'), accessorFn: (row) => row.adFormat || '', cell: ({ row }) => (
{getAdFormatLabel(row.original.adFormat)} {row.original.adFormat === 'mid-roll' && row.original.duration && ( ({row.original.duration}s) )}
), meta: { headerClass: 'px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-foreground/50', cellClass: 'px-6 py-3', }, }, { id: 'vastUrl', header: t('settings.adsVast.table.vastUrl'), accessorFn: (row) => row.vastTagUrl || '', cell: ({ row }) => (
{row.original.vastTagUrl || ''} copyToClipboard(row.original.vastTagUrl || '')} v-slots={{ icon: () => }} />
), enableSorting: false, meta: { headerClass: 'px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-foreground/50', cellClass: 'px-6 py-3', }, }, { id: 'status', header: t('common.status'), accessorFn: (row) => Number(Boolean(row.isActive)), cell: ({ row }) => (
emit('toggle-active', { template: row.original, value })} />
), meta: { headerClass: 'px-6 py-3 text-center text-xs font-medium uppercase tracking-wider text-foreground/50', cellClass: 'px-6 py-3 text-center', }, }, { id: 'actions', header: t('common.actions'), enableSorting: false, cell: ({ row }) => (
{!row.original.isDefault && ( emit('set-default', row.original)} > {t('settings.adsVast.actions.setDefault')} )} emit('edit', row.original)} v-slots={{ icon: () => }} /> emit('delete', row.original)} v-slots={{ icon: () => }} />
), meta: { headerClass: 'px-6 py-3 text-center text-xs font-medium uppercase tracking-wider text-foreground/50 [&>div]:justify-center', cellClass: 'px-6 py-3 text-center', }, }, ]); return () => ( <> {props.isInitialLoading ? ( ) : ( row.id || `${row.name || 'template'}:${row.vastTagUrl || index}` } wrapperClass="mt-4 border-b border-border rounded-none border-x-0 border-t-0 bg-transparent" tableClass="w-full" headerRowClass="bg-muted/30" bodyRowClass="border-b border-border hover:bg-muted/30" v-slots={{ empty: () => (

{t('settings.adsVast.emptyTitle')}

{t('settings.adsVast.emptySubtitle')}

) }} /> )} ); }, });