import { useState } from 'react' import { useNavigate } from '@tanstack/react-router' import { Alert } from '@/components/ui/Alert' import { Button } from '@/components/ui/Button' import { Modal } from '@/components/ui/Modal' import { TextField } from '@/components/ui/TextField' import { toast } from '@/components/ui/toast' import { errorMessage } from '@/lib/api' import { defaultCopyName, useCopyGarden, type Garden } from '@/lib/gardens' /** * Duplicate a garden under a new name. The name is prefilled with the server's * own default so what you see is what you get; the beds and everything currently * planted in them come along, while the source's share link and shares do not. * On success we land in the copy — the point of copying is to start editing it. */ export function CopyGardenModal({ garden, onClose }: { garden: Garden; onClose: () => void }) { const copy = useCopyGarden() const navigate = useNavigate() const [name, setName] = useState(() => defaultCopyName(garden.name)) const [error, setError] = useState(null) async function onSubmit(e: React.FormEvent) { e.preventDefault() setError(null) try { const created = await copy.mutateAsync({ id: garden.id, name: name.trim() }) toast.info(`Copied to “${created.name}”.`) onClose() navigate({ to: '/gardens/$gardenId', params: { gardenId: String(created.id) } }) } catch (err) { setError(errorMessage(err, 'Could not copy the garden.')) } } return (

Make a copy of {garden.name} with its beds and everything planted in them. The copy is private — the original's share link and people you've shared it with aren't carried over.

setName(e.target.value)} /> {error && {error}}
) }