@erxes/ui 4.0.0-v2.13 → 4.0.0-v2.14

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.
Files changed (63) hide show
  1. package/CATALOG.md +3 -2
  2. package/README.md +1 -1
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.js +2 -2
  5. package/dist/modules/conditions/ConditionsEditor.d.ts +5 -3
  6. package/dist/modules/conditions/ConditionsEditor.js +139 -177
  7. package/dist/modules/conditions/DateEditors.d.ts +8 -0
  8. package/dist/modules/conditions/DateEditors.js +188 -0
  9. package/dist/modules/conditions/FieldPicker.d.ts +17 -0
  10. package/dist/modules/conditions/FieldPicker.js +99 -0
  11. package/dist/modules/conditions/RuleParts.d.ts +30 -0
  12. package/dist/modules/conditions/RuleParts.js +78 -0
  13. package/dist/modules/conditions/RuleRow.d.ts +27 -0
  14. package/dist/modules/conditions/RuleRow.js +156 -0
  15. package/dist/modules/conditions/TypedInput.d.ts +13 -0
  16. package/dist/modules/conditions/TypedInput.js +36 -0
  17. package/dist/modules/conditions/ValueEditor.d.ts +18 -0
  18. package/dist/modules/conditions/ValueEditor.js +164 -0
  19. package/dist/modules/conditions/conditions.d.ts +14 -9
  20. package/dist/modules/conditions/conditions.js +1 -75
  21. package/dist/modules/conditions/fieldIcons.d.ts +6 -0
  22. package/dist/modules/conditions/fieldIcons.js +19 -0
  23. package/dist/modules/conditions/fieldOptions.d.ts +13 -0
  24. package/dist/modules/conditions/fieldOptions.js +26 -0
  25. package/dist/modules/conditions/filterRule.d.ts +45 -0
  26. package/dist/modules/conditions/filterRule.js +169 -0
  27. package/dist/modules/conditions/filterText.d.ts +16 -0
  28. package/dist/modules/conditions/filterText.js +156 -0
  29. package/dist/modules/filter/components/Filter.d.ts +1 -1
  30. package/dist/modules/filter/components/Filter.js +1 -1
  31. package/dist/modules/list/ListFilter.d.ts +16 -5
  32. package/dist/modules/list/ListFilter.js +50 -17
  33. package/dist/modules/list/ViewBar.d.ts +2 -0
  34. package/dist/modules/list/ViewBar.js +21 -10
  35. package/dist/modules/list/filter/AddFilter.d.ts +10 -0
  36. package/dist/modules/list/filter/AddFilter.js +140 -0
  37. package/dist/modules/list/filter/AdvancedPopover.d.ts +8 -0
  38. package/dist/modules/list/filter/AdvancedPopover.js +103 -0
  39. package/dist/modules/list/filter/ListFilterBar.d.ts +3 -3
  40. package/dist/modules/list/filter/ListFilterBar.js +104 -205
  41. package/dist/modules/list/filter/RuleChips.d.ts +12 -2
  42. package/dist/modules/list/filter/RuleChips.js +132 -134
  43. package/dist/modules/list/filter/SearchChip.d.ts +6 -0
  44. package/dist/modules/list/filter/SearchChip.js +69 -0
  45. package/dist/modules/list/filter/filterRules.d.ts +15 -40
  46. package/dist/modules/list/filter/filterRules.js +27 -156
  47. package/dist/modules/list/filter/listFilterContext.d.ts +25 -18
  48. package/dist/modules/list/filter/listFilterContext.js +8 -24
  49. package/dist/modules/list/filter/listFilterQuery.d.ts +4 -0
  50. package/dist/modules/list/filter/listFilterQuery.js +10 -2
  51. package/dist/theme.css +3 -2
  52. package/exports.txt +1 -0
  53. package/package.json +1 -1
  54. package/dist/modules/conditions/ConditionValueInput.d.ts +0 -10
  55. package/dist/modules/conditions/ConditionValueInput.js +0 -166
  56. package/dist/modules/conditions/PathPicker.d.ts +0 -8
  57. package/dist/modules/conditions/PathPicker.js +0 -56
  58. package/dist/modules/list/filter/AdvancedFilterDialog.d.ts +0 -5
  59. package/dist/modules/list/filter/AdvancedFilterDialog.js +0 -52
  60. package/dist/modules/list/filter/FieldMenus.d.ts +0 -5
  61. package/dist/modules/list/filter/FieldMenus.js +0 -184
  62. package/dist/modules/list/filter/fieldIcons.d.ts +0 -7
  63. package/dist/modules/list/filter/fieldIcons.js +0 -16
