@erxes/ui 4.0.0-v2.8 → 4.0.0-v2.9

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 (45) hide show
  1. package/README.md +8 -3
  2. package/dist/hooks/use-item-keys.d.ts +10 -0
  3. package/dist/hooks/use-item-keys.js +23 -0
  4. package/dist/index.d.ts +9 -2
  5. package/dist/index.js +9 -2
  6. package/dist/lib/format.d.ts +2 -0
  7. package/dist/lib/format.js +6 -1
  8. package/dist/lib/money.d.ts +10 -0
  9. package/dist/lib/money.js +18 -0
  10. package/dist/lib/text.d.ts +7 -0
  11. package/dist/lib/text.js +14 -0
  12. package/dist/modules/conditions/ConditionValueInput.d.ts +10 -0
  13. package/dist/modules/conditions/ConditionValueInput.js +166 -0
  14. package/dist/modules/conditions/ConditionsEditor.d.ts +11 -0
  15. package/dist/modules/conditions/ConditionsEditor.js +209 -0
  16. package/dist/modules/conditions/LazyConditionsEditor.d.ts +9 -0
  17. package/dist/modules/conditions/LazyConditionsEditor.js +10 -0
  18. package/dist/modules/conditions/PathPicker.d.ts +8 -0
  19. package/dist/modules/conditions/PathPicker.js +51 -0
  20. package/dist/modules/conditions/conditions.d.ts +64 -0
  21. package/dist/modules/conditions/conditions.js +135 -0
  22. package/dist/modules/list/ListFilter.d.ts +25 -11
  23. package/dist/modules/list/ListFilter.js +30 -26
  24. package/dist/modules/list/filter/AdvancedFilterDialog.d.ts +5 -0
  25. package/dist/modules/list/filter/AdvancedFilterDialog.js +52 -0
  26. package/dist/modules/list/filter/FieldMenus.d.ts +5 -0
  27. package/dist/modules/list/filter/FieldMenus.js +184 -0
  28. package/dist/modules/list/filter/ListFilterBar.d.ts +6 -0
  29. package/dist/modules/list/filter/ListFilterBar.js +246 -0
  30. package/dist/modules/list/filter/RecordCount.d.ts +7 -0
  31. package/dist/modules/list/filter/RecordCount.js +22 -0
  32. package/dist/modules/list/filter/RuleChips.d.ts +2 -0
  33. package/dist/modules/list/filter/RuleChips.js +157 -0
  34. package/dist/modules/list/filter/fieldIcons.d.ts +7 -0
  35. package/dist/modules/list/filter/fieldIcons.js +16 -0
  36. package/dist/modules/list/filter/filterRules.d.ts +46 -0
  37. package/dist/modules/list/filter/filterRules.js +166 -0
  38. package/dist/modules/list/filter/listFilterContext.d.ts +29 -0
  39. package/dist/modules/list/filter/listFilterContext.js +34 -0
  40. package/dist/modules/list/filter/listFilterQuery.d.ts +15 -0
  41. package/dist/modules/list/filter/listFilterQuery.js +21 -0
  42. package/dist/modules/pickers/LookupPicker.d.ts +50 -0
  43. package/dist/modules/pickers/LookupPicker.js +83 -0
  44. package/dist/theme.css +2 -0
  45. package/package.json +2 -1
