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

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 (47) hide show
  1. package/README.md +13 -1
  2. package/dist/components/icon-tip.d.ts +7 -0
  3. package/dist/components/icon-tip.js +16 -0
  4. package/dist/hooks/use-page-hotkey.d.ts +11 -0
  5. package/dist/hooks/use-page-hotkey.js +21 -0
  6. package/dist/index.d.ts +17 -3
  7. package/dist/index.js +15 -2
  8. package/dist/modules/list/DisplayMenu.d.ts +20 -0
  9. package/dist/modules/list/DisplayMenu.js +86 -0
  10. package/dist/modules/list/ListFilter.d.ts +44 -0
  11. package/dist/modules/list/ListFilter.js +39 -0
  12. package/dist/modules/list/ViewBar.d.ts +13 -0
  13. package/dist/modules/list/ViewBar.js +30 -0
  14. package/dist/modules/record-list/RecordList.d.ts +12 -0
  15. package/dist/modules/record-list/RecordList.js +17 -0
  16. package/dist/modules/record-list/RecordListProvider.d.ts +30 -0
  17. package/dist/modules/record-list/RecordListProvider.js +57 -0
  18. package/dist/modules/record-list/RecordListRoot.d.ts +28 -0
  19. package/dist/modules/record-list/RecordListRoot.js +124 -0
  20. package/dist/modules/record-list/selection.d.ts +22 -0
  21. package/dist/modules/record-list/selection.js +54 -0
  22. package/dist/modules/record-panel/PanelDescriptionEditor.d.ts +9 -0
  23. package/dist/modules/record-panel/PanelDescriptionEditor.js +39 -0
  24. package/dist/modules/record-panel/PanelDescriptionField.d.ts +9 -0
  25. package/dist/modules/record-panel/PanelDescriptionField.js +35 -0
  26. package/dist/modules/record-panel/PanelField.d.ts +11 -0
  27. package/dist/modules/record-panel/PanelField.js +41 -0
  28. package/dist/modules/record-panel/PanelForm.d.ts +22 -0
  29. package/dist/modules/record-panel/PanelForm.js +81 -0
  30. package/dist/modules/record-panel/PanelLabel.d.ts +5 -0
  31. package/dist/modules/record-panel/PanelLabel.js +50 -0
  32. package/dist/modules/record-panel/PanelSection.d.ts +9 -0
  33. package/dist/modules/record-panel/PanelSection.js +21 -0
  34. package/dist/modules/record-panel/panelExpansion.d.ts +10 -0
  35. package/dist/modules/record-panel/panelExpansion.js +7 -0
  36. package/dist/modules/record-panel/useSaveOnLeave.d.ts +14 -0
  37. package/dist/modules/record-panel/useSaveOnLeave.js +42 -0
  38. package/dist/modules/records/openRecord.d.ts +18 -4
  39. package/dist/modules/records/openRecord.js +32 -13
  40. package/dist/types/UIConfig.d.ts +82 -0
  41. package/dist/utils/host-component.d.ts +10 -0
  42. package/dist/utils/host-component.js +34 -0
  43. package/dist/utils/plugin-runtime.d.ts +3 -1
  44. package/dist/utils/plugin-runtime.js +51 -9
  45. package/dist/utils/record-timeline.d.ts +2 -4
  46. package/dist/utils/record-timeline.js +4 -17
  47. package/package.json +1 -1
@@ -5,21 +5,35 @@ export interface RecordRef {
5
5
  contentId: string;
6
6
  }
7
7
  type OpenRecord = (record: RecordRef) => void;
8
+ /** Opens a draft of a record type, starting with `defaults` (e.g. the stage it was added in). */
9
+ type OpenNewRecord = (contentType: string, defaults?: Record<string, unknown>) => void;
8
10
  /**
9
11
  * How records open, provided by the host app (web): beside the page in its side panel, or,
10
12
  * inside a record, on top of that record so Back returns to it. erxes-ui is a Module
11
13
  * Federation singleton, so every plugin reads the host's.
12
14
  */
