@erxes/ui 4.0.0-v2.7 → 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 (55) hide show
  1. package/README.md +10 -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 +11 -2
  5. package/dist/index.js +10 -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/DisplayMenu.d.ts +14 -7
  23. package/dist/modules/list/DisplayMenu.js +48 -28
  24. package/dist/modules/list/ListFilter.d.ts +25 -11
  25. package/dist/modules/list/ListFilter.js +30 -26
  26. package/dist/modules/list/filter/AdvancedFilterDialog.d.ts +5 -0
  27. package/dist/modules/list/filter/AdvancedFilterDialog.js +52 -0
  28. package/dist/modules/list/filter/FieldMenus.d.ts +5 -0
  29. package/dist/modules/list/filter/FieldMenus.js +184 -0
  30. package/dist/modules/list/filter/ListFilterBar.d.ts +6 -0
  31. package/dist/modules/list/filter/ListFilterBar.js +246 -0
  32. package/dist/modules/list/filter/RecordCount.d.ts +7 -0
  33. package/dist/modules/list/filter/RecordCount.js +22 -0
  34. package/dist/modules/list/filter/RuleChips.d.ts +2 -0
  35. package/dist/modules/list/filter/RuleChips.js +157 -0
  36. package/dist/modules/list/filter/fieldIcons.d.ts +7 -0
  37. package/dist/modules/list/filter/fieldIcons.js +16 -0
  38. package/dist/modules/list/filter/filterRules.d.ts +46 -0
  39. package/dist/modules/list/filter/filterRules.js +166 -0
  40. package/dist/modules/list/filter/listFilterContext.d.ts +29 -0
  41. package/dist/modules/list/filter/listFilterContext.js +34 -0
  42. package/dist/modules/list/filter/listFilterQuery.d.ts +15 -0
  43. package/dist/modules/list/filter/listFilterQuery.js +21 -0
  44. package/dist/modules/pickers/LookupPicker.d.ts +50 -0
  45. package/dist/modules/pickers/LookupPicker.js +83 -0
  46. package/dist/modules/record-list/RecordList.d.ts +12 -0
  47. package/dist/modules/record-list/RecordList.js +17 -0
  48. package/dist/modules/record-list/RecordListProvider.d.ts +30 -0
  49. package/dist/modules/record-list/RecordListProvider.js +57 -0
  50. package/dist/modules/record-list/RecordListRoot.d.ts +28 -0
  51. package/dist/modules/record-list/RecordListRoot.js +124 -0
  52. package/dist/modules/record-list/selection.d.ts +22 -0
  53. package/dist/modules/record-list/selection.js +54 -0
  54. package/dist/theme.css +2 -0
  55. package/package.json +2 -1
@@ -1,11 +1,15 @@
1
+ import { TConditionLookups } from "../conditions/conditions.js";
1
2
  import { ElementType, ReactNode } from "react";