@@ -0,0 +1,246 @@
1
+ import { Button } from "../../../components/button.js";
2
+ import { Combobox } from "../../../components/combobox.js";
3
+ import { Command } from "../../../components/command.js";
4
+ import { IconTip } from "../../../components/icon-tip.js";
5
+ import { Popover } from "../../../components/popover.js";
6
+ 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";
15
+ import { useListConditions } from "./listFilterQuery.js";
16
+ import { RecordCount } from "./RecordCount.js";
17
+ import { RuleChips } from "./RuleChips.js";
18
+ import { IconAdjustmentsHorizontal, IconFilter2, IconSearch } from "@tabler/icons-react";
19
+ import { jsx, jsxs } from "react/jsx-runtime";
20
+ import { useCallback, useMemo, useState } from "react";
21
+ import { useTranslation } from "react-i18next";
22
+ import { useQueryState } from "nuqs";
23
+ import { useAutoAnimate } from "@formkit/auto-animate/react";
24
+ // 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
+ const inGroup = fields.filter((field) => (field.group ?? "fields") === group);
54
+ if (!inGroup.length) return null;
55
+ return /* @__PURE__ */ jsx(Command.Group, {
56
+ heading: t(`group.${group}`, label),
57
+ children: inGroup.map((field) => {
58
+ const Icon = field.icon ?? FIELD_ICONS[field.type];
59
+ return /* @__PURE__ */ jsxs(Filter.Item, {
60
+ value: field.path,
61
+ keywords: [field.label],
62
+ children: [/* @__PURE__ */ jsx(Icon, {}), /* @__PURE__ */ jsx("span", {
63
+ className: "truncate",
64
+ children: field.label
65
+ })]
66
+ }, field.path);
67
+ })
68
+ }, group);
69
+ });
70
+ };
71
+ /** The menu's first page: "Add filter…", search, the advanced filter, then the fields. */
72
+ const RootMenu = ({ loading, error }) => {
73
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
74
+ const { openAdvanced } = useListFilter();
75
+ const search = t("search", "Search");
76
+ const advanced = t("advanced", "Advanced filter");
77
+ return /* @__PURE__ */ jsxs(Command, { children: [/* @__PURE__ */ jsx(Filter.CommandInput, {
78
+ placeholder: t("add", "Add filter…"),
79
+ variant: "secondary",
80
+ focusOnMount: true
81
+ }), /* @__PURE__ */ jsxs(Command.List, {
82
+ className: "max-h-popup",
83
+ children: [
84
+ /* @__PURE__ */ jsx(Command.Empty, { children: t("no-match", "No field matches") }),
85
+ /* @__PURE__ */ jsxs(Filter.Item, {
86
+ value: "searchValue",
87
+ keywords: [search],
88
+ inDialog: true,
89
+ children: [/* @__PURE__ */ jsx(IconSearch, {}), search]
90
+ }),
91
+ /* @__PURE__ */ jsxs(Filter.CommandItem, {
92
+ value: "advanced",
93
+ keywords: [advanced],
94
+ onSelect: openAdvanced,
95
+ children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, {}), advanced]
96
+ }),
97
+ loading && /* @__PURE__ */ jsx("p", {
98
+ className: "px-2 py-1.5 text-xs text-muted-foreground",
99
+ children: t("loading", "Loading fields…")
100
+ }),
101
+ error && /* @__PURE__ */ jsx("p", {
102
+ role: "alert",
103
+ className: "px-2 py-1.5 text-xs text-destructive",
104
+ children: error.message
105
+ }),
106
+ /* @__PURE__ */ jsx(FieldItems, {})
107
+ ]
108
+ })] });
109
+ };
110
+ /** The search, as a chip (it opens the search box). */
111
+ const SearchChip = () => {
112
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
113
+ const [searchValue] = useQueryState("searchValue");
114
+ return /* @__PURE__ */ jsxs(Filter.BarItem, {
115
+ queryKey: "searchValue",
116
+ children: [/* @__PURE__ */ jsxs(Filter.BarName, { children: [/* @__PURE__ */ jsx(IconSearch, {}), t("search", "Search")] }), /* @__PURE__ */ jsx(Filter.BarButton, {
117
+ filterKey: "searchValue",
118
+ inDialog: true,
119
+ children: searchValue
120
+ })]
121
+ });
122
+ };
123
+ /** Opens the filter menu; it shows how many conditions apply. */
124
+ const FilterButton = ({ count, onClick }) => {
125
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
126
+ const label = t("label", "Filter");
127
+ return /* @__PURE__ */ jsx(IconTip, {
128
+ label,
129
+ hotkey: "F",
130
+ children: /* @__PURE__ */ jsxs(Popover.Trigger, {
131
+ render: /* @__PURE__ */ jsx(Button, {
132
+ size: "sm",
133
+ "aria-label": label,
134
+ "aria-keyshortcuts": "F",
135
+ variant: count > 0 ? "secondary" : "ghost",
136
+ tone: count > 0 ? "default" : "muted",
137
+ className: "h-7 min-w-7",
138
+ onClick
139
+ }),
140
+ children: [/* @__PURE__ */ jsx(IconFilter2, { className: "size-4" }), count > 0 && /* @__PURE__ */ jsx("span", {
141
+ className: "text-xs tabular-nums",
142
+ children: count
143
+ })]
144
+ })
145
+ });
146
+ };
147
+ /** The bar's context: the list's conditions as parts, and the one the menu is editing. */
148
+ const useListFilterState = (fields, lookups) => {
149
+ const { advanced, setAdvanced } = useListConditions();
150
+ const { setOpen, setView } = useFilterContext();
151
+ const [editing, setEditing] = useState(null);
152
+ const [advancedOpen, setAdvancedOpen] = useState(false);
153
+ const parts = useMemo(() => partsOf(advanced), [advanced]);
154
+ const openMenu = useCallback((view, part) => {
155
+ setEditing(part);
156
+ setView(view);
157
+ setOpen(true);
158
+ }, [setOpen, setView]);
159
+ usePageHotkey("f", () => openMenu("root", null));
160
+ return {
161
+ state: useMemo(() => ({
162
+ fields,
163
+ lookups,
164
+ parts,
165
+ editing,
166
+ setPart: (index, next) => setAdvanced(groupOf(withPart(parts, index, next))),
167
+ edit: (index) => {
168
+ const part = parts[index];
169
+ const field = part && isRule(part) && fields.find((one) => one.path === part.path);
170
+ if (part && isRule(part) && field && inMenu(part, field)) openMenu(field.path, index);
171
+ else setAdvancedOpen(true);
172
+ },
173
+ openAdvanced: () => setAdvancedOpen(true)
174
+ }), [
175
+ fields,
176
+ lookups,
177
+ parts,
178
+ editing,
179
+ setAdvanced,
180
+ openMenu
181
+ ]),
182
+ setEditing,
183
+ advancedOpen,
184
+ setAdvancedOpen
185
+ };
186
+ };
187
+ /**
188
+ * Inside `<Filter>`: its chips, the list's status and the count, then the list's controls
189
+ * and the menu (`F`).
190
+ */
191
+ const ListFilterBar = ({ id, fields, loading, error, status, totalCount, totalCapped, children, members, tags }) => {
192
+ const [chipsRef] = useAutoAnimate({ duration: 150 });
193
+ const { search } = useListConditions();
194
+ const lookups = useMemo(() => ({
195
+ members,
196
+ tags
197
+ }), [members, tags]);
198
+ const { state, setEditing, advancedOpen, setAdvancedOpen } = useListFilterState(fields, lookups);
199
+ return /* @__PURE__ */ jsxs(ListFilterContext.Provider, {
200
+ value: state,
201
+ children: [
202
+ /* @__PURE__ */ jsxs("div", {
203
+ ref: chipsRef,
204
+ className: "flex min-w-0 items-center gap-1 overflow-x-auto py-1 hide-scroll",
205
+ children: [/* @__PURE__ */ jsx(SearchChip, {}), /* @__PURE__ */ jsx(RuleChips, {})]
206
+ }),
207
+ status,
208
+ /* @__PURE__ */ jsx(RecordCount, {
209
+ totalCount,
210
+ totalCapped
211
+ }),
212
+ /* @__PURE__ */ jsx("div", {
213
+ "aria-hidden": true,
214
+ className: "mx-1 h-4 w-px shrink-0 bg-border"
215
+ }),
216
+ children,
217
+ /* @__PURE__ */ jsxs(Filter.Popover, {
218
+ scope: `${id}-list`,
219
+ children: [/* @__PURE__ */ jsx(FilterButton, {
220
+ count: state.parts.length + (search ? 1 : 0),
221
+ onClick: () => setEditing(null)
222
+ }), /* @__PURE__ */ jsxs(Combobox.Content, {
223
+ align: "end",
224
+ className: "w-80",
225
+ children: [/* @__PURE__ */ jsx(Filter.View, { children: /* @__PURE__ */ jsx(RootMenu, {
226
+ loading,
227
+ error
228
+ }) }), fields.map((field) => /* @__PURE__ */ jsx(Filter.View, {
229
+ filterKey: field.path,
230
+ children: /* @__PURE__ */ jsx(FieldMenu, { field }, `${state.editing}`)
231
+ }, field.path))]
232
+ })]
233
+ }),
234
+ /* @__PURE__ */ jsx(Filter.Dialog, { children: /* @__PURE__ */ jsx(Filter.View, {
235
+ filterKey: "searchValue",
236
+ inDialog: true,
237
+ children: /* @__PURE__ */ jsx(Filter.DialogStringView, { filterKey: "searchValue" })
238
+ }) }),
239
+ /* @__PURE__ */ jsx(AdvancedFilterDialog, {
240
+ open: advancedOpen,
241
+ onOpenChange: setAdvancedOpen
242
+ })
243
+ ]
244
+ });
245
+ };
246
+ export { ListFilterBar };
@@ -0,0 +1,7 @@
1
+ /** How many records a list matches, as its list hook gives them. */
2
+ export interface Total {
3
+ totalCount?: number | null;
4
+ totalCapped?: boolean;
5
+ }
6
+ /** The list's count, quietly, after its filter; hidden on narrow screens. */
7
+ export declare const RecordCount: ({ totalCount, totalCapped }: Total) => import("react").JSX.Element;
@@ -0,0 +1,22 @@
1
+ import { Skeleton } from "../../../components/skeleton.js";
2
+ import { useFormat } from "../../../lib/format.js";
3
+ import { isUndefinedOrNull } from "../../../utils/IsUndefinedOrNull.js";
4
+ import { jsx } from "react/jsx-runtime";
5
+ import { useTranslation } from "react-i18next";
6
+ // src/modules/list/filter/RecordCount.tsx
7
+ /** The list's count, quietly, after its filter; hidden on narrow screens. */
8
+ const RecordCount = ({ totalCount, totalCapped }) => {
9
+ const { t } = useTranslation("views");
10
+ const fmt = useFormat();
11
+ if (isUndefinedOrNull(totalCount)) return /* @__PURE__ */ jsx(Skeleton, { className: "hidden h-3.5 w-14 md:block" });
12
+ return /* @__PURE__ */ jsx("span", {
13
+ className: "hidden shrink-0 whitespace-nowrap px-1 text-xs tabular-nums text-muted-foreground md:inline",
14
+ children: t("count", {
15
+ count: totalCapped ? 2 : totalCount,
16
+ formatted: fmt.count(totalCount, totalCapped),
17
+ defaultValue_one: "{{formatted}} record",
18
+ defaultValue_other: "{{formatted}} records"
19
+ })
20
+ });
21
+ };
22
+ export { RecordCount };
@@ -0,0 +1,2 @@
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[];
@@ -0,0 +1,157 @@
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";
11
+ import { useTranslation } from "react-i18next";
12
+ // 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
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
16
+ return /* @__PURE__ */ jsxs("div", {
17
+ className: "flex h-7 items-stretch gap-px rounded bg-muted text-sm font-medium shadow-xs",
18
+ children: [
19
+ /* @__PURE__ */ jsx(Filter.BarName, { children: name }),
20
+ /* @__PURE__ */ jsx(Filter.BarButton, {
21
+ onClick: onOpen,
22
+ children
23
+ }),
24
+ /* @__PURE__ */ jsx(Filter.BarClose, {
25
+ "aria-label": t("remove", "Remove filter"),
26
+ onClick: onRemove
27
+ })
28
+ ]
29
+ });
30
+ };
31
+ /** Unknown paths (a removed property) read as text. */
32
+ const textField = (path) => ({
33
+ path,
34
+ label: path,
35
+ type: "text"
36
+ });
37
+ /** English for the `views` namespace's `filter.unit.<unit>`, by plural form. */
38
+ const UNIT_LABELS = {
39
+ days: {
40
+ defaultValue_one: "{{count}} day",
41
+ defaultValue_other: "{{count}} days"
42
+ },
43
+ weeks: {
44
+ defaultValue_one: "{{count}} week",
45
+ defaultValue_other: "{{count}} weeks"
46
+ },
47
+ months: {
48
+ defaultValue_one: "{{count}} month",
49
+ defaultValue_other: "{{count}} months"
50
+ }
51
+ };
52
+ /** A rule as words: the operator and the value. */
53
+ const useRuleText = (rule, field) => {
54
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
55
+ const { t: opT } = useTranslation("automations");
56
+ const { options } = useFieldOptions(field);
57
+ const money = useMoney();
58
+ const fmt = useFormat();
59
+ const op = opT(`op.${rule.op}`, OPERATOR_LABELS[rule.op]);
60
+ const labelOf = (value) => options.find((option) => option.value === value)?.label ?? value;
61
+ const preset = field.type === "date" ? presetOf(rule) : void 0;
62
+ const span = RelativeAmount.safeParse(rule.value);
63
+ const numberOp = field.type === "number" ? oneOf(NUMBER_OPS, rule.op) : void 0;
64
+ const is = t("is", "is");
65
+ if (preset) return {
66
+ op: "",
67
+ value: t(`date.${preset.key}`, preset.label)
68
+ };
69
+ if (rule.op === "exists" || rule.op === "not_exists") return {
70
+ op,
71
+ value: ""
72
+ };
73
+ if (span.success) {
74
+ const { amount, unit } = span.data;
75
+ return {
76
+ op,
77
+ value: t(`unit.${unit}`, {
78
+ count: amount,
79
+ ...UNIT_LABELS[unit]
80
+ })
81
+ };
82
+ }
83
+ if (field.type === "boolean") return {
84
+ op: is,
85
+ value: textOf(rule.value) === "true" ? t("yes", "Yes") : t("no", "No")
86
+ };
87
+ if (numberOp) {
88
+ const n = Number(rule.value);
89
+ return {
90
+ op: NUMBER_SYMBOLS[numberOp],
91
+ value: field.currency ? money(n, field.currency) : fmt.number(n)
92
+ };
93
+ }
94
+ if (rule.op === "contains" && field.type === "text") return {
95
+ op,
96
+ value: `“${textOf(rule.value)}”`
97
+ };
98
+ const values = valuesOf(rule).map(labelOf);
99
+ if (rule.op === "in" || rule.op === "eq") return {
100
+ op: values.length > 1 ? t("is-any-of", "is any of") : is,
101
+ value: values.join(", ")
102
+ };
103
+ return {
104
+ op,
105
+ value: values.join(", ")
106
+ };
107
+ };
108
+ const RuleChip = ({ rule, index }) => {
109
+ const { fields, edit, setPart } = useListFilter();
110
+ const field = fields.find((one) => one.path === rule.path) ?? textField(rule.path);
111
+ const text = useRuleText(rule, field);
112
+ const Icon = field.icon ?? FIELD_ICONS[field.type];
113
+ return /* @__PURE__ */ jsxs(Chip, {
114
+ name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Icon, {}), field.label] }),
115
+ onOpen: () => edit(index),
116
+ onRemove: () => setPart(index, null),
117
+ children: [text.op && /* @__PURE__ */ jsx("span", {
118
+ className: "font-normal text-muted-foreground",
119
+ children: text.op
120
+ }), /* @__PURE__ */ jsx("span", {
121
+ className: "truncate",
122
+ children: text.value
123
+ })]
124
+ });
125
+ };
126
+ /** A group from the advanced filter: how many conditions; it opens the advanced filter. */
127
+ const GroupChip = ({ group, index }) => {
128
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
129
+ const { openAdvanced, setPart } = useListFilter();
130
+ const count = group.rules.length;
131
+ return /* @__PURE__ */ jsx(Chip, {
132
+ name: /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(IconAdjustmentsHorizontal, {}), t("advanced", "Advanced filter")] }),
133
+ onOpen: openAdvanced,
134
+ onRemove: () => setPart(index, null),
135
+ children: group.match === "any" ? t("any-of", {
136
+ count,
137
+ defaultValue_one: "any of {{count}} condition",
138
+ defaultValue_other: "any of {{count}} conditions"
139
+ }) : t("conditions", {
140
+ count,
141
+ defaultValue_one: "{{count}} condition",
142
+ defaultValue_other: "{{count}} conditions"
143
+ })
144
+ });
145
+ };
146
+ /** Each part of the list's filter as a chip (parts are distinct, so a part is its own key). */
147
+ const RuleChips = () => {
148
+ const { parts } = useListFilter();
149
+ return parts.map((part, index) => isRule(part) ? /* @__PURE__ */ jsx(RuleChip, {
150
+ rule: part,
151
+ index
152
+ }, JSON.stringify(part)) : /* @__PURE__ */ jsx(GroupChip, {
153
+ group: part,
154
+ index
155
+ }, JSON.stringify(part)));
156
+ };
157
+ export { RuleChips };
@@ -0,0 +1,7 @@
1
+ import { TListFilterField } from "../ListFilter.js";
2
+ import { IconHash } from "@tabler/icons-react";
3
+ /**
4
+ * A field's icon in the filter menu and on its chips, by type, unless it brings its own:
5
+ * `field.icon ?? FIELD_ICONS[field.type]`.
6
+ */
7
+ export declare const FIELD_ICONS: Record<TListFilterField['type'], typeof IconHash>;
@@ -0,0 +1,16 @@
1
+ import { IconCalendar, IconHash, IconLetterCase, IconSelector, IconTags, IconToggleLeft, IconUser } from "@tabler/icons-react";
2
+ // src/modules/list/filter/fieldIcons.ts
3
+ /**
4
+ * A field's icon in the filter menu and on its chips, by type, unless it brings its own:
5
+ * `field.icon ?? FIELD_ICONS[field.type]`.
6
+ */
7
+ const FIELD_ICONS = {
8
+ text: IconLetterCase,
9
+ number: IconHash,
10
+ date: IconCalendar,
11
+ boolean: IconToggleLeft,
12
+ select: IconSelector,
13
+ member: IconUser,
14
+ tags: IconTags
15
+ };
16
+ export { FIELD_ICONS };
@@ -0,0 +1,46 @@
1
+ import { RelativeAmount, TConditionGroup, TConditionRule } from "../../conditions/conditions.js";
2
+ import { TListFilterField } from "../ListFilter.js";
3
+ /** One thing the list's filter requires: a rule, or a group of rules (from the advanced filter). */
4
+ export type FilterPart = TConditionRule | TConditionGroup;
5
+ type Operator = TConditionRule['op'];
6
+ /** The list's filter as parts that must all hold; an "any" group of several rules is one part. */
7
+ export declare const partsOf: (group: TConditionGroup | null) => FilterPart[];
8
+ /** Parts back to the list's filter: null when there are none. */
9
+ export declare const groupOf: (parts: FilterPart[]) => TConditionGroup | null;
10
+ /**
11
+ * Replaces the part at `index`, appends when `index` is null, removes when
12
+ * `next` is null. Parts stay distinct: a part already there isn't added twice.
13
+ */
14
+ export declare const withPart: (parts: FilterPart[], index: number | null, next: FilterPart | null) => FilterPart[];
15
+ /** Comparisons of a number, as the menu writes them. */
16
+ export declare const NUMBER_OPS: readonly ['eq', 'neq', 'gt', 'gte', 'lt', 'lte'];
17
+ export declare const NUMBER_SYMBOLS: Record<(typeof NUMBER_OPS)[number], string>;
18
+ /** Whether the menu can show and edit this rule. */
19
+ export declare const inMenu: (rule: TConditionRule, field: TListFilterField | undefined) => boolean;
20
+ /** Which part the menu of `field` changes: the one being edited, else its existing single rule, else a new one. */
21
+ export declare const targetOf: (parts: FilterPart[], field: TListFilterField, editing: number | null) => number | null;
22
+ /**
23
+ * The values a rule picks: the list of "is any of", or the one of "is". A value that isn't text or
24
+ * numbers (a date range, a relative amount) picks nothing.
25
+ */
26
+ export declare const valuesOf: (rule: TConditionRule | undefined) => string[];
27
+ /** An "is any of" rule; null when nothing is picked. */
28
+ export declare const anyOf: (path: string, values: string[]) => TConditionRule | null;
29
+ /**
30
+ * Date filters in one pick, on the relative operators (they stay current in a saved view);
31
+ * `label` is the English for the `views` namespace's `filter.date.<key>`.
32
+ */
33
+ export declare const DATE_PRESETS: {
34
+ key: string;
35
+ label: string;
36
+ op: Operator;
37
+ value?: RelativeAmount;
38
+ }[];
39
+ /** The preset a rule is, if any. */
40
+ export declare const presetOf: (rule: TConditionRule | undefined) => {
41
+ key: string;
42
+ label: string;
43
+ op: Operator;
44
+ value?: RelativeAmount;
45
+ } | undefined;
46
+ export {};
@@ -0,0 +1,166 @@
1
+ import { isRule } from "../../conditions/conditions.js";
2
+ import { z } from "zod";
3
+ // src/modules/list/filter/filterRules.ts
4
+ /** The list's filter as parts that must all hold; an "any" group of several rules is one part. */
5
+ const partsOf = (group) => !group ? [] : group.match === "all" || group.rules.length < 2 ? group.rules : [group];
6
+ /** Parts back to the list's filter: null when there are none. */
7
+ const groupOf = (parts) => parts.length ? {
8
+ match: "all",
9
+ rules: parts
10
+ } : null;
11
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
12
+ /**
13
+ * Replaces the part at `index`, appends when `index` is null, removes when
14
+ * `next` is null. Parts stay distinct: a part already there isn't added twice.
15
+ */
16
+ const withPart = (parts, index, next) => {
17
+ if (next && parts.some((part, at) => at !== index && same(part, next))) return index === null ? parts : parts.filter((_, at) => at !== index);
18
+ if (index === null) return next ? [...parts, next] : parts;
19
+ return next ? parts.map((part, at) => at === index ? next : part) : parts.filter((_, at) => at !== index);
20
+ };
21
+ /** Comparisons of a number, as the menu writes them. */
22
+ const NUMBER_OPS = [
23
+ "eq",
24
+ "neq",
25
+ "gt",
26
+ "gte",
27
+ "lt",
28
+ "lte"
29
+ ];
30
+ const NUMBER_SYMBOLS = {
31
+ eq: "=",
32
+ neq: "≠",
33
+ gt: ">",
34
+ gte: "≥",
35
+ lt: "<",
36
+ lte: "≤"
37
+ };
38
+ /** The operators the filter menu shows and edits for each kind of field; other rules open the advanced filter. */
39
+ const MENU_OPS = {
40
+ select: ["in", "eq"],
41
+ member: ["in", "eq"],
42
+ tags: ["in", "eq"],
43
+ boolean: ["eq"],
44
+ date: [
45
+ "within_last",
46
+ "more_than_ago",
47
+ "within_next",
48
+ "exists",
49
+ "not_exists"
50
+ ],
51
+ number: NUMBER_OPS,
52
+ text: ["contains"]
53
+ };
54
+ /** Whether the menu can show and edit this rule. */
55
+ const inMenu = (rule, field) => !!field && MENU_OPS[field.type].includes(rule.op);
56
+ /** Fields with one value at a time (a choice, a yes/no, a date range): picking again changes it rather than adding one. */
57
+ const SINGLE = /* @__PURE__ */ new Set([
58
+ "select",
59
+ "member",
60
+ "tags",
61
+ "boolean",
62
+ "date"
63
+ ]);
64
+ /** Which part the menu of `field` changes: the one being edited, else its existing single rule, else a new one. */
65
+ const targetOf = (parts, field, editing) => {
66
+ if (editing !== null) return editing;
67
+ if (!SINGLE.has(field.type)) return null;
68
+ const index = parts.findIndex((part) => isRule(part) && part.path === field.path && inMenu(part, field));
69
+ return index < 0 ? null : index;
70
+ };
71
+ const Values = z.array(z.union([z.string(), z.number()]).transform(String));
72
+ /**
73
+ * The values a rule picks: the list of "is any of", or the one of "is". A value that isn't text or
74
+ * numbers (a date range, a relative amount) picks nothing.
75
+ */
76
+ const valuesOf = (rule) => {
77
+ if (!rule || rule.value === void 0 || rule.value === null) return [];
78
+ return Values.safeParse(rule.op === "in" ? rule.value : [rule.value]).data ?? [];
79
+ };
80
+ /** An "is any of" rule; null when nothing is picked. */
81
+ const anyOf = (path, values) => values.length ? {
82
+ path,
83
+ op: "in",
84
+ value: values
85
+ } : null;
86
+ const days = (amount) => ({
87
+ amount,
88
+ unit: "days"
89
+ });
90
+ const months = (amount) => ({
91
+ amount,
92
+ unit: "months"
93
+ });
94
+ /**
95
+ * Date filters in one pick, on the relative operators (they stay current in a saved view);
96
+ * `label` is the English for the `views` namespace's `filter.date.<key>`.
97
+ */
98
+ const DATE_PRESETS = [
99
+ {
100
+ key: "past-day",
101
+ label: "Past day",
102
+ op: "within_last",
103
+ value: days(1)
104
+ },
105
+ {
106
+ key: "past-week",
107
+ label: "Past 7 days",
108
+ op: "within_last",
109
+ value: days(7)
110
+ },
111
+ {
112
+ key: "past-month",
113
+ label: "Past 30 days",
114
+ op: "within_last",
115
+ value: days(30)
116
+ },
117
+ {
118
+ key: "past-quarter",
119
+ label: "Past 3 months",
120
+ op: "within_last",
121
+ value: months(3)
122
+ },
123
+ {
124
+ key: "past-year",
125
+ label: "Past year",
126
+ op: "within_last",
127
+ value: months(12)
128
+ },
129
+ {
130
+ key: "older-month",
131
+ label: "More than 30 days ago",
132
+ op: "more_than_ago",
133
+ value: days(30)
134
+ },
135
+ {
136
+ key: "older-year",
137
+ label: "More than a year ago",
138
+ op: "more_than_ago",
139
+ value: months(12)
140
+ },
141
+ {
142
+ key: "next-week",
143
+ label: "Next 7 days",
144
+ op: "within_next",
145
+ value: days(7)
146
+ },
147
+ {
148
+ key: "next-month",
149
+ label: "Next 30 days",
150
+ op: "within_next",
151
+ value: days(30)
152
+ },
153
+ {
154
+ key: "set",
155
+ label: "Is set",
156
+ op: "exists"
157
+ },
158
+ {
159
+ key: "empty",
160
+ label: "Is empty",
161
+ op: "not_exists"
162
+ }
163
+ ];
164
+ /** The preset a rule is, if any. */
165
+ const presetOf = (rule) => rule && DATE_PRESETS.find((preset) => preset.op === rule.op && JSON.stringify(preset.value) === JSON.stringify(rule.value));
166
+ export { DATE_PRESETS, NUMBER_OPS, NUMBER_SYMBOLS, anyOf, groupOf, inMenu, partsOf, presetOf, targetOf, valuesOf, withPart };