@@ -1,217 +1,140 @@
1
1
  import { Button } from "../../../components/button.js";
2
- import { Combobox } from "../../../components/combobox.js";
3
- import { Command } from "../../../components/command.js";
4
2
  import { IconTip } from "../../../components/icon-tip.js";
5
- import { Popover } from "../../../components/popover.js";
6
3
  import { usePageHotkey } from "../../../hooks/use-page-hotkey.js";
7
- import { isRule } from "../../conditions/conditions.js";
8
- import { Filter } from "../../filter/components/Filter.js";
9
- import { useFilterContext } from "../../filter/hooks/useFilterContext.js";
10
- import { AdvancedFilterDialog } from "./AdvancedFilterDialog.js";
11
- import { FieldMenu } from "./FieldMenus.js";
12
- import { FIELD_ICONS } from "./fieldIcons.js";
13
- import { groupOf, inMenu, partsOf, withPart } from "./filterRules.js";
14
- import { ListFilterContext, useListFilter } from "./listFilterContext.js";
4
+ import { ruleCount } from "../../conditions/filterRule.js";
5
+ import { useFilterRowSlot } from "../ViewBar.js";
6
+ import { AddFilter } from "./AddFilter.js";
7
+ import { AdvancedPopover } from "./AdvancedPopover.js";
8
+ import { chipsOf, groupOf, partsOf, withPart } from "./filterRules.js";
9
+ import { ListFilterContext } from "./listFilterContext.js";
15
10
  import { useListConditions } from "./listFilterQuery.js";
16
11
  import { RecordCount } from "./RecordCount.js";
17
12
  import { RuleChips } from "./RuleChips.js";
18
- import { IconAdjustmentsHorizontal, IconFilter2, IconSearch } from "@tabler/icons-react";
13
+ import { SearchChip } from "./SearchChip.js";
14
+ import { IconFilter2, IconPlus } from "@tabler/icons-react";
19
15
  import { jsx, jsxs } from "react/jsx-runtime";
20
- import { useCallback, useMemo, useState } from "react";
16
+ import { useMemo, useRef, useState } from "react";
21
17
  import { useTranslation } from "react-i18next";
22
- import { useQueryState } from "nuqs";
18
+ import { createPortal } from "react-dom";
23
19
  import { useAutoAnimate } from "@formkit/auto-animate/react";
24
20
  // src/modules/list/filter/ListFilterBar.tsx
