import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { Link, useLocation } from 'react-router' import { Notice } from '@shared/ui/Notice' import { ApiError } from '@shared/api/client' import { useSession } from '@features/auth/session' import { listSlots } from '@features/configs/api' import { copyToSlot } from '../api' const SLOTS = [1, 2, 3, 4] as const export function CopyToSlot({ listingId }: { listingId: string }) { const { t } = useTranslation('showcase') const { status } = useSession() const location = useLocation() const client = useQueryClient() const [error, setError] = useState(null) const [copiedSlot, setCopiedSlot] = useState(null) const { data: slots } = useQuery({ queryKey: ['configs'], queryFn: listSlots, enabled: status === 'authenticated' }) const occupied = new Set((slots ?? []).map((s) => s.slot)) const mutation = useMutation({ mutationFn: (slot: number) => copyToSlot(listingId, slot), onSuccess: (_data, slot) => { setError(null) setCopiedSlot(slot) client.invalidateQueries({ queryKey: ['configs'] }) }, onError: (err) => { setError(err instanceof ApiError && err.status === 409 ? t('copy.conflict') : t('page.error')) }, }) if (status !== 'authenticated') { return (

{t('copy.loginPrompt')}

{t('copy.login')}
) } return (

{t('copy.heading')}

{copiedSlot ? ( {t('copy.success', { slot: copiedSlot })}{' '} {t('copy.openConfigs')} ) : null} {error ? {error} : null}
{SLOTS.map((slot) => ( ))}
) }