ObjectTable.tsx
8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** 一类对象的表格:属性成列,可以筛选、点表头排序、翻页。点一行打开这个对象。 */
import { ArrowDown, ArrowUp, ListFilter, X } from "lucide-react";
import { useEffect, useState } from "react";
import { isNumeric, ObjectPicker, Pager, Value, Select } from "@/components/kit";
import { Empty, ErrorNote, Spinner } from "@/components/ui";
import type { FieldMeta } from "@/lib/format";
import { errText, onto, type ObjectRec } from "@/lib/onto";
import { cn } from "@/lib/utils";
const OPS: Record<string, string> = { eq: "等于", ne: "不等于", gt: "大于", ge: "不小于", lt: "小于", le: "不大于",
contains: "包含", empty: "为空", not_empty: "不为空" };
const PAGE = 30;
interface Filter { property: string; op: string; value: any }
function FilterValue({ f, field, onChange }: { f: Filter; field?: FieldMeta; onChange: (v: any) => void }) {
if (["empty", "not_empty"].includes(f.op)) return null;
if (field?.data_type === "boolean") {
return (
<Select className="input h-8 w-20" value={String(f.value ?? "")} onChange={(e) => onChange(e.target.value === "" ? "" : Number(e.target.value))}>
<option value="">请选择</option><option value="1">是</option><option value="0">否</option>
</Select>
);
}
const opts = field?.enum?.length ? field.enum : Object.keys(field?.tones || {});
if (opts.length && f.op !== "contains") {
return (
<Select className="input h-8 w-32" value={f.value ?? ""} onChange={(e) => onChange(e.target.value)}>
<option value="">请选择</option>{opts.map((x) => <option key={x}>{x}</option>)}
</Select>
);
}
if (field?.ref && f.op === "eq") return <ObjectPicker className="w-52" type={field.ref.type_id} value={f.value} onChange={onChange} />;
return <input className="input h-8 w-32" type={isNumeric(field) ? "number" : "text"} placeholder="值" value={f.value ?? ""}
onChange={(e) => onChange(isNumeric(field) && e.target.value !== "" ? Number(e.target.value) : e.target.value)} />;
}
export function ObjectTable({ type, q, onOpen }: { type: string; q: string; onOpen: (key: string) => void }) {
const [filters, setFilters] = useState<Filter[]>([]);
const [quick, setQuick] = useState<string[]>([]);
const [sort, setSort] = useState<{ property: string; dir: "asc" | "desc" } | null>(null);
const [offset, setOffset] = useState(0);
const [data, setData] = useState<{ items: ObjectRec[]; total: number; fields: FieldMeta[] } | null>(null);
const [err, setErr] = useState("");
const [tick, setTick] = useState(0);
useEffect(() => { setFilters([]); setQuick([]); setSort(null); setOffset(0); setData(null); }, [type]);
useEffect(() => { setOffset(0); }, [q, JSON.stringify(filters), JSON.stringify(sort), JSON.stringify(quick)]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
let alive = true;
const h = window.setTimeout(() => {
const fs = [
...filters.filter((f) => f.property && (f.value !== "" && f.value !== undefined && f.value !== null || ["empty", "not_empty"].includes(f.op))),
...quick.map((p) => ({ property: p, op: "eq", value: 1 })),
];
void onto.queryObjects(type, { filters: fs, sort: sort ? [sort] : [], q, limit: PAGE, offset })
.then((r) => { if (alive) { setData({ items: r.items, total: r.total, fields: r.fields || [] }); setErr(""); } })
.catch((e) => { if (alive) setErr(errText(e)); });
}, 250);
return () => { alive = false; window.clearTimeout(h); };
}, [type, q, offset, tick, JSON.stringify(filters), JSON.stringify(sort), JSON.stringify(quick)]); // eslint-disable-line react-hooks/exhaustive-deps
const fields = data?.fields || [];
// 「是 / 否」类的属性(可订货、超储、呆滞……)做成一排开关,点一下就只看这些
const flags = fields.filter((f) => f.data_type === "boolean");
const set = (i: number, p: Partial<Filter>) => setFilters((fs) => fs.map((f, j) => (j === i ? { ...f, ...p } : f)));
const toggleSort = (name: string) => setSort((s) => (s?.property === name ? (s.dir === "asc" ? { property: name, dir: "desc" } : null) : { property: name, dir: "asc" }));
if (err && !data) return <ErrorNote onRetry={() => setTick((t) => t + 1)}>{err}</ErrorNote>;
return (
<div className="card flex min-h-0 flex-1 flex-col overflow-hidden">
<div className="flex flex-wrap items-center gap-2 border-b border-line px-3 py-2.5">
{flags.length > 0 && (
<div className="flex flex-wrap items-center gap-1.5">
<span className="text-label text-muted">只看</span>
{flags.map((f) => (
<button key={f.name} className={cn("chip", quick.includes(f.name) && "border-brand bg-brand/5 text-brand")}
onClick={() => setQuick((qs) => (qs.includes(f.name) ? qs.filter((x) => x !== f.name) : [...qs, f.name]))}>{f.label}</button>
))}
</div>
)}
<button className="btn btn-sm" onClick={() => setFilters((fs) => [...fs, { property: "", op: "eq", value: "" }])}>
<ListFilter className="h-3.5 w-3.5" />加筛选
</button>
{(filters.length > 0 || quick.length > 0) && (
<button className="btn btn-ghost btn-sm text-muted" onClick={() => { setFilters([]); setQuick([]); }}>清空筛选</button>
)}
</div>
{filters.length > 0 && (
<div className="flex flex-wrap items-center gap-2 border-b border-line bg-ink/[0.015] px-3 py-2">
{filters.map((f, i) => {
const field = fields.find((x) => x.name === f.property);
return (
<span key={i} className="flex items-center gap-1.5 rounded-lg border border-line bg-surface p-1">
<Select className="input h-8 w-36" value={f.property} onChange={(e) => set(i, { property: e.target.value, value: "" })}>
<option value="">选属性</option>
{fields.map((p) => <option key={p.name} value={p.name}>{p.label}</option>)}
</Select>
{field?.data_type !== "boolean" && (
<Select className="input h-8 w-24" value={f.op} onChange={(e) => set(i, { op: e.target.value })}>
{Object.entries(OPS).map(([k, v]) => <option key={k} value={k}>{v}</option>)}
</Select>
)}
<FilterValue f={f} field={field} onChange={(v) => set(i, { value: v })} />
<button className="px-1 text-muted hover:text-bad" title="去掉这个条件" onClick={() => setFilters((fs) => fs.filter((_, j) => j !== i))}><X className="h-3.5 w-3.5" /></button>
</span>
);
})}
</div>
)}
{err && <div className="p-3"><ErrorNote onRetry={() => setTick((t) => t + 1)}>{err}</ErrorNote></div>}
<div className="min-h-0 flex-1 overflow-auto">
{!data ? <div className="grid place-items-center p-10"><Spinner /></div> : data.items.length === 0 ? (
<Empty title="没有符合条件的记录" desc={filters.length || quick.length || q ? "放宽筛选条件试试。" : undefined} />
) : (
<table className="tbl">
<thead>
<tr>
<th className="sticky left-0 z-[2] bg-surface">名称</th>
{fields.map((p) => (
<th key={p.name} className={cn(isNumeric(p) && "text-right")} title={p.description || undefined}>
<button className="inline-flex items-center gap-1 hover:text-ink" onClick={() => toggleSort(p.name)}>
{p.label}
{sort?.property === p.name && (sort.dir === "asc" ? <ArrowUp className="h-3 w-3" /> : <ArrowDown className="h-3 w-3" />)}
</button>
</th>
))}
</tr>
</thead>
<tbody>
{data.items.map((o) => (
<tr key={o.key} className="row-link group" onClick={() => onOpen(o.key)}>
<td className="sticky left-0 z-[1] max-w-[18rem] truncate whitespace-nowrap bg-surface font-medium group-hover:bg-[rgb(var(--elevated))]">{o.label}</td>
{fields.map((p) => (
<td key={p.name} className={cn("max-w-[16rem] truncate whitespace-nowrap", isNumeric(p) && "num")}>
<Value v={(o.properties || {})[p.name]} field={p} refInfo={o.refs?.[p.name]} />
</td>
))}
</tr>
))}
</tbody>
</table>
)}
</div>
{data && <Pager total={data.total} limit={PAGE} offset={offset} onChange={setOffset} />}
</div>
);
}