"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useCallback, useEffect, useState } from "react"; import { api } from "@/lib/api/client"; import type { components } from "@/lib/api/schema"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; type Tree = components["schemas"]["TreeRead"]; export default function TreesPage() { const router = useRouter(); const [trees, setTrees] = useState([]); const [deleted, setDeleted] = useState([]); const [name, setName] = useState(""); const [ready, setReady] = useState(false); const load = useCallback(async () => { const { data, response } = await api.GET("/api/v1/trees"); if (response.status === 401) { router.push("/login"); return; } setTrees(data ?? []); const del = await api.GET("/api/v1/trees", { params: { query: { deleted: true } } }); setDeleted(del.data ?? []); setReady(true); }, [router]); useEffect(() => { load(); }, [load]); async function createTree(e: React.FormEvent) { e.preventDefault(); if (!name.trim()) return; const { error } = await api.POST("/api/v1/trees", { body: { name } }); if (!error) { setName(""); load(); } } async function remove(id: string) { await api.DELETE("/api/v1/trees/{tree_id}", { params: { path: { tree_id: id } } }); load(); } async function restore(id: string) { await api.POST("/api/v1/trees/{tree_id}/restore", { params: { path: { tree_id: id } } }); load(); } // Optimistic visibility change so the dropdown reflects the pick immediately. async function setVisibility(id: string, visibility: NonNullable) { setTrees((cur) => cur.map((t) => (t.id === id ? { ...t, visibility } : t))); await api.PATCH("/api/v1/trees/{tree_id}", { params: { path: { tree_id: id } }, body: { visibility }, }); load(); } if (!ready) return

Loading…

; return (

Your trees

setName(e.target.value)} />
{trees.length === 0 ? (

No trees yet — create your first one above.

) : (
    {trees.map((tree) => (
  • {tree.name}
  • ))}
)} {deleted.length > 0 && (

Recently deleted

    {deleted.map((tree) => (
  • {tree.name}
  • ))}
)}
); }