25
- /** Built-in fields first, the record's properties last; each with its heading's English. */
26
- const GROUPS = [
27
- {
28
- group: "fields",
29
- label: "Fields"
30
- },
31
- {
32
- group: "lead",
33
- label: "Lead"
34
- },
35
- {
36
- group: "company",
37
- label: "Company"
38
- },
39
- {
40
- group: "activity",
41
- label: "Activity"
42
- },
43
- {
44
- group: "custom",
45
- label: "Properties"
46
- }
47
- ];
48
- /** Every field, grouped; each opens its submenu. The search box filters them by name. */
49
- const FieldItems = () => {
50
- const { t } = useTranslation("views", { keyPrefix: "filter" });
51
- const { fields } = useListFilter();
52
- return [...GROUPS.map(({ group, label }) => ({
53
- key: group,
54
- heading: t(`group.${group}`, label),
55
- fields: fields.filter((field) => !field.groupLabel && (field.group ?? "fields") === group)
56
- })), ...[...new Set(fields.flatMap((field) => field.groupLabel ?? []))].map((heading) => ({
57
- key: `label:${heading}`,
58
- heading,
59
- fields: fields.filter((field) => field.groupLabel === heading)
60
- }))].map(({ key, heading, fields: inGroup }) => {
61
- if (!inGroup.length) return null;
62
- return /* @__PURE__ */ jsx(Command.Group, {
63
- heading,
64
- children: inGroup.map((field) => {
65
- const Icon = field.icon ?? FIELD_ICONS[field.type];
66
- return /* @__PURE__ */ jsxs(Filter.Item, {
67
- value: field.path,
68
- keywords: [field.label],
69
- children: [/* @__PURE__ */ jsx(Icon, {}), /* @__PURE__ */ jsx("span", {
70
- className: "truncate",
71
- children: field.label
72
- })]
73
- }, field.path);
74
- })
75
- }, key);
76
- });
77
- };
78
- /** The menu's first page: "Add filter…", search, the advanced filter, then the fields. */
79
- const RootMenu = ({ loading, error }) => {
80
- const { t } = useTranslation("views", { keyPrefix: "filter" });
81
- const { openAdvanced } = useListFilter();
82
- const search = t("search", "Search");
83
- const advanced = t("advanced", "Advanced filter");
84
- return /* @__PURE__ */ jsxs(Command, { children: [/* @__PURE__ */ jsx(Filter.CommandInput, {
85
- placeholder: t("add", "Add filter…"),
86
- variant: "secondary",
87
- focusOnMount: true
88
- }), /* @__PURE__ */ jsxs(Command.List, {
89
- className: "max-h-popup",
90
- children: [
91
- /* @__PURE__ */ jsx(Command.Empty, { children: t("no-match", "No field matches") }),
92
- /* @__PURE__ */ jsxs(Filter.Item, {
93
- value: "searchValue",
94
- keywords: [search],
95
- inDialog: true,
96
- children: [/* @__PURE__ */ jsx(IconSearch, {}), search]
97
- }),
98
- /* @__PURE__ */ jsxs(Filter.CommandItem, {
99
- value: "advanced",
100
- keywords: [advanced],
101
- onSelect: openAdvanced,
102
- children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, {}), advanced]
103
- }),
104
- loading && /* @__PURE__ */ jsx("p", {
105
- className: "px-2 py-1.5 text-xs text-muted-foreground",
106
- children: t("loading", "Loading fields…")
107
- }),
108
- error && /* @__PURE__ */ jsx("p", {
109
- role: "alert",
110
- className: "px-2 py-1.5 text-xs text-destructive",
111
- children: error.message
112
- }),
113
- /* @__PURE__ */ jsx(FieldItems, {})
114
- ]
115
- })] });
116
- };
117
- /** The search, as a chip (it opens the search box). */
118
- const SearchChip = () => {
119
- const { t } = useTranslation("views", { keyPrefix: "filter" });
120
- const [searchValue] = useQueryState("searchValue");
121
- return /* @__PURE__ */ jsxs(Filter.BarItem, {
122
- queryKey: "searchValue",
123
- children: [/* @__PURE__ */ jsxs(Filter.BarName, { children: [/* @__PURE__ */ jsx(IconSearch, {}), t("search", "Search")] }), /* @__PURE__ */ jsx(Filter.BarButton, {
124
- filterKey: "searchValue",
125
- inDialog: true,
126
- children: searchValue
127
- })]
128
- });
129
- };
130
- /** Opens the filter menu; it shows how many conditions apply. */
131
- const FilterButton = ({ count, onClick }) => {
21
+ /** The view bar's filter button: it opens the field picker and shows how many conditions apply. */
22
+ const FilterButton = ({ count }) => {
132
23
  const { t } = useTranslation("views", { keyPrefix: "filter" });
133
24
  const label = t("label", "Filter");
134
25
  return /* @__PURE__ */ jsx(IconTip, {
135
26
  label,
136
27
  hotkey: "F",
137
- children: /* @__PURE__ */ jsxs(Popover.Trigger, {
138
- render: /* @__PURE__ */ jsx(Button, {
28
+ children: /* @__PURE__ */ jsx(AddFilter, {
29
+ from: "bar",
30
+ trigger: /* @__PURE__ */ jsxs(Button, {
139
31
  size: "sm",
140
- "aria-label": label,
32
+ "aria-label": count > 0 ? `${label} ${count}` : label,
141
33
  "aria-keyshortcuts": "F",
142
34
  variant: count > 0 ? "secondary" : "ghost",
143
35
  tone: count > 0 ? "default" : "muted",
144
36
  className: "h-7 min-w-7",
145
- onClick
146
- }),
147
- children: [/* @__PURE__ */ jsx(IconFilter2, { className: "size-4" }), count > 0 && /* @__PURE__ */ jsx("span", {
148
- className: "text-xs tabular-nums",
149
- children: count
150
- })]
37
+ children: [/* @__PURE__ */ jsx(IconFilter2, { className: "size-4" }), count > 0 && /* @__PURE__ */ jsx("span", {
38
+ className: "text-xs tabular-nums",
39
+ children: count
40
+ })]
41
+ })
151
42
  })
152
43
  });
153
44
  };
154
- /** The bar's context: the list's conditions as parts, and the one the menu is editing. */
155
- const useListFilterState = (fields, lookups) => {
45
+ /**
46
+ * Under the view bar while the list is filtered: the conditions as chips that wrap, "+ Filter",
47
+ * Clear, and what the open view changed. The advanced filter opens anchored to it.
48
+ */
49
+ const FilterRow = ({ status }) => {
50
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
51
+ const { clear } = useListConditions();
52
+ const [chipsRef] = useAutoAnimate({ duration: 150 });
53
+ const rowRef = useRef(null);
54
+ return /* @__PURE__ */ jsxs("div", {
55
+ ref: rowRef,
56
+ role: "toolbar",
57
+ "aria-label": t("row", "Filters"),
58
+ className: "flex min-h-10 shrink-0 items-start gap-2 border-b bg-background px-3 py-1.5",
59
+ children: [
60
+ /* @__PURE__ */ jsxs("div", {
61
+ ref: chipsRef,
62
+ className: "flex min-w-0 flex-1 flex-wrap items-center gap-1.5",
63
+ children: [
64
+ /* @__PURE__ */ jsx(SearchChip, {}),
65
+ /* @__PURE__ */ jsx(RuleChips, {}),
66
+ /* @__PURE__ */ jsx(AddFilter, {
67
+ from: "row",
68
+ trigger: /* @__PURE__ */ jsxs(Button, {
69
+ size: "sm",
70
+ variant: "ghost",
71
+ tone: "muted",
72
+ className: "h-7",
73
+ children: [/* @__PURE__ */ jsx(IconPlus, {}), t("label", "Filter")]
74
+ })
75
+ })
76
+ ]
77
+ }),
78
+ /* @__PURE__ */ jsxs("div", {
79
+ className: "flex h-7 shrink-0 items-center gap-1",
80
+ children: [/* @__PURE__ */ jsx(Button, {
81
+ size: "sm",
82
+ variant: "ghost",
83
+ tone: "muted",
84
+ className: "h-7",
85
+ onClick: clear,
86
+ children: t("clear", "Clear")
87
+ }), status]
88
+ }),
89
+ /* @__PURE__ */ jsx(AdvancedPopover, { anchor: rowRef })
90
+ ]
91
+ });
92
+ };
93
+ /** The filter's shared state: the conditions from the address, the open pickers. */
94
+ const useListFilterState = ({ fields, members, tags, totalCount, totalCapped, loading, error }) => {
156
95
  const { advanced, setAdvanced } = useListConditions();
157
- const { setOpen, setView } = useFilterContext();
158
- const [editing, setEditing] = useState(null);
96
+ const [adding, setAdding] = useState(null);
159
97
  const [advancedOpen, setAdvancedOpen] = useState(false);
98
+ const lookups = useMemo(() => ({
99
+ members,
100
+ tags
101
+ }), [members, tags]);
160
102
  const parts = useMemo(() => partsOf(advanced), [advanced]);
161
- const openMenu = useCallback((view, part) => {
162
- setEditing(part);
163
- setView(view);
164
- setOpen(true);
165
- }, [setOpen, setView]);
166
- usePageHotkey("f", () => openMenu("root", null));
167
103
  return {
168
- state: useMemo(() => ({
169
- fields,
170
- lookups,
171
- parts,
172
- editing,
173
- setPart: (index, next) => setAdvanced(groupOf(withPart(parts, index, next))),
174
- edit: (index) => {
175
- const part = parts[index];
176
- const field = part && isRule(part) && fields.find((one) => one.path === part.path);
177
- if (part && isRule(part) && field && inMenu(part, field)) openMenu(field.path, index);
178
- else setAdvancedOpen(true);
179
- },
180
- openAdvanced: () => setAdvancedOpen(true)
181
- }), [
182
- fields,
183
- lookups,
184
- parts,
185
- editing,
186
- setAdvanced,
187
- openMenu
188
- ]),
189
- setEditing,
104
+ fields,
105
+ lookups,
106
+ advanced,
107
+ chips: chipsOf(advanced),
108
+ parts,
109
+ setPart: (index, next) => setAdvanced(groupOf(withPart(parts, index, next))),
110
+ setAdvanced,
111
+ adding,
112
+ setAdding,
190
113
  advancedOpen,
191
- setAdvancedOpen
114
+ setAdvancedOpen,
115
+ totalCount,
116
+ totalCapped,
117
+ loading,
118
+ error
192
119
  };
193
120
  };
194
121
  /**
195
- * Inside `<Filter>`: its chips, the list's status and the count, then the list's controls
196
- * and the menu (`F`).
122
+ * In the view bar: the count, the list's own controls and the filter button (`F`). Under it,
123
+ * while the list is filtered, the filter row: in the view bar's slot, else right after the bar.
197
124
  */
198
- const ListFilterBar = ({ id, fields, loading, error, status, totalCount, totalCapped, children, members, tags }) => {
199
- const [chipsRef] = useAutoAnimate({ duration: 150 });
200
- const { search } = useListConditions();
201
- const lookups = useMemo(() => ({
202
- members,
203
- tags
204
- }), [members, tags]);
205
- const { state, setEditing, advancedOpen, setAdvancedOpen } = useListFilterState(fields, lookups);
125
+ const ListFilterBar = (props) => {
126
+ const { status, totalCount, totalCapped, children } = props;
127
+ const state = useListFilterState(props);
128
+ const { advanced, search, active } = useListConditions();
129
+ const slot = useFilterRowSlot();
130
+ const count = (advanced ? ruleCount(advanced) : 0) + (search ? 1 : 0);
131
+ const rowShown = active || state.advancedOpen;
132
+ usePageHotkey("f", () => state.setAdding(rowShown ? "row" : "bar"));
133
+ const row = rowShown && /* @__PURE__ */ jsx(FilterRow, { status });
206
134
  return /* @__PURE__ */ jsxs(ListFilterContext.Provider, {
207
135
  value: state,
208
136
  children: [
209
- /* @__PURE__ */ jsxs("div", {
210
- ref: chipsRef,
211
- className: "flex min-w-0 items-center gap-1 overflow-x-auto py-1 hide-scroll",
212
- children: [/* @__PURE__ */ jsx(SearchChip, {}), /* @__PURE__ */ jsx(RuleChips, {})]
213
- }),
214
- status,
137
+ !rowShown && status,
215
138
  /* @__PURE__ */ jsx(RecordCount, {
216
139
  totalCount,
217
140
  totalCapped
@@ -221,32 +144,8 @@ const ListFilterBar = ({ id, fields, loading, error, status, totalCount, totalCa
221
144
  className: "mx-1 h-4 w-px shrink-0 bg-border"
222
145
  }),
223
146
  children,
224
- /* @__PURE__ */ jsxs(Filter.Popover, {
225
- scope: `${id}-list`,
226
- children: [/* @__PURE__ */ jsx(FilterButton, {
227
- count: state.parts.length + (search ? 1 : 0),
228
- onClick: () => setEditing(null)
229
- }), /* @__PURE__ */ jsxs(Combobox.Content, {
230
- align: "end",
231
- className: "w-80",
232
- children: [/* @__PURE__ */ jsx(Filter.View, { children: /* @__PURE__ */ jsx(RootMenu, {
233
- loading,
234
- error
235
- }) }), fields.map((field) => /* @__PURE__ */ jsx(Filter.View, {
236
- filterKey: field.path,
237
- children: /* @__PURE__ */ jsx(FieldMenu, { field }, `${state.editing}`)
238
- }, field.path))]
239
- })]
240
- }),
241
- /* @__PURE__ */ jsx(Filter.Dialog, { children: /* @__PURE__ */ jsx(Filter.View, {
242
- filterKey: "searchValue",
243
- inDialog: true,
244
- children: /* @__PURE__ */ jsx(Filter.DialogStringView, { filterKey: "searchValue" })
245
- }) }),
246
- /* @__PURE__ */ jsx(AdvancedFilterDialog, {
247
- open: advancedOpen,
248
- onOpenChange: setAdvancedOpen
249
- })
147
+ /* @__PURE__ */ jsx(FilterButton, { count }),
148
+ row && (slot ? createPortal(row, slot) : row)
250
149
  ]
251
150
  });
252
151
  };
@@ -1,2 +1,12 @@
1
- /** Each part of the list's filter as a chip (parts are distinct, so a part is its own key). */
2
- export declare const RuleChips: () => import("react").JSX.Element[];
1
+ import { ReactNode } from "react";
2
+ /** A chip: joined segments on a muted ground, the last one a remove button. */
3
+ export declare const Chip: ({ children, removeLabel, onRemove }: {
4
+ children: ReactNode;
5
+ removeLabel: string;
6
+ onRemove: () => void;
7
+ }) => import("react").JSX.Element;
8
+ /**
9
+ * The list's conditions as chips, one per rule (keyed by place, so an open editor stays open as
10
+ * its rule changes), or the advanced filter's chip.
11
+ */
12
+ export declare const RuleChips: () => import("react").JSX.Element[] | import("react").JSX.Element;
@@ -1,153 +1,151 @@
1
- import { useFormat } from "../../../lib/format.js";
2
- import { useMoney } from "../../../lib/money.js";
3
- import { oneOf, textOf } from "../../../lib/text.js";
4
- import { OPERATOR_LABELS, RelativeAmount, isRule } from "../../conditions/conditions.js";
5
- import { Filter } from "../../filter/components/Filter.js";
6
- import { FIELD_ICONS } from "./fieldIcons.js";
7
- import { NUMBER_OPS, NUMBER_SYMBOLS, presetOf, valuesOf } from "./filterRules.js";
8
- import { useFieldOptions, useListFilter } from "./listFilterContext.js";
9
- import { IconAdjustmentsHorizontal } from "@tabler/icons-react";
10
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1
+ import { Button } from "../../../components/button.js";
2
+ import { useItemKeys } from "../../../hooks/use-item-keys.js";
3
+ import { NO_VALUE } from "../../conditions/conditions.js";
4
+ import { isFilled, opOf, pathOf, ruleCount, withField, withOperator } from "../../conditions/filterRule.js";
5
+ import { FieldIcon } from "../../conditions/fieldIcons.js";
6
+ import { useRuleText } from "../../conditions/filterText.js";
7
+ import { FieldPart, OperatorPart, ValuePart } from "../../conditions/RuleParts.js";
8
+ import { useFieldAt, useListFilter } from "./listFilterContext.js";
9
+ import { IconAdjustmentsHorizontal, IconX } from "@tabler/icons-react";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { useState } from "react";
11
12
  import { useTranslation } from "react-i18next";
12
13
  // src/modules/list/filter/RuleChips.tsx
13
- /** A filter bar chip: what it filters, the condition (opens it), remove. */
14
- const Chip = ({ name, children, onOpen, onRemove }) => {
15
- return /* @__PURE__ */ jsxs("div", {
16
- className: "flex h-7 items-stretch gap-px rounded bg-muted text-sm font-medium shadow-xs",
17
- children: [
18
- /* @__PURE__ */ jsx(Filter.BarName, { children: name }),
19
- /* @__PURE__ */ jsx(Filter.BarButton, {
20
- onClick: onOpen,
21
- children
22
- }),
23
- /* @__PURE__ */ jsx(Filter.BarClose, { onClick: onRemove })
24
- ]
25
- });
26
- };
27
- /** Unknown paths (a removed property) read as text. */
28
- const textField = (path) => ({
29
- path,
30
- label: path,
31
- type: "text"
14
+ /** A chip: joined segments on a muted ground, the last one a remove button. */
15
+ const Chip = ({ children, removeLabel, onRemove }) => /* @__PURE__ */ jsxs("div", {
16
+ className: "flex h-7 max-w-full min-w-0 items-stretch gap-px overflow-hidden rounded bg-muted text-sm shadow-xs",
17
+ children: [children, /* @__PURE__ */ jsx(Button, {
18
+ variant: "segment",
19
+ size: "icon",
20
+ "aria-label": removeLabel,
21
+ onClick: onRemove,
22
+ children: /* @__PURE__ */ jsx(IconX, {})
23
+ })]
32
24
  });
33
- /** English for the `views` namespace's `filter.unit.<unit>`, by plural form. */
34
- const UNIT_LABELS = {
35
- days: {
36
- defaultValue_one: "{{count}} day",
37
- defaultValue_other: "{{count}} days"
38
- },
39
- weeks: {
40
- defaultValue_one: "{{count}} week",
41
- defaultValue_other: "{{count}} weeks"
42
- },
43
- months: {
44
- defaultValue_one: "{{count}} month",
45
- defaultValue_other: "{{count}} months"
46
- }
47
- };
48
- /** A rule as words: the operator and the value. */
49
- const useRuleText = (rule, field) => {
25
+ /** A segment of a chip that opens its editor. */
26
+ const Segment = ({ className, ...props }) => /* @__PURE__ */ jsx(Button, {
27
+ variant: "segment",
28
+ className: className ?? "max-w-60 min-w-0 gap-1.5 px-2",
29
+ ...props
30
+ });
31
+ /**
32
+ * A condition as a chip: its field, operator and value, each opening its editor in place, and
33
+ * remove. A change that leaves the rule unfinished (an operator that needs another value) waits
34
+ * on the chip until the value is picked; closing it unfinished keeps the rule as it was.
35
+ */
36
+ const RuleChip = ({ rule, index }) => {
50
37
  const { t } = useTranslation("views", { keyPrefix: "filter" });
51
- const { t: opT } = useTranslation("automations");
52
- const { options } = useFieldOptions(field);
53
- const money = useMoney();
54
- const fmt = useFormat();
55
- const op = opT(`op.${rule.op}`, OPERATOR_LABELS[rule.op]);
56
- const labelOf = (value) => options.find((option) => option.value === value)?.label ?? value;
57
- const preset = field.type === "date" ? presetOf(rule) : void 0;
58
- const span = RelativeAmount.safeParse(rule.value);
59
- const numberOp = field.type === "number" ? oneOf(NUMBER_OPS, rule.op) : void 0;
60
- const is = t("is", "is");
61
- if (preset) return {
62
- op: "",
63
- value: t(`date.${preset.key}`, preset.label)
64
- };
65
- if (rule.op === "exists" || rule.op === "not_exists") return {
66
- op,
67
- value: ""
68
- };
69
- if (span.success) {
70
- const { amount, unit } = span.data;
71
- return {
72
- op,
73
- value: t(`unit.${unit}`, {
74
- count: amount,
75
- ...UNIT_LABELS[unit]
76
- })
77
- };
78
- }
79
- if (field.type === "boolean") return {
80
- op: is,
81
- value: textOf(rule.value) === "true" ? t("yes", "Yes") : t("no", "No")
38
+ const { fields, lookups, setPart } = useListFilter();
39
+ const [draft, setDraft] = useState(null);
40
+ const [open, setOpen] = useState(null);
41
+ const shown = draft ?? rule;
42
+ const field = useFieldAt(pathOf(shown));
43
+ const text = useRuleText(shown, field, lookups);
44
+ /** A finished rule applies; an unfinished one waits here, with its value open. */
45
+ const change = (next) => {
46
+ if (isFilled(next)) {
47
+ setDraft(null);
48
+ setPart(index, next);
49
+ } else {
50
+ setDraft(next);
51
+ setOpen("value");
52
+ }
82
53
  };
83
- if (numberOp) {
84
- const n = Number(rule.value);
85
- return {
86
- op: NUMBER_SYMBOLS[numberOp],
87
- value: field.currency ? money(n, field.currency) : fmt.number(n)
88
- };
89
- }
90
- if (rule.op === "contains" && field.type === "text") return {
91
- op,
92
- value: `“${textOf(rule.value)}”`
54
+ const toggle = (part) => (next) => {
55
+ setOpen(next ? part : null);
56
+ if (!next) setDraft(null);
93
57
  };
94
- const values = valuesOf(rule).map(labelOf);
95
- if (rule.op === "in" || rule.op === "eq") return {
96
- op: values.length > 1 ? t("is-any-of", "is any of") : is,
97
- value: values.join(", ")
58
+ const pick = (next) => {
59
+ setOpen(null);
60
+ change(next);
98
61
  };
99
- return {
100
- op,
101
- value: values.join(", ")
102
- };
103
- };
104
- const RuleChip = ({ rule, index }) => {
105
- const { fields, edit, setPart } = useListFilter();
106
- const field = fields.find((one) => one.path === rule.path) ?? textField(rule.path);
107
- const text = useRuleText(rule, field);
108
- const Icon = field.icon ?? FIELD_ICONS[field.type];
109
62
  return /* @__PURE__ */ jsxs(Chip, {
110
- name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Icon, {}), field.label] }),
111
- onOpen: () => edit(index),
63
+ removeLabel: t("remove-named", {
64
+ name: field.label,
65
+ defaultValue: "Remove {{name}} filter"
66
+ }),
112
67
  onRemove: () => setPart(index, null),
113
- children: [text.op && /* @__PURE__ */ jsx("span", {
114
- className: "font-normal text-muted-foreground",
115
- children: text.op
116
- }), /* @__PURE__ */ jsx("span", {
117
- className: "truncate",
118
- children: text.value
119
- })]
68
+ children: [
69
+ /* @__PURE__ */ jsx(FieldPart, {
70
+ fields,
71
+ field,
72
+ open: open === "field",
73
+ onOpenChange: toggle("field"),
74
+ onSelect: (next) => pick(withField(shown, field, next)),
75
+ button: /* @__PURE__ */ jsxs(Segment, {
76
+ className: "max-w-48 min-w-0 gap-1.5 rounded-l px-2 font-medium",
77
+ children: [/* @__PURE__ */ jsx(FieldIcon, {
78
+ field,
79
+ className: "text-muted-foreground"
80
+ }), /* @__PURE__ */ jsx("span", {
81
+ className: "truncate",
82
+ children: field.label
83
+ })]
84
+ })
85
+ }),
86
+ /* @__PURE__ */ jsx(OperatorPart, {
87
+ rule: shown,
88
+ field,
89
+ open: open === "op",
90
+ onOpenChange: toggle("op"),
91
+ onSelect: (op) => pick(withOperator(shown, op)),
92
+ button: /* @__PURE__ */ jsx(Segment, {
93
+ className: "shrink-0 px-2 text-muted-foreground",
94
+ children: text.op
95
+ })
96
+ }),
97
+ !NO_VALUE.has(opOf(shown)) && /* @__PURE__ */ jsx(ValuePart, {
98
+ rule: shown,
99
+ field,
100
+ lookups,
101
+ onChange: change,
102
+ open: open === "value",
103
+ onOpenChange: toggle("value"),
104
+ button: /* @__PURE__ */ jsx(Segment, {
105
+ "aria-label": t("value-of", {
106
+ name: field.label,
107
+ value: text.value,
108
+ defaultValue: "{{name}}: {{value}}"
109
+ }),
110
+ children: /* @__PURE__ */ jsx("span", {
111
+ className: "truncate font-medium",
112
+ children: text.value
113
+ })
114
+ })
115
+ })
116
+ ]
120
117
  });
121
118
  };
122
- /** A group from the advanced filter: how many conditions; it opens the advanced filter. */
123
- const GroupChip = ({ group, index }) => {
119
+ /** A filter with "or" or nested groups: one chip, which opens the advanced filter. */
120
+ const AdvancedChip = () => {
124
121
  const { t } = useTranslation("views", { keyPrefix: "filter" });
125
- const { openAdvanced, setPart } = useListFilter();
126
- const count = group.rules.length;
122
+ const { advanced, setAdvanced, setAdvancedOpen } = useListFilter();
123
+ const count = advanced ? ruleCount(advanced) : 0;
127
124
  return /* @__PURE__ */ jsx(Chip, {
128
- name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, {}), t("advanced", "Advanced filter")] }),
129
- onOpen: openAdvanced,
130
- onRemove: () => setPart(index, null),
131
- children: group.match === "any" ? t("any-of", {
132
- count,
133
- defaultValue_one: "any of {{count}} condition",
134
- defaultValue_other: "any of {{count}} conditions"
135
- }) : t("conditions", {
136
- count,
137
- defaultValue_one: "{{count}} condition",
138
- defaultValue_other: "{{count}} conditions"
125
+ removeLabel: t("remove-advanced", "Remove the advanced filter"),
126
+ onRemove: () => setAdvanced(null),
127
+ children: /* @__PURE__ */ jsxs(Segment, {
128
+ className: "min-w-0 gap-1.5 rounded-l px-2 font-medium",
129
+ onClick: () => setAdvancedOpen(true),
130
+ children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, { className: "text-muted-foreground" }), t("advanced-chip", {
131
+ count,
132
+ defaultValue_one: "Advanced · {{count}} rule",
133
+ defaultValue_other: "Advanced · {{count}} rules"
134
+ })]
139
135
  })
140
136
  });
141
137
  };
142
- /** Each part of the list's filter as a chip (parts are distinct, so a part is its own key). */
138
+ /**
139
+ * The list's conditions as chips, one per rule (keyed by place, so an open editor stays open as
140
+ * its rule changes), or the advanced filter's chip.
141
+ */
143
142
  const RuleChips = () => {
144
- const { parts } = useListFilter();
145
- return parts.map((part, index) => isRule(part) ? /* @__PURE__ */ jsx(RuleChip, {
146
- rule: part,
147
- index
148
- }, JSON.stringify(part)) : /* @__PURE__ */ jsx(GroupChip, {
149
- group: part,
143
+ const { chips } = useListFilter();
144
+ const keys = useItemKeys(chips?.length ?? 0);
145
+ if (!chips) return /* @__PURE__ */ jsx(AdvancedChip, {});
146
+ return chips.map((rule, index) => /* @__PURE__ */ jsx(RuleChip, {
147
+ rule,
150
148
  index
151
- }, JSON.stringify(part)));
149
+ }, keys.keyAt(index)));
152
150
  };
153
- export { RuleChips };
151
+ export { Chip, RuleChips };