@protobase/ui 0.12.11 → 0.12.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -62,12 +62,15 @@ const stateToClauses = (config, state, now = /* @__PURE__ */ new Date()) => {
62
62
  if (values.length === 0) return [];
63
63
  return [values.length === 1 ? w.eq(group.id, values[0]) : w.in(group.id, values)];
64
64
  });
65
- const range = config.range && state.range ? [w.gte(config.range.id, state.range[0]), w.lte(config.range.id, state.range[1])] : [];
65
+ const ranges = (config.ranges ?? []).flatMap((range) => {
66
+ const span = state.ranges?.[range.id];
67
+ return span ? [w.gte(range.id, span[0]), w.lte(range.id, span[1])] : [];
68
+ });
66
69
  const date = config.dateField && state.date ? dateClauses(config.dateField, state.date, now, config.dateKind) : [];
67
70
  const toggles = (config.toggles ?? []).filter((toggle) => state.toggles.includes(toggle.id)).map((toggle) => w.eq(toggle.id, true));
68
71
  return [
69
72
  ...facets,
70
- ...range,
73
+ ...ranges,
71
74
  ...date,
72
75
  ...toggles
73
76
  ];
@@ -103,7 +106,8 @@ const textToState = (config, text, now = /* @__PURE__ */ new Date()) => {
103
106
  const extras = [];
104
107
  const facetIds = new Set(config.facets.map((group) => group.id));
105
108
  const toggleIds = new Set((config.toggles ?? []).map((toggle) => toggle.id));
106
- const bounds = { clauses: [] };
109
+ const rangeIds = new Set((config.ranges ?? []).map((range) => range.id));
110
+ const bounds = /* @__PURE__ */ new Map();
107
111
  let dateClause;
108
112
  let todayEnd;
109
113
  for (const clause of parsed.ast ? flatten(parsed.ast) : []) if (clause.kind === "compare" && facetIds.has(fieldName(clause)) && clause.op === "=" && literalText(clause.value) !== void 0) state.facets[fieldName(clause)] = [...state.facets[fieldName(clause)] ?? [], literalText(clause.value)];
@@ -113,18 +117,23 @@ const textToState = (config, text, now = /* @__PURE__ */ new Date()) => {
113
117
  dateClause = clause;
114
118
  } else if (clause.kind === "compare" && config.dateField === fieldName(clause) && clause.op === "<" && (clause.value.kind === "timestamp" || clause.value.kind === "string") && periodStart("today", now).startsWith(clause.value.value.slice(0, 10))) todayEnd = clause;
115
119
  else if (clause.kind === "compare" && toggleIds.has(fieldName(clause)) && clause.op === "=" && clause.value.kind === "boolean" && clause.value.value) state.toggles.push(fieldName(clause));
116
- else if (clause.kind === "compare" && config.range?.id === fieldName(clause) && (clause.op === ">=" || clause.op === "<=") && clause.value.kind === "number") {
117
- if (clause.op === ">=") bounds.low = clause.value.value;
118
- else bounds.high = clause.value.value;
119
- bounds.clauses.push(clause);
120
+ else if (clause.kind === "compare" && rangeIds.has(fieldName(clause)) && (clause.op === ">=" || clause.op === "<=") && clause.value.kind === "number") {
121
+ const range = bounds.get(fieldName(clause)) ?? { clauses: [] };
122
+ if (clause.op === ">=") range.low = clause.value.value;
123
+ else range.high = clause.value.value;
124
+ range.clauses.push(clause);
125
+ bounds.set(fieldName(clause), range);
120
126
  } else extras.push(clause);
121
127
  if (state.date === "yesterday" && !todayEnd) {
122
128
  extras.push(dateClause);
123
129
  state.date = void 0;
124
130
  }
125
131
  if (todayEnd && state.date !== "yesterday") extras.push(todayEnd);
126
- if (bounds.low !== void 0 && bounds.high !== void 0) state.range = [bounds.low, bounds.high];
127
- else extras.push(...bounds.clauses);
132
+ for (const [id, range] of bounds) if (range.low !== void 0 && range.high !== void 0) state.ranges = {
133
+ ...state.ranges,
134
+ [id]: [range.low, range.high]
135
+ };
136
+ else extras.push(...range.clauses);
128
137
  return {
129
138
  ok: true,
130
139
  state,
@@ -0,0 +1,14 @@
1
+ //#region src/live/reference-options.ts
2
+ /** Records found by a search as options, with the count of each that the facet counts know. */
3
+ const matchOptions = (matches, counted) => {
4
+ const counts = new Map(counted.map((option) => [option.value, option.count]));
5
+ return matches.map(([value, label]) => ({
6
+ value,
7
+ label,
8
+ ...counts.get(value) !== void 0 && { count: counts.get(value) }
9
+ }));
10
+ };
11
+ /** The keys a facet needs names for: the values it counted and those chosen, which may lie outside the counted ones. */
12
+ const labelKeys = (counted, chosen) => [.../* @__PURE__ */ new Set([...counted, ...chosen])];
13
+ //#endregion
14
+ export { labelKeys, matchOptions };
@@ -3,21 +3,28 @@ import { keys } from "../data/query-keys.js";
3
3
  import { useClient } from "../data/api-provider.js";
4
4
  import { humanize } from "./naming.js";
5
5
  import { fetchLabels } from "./relation-labels.js";
6
+ import { setRange } from "../filter-panel/filter-state.js";
6
7
  import { stateToText } from "./filter-string.js";
7
- import { useHistogram } from "../data/use-aggregates.js";
8
- import { widgetFields } from "./view-fields.js";
8
+ import { labelKeys, matchOptions } from "./reference-options.js";
9
+ import { useReferenceSearch } from "./use-reference-search.js";
10
+ import { rangeHistogram, widgetFields } from "./view-fields.js";
9
11
  import { useQueries, useQueryClient } from "@tanstack/react-query";
10
12
  import { enumLabel } from "@protobase/schema";
11
13
  //#region src/live/use-filter-config.ts
14
+ const histogramBuckets = 24;
12
15
  /**
13
- * The panel's config for a view: facet counts come from `:facets`, each computed with every
14
- * other active filter applied but not its own, so choosing one value does not hide the rest.
16
+ * The panel's config for a view: facet counts come from `:facets` and each range's bounds from `:histogram`, each
17
+ * computed with every other active filter applied but not its own, so choosing one value does not hide the rest.
15
18
  */
16
19
  const useFilterConfig = ({ model, resources, views, view, state, extras }) => {
17
20
  const client = useClient();
18
21
  const queryClient = useQueryClient();
19
22
  const skeleton = widgetFields(view, model);
20
23
  const label = (name) => view?.fields[name]?.label ?? humanize(name);
24
+ const targetOf = (name) => {
25
+ const field = model.fields[name];
26
+ return field?.type === "relation" && field.relation ? resources[field.relation.resource] : void 0;
27
+ };
21
28
  const facetQueries = useQueries({ queries: skeleton.facets.map((group) => {
22
29
  const filter = stateToText(skeleton, {
23
30
  ...state,
@@ -35,46 +42,66 @@ const useFilterConfig = ({ model, resources, views, view, state, extras }) => {
35
42
  };
36
43
  }) });
37
44
  const labelQueries = useQueries({ queries: skeleton.facets.map((group, index) => {
38
- const field = model.fields[group.id];
39
- const target = field?.type === "relation" && field.relation ? resources[field.relation.resource] : void 0;
40
- const ids = (facetQueries[index]?.data ?? []).map((facet) => String(facet.value));
45
+ const target = targetOf(group.id);
46
+ const ids = labelKeys((facetQueries[index]?.data ?? []).flatMap((facet) => facet.value === null ? [] : [String(facet.value)]), state.facets[group.id] ?? []);
41
47
  return {
42
48
  queryKey: keys.labels(`facet:${model.name}.${group.id}`, ids.join(",")),
43
49
  enabled: Boolean(target) && ids.length > 0,
44
50
  queryFn: async () => [...await fetchLabels(queryClient, client, target, ids, views[target.name])]
45
51
  };
46
52
  }) });
47
- const rangeFilter = stateToText({
48
- ...skeleton,
49
- range: void 0
50
- }, state, extras);
51
- const histogram = useHistogram(model.name, skeleton.range?.id ?? "", rangeFilter, 24, Boolean(skeleton.range));
52
- const rangeHints = skeleton.range ? view?.fields[skeleton.range.id] : void 0;
53
- const prefix = rangeHints?.prefix;
54
- const suffix = rangeHints?.suffix ?? "";
55
- return {
56
- facets: skeleton.facets.map((group, index) => {
57
- const field = model.fields[group.id];
58
- const names = new Map(labelQueries[index]?.data ?? []);
59
- return {
60
- ...group,
61
- label: label(group.id),
62
- options: (facetQueries[index]?.data ?? []).flatMap((facet) => facet.value === null ? [] : [{
63
- value: String(facet.value),
64
- label: field?.type === "enum" ? enumLabel(String(facet.value), view?.fields[group.id]?.valueLabels) : names.get(String(facet.value)) ?? String(facet.value),
65
- count: facet.count
66
- }])
67
- };
68
- }),
69
- ...skeleton.range && histogram.data && histogram.data.min !== null && histogram.data.max !== null && { range: {
70
- ...skeleton.range,
71
- label: label(skeleton.range.id),
72
- min: Math.floor(histogram.data.min),
73
- max: Math.ceil(histogram.data.max),
74
- step: Math.max(1, Math.round((histogram.data.max - histogram.data.min) / 100)),
75
- histogram: histogram.data.buckets.map((bucket) => bucket.count),
53
+ const searches = useReferenceSearch(skeleton.facets.map((group) => ({
54
+ id: group.id,
55
+ target: group.combobox ? targetOf(group.id) : void 0
56
+ })), views);
57
+ const ranges = skeleton.ranges ?? [];
58
+ const histogramQueries = useQueries({ queries: ranges.map((range) => {
59
+ const filter = stateToText(skeleton, setRange(state, range.id, void 0), extras);
60
+ const buckets = rangeHistogram(view, range.id) ? histogramBuckets : 1;
61
+ return {
62
+ queryKey: keys.histogram(model.name, range.id, buckets, filter),
63
+ queryFn: () => client.histogram(model.name, range.id, {
64
+ filter,
65
+ buckets
66
+ })
67
+ };
68
+ }) });
69
+ const facetGroup = (group, index) => {
70
+ const field = model.fields[group.id];
71
+ const names = new Map(labelQueries[index]?.data ?? []);
72
+ const counted = (facetQueries[index]?.data ?? []).flatMap((facet) => facet.value === null ? [] : [{
73
+ value: String(facet.value),
74
+ label: field?.type === "enum" ? enumLabel(String(facet.value), view?.fields[group.id]?.valueLabels) : names.get(String(facet.value)) ?? String(facet.value),
75
+ count: facet.count
76
+ }]);
77
+ const search = searches[index];
78
+ return {
79
+ ...group,
80
+ label: label(group.id),
81
+ options: search.matches ? matchOptions(search.matches, counted) : counted,
82
+ ...names.size > 0 && { labels: Object.fromEntries(names) },
83
+ ...search.onSearch && { onSearch: search.onSearch }
84
+ };
85
+ };
86
+ const rangeConfig = (range, index) => {
87
+ const data = histogramQueries[index]?.data;
88
+ if (!data || data.min === null || data.max === null) return [];
89
+ const hints = view?.fields[range.id];
90
+ const prefix = hints?.prefix;
91
+ const suffix = hints?.suffix ?? "";
92
+ return [{
93
+ ...range,
94
+ label: label(range.id),
95
+ min: Math.floor(data.min),
96
+ max: Math.ceil(data.max),
97
+ step: Math.max(1, Math.round((data.max - data.min) / 100)),
98
+ histogram: rangeHistogram(view, range.id) ? data.buckets.map((bucket) => bucket.count) : [],
76
99
  format: (value) => `${prefix === "€" ? formatMoney(value * 100).replace(".00", "") : String(value)}${suffix}`
77
- } },
100
+ }];
101
+ };
102
+ return {
103
+ facets: skeleton.facets.map(facetGroup),
104
+ ranges: ranges.flatMap(rangeConfig),
78
105
  ...skeleton.dateField && {
79
106
  datePresets: skeleton.datePresets ?? true,
80
107
  dateField: skeleton.dateField,
@@ -0,0 +1,53 @@
1
+ import { useClient } from "../data/api-provider.js";
2
+ import { labelFieldOf } from "./model-helpers.js";
3
+ import { useState } from "react";
4
+ import { keepPreviousData, useQueries } from "@tanstack/react-query";
5
+ import { createWhere, printFilter } from "@protobase/schema";
6
+ //#region src/live/use-reference-search.ts
7
+ const where = createWhere();
8
+ /**
9
+ * The search text typed into each facet's combobox and the records of its target resource that match it. A facet
10
+ * without a target, or whose target has no search fields or a composite key, gets nothing: its options are filtered
11
+ * by name instead.
12
+ */
13
+ const useReferenceSearch = (targets, views) => {
14
+ const client = useClient();
15
+ const [texts, setTexts] = useState({});
16
+ const searchable = (target) => Boolean(target?.search?.length) && target.primaryKey.length === 1;
17
+ const results = useQueries({ queries: targets.map(({ id, target }) => {
18
+ const text = (texts[id] ?? "").trim();
19
+ return {
20
+ queryKey: [
21
+ "reference-search",
22
+ id,
23
+ target?.name ?? "",
24
+ text
25
+ ],
26
+ enabled: searchable(target) && text !== "",
27
+ placeholderData: keepPreviousData,
28
+ queryFn: async () => {
29
+ const keyField = target.primaryKey[0];
30
+ const labelField = labelFieldOf(target, views[target.name]);
31
+ return (await client.list(target.name, {
32
+ filter: printFilter(where.search(text)),
33
+ fields: [keyField, labelField],
34
+ pageSize: 20
35
+ })).items.map((item) => [String(item[keyField]), String(item[labelField])]);
36
+ }
37
+ };
38
+ }) });
39
+ return targets.map(({ id, target }, index) => {
40
+ if (!searchable(target)) return {};
41
+ const onSearch = (text) => setTexts((current) => ({
42
+ ...current,
43
+ [id]: text
44
+ }));
45
+ if ((texts[id] ?? "").trim() === "") return { onSearch };
46
+ return {
47
+ onSearch,
48
+ matches: results[index]?.data ?? []
49
+ };
50
+ });
51
+ };
52
+ //#endregion
53
+ export { useReferenceSearch };
@@ -1,25 +1,40 @@
1
1
  //#region src/live/view-fields.ts
2
- /** The widgets of a view in the shape `filter-string` talks to; options and counts are filled in later. */
2
+ const numberTypes = [
3
+ "integer",
4
+ "bigint",
5
+ "decimal"
6
+ ];
7
+ const isNumber = (model, field) => {
8
+ const type = model?.fields[field]?.type;
9
+ return type !== void 0 && numberTypes.includes(type);
10
+ };
11
+ /** A filter on a number field is a range slider, whichever widget the view asked for. */
12
+ const rangeOf = (widget, model) => widget.kind === "range" || widget.kind === "facets" && isNumber(model, widget.field);
13
+ const emptyRange = (field) => ({
14
+ id: field,
15
+ label: field,
16
+ min: 0,
17
+ max: 0,
18
+ step: 1,
19
+ histogram: [],
20
+ format: String
21
+ });
22
+ /**
23
+ * The widgets of a view in the shape `filter-string` talks to; options and counts are filled in later. Facets on a
24
+ * reference to another resource's records are a combobox, which searches those records.
25
+ */
3
26
  const widgetFields = (view, model) => {
4
27
  const widgets = view?.filters ?? [];
5
- const range = widgets.find((widget) => widget.kind === "range");
6
28
  const date = widgets.find((widget) => widget.kind === "dateRange");
7
29
  return {
8
- facets: widgets.flatMap((widget) => widget.kind === "facets" ? [{
30
+ facets: widgets.flatMap((widget) => widget.kind === "facets" && !rangeOf(widget, model) ? [{
9
31
  id: widget.field,
10
32
  label: widget.field,
11
33
  options: [],
12
- searchable: widget.search
34
+ searchable: widget.search,
35
+ ...model?.fields[widget.field]?.type === "relation" && { combobox: true }
13
36
  }] : []),
14
- ...range && { range: {
15
- id: range.field,
16
- label: range.field,
17
- min: 0,
18
- max: 0,
19
- step: 1,
20
- histogram: [],
21
- format: String
22
- } },
37
+ ranges: widgets.flatMap((widget) => rangeOf(widget, model) ? [emptyRange(widget.field)] : []),
23
38
  ...date && {
24
39
  dateField: date.field,
25
40
  dateKind: model?.fields[date.field]?.type === "date" ? "date" : "timestamp",
@@ -31,5 +46,7 @@ const widgetFields = (view, model) => {
31
46
  }] : [])
32
47
  };
33
48
  };
49
+ /** Whether a range draws its histogram: only a `range` widget that asks for one. */
50
+ const rangeHistogram = (view, field) => (view?.filters ?? []).some((widget) => widget.kind === "range" && widget.field === field && widget.histogram);
34
51
  //#endregion
35
- export { widgetFields };
52
+ export { rangeHistogram, widgetFields };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.12.11";
2
+ var version = "0.12.13";
3
3
  var protobase = { "pluginApi": 1 };
4
4
  //#endregion
5
5
  export { protobase, version };
@@ -0,0 +1,14 @@
1
+ //#region src/primitives/menu-keys.ts
2
+ /**
3
+ * The item of an open menu a key moves the focus to, from the item at `index` (-1 for none) of `count`: the arrows step
4
+ * and wrap, Home and End jump to the ends. `undefined` for a key that does not move it.
5
+ */
6
+ const menuKeyTarget = (key, index, count) => {
7
+ if (count === 0) return void 0;
8
+ if (key === "Home") return 0;
9
+ if (key === "End") return count - 1;
10
+ if (key === "ArrowDown") return index < 0 ? 0 : (index + 1) % count;
11
+ if (key === "ArrowUp") return index < 0 ? count - 1 : (index - 1 + count) % count;
12
+ };
13
+ //#endregion
14
+ export { menuKeyTarget };
@@ -0,0 +1,88 @@
1
+ import { cn } from "../lib/cn.js";
2
+ import { menuKeyTarget } from "./menu-keys.js";
3
+ import { useEffect, useRef, useState } from "react";
4
+ import { Check, ChevronDown } from "lucide-react";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ //#region src/primitives/menu-select.tsx
7
+ /**
8
+ * The choices of a Segmented switch in a menu, for a row without room for every option: a button with the chosen
9
+ * option's icon and label, opening the options under its right edge.
10
+ */
11
+ const MenuSelect = ({ value, options, onChange, label }) => {
12
+ const [open, setOpen] = useState(false);
13
+ const root = useRef(null);
14
+ const trigger = useRef(null);
15
+ const items = useRef([]);
16
+ const chosen = options.findIndex((option) => option.value === value);
17
+ const current = options[chosen];
18
+ useEffect(() => {
19
+ if (!open) return;
20
+ items.current[Math.max(chosen, 0)]?.focus();
21
+ const close = (event) => {
22
+ if (event.target instanceof Node && !root.current?.contains(event.target)) setOpen(false);
23
+ };
24
+ document.addEventListener("pointerdown", close);
25
+ return () => document.removeEventListener("pointerdown", close);
26
+ }, [open]);
27
+ const closeToTrigger = () => {
28
+ setOpen(false);
29
+ trigger.current?.focus();
30
+ };
31
+ const onKeyDown = (event) => {
32
+ if (event.key === "Escape") return closeToTrigger();
33
+ if (event.key === "Tab") return setOpen(false);
34
+ const target = menuKeyTarget(event.key, items.current.indexOf(document.activeElement), options.length);
35
+ if (target === void 0) return;
36
+ event.preventDefault();
37
+ items.current[target]?.focus();
38
+ };
39
+ return /* @__PURE__ */ jsxs("div", {
40
+ ref: root,
41
+ className: "relative shrink-0",
42
+ children: [/* @__PURE__ */ jsxs("button", {
43
+ ref: trigger,
44
+ type: "button",
45
+ "aria-label": current ? `${label}: ${current.label}` : label,
46
+ "aria-haspopup": "menu",
47
+ "aria-expanded": open,
48
+ onClick: () => setOpen(!open),
49
+ className: "inline-flex h-8 items-center gap-1.5 rounded-md border border-border-strong bg-background px-2.5 text-[13px] font-medium shadow-sm transition-colors hover:bg-muted pointer-coarse:h-11",
50
+ children: [
51
+ current?.icon && /* @__PURE__ */ jsx(current.icon, { className: "size-3.5" }),
52
+ current?.label ?? label,
53
+ /* @__PURE__ */ jsx(ChevronDown, { className: "size-3.5 text-muted-foreground" })
54
+ ]
55
+ }), open && /* @__PURE__ */ jsx("ul", {
56
+ role: "menu",
57
+ "aria-label": label,
58
+ onKeyDown,
59
+ className: "absolute right-0 top-full z-30 mt-1 min-w-40 rounded-md border bg-background py-1 text-[13px] shadow-pop",
60
+ children: options.map((option, index) => /* @__PURE__ */ jsx("li", {
61
+ role: "none",
62
+ children: /* @__PURE__ */ jsxs("button", {
63
+ ref: (element) => {
64
+ items.current[index] = element;
65
+ },
66
+ type: "button",
67
+ role: "menuitemradio",
68
+ "aria-checked": option.value === value,
69
+ onClick: () => {
70
+ closeToTrigger();
71
+ if (option.value !== value) onChange(option.value);
72
+ },
73
+ className: cn("flex min-h-9 w-full items-center gap-2 whitespace-nowrap px-3 py-1.5 text-left hover:bg-muted focus:bg-muted focus:outline-none pointer-coarse:min-h-11", option.value === value && "font-medium"),
74
+ children: [
75
+ option.icon ? /* @__PURE__ */ jsx(option.icon, { className: "size-3.5" }) : /* @__PURE__ */ jsx("span", { className: "size-3.5" }),
76
+ /* @__PURE__ */ jsx("span", {
77
+ className: "flex-1",
78
+ children: option.label
79
+ }),
80
+ option.value === value && /* @__PURE__ */ jsx(Check, { className: "size-3.5 text-primary-text" })
81
+ ]
82
+ })
83
+ }, option.value))
84
+ })]
85
+ });
86
+ };
87
+ //#endregion
88
+ export { MenuSelect };