import { useState } from 'react' import { AdminTable, type Column } from '../../components/admin/AdminTable' import { EvolutionFormModal } from '../../components/admin/EvolutionFormModal' import { useEvolutionList, useCreateEvolution, useUpdateEvolution, useDeleteEvolution, } from '../../hooks/useAdmin' import { exportEvolutions } from '../../api/admin' import { downloadJson } from '../../utils/download' import type { EvolutionAdmin, CreateEvolutionInput, UpdateEvolutionInput } from '../../types' const PAGE_SIZE = 50 export function AdminEvolutions() { const [search, setSearch] = useState('') const [page, setPage] = useState(0) const offset = page * PAGE_SIZE const { data, isLoading } = useEvolutionList(search || undefined, PAGE_SIZE, offset) const evolutions = data?.items ?? [] const total = data?.total ?? 0 const totalPages = Math.ceil(total / PAGE_SIZE) const createEvolution = useCreateEvolution() const updateEvolution = useUpdateEvolution() const deleteEvolution = useDeleteEvolution() const [showCreate, setShowCreate] = useState(false) const [editing, setEditing] = useState(null) const columns: Column[] = [ { header: 'From', accessor: (e) => (
{e.fromPokemon.spriteUrl && ( )} {e.fromPokemon.name}
), }, { header: 'To', accessor: (e) => (
{e.toPokemon.spriteUrl && ( )} {e.toPokemon.name}
), }, { header: 'Trigger', accessor: (e) => e.trigger }, { header: 'Level', accessor: (e) => e.minLevel ?? '-' }, { header: 'Item', accessor: (e) => e.item ?? '-' }, ] return (

Evolutions

{ setSearch(e.target.value) setPage(0) }} placeholder="Search by pokemon name, trigger, or item..." className="w-full max-w-sm px-3 py-2 border rounded-md dark:bg-gray-700 dark:border-gray-600" /> {total} evolutions
e.id} onRowClick={(e) => setEditing(e)} /> {totalPages > 1 && (
Showing {offset + 1}-{Math.min(offset + PAGE_SIZE, total)} of {total}
Page {page + 1} of {totalPages}
)} {showCreate && ( createEvolution.mutate(data as CreateEvolutionInput, { onSuccess: () => setShowCreate(false), }) } onClose={() => setShowCreate(false)} isSubmitting={createEvolution.isPending} /> )} {editing && ( updateEvolution.mutate( { id: editing.id, data: data as UpdateEvolutionInput }, { onSuccess: () => setEditing(null) }, ) } onClose={() => setEditing(null)} isSubmitting={updateEvolution.isPending} onDelete={() => deleteEvolution.mutate(editing.id, { onSuccess: () => setEditing(null), }) } isDeleting={deleteEvolution.isPending} /> )}
) }