import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { Link } from '@tanstack/react-router' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { HttpError } from '@/shared/api/client' import { Badge } from '@/shared/ui/badge' import { Button } from '@/shared/ui/button' import { Input } from '@/shared/ui/input' import { toast } from '@/shared/ui/toast-store' import { createChannel, listChannels } from './api' function slugify(value: string) { return value .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/(^-|-$)/g, '') } export function ChannelsPanel() { const { t } = useTranslation() const queryClient = useQueryClient() const [name, setName] = useState('') const [slug, setSlug] = useState('') const { data, isLoading } = useQuery({ queryKey: ['admin', 'channels'], queryFn: listChannels }) const invalidate = () => queryClient.invalidateQueries({ queryKey: ['admin', 'channels'] }) const onError = (error: unknown) => toast.error(error instanceof HttpError ? error.detail : t('common.error')) const createMutation = useMutation({ mutationFn: () => createChannel({ name: name.trim(), slug: slug || slugify(name) }), onSuccess: () => { setName('') setSlug('') invalidate() }, onError, }) return (

{t('admin.channels.title')}

setName(e.target.value)} /> setSlug(slugify(e.target.value))} />
{isLoading && ( )} {data?.map((channel) => ( ))}
{t('admin.channels.name')} {t('admin.channels.slug')} {t('admin.channels.state')}
{t('common.loading')}
{channel.name} {channel.slug} {channel.isEnabled ? ( {t('admin.channels.enabled')} ) : ( {t('admin.channels.disabled')} )}
) }