import { useMemo } from 'react' import { Link } from '@tanstack/react-router' import { useQuery } from '@tanstack/react-query' import { IconButton } from '@/components/ui/Button' import { Tag } from '@/components/ui/Tag' import type { Garden } from '@/lib/gardens' import { useGardenFull } from '@/lib/objects' import { planYearOf } from '@/lib/plan' import { sharesQueryOptions } from '@/lib/shares' import { formatSize } from '@/lib/units' import { kindPlural } from '@/editor/kinds' import { GardenThumb } from './GardenThumb' // The order kinds are counted in on the card's meta line. const COUNTED_KINDS = ['bed', 'grow_bag', 'container', 'in_ground', 'tree', 'path', 'structure'] /** * One garden as a card: the plot thumbnail (a link into the editor), name + an * optional `plan` tag, size, a counts line, who it's shared with, and a footer * of Open + share / copy / edit / delete. A garden shared WITH you shows its * role and a leave action instead of the owner's tools. */ export function GardenCard({ garden, currentUserId, onShare, onCopy, onEdit, onDelete, onLeave, }: { garden: Garden currentUserId?: number onShare: () => void onCopy: () => void onEdit: () => void onDelete: () => void onLeave: () => void }) { const owner = currentUserId != null && garden.ownerId === currentUserId const full = useGardenFull(garden.id) const shares = useQuery({ ...sharesQueryOptions(garden.id), enabled: owner }) const planYear = planYearOf(garden.name) const meta = useMemo(() => { const data = full.data if (!data) return full.isError ? 'Could not load the plot.' : '…' if (data.objects.length === 0) return 'Bare ground — drag your first bed on.' const counts = new Map() for (const o of data.objects) counts.set(o.kind, (counts.get(o.kind) ?? 0) + 1) const parts = COUNTED_KINDS.filter((k) => counts.has(k)).map((k) => kindPlural(k, counts.get(k)!)) for (const [k, n] of counts) if (!COUNTED_KINDS.includes(k)) parts.push(kindPlural(k, n)) const plops = data.plantings.length parts.push(`${plops} ${plops === 1 ? 'planting' : 'plantings'}`) const since = new Date(garden.createdAt).getFullYear() if (Number.isFinite(since)) parts.push(`tended since ${since}`) return parts.join(' · ') }, [full.data, full.isError, garden.createdAt]) const sharedLine = (() => { if (!owner) return garden.myRole ? `Shared with you · ${garden.myRole}` : 'Shared with you' const list = shares.data ?? [] if (list.length === 0) return null const first = list[0] const who = first.email.includes('@') ? first.email.slice(0, first.email.indexOf('@') + 1) : first.displayName return list.length === 1 ? `Shared with ${who} · ${first.role}` : `Shared with ${who} +${list.length - 1}` })() return (
{garden.name} {planYear != null && plan} {formatSize(garden.widthCm, garden.heightCm, garden.unitPref)}
{meta}
{sharedLine &&
{sharedLine}
}
Open {owner ? ( <> ) : ( )}
) }