"use client"; import { useMemo, useState } from "react"; import { Plus, Trash2 } from "lucide-react"; import { buildDataview, type DataviewColumn, type DataviewQueryType } from "@/lib/tools/dataview"; import { CodeOutput } from "@/components/tools/CodeOutput"; const QUERY_TYPES: DataviewQueryType[] = ["TABLE", "LIST", "TASK", "CALENDAR"]; const field = "w-full p-2 text-sm"; const fieldStyle = { border: "1px solid var(--border)", borderRadius: "2px", backgroundColor: "var(--background)", color: "var(--foreground)" } as const; export function DataviewForm() { const [queryType, setQueryType] = useState("TABLE"); const [withoutId, setWithoutId] = useState(false); const [columns, setColumns] = useState([ { expr: "file.name", alias: "Заметка" }, { expr: "file.mtime", alias: "Изменено" }, ]); const [from, setFrom] = useState("#project"); const [where, setWhere] = useState(""); const [sortField, setSortField] = useState("file.mtime"); const [sortDir, setSortDir] = useState<"ASC" | "DESC">("DESC"); const [limit, setLimit] = useState(""); const query = useMemo( () => buildDataview({ queryType, withoutId, columns, from, where, sortField, sortDir, limit: limit ? Number(limit) : undefined, }), [queryType, withoutId, columns, from, where, sortField, sortDir, limit], ); const showColumns = queryType !== "TASK"; const colLabel = queryType === "CALENDAR" ? "поле даты" : queryType === "LIST" ? "поле (берётся первое)" : "колонки"; function updateCol(i: number, patch: Partial) { setColumns((prev) => prev.map((c, idx) => (idx === i ? { ...c, ...patch } : c))); } return (
{(queryType === "TABLE" || queryType === "LIST") && ( )} {showColumns && (
{colLabel} {columns.map((c, i) => (
updateCol(i, { expr: e.target.value })} /> {queryType === "TABLE" && ( updateCol(i, { alias: e.target.value })} /> )}
))} {queryType === "TABLE" && ( )}
)}
); }