2
- /** A field a plugin's list filters by, as its menu, chip and the advanced filter show it. */
3
+ /** A field a list filters by, as its menu, chip and the advanced filter show it. */
3
4
  export type TListFilterField = {
4
5
  /** The rules' `path` in the filter the list's API gets: `subject.<column>`, e.g. `subject.stageId`. */
5
- path: `subject.${string}`;
6
+ path: string;
6
7
  label: string;
7
- /** How it compares: contains (text), ranges (number), relative dates, yes/no, a pick. */
8
- type: 'text' | 'number' | 'date' | 'boolean' | 'select' | 'member';
8
+ /**
9
+ * How it compares: contains (text), ranges (number), relative dates, yes/no, a pick from
10
+ * `options`, a team member (from `members`), a tag (from `tags`).
11
+ */
12
+ type: 'text' | 'number' | 'date' | 'boolean' | 'select' | 'member' | 'tags';
9
13
  /** A `select`'s choices. */
10
14
  options?: {
11
15
  value: string;
@@ -13,8 +17,12 @@ export type TListFilterField = {
13
17
  }[];
14
18
  /** Instead of the type's icon. */
15
19
  icon?: ElementType;
20
+ /** The menu's heading it lists under; `fields` by default. */
21
+ group?: 'fields' | 'lead' | 'company' | 'activity' | 'custom';
22
+ /** A `number` that is money: stored in this currency's smallest unit, typed in its major one. */
23
+ currency?: string;
16
24
  };
17
- export type TListFilterProps = {
25
+ export type TListFilterProps = TConditionLookups & {
18
26
  /** Unique per list, e.g. `sales-deals`: its menu's hotkey scope. */
19
27
  id: string;
20
28
  fields: TListFilterField[];
@@ -24,15 +32,21 @@ export type TListFilterProps = {
24
32
  totalCapped?: boolean;
25
33
  /** The list's own controls, before the menu: `DisplayMenu`, an Archived toggle. */
26
34
  children?: ReactNode;
35
+ /** While `fields` load, and why they didn't; the menu says so. */
36
+ loading?: boolean;
37
+ error?: Error | null;
38
+ /** Beside the chips, before the count: what a saved view changed. */
39
+ status?: ReactNode;
27
40
  };
28
- /** Web registers core's filter bar. */
29
- export declare const setListFilter: (component: import("react").ComponentType<TListFilterProps> | null) => void;
30
41
  /**
31
- * A plugin list's filter bar, core's own: the menu of `fields` (`F`), search, the advanced
32
- * filter, and each condition as a chip. The conditions and search live in the address (shared
33
- * links and reloads keep them); `useListFilterParams()` reads them for the list's query.
42
+ * A list's filter bar: the menu of `fields` (`F`), search, the advanced filter, and each
43
+ * condition as a chip. The conditions and search live in the address (shared links and reloads
44
+ * keep them); `useListFilterParams()` reads them for the list's query. `member` fields pick
45
+ * from `members` and `tags` fields from `tags`: in erxes `memberQueries` from
46
+ * `@erxes/core-contract/client` and the tags query of the record type; outside erxes, the
47
+ * plugin's own.
34
48
  */
35
- export declare const ListFilter: (props: TListFilterProps) => import("react").JSX.Element | null;
49
+ export declare const ListFilter: (props: TListFilterProps) => import("react").JSX.Element;
36
50
  /**
37
51
  * What `ListFilter` holds, for the list's API: `filter`, core's condition group
38
52
  * (`ConditionGroup` from `@erxes/core-contract`) as JSON, its rules on the fields' paths,
@@ -1,25 +1,32 @@
1
- import { hostComponent } from "../../utils/host-component.js";
2
- import { z } from "zod";
3
- import { parseAsString, useQueryStates } from "nuqs";
1
+ import { Filter } from "../filter/components/Filter.js";
2
+ import { useListConditions } from "./filter/listFilterQuery.js";
3
+ import { jsx } from "react/jsx-runtime";
4
+ import { Suspense, lazy } from "react";
5
+ import { useAutoAnimate } from "@formkit/auto-animate/react";
4
6
  // src/modules/list/ListFilter.tsx
5
- const listFilter = hostComponent();
6
- /** Web registers core's filter bar. */
7
- const setListFilter = listFilter.set;
8
7
  /**
9
- * A plugin list's filter bar, core's own: the menu of `fields` (`F`), search, the advanced
10
- * filter, and each condition as a chip. The conditions and search live in the address (shared
11
- * links and reloads keep them); `useListFilterParams()` reads them for the list's query.
8
+ * The bar, fetched the first time a list renders one: erxes-ui is one shared chunk every route
9
+ * loads, and the bar with its condition editor is too much of it for pages without a list.
12
10
  */
13
- const ListFilter = listFilter.Host;
14
- const FilterGroup = z.looseObject({ rules: z.array(z.unknown()).min(1) });
15
- /** The filter as JSON text when it has a condition; anything else in the address reads as none. */
16
- const filterText = (text) => {
17
- if (!text) return void 0;
18
- try {
19
- return FilterGroup.safeParse(JSON.parse(text)).success ? text : void 0;
20
- } catch {
21
- return;
22
- }
11
+ const ListFilterBar = lazy(async () => ({ default: (await import("./filter/ListFilterBar.js")).ListFilterBar }));
12
+ /**
13
+ * A list's filter bar: the menu of `fields` (`F`), search, the advanced filter, and each
14
+ * condition as a chip. The conditions and search live in the address (shared links and reloads
15
+ * keep them); `useListFilterParams()` reads them for the list's query. `member` fields pick
16
+ * from `members` and `tags` fields from `tags`: in erxes `memberQueries` from
17
+ * `@erxes/core-contract/client` and the tags query of the record type; outside erxes, the
18
+ * plugin's own.
19
+ */
20
+ const ListFilter = (props) => {
21
+ const [barRef] = useAutoAnimate({ duration: 150 });
22
+ return /* @__PURE__ */ jsx(Filter, {
23
+ id: `${props.id}-list-filter`,
24
+ children: /* @__PURE__ */ jsx(Filter.Bar, {
25
+ ref: barRef,
26
+ className: "min-w-0 flex-nowrap justify-end",
27
+ children: /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(ListFilterBar, { ...props }) })
28
+ })
29
+ });
23
30
  };
24
31
  /**
25
32
  * What `ListFilter` holds, for the list's API: `filter`, core's condition group
@@ -27,13 +34,10 @@ const filterText = (text) => {
27
34
  * and `search`. Both are undefined when unset.
28
35
  */
29
36
  const useListFilterParams = () => {
30
- const [{ advanced, searchValue }] = useQueryStates({
31
- advanced: parseAsString,
32
- searchValue: parseAsString
33
- });
37
+ const { advanced, search } = useListConditions();
34
38
  return {
35
- filter: filterText(advanced),
36
- search: searchValue || void 0
39
+ filter: advanced ? JSON.stringify(advanced) : void 0,
40
+ search: search || void 0
37
41
  };
38
42
  };
39
- export { ListFilter, setListFilter, useListFilterParams };
43
+ export { ListFilter, useListFilterParams };
@@ -0,0 +1,5 @@
1
+ /** The full builder (every field, and/or groups), for what the filter menu can't say. */
2
+ export declare const AdvancedFilterDialog: ({ open, onOpenChange }: {
3
+ open: boolean;
4
+ onOpenChange: (open: boolean) => void;
5
+ }) => import("react").JSX.Element;
@@ -0,0 +1,52 @@
1
+ import { Button } from "../../../components/button.js";
2
+ import { Dialog } from "../../../components/dialog.js";
3
+ import { ConditionsEditor } from "../../conditions/ConditionsEditor.js";
4
+ import { useListFilter } from "./listFilterContext.js";
5
+ import { useListConditions } from "./listFilterQuery.js";
6
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
+ import { useState } from "react";
8
+ import { useTranslation } from "react-i18next";
9
+ // src/modules/list/filter/AdvancedFilterDialog.tsx
10
+ /** Mounted each time the dialog opens, so the draft starts from the list's filter. */
11
+ const AdvancedFilterForm = ({ onDone }) => {
12
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
13
+ const { fields, lookups } = useListFilter();
14
+ const { advanced, setAdvanced } = useListConditions();
15
+ const [draft, setDraft] = useState(advanced);
16
+ const apply = (next) => {
17
+ setAdvanced(next);
18
+ onDone();
19
+ };
20
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ConditionsEditor, {
21
+ fields: fields.map(({ options, ...field }) => ({
22
+ ...field,
23
+ options: options?.map((option) => option.value)
24
+ })),
25
+ ...lookups,
26
+ value: draft ?? void 0,
27
+ onChange: (next) => setDraft(next ?? null)
28
+ }), /* @__PURE__ */ jsxs(Dialog.Footer, { children: [/* @__PURE__ */ jsx(Button, {
29
+ variant: "ghost",
30
+ onClick: () => apply(null),
31
+ children: t("clear", "Clear")
32
+ }), /* @__PURE__ */ jsx(Button, {
33
+ onClick: () => apply(draft),
34
+ children: t("apply", "Apply")
35
+ })] })] });
36
+ };
37
+ /** The full builder (every field, and/or groups), for what the filter menu can't say. */
38
+ const AdvancedFilterDialog = ({ open, onOpenChange }) => {
39
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
40
+ return /* @__PURE__ */ jsx(Dialog, {
41
+ open,
42
+ onOpenChange,
43
+ children: /* @__PURE__ */ jsxs(Dialog.Content, {
44
+ className: "sm:max-w-3xl",
45
+ children: [/* @__PURE__ */ jsx(Dialog.HeaderCombined, {
46
+ title: t("advanced", "Advanced filter"),
47
+ description: t("advanced-title", "Show records where")
48
+ }), /* @__PURE__ */ jsx(AdvancedFilterForm, { onDone: () => onOpenChange(false) })]
49
+ })
50
+ });
51
+ };
52
+ export { AdvancedFilterDialog };
@@ -0,0 +1,5 @@
1
+ import { TListFilterField } from "../ListFilter.js";
2
+ /** A field's submenu, by its type. */
3
+ export declare const FieldMenu: ({ field }: {
4
+ field: TListFilterField;
5
+ }) => import("react").JSX.Element;
@@ -0,0 +1,184 @@
1
+ import { Button } from "../../../components/button.js";
2
+ import { Combobox } from "../../../components/combobox.js";
3
+ import { Command } from "../../../components/command.js";
4
+ import { Input } from "../../../components/input.js";
5
+ import { Select } from "../../../components/select.js";
6
+ import { amountStep, fromAmountInput, toAmountInput } from "../../../lib/money.js";
7
+ import { oneOf, textOf } from "../../../lib/text.js";
8
+ import { isRule } from "../../conditions/conditions.js";
9
+ import { useFilterContext } from "../../filter/hooks/useFilterContext.js";
10
+ import { EntityPicker } from "../../pickers/EntityPicker.js";
11
+ import { DATE_PRESETS, NUMBER_OPS, NUMBER_SYMBOLS, anyOf, presetOf, targetOf, valuesOf } from "./filterRules.js";
12
+ import { useFieldOptions, useListFilter } from "./listFilterContext.js";
13
+ import { jsx, jsxs } from "react/jsx-runtime";
14
+ import { useEffect, useRef, useState } from "react";
15
+ import { useTranslation } from "react-i18next";
16
+ // src/modules/list/filter/FieldMenus.tsx
17
+ /** The part this field's menu changes, and that part when it is a rule of the field. */
18
+ const useTarget = (field) => {
19
+ const { parts, setPart, editing } = useListFilter();
20
+ const { resetFilterState } = useFilterContext();
21
+ const index = targetOf(parts, field, editing);
22
+ const part = index === null ? void 0 : parts[index];
23
+ return {
24
+ rule: part && isRule(part) && part.path === field.path ? part : void 0,
25
+ set: (next) => setPart(index, next),
26
+ /** Sets the rule and closes the menu. */
27
+ apply: (next) => {
28
+ setPart(index, next);
29
+ resetFilterState();
30
+ }
31
+ };
32
+ };
33
+ /** A submenu's box takes the keyboard as the submenu opens. */
34
+ const useFocusOnMount = () => {
35
+ const input = useRef(null);
36
+ useEffect(() => input.current?.focus(), []);
37
+ return input;
38
+ };
39
+ /** Options (a choice, members, tags): each pick adds to or takes from one "is any of" rule; the menu stays open. */
40
+ const OptionsMenu = ({ field }) => {
41
+ const { options, loading, error } = useFieldOptions(field);
42
+ const { rule, set } = useTarget(field);
43
+ return /* @__PURE__ */ jsx(EntityPicker, {
44
+ multiple: true,
45
+ items: options,
46
+ itemKey: (option) => ({
47
+ id: option.value,
48
+ label: option.label
49
+ }),
50
+ value: valuesOf(rule),
51
+ onValueChange: (values) => set(anyOf(field.path, values)),
52
+ loading,
53
+ error,
54
+ searchPlaceholder: field.label
55
+ });
56
+ };
57
+ const DateMenu = ({ field }) => {
58
+ const { t } = useTranslation("views", { keyPrefix: "filter.date" });
59
+ const { rule, apply } = useTarget(field);
60
+ const current = presetOf(rule);
61
+ return /* @__PURE__ */ jsx(Command, { children: /* @__PURE__ */ jsx(Command.List, { children: DATE_PRESETS.map((preset) => /* @__PURE__ */ jsxs(Command.Item, {
62
+ value: preset.key,
63
+ onSelect: () => apply({
64
+ path: field.path,
65
+ op: preset.op,
66
+ value: preset.value
67
+ }),
68
+ children: [t(preset.key, preset.label), /* @__PURE__ */ jsx(Combobox.Check, { checked: current?.key === preset.key })]
69
+ }, preset.key)) }) });
70
+ };
71
+ const BooleanMenu = ({ field }) => {
72
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
73
+ const { rule, apply } = useTarget(field);
74
+ const value = rule ? textOf(rule.value) : null;
75
+ return /* @__PURE__ */ jsx(Command, { children: /* @__PURE__ */ jsx(Command.List, { children: ["true", "false"].map((option) => /* @__PURE__ */ jsxs(Command.Item, {
76
+ value: option,
77
+ onSelect: () => apply({
78
+ path: field.path,
79
+ op: "eq",
80
+ value: option === "true"
81
+ }),
82
+ children: [option === "true" ? t("yes", "Yes") : t("no", "No"), /* @__PURE__ */ jsx(Combobox.Check, { checked: value === option })]
83
+ }, option)) }) });
84
+ };
85
+ /** A number as typed: money in the currency's major unit. */
86
+ const numberText = (value, currency) => {
87
+ const n = Number(value);
88
+ if (value === void 0 || value === null || !Number.isFinite(n)) return "";
89
+ return currency ? toAmountInput(n, currency) : String(n);
90
+ };
91
+ /** An operator and a value; Enter or Apply adds the rule (or changes the one being edited). */
92
+ const NumberMenu = ({ field }) => {
93
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
94
+ const { rule, apply } = useTarget(field);
95
+ const [op, setOp] = useState(() => oneOf(NUMBER_OPS, rule?.op ?? null) ?? "gte");
96
+ const [text, setText] = useState(() => numberText(rule?.value, field.currency));
97
+ const valid = text.trim() !== "" && Number.isFinite(Number(text));
98
+ const input = useFocusOnMount();
99
+ return /* @__PURE__ */ jsxs("form", {
100
+ className: "flex items-center gap-1.5 p-2",
101
+ onSubmit: (event) => {
102
+ event.preventDefault();
103
+ if (!valid) return;
104
+ const value = field.currency ? fromAmountInput(text, field.currency) : Number(text);
105
+ apply({
106
+ path: field.path,
107
+ op,
108
+ value
109
+ });
110
+ },
111
+ children: [
112
+ /* @__PURE__ */ jsxs(Select, {
113
+ value: op,
114
+ onValueChange: (next) => setOp(oneOf(NUMBER_OPS, next) ?? op),
115
+ children: [/* @__PURE__ */ jsx(Select.Trigger, {
116
+ className: "h-8 w-16 shrink-0",
117
+ children: /* @__PURE__ */ jsx(Select.Value, {})
118
+ }), /* @__PURE__ */ jsx(Select.Content, { children: NUMBER_OPS.map((option) => /* @__PURE__ */ jsx(Select.Item, {
119
+ value: option,
120
+ children: NUMBER_SYMBOLS[option]
121
+ }, option)) })]
122
+ }),
123
+ /* @__PURE__ */ jsx(Input, {
124
+ ref: input,
125
+ type: "number",
126
+ className: "h-8 min-w-0 flex-1",
127
+ step: field.currency ? amountStep(field.currency) : "any",
128
+ placeholder: field.currency ?? t("value", "Value"),
129
+ value: text,
130
+ onChange: (event) => setText(event.target.value)
131
+ }),
132
+ /* @__PURE__ */ jsx(Button, {
133
+ type: "submit",
134
+ size: "sm",
135
+ className: "h-8",
136
+ disabled: !valid,
137
+ children: t("apply", "Apply")
138
+ })
139
+ ]
140
+ });
141
+ };
142
+ /** "Contains" text; an empty box removes the rule being edited. */
143
+ const TextMenu = ({ field }) => {
144
+ const { t } = useTranslation("views", { keyPrefix: "filter" });
145
+ const { rule, apply } = useTarget(field);
146
+ const [text, setText] = useState(() => textOf(rule?.value));
147
+ const input = useFocusOnMount();
148
+ return /* @__PURE__ */ jsxs("form", {
149
+ className: "flex items-center gap-1.5 p-2",
150
+ onSubmit: (event) => {
151
+ event.preventDefault();
152
+ apply(text.trim() ? {
153
+ path: field.path,
154
+ op: "contains",
155
+ value: text.trim()
156
+ } : null);
157
+ },
158
+ children: [/* @__PURE__ */ jsx(Input, {
159
+ ref: input,
160
+ className: "h-8 min-w-0 flex-1",
161
+ placeholder: t("contains", "Contains…"),
162
+ value: text,
163
+ onChange: (event) => setText(event.target.value)
164
+ }), /* @__PURE__ */ jsx(Button, {
165
+ type: "submit",
166
+ size: "sm",
167
+ className: "h-8",
168
+ children: t("apply", "Apply")
169
+ })]
170
+ });
171
+ };
172
+ /** A field's submenu, by its type. */
173
+ const FieldMenu = ({ field }) => {
174
+ switch (field.type) {
175
+ case "select":
176
+ case "member":
177
+ case "tags": return /* @__PURE__ */ jsx(OptionsMenu, { field });
178
+ case "date": return /* @__PURE__ */ jsx(DateMenu, { field });
179
+ case "boolean": return /* @__PURE__ */ jsx(BooleanMenu, { field });
180
+ case "number": return /* @__PURE__ */ jsx(NumberMenu, { field });
181
+ case "text": return /* @__PURE__ */ jsx(TextMenu, { field });
182
+ }
183
+ };
184
+ export { FieldMenu };
@@ -0,0 +1,6 @@
1
+ import { TListFilterProps } from "../ListFilter.js";
2
+ /**
3
+ * Inside `<Filter>`: its chips, the list's status and the count, then the list's controls
4
+ * and the menu (`F`).
5
+ */
6
+ export declare const ListFilterBar: ({ id, fields, loading, error, status, totalCount, totalCapped, children, members, tags }: TListFilterProps) => import("react").JSX.Element;
@@ -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 };