13
- export declare const RecordOpenerProvider: ({ open, children }: PropsWithChildren<{
15
+ export declare const RecordOpenerProvider: ({ open, openNew, children }: PropsWithChildren<{
14
16
  open: OpenRecord;
17
+ openNew?: OpenNewRecord;
15
18
  }>) => import("react").JSX.Element;
16
19
  /**
17
- * `const { open } = useOpenRecord(); open({ contentType: 'core:customer', contentId })` opens
18
- * the record in core's side panel, the plugin page staying under it. `canOpen` is false
19
- * outside erxes (a plugin page rendered on its own), where `open` does nothing.
20
+ * `const { open, openNew } = useOpenRecord()`: `open({ contentType: 'sales:deal', contentId })`
21
+ * opens a record in core's side panel, the page staying under it, and
22
+ * `openNew('sales:deal', { stageId })` opens a draft that becomes the record on Save. Any
23
+ * type core or an installed plugin registers opens (`IUIConfig.recordTypes`). `canOpen` is
24
+ * false outside erxes (a plugin page rendered on its own), where both do nothing.
20
25
  */
21
26
  export declare const useOpenRecord: () => {
22
27
  open: OpenRecord;
28
+ openNew: OpenNewRecord;
23
29
  canOpen: boolean;
24
30
  };
31
+ /** Web registers how a record draws on its own page. */
32
+ export declare const setRecordPage: (component: import("react").ComponentType<RecordRef> | null) => void;
33
+ /**
34
+ * A record on its own page, as core draws it for `core:customer`: the record's View at full
35
+ * size, under the page header, with Back to its list. A plugin routes its record type's
36
+ * `listPath/:id` to `<RecordPage contentType="sales:deal" contentId={id} />`.
37
+ */
38
+ export declare const RecordPage: (props: RecordRef) => import("react").JSX.Element | null;
25
39
  export {};
@@ -1,27 +1,46 @@
1
+ import { hostComponent } from "../../utils/host-component.js";
1
2
  import { jsx } from "react/jsx-runtime";
2
- import { createContext, use } from "react";
3
+ import { createContext, use, useMemo } from "react";
3
4
  // src/modules/records/openRecord.tsx
4
5
  const RecordOpenerContext = createContext(null);
6
+ const ignore = () => void 0;
5
7
  /**
6
8
  * How records open, provided by the host app (web): beside the page in its side panel, or,
7
9
  * inside a record, on top of that record so Back returns to it. erxes-ui is a Module
8
10
  * Federation singleton, so every plugin reads the host's.
9
11
  */
10
- const RecordOpenerProvider = ({ open, children }) => /* @__PURE__ */ jsx(RecordOpenerContext, {
11
- value: open,
12
- children
13
- });
14
- const ignore = () => void 0;
12
+ const RecordOpenerProvider = ({ open, openNew = ignore, children }) => {
13
+ const opener = useMemo(() => ({
14
+ open,
15
+ openNew
16
+ }), [open, openNew]);
17
+ return /* @__PURE__ */ jsx(RecordOpenerContext, {
18
+ value: opener,
19
+ children
20
+ });
21
+ };
15
22
  /**
16
- * `const { open } = useOpenRecord(); open({ contentType: 'core:customer', contentId })` opens
17
- * the record in core's side panel, the plugin page staying under it. `canOpen` is false
18
- * outside erxes (a plugin page rendered on its own), where `open` does nothing.
23
+ * `const { open, openNew } = useOpenRecord()`: `open({ contentType: 'sales:deal', contentId })`
24
+ * opens a record in core's side panel, the page staying under it, and
25
+ * `openNew('sales:deal', { stageId })` opens a draft that becomes the record on Save. Any
26
+ * type core or an installed plugin registers opens (`IUIConfig.recordTypes`). `canOpen` is
27
+ * false outside erxes (a plugin page rendered on its own), where both do nothing.
19
28
  */
20
29
  const useOpenRecord = () => {
21
- const open = use(RecordOpenerContext);
30
+ const opener = use(RecordOpenerContext);
22
31
  return {
23
- open: open ?? ignore,
24
- canOpen: open !== null
32
+ open: opener?.open ?? ignore,
33
+ openNew: opener?.openNew ?? ignore,
34
+ canOpen: opener !== null
25
35
  };
26
36
  };
27
- export { RecordOpenerProvider, useOpenRecord };
37
+ const page = hostComponent();
38
+ /** Web registers how a record draws on its own page. */
39
+ const setRecordPage = page.set;
40
+ /**
41
+ * A record on its own page, as core draws it for `core:customer`: the record's View at full
42
+ * size, under the page header, with Back to its list. A plugin routes its record type's
43
+ * `listPath/:id` to `<RecordPage contentType="sales:deal" contentId={id} />`.
44
+ */
45
+ const RecordPage = page.Host;
46
+ export { RecordOpenerProvider, RecordPage, setRecordPage, useOpenRecord };
@@ -1,5 +1,7 @@
1
1
  import { TContactContentType } from "../modules/records/contacts.js";
2
+ import { RecordRef } from "../modules/records/openRecord.js";
2
3
  import { Icon } from "@tabler/icons-react";
4
+ import { UseQueryOptions } from "@tanstack/react-query";
3
5
  /** A property input's own settings, stored with the custom field; each input defines its keys. */
4
6
  export type TPropertyInputMeta = Record<string, unknown>;
5
7
  export type TActivityRowProps = {
@@ -36,12 +38,92 @@ export type TRecordSection = {
36
38
  icon: React.ElementType;
37
39
  component: React.ComponentType<TRecordSectionProps>;
38
40
  };
41
+ /** What each slot of a plugin record is given: the record. */
42
+ export type TRecordSlotProps = RecordRef;
43
+ /** The customers and companies a record is about: what its relation tabs list records of. */
44
+ export type TRecordContacts = {
45
+ customerIds: string[];
46
+ companyIds: string[];
47
+ };
48
+ /** A record's own tab, placed after core's Detail tab or after its Activity tab. */
49
+ export type TRecordTab = {
50
+ value: string;
51
+ label: string;
52
+ icon: Icon;
53
+ after?: 'detail' | 'activity';
54
+ component: React.ComponentType<TRecordSlotProps>;
55
+ };
56
+ /** What a plugin record's draft is given: its form, saved as the record on Save. */
57
+ export type TRecordDraftProps = {
58
+ /** What its opener passed (`useOpenRecord().openNew(type, defaults)`), e.g. a stage. */
59
+ defaults: Record<string, unknown>;
60
+ /** Call with the saved record's id: the record takes the draft's place in the panel. */
61
+ onCreated: (id: string) => void;
62
+ /** Leaves the draft: back to the record it came from, else closes the panel. */
63
+ onCancel: () => void;
64
+ };
65
+ /**
66
+ * A record type a plugin shows in core's side panel. Core draws the panel as it draws a
67
+ * customer's: header with Back, expand and close, the tabs (Detail, the type's own `tabs`,
68
+ * every allowed plugin's relation tabs for it, Activity), resizing, the address
69
+ * `?panel=/detail/<contentType>/<id>`, drafts. The plugin fills the slots. Open one with
70
+ * `useOpenRecord().open({ contentType, contentId })`, a draft with `openNew(contentType)`.
71
+ */
72
+ export type TPanelRecordType = {
73
+ /** `<plugin name>:<thing>`, e.g. `sales:deal`; it must start with the plugin's `name`. */
74
+ contentType: string;
75
+ /** What one is called, e.g. "Deal": a draft's header reads "New deal". */
76
+ title: string;
77
+ icon: React.ElementType;
78
+ /**
79
+ * The record's own page, where the panel's expand button leads: a route of the plugin's,
80
+ * under its `path`, that renders `<RecordPage contentType contentId />`.
81
+ */
82
+ detailPath: (contentId: string) => string;
83
+ /** The header's title: the record's name, editable in place if the plugin wants. */
84
+ Title: React.ComponentType<TRecordSlotProps>;
85
+ /** The header's own actions (a "more" menu), before expand and close. */
86
+ Actions?: React.ComponentType<TRecordSlotProps>;
87
+ /** The Detail tab: `PanelSection`s of `PanelField` rows. */
88
+ Detail: React.ComponentType<TRecordSlotProps>;
89
+ tabs?: TRecordTab[];
90
+ /** A record not created yet; without it, the type has no drafts. */
91
+ New?: React.ComponentType<TRecordDraftProps>;
92
+ /**
93
+ * The record's customers and companies, as query options (often the record's own query with
94
+ * `select`). Without it, plugins' relation tabs don't show on this type.
95
+ */
96
+ contacts?: (contentId: string) => UseQueryOptions<TRecordContacts, Error, TRecordContacts>;
97
+ /** A timeline type of the plugin's manifest (`timelines`): core's Activity tab shows it. */
98
+ activity?: string;
99
+ };
100
+ /** What a relation tab is given: the record it shows on, and that record's contacts. */
101
+ export type TRelationTabProps = TRecordSlotProps & TRecordContacts;
102
+ /**
103
+ * A tab a plugin adds to other records' panels, listing its own records of the same customers
104
+ * and companies: a customer's deals, a task's deals. Shown on core's customers and companies
105
+ * (their contacts are themselves) and on plugin record types that give `contacts`. A record
106
+ * clicked in it opens on top in the same panel (`useOpenRecord().open`), and Back returns.
107
+ */
108
+ export type TRelationTab = {
109
+ /** Unique within the plugin, e.g. `deals`. */
110
+ name: string;
111
+ title: string;
112
+ icon: Icon;
113
+ /** The content types it shows on (`core:customer`, `tasks:task`); every one when omitted. */
114
+ hosts?: string[];
115
+ component: React.ComponentType<TRelationTabProps>;
116
+ };
39
117
  export type IUIConfig = {
40
118
  name: string;
41
119
  path: string;
42
120
  icon?: React.ElementType;
43
121
  /** Sections on core's customer and company detail, shown to members who may use the plugin. */
44
122
  recordSections?: TRecordSection[];
123
+ /** The plugin's records core's side panel opens, for members who may use the plugin. */
124
+ recordTypes?: TPanelRecordType[];
125
+ /** Tabs on other records' panels listing the plugin's records of their contacts. */
126
+ relationTabs?: TRelationTab[];
45
127
  i18n?: boolean;
46
128
  i18nNamespace?: string;
47
129
  hasFloatingWidget?: boolean;
@@ -0,0 +1,10 @@
1
+ import { ComponentType } from "react";
2
+ /**
3
+ * A component the host app (web) provides at startup and plugin UIs render: core's timeline,
4
+ * a record's own page, a list's filter. erxes-ui is a Module Federation singleton, so every
5
+ * plugin gets the host's. Outside erxes nothing is registered and it renders nothing.
6
+ */
7
+ export declare const hostComponent: <P extends object>() => {
8
+ set: (component: ComponentType<P> | null) => void;
9
+ Host: (props: P) => import("react").JSX.Element | null;
10
+ };
@@ -0,0 +1,34 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { Suspense, useSyncExternalStore } from "react";
3
+ // src/utils/host-component.tsx
4
+ /**
5
+ * A component the host app (web) provides at startup and plugin UIs render: core's timeline,
6
+ * a record's own page, a list's filter. erxes-ui is a Module Federation singleton, so every
7
+ * plugin gets the host's. Outside erxes nothing is registered and it renders nothing.
8
+ */
9
+ const hostComponent = () => {
10
+ let registered = null;
11
+ const listeners = /* @__PURE__ */ new Set();
12
+ const subscribe = (listener) => {
13
+ listeners.add(listener);
14
+ return () => listeners.delete(listener);
15
+ };
16
+ const read = () => registered;
17
+ const set = (component) => {
18
+ registered = component;
19
+ listeners.forEach((listener) => listener());
20
+ };
21
+ const Host = (props) => {
22
+ const Component = useSyncExternalStore(subscribe, read, read);
23
+ if (!Component) return null;
24
+ return /* @__PURE__ */ jsx(Suspense, {
25
+ fallback: null,
26
+ children: /* @__PURE__ */ jsx(Component, { ...props })
27
+ });
28
+ };
29
+ return {
30
+ set,
31
+ Host
32
+ };
33
+ };
34
+ export { hostComponent };
@@ -33,7 +33,9 @@ export declare const setPluginRuntime: (next: PluginRuntime | null) => void;
33
33
  export declare const getPluginRuntime: () => PluginRuntime | null;
34
34
  /**
35
35
  * `fetch` against a plugin's API as the signed-in member. Relative paths are
36
- * resolved against the plugin's API address; a 401 renews the token once.
36
+ * resolved against the plugin's API address; a 401 renews the token once and
37
+ * sends the call again, so `init.body` must be one that can be sent twice
38
+ * (text, an ArrayBuffer, a Blob or FormData, not a stream).
37
39
  */
38
40
  export declare const pluginFetch: (plugin: string, path: string, init?: RequestInit) => Promise<Response>;
39
41
  /**
@@ -4,6 +4,8 @@ import { z } from "zod";
4
4
  // src/utils/plugin-runtime.ts
5
5
  let runtime = null;
6
6
  const sessions = /* @__PURE__ */ new Map();
7
+ /** A token is replaced this long before it ends, so no call carries one about to expire. */
8
+ const RENEW_BEFORE_MS = 3e4;
7
9
  const setPluginRuntime = (next) => {
8
10
  runtime = next;
9
11
  sessions.clear();
@@ -13,19 +15,58 @@ const need = () => {
13
15
  if (!runtime) throw new Error("erxes plugin runtime is not available (open the plugin inside erxes)");
14
16
  return runtime;
15
17
  };
16
- /** A cached session, renewed 30 seconds before it ends. */
17
- const sessionOf = (plugin, fresh = false) => {
18
- const cached = sessions.get(plugin);
19
- if (cached && !fresh) return cached.then((session) => new Date(session.expiresAt).getTime() - Date.now() > 3e4 ? session : sessionOf(plugin, true));
20
- const request = need().session(plugin);
18
+ const Lifetime = jsonOf(z.object({
19
+ iat: z.number(),
20
+ exp: z.number()
21
+ }));
22
+ /** A JWT's claims as text; '' for a token that isn't one (its lifetime then comes from `expiresAt`). */
23
+ const claimsText = (token) => {
24
+ try {
25
+ return atob((token.split(".")[1] ?? "").replaceAll("-", "+").replaceAll("_", "/"));
26
+ } catch {
27
+ return "";
28
+ }
29
+ };
30
+ /**
31
+ * How long a token lasts, from its own claims (both core's clock), so a
32
+ * browser whose clock is set wrong still renews on time.
33
+ */
34
+ const lifetimeMs = (session) => {
35
+ const claims = Lifetime.safeParse(claimsText(session.token));
36
+ return claims.success ? (claims.data.exp - claims.data.iat) * 1e3 : new Date(session.expiresAt).getTime() - Date.now();
37
+ };
38
+ const renew = (plugin) => {
39
+ const request = need().session(plugin).then((session) => ({
40
+ session,
41
+ endsAt: Date.now() + lifetimeMs(session)
42
+ }));
21
43
  sessions.set(plugin, request);
22
- request.catch(() => sessions.delete(plugin));
44
+ request.catch(() => {
45
+ if (sessions.get(plugin) === request) sessions.delete(plugin);
46
+ });
23
47
  return request;
24
48
  };
49
+ /**
50
+ * This member's session for `plugin`, renewed 30 seconds before it ends.
51
+ * `refused` is a token the plugin just turned down: it is replaced once, for
52
+ * every call that failed with it at the same time.
53
+ */
54
+ const sessionOf = async (plugin, refused) => {
55
+ const cached = sessions.get(plugin);
56
+ if (cached) {
57
+ const held = await cached.catch(() => null);
58
+ if (held && held.session.token !== refused && held.endsAt - Date.now() > RENEW_BEFORE_MS) return held.session;
59
+ const renewing = sessions.get(plugin);
60
+ if (renewing && renewing !== cached) return (await renewing).session;
61
+ }
62
+ return (await renew(plugin)).session;
63
+ };
25
64
  const join = (base, path) => `${base.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
26
65
  /**
27
66
  * `fetch` against a plugin's API as the signed-in member. Relative paths are
28
- * resolved against the plugin's API address; a 401 renews the token once.
67
+ * resolved against the plugin's API address; a 401 renews the token once and
68
+ * sends the call again, so `init.body` must be one that can be sent twice
69
+ * (text, an ArrayBuffer, a Blob or FormData, not a stream).
29
70
  */
30
71
  const pluginFetch = async (plugin, path, init = {}) => {
31
72
  const send = async (session) => {
@@ -38,8 +79,9 @@ const pluginFetch = async (plugin, path, init = {}) => {
38
79
  credentials: "omit"
39
80
  });
40
81
  };
41
- const response = await send(await sessionOf(plugin));
42
- return response.status === 401 ? send(await sessionOf(plugin, true)) : response;
82
+ const session = await sessionOf(plugin);
83
+ const response = await send(session);
84
+ return response.status === 401 ? send(await sessionOf(plugin, session.token)) : response;
43
85
  };
44
86
  const errorBody = z.object({ message: z.string() });
45
87
  /**
@@ -1,14 +1,12 @@
1
- import { ComponentType } from "react";
2
1
  /**
3
2
  * Core's timeline, for plugin UIs: `<RecordTimeline type="hr.employee" id={employee.id} />`
4
3
  * shows notes, mentions, pins, reactions, logged calls and the plugin's own
5
4
  * entries for one of its records (the type must be in the plugin's manifest
6
- * `timelines`). The host app (web) registers the component; erxes-ui is a
7
- * Module Federation singleton, so every plugin gets the host's.
5
+ * `timelines`). The host app (web) registers the component.
8
6
  */
9
7
  export type RecordTimelineProps = {
10
8
  type: string;
11
9
  id: string;
12
10
  };
13
- export declare const setRecordTimeline: (component: ComponentType<RecordTimelineProps> | null) => void;
11
+ export declare const setRecordTimeline: (component: import("react").ComponentType<RecordTimelineProps> | null) => void;
14
12
  export declare const RecordTimeline: (props: RecordTimelineProps) => import("react").JSX.Element | null;
@@ -1,19 +1,6 @@
1
- import { jsx } from "react/jsx-runtime";
2
- import { useSyncExternalStore } from "react";
1
+ import { hostComponent } from "./host-component.js";
3
2
  // src/utils/record-timeline.tsx
4
- let registered = null;
5
- const listeners = /* @__PURE__ */ new Set();
6
- const setRecordTimeline = (component) => {
7
- registered = component;
8
- listeners.forEach((listener) => listener());
9
- };
10
- const subscribe = (listener) => {
11
- listeners.add(listener);
12
- return () => listeners.delete(listener);
13
- };
14
- const RecordTimeline = (props) => {
15
- const Component = useSyncExternalStore(subscribe, () => registered, () => registered);
16
- if (!Component) return null;
17
- return /* @__PURE__ */ jsx(Component, { ...props });
18
- };
3
+ const timeline = hostComponent();
4
+ const setRecordTimeline = timeline.set;
5
+ const RecordTimeline = timeline.Host;
19
6
  export { RecordTimeline, setRecordTimeline };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erxes/ui",
3
- "version": "4.0.0-v2.6",
3
+ "version": "4.0.0-v2.8",
4
4
  "private": false,
5
5
  "description": "erxes design system: UI primitives, layout, hooks and shared frontend state",
6
6
  "license": "AGPL-3.0-only",