nexus-shared 1.1.20 → 2.0.0
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.
- package/LICENSE +21 -0
- package/README.md +903 -0
- package/dist/Client.Index.d.ts +44 -0
- package/dist/Client.Index.js +49 -0
- package/dist/Components/Documents/Button.d.ts +29 -0
- package/dist/Components/Documents/Button.js +11 -0
- package/dist/Components/Documents/Menu.d.ts +102 -0
- package/dist/Components/Documents/Menu.js +238 -0
- package/dist/Components/Documents/SplitButton.d.ts +64 -0
- package/dist/Components/Documents/SplitButton.js +127 -0
- package/dist/Components/Documents/TabButtons.d.ts +62 -0
- package/dist/Components/Documents/TabButtons.js +19 -0
- package/dist/Components/Forms/ApiForm.d.ts +21 -0
- package/dist/Components/Forms/ApiForm.js +99 -0
- package/dist/Components/Forms/Crud.d.ts +17 -0
- package/dist/Components/Forms/Crud.js +556 -0
- package/dist/Components/Forms/CrudParts.d.ts +15 -0
- package/dist/Components/Forms/CrudParts.js +36 -0
- package/dist/Components/Forms/Form.d.ts +52 -0
- package/dist/Components/Forms/Form.js +319 -0
- package/dist/Components/Forms/SubmitForm.d.ts +65 -0
- package/dist/Components/Forms/SubmitForm.js +198 -0
- package/dist/Components/Inputs/Calendar.d.ts +69 -0
- package/dist/Components/Inputs/Calendar.js +258 -0
- package/dist/Components/Inputs/CheckBox.d.ts +53 -0
- package/dist/Components/Inputs/CheckBox.js +87 -0
- package/dist/Components/Inputs/CheckBoxGroup.d.ts +20 -0
- package/dist/Components/Inputs/CheckBoxGroup.js +80 -0
- package/dist/Components/Inputs/ChoiceField.d.ts +122 -0
- package/dist/Components/Inputs/ChoiceField.js +169 -0
- package/dist/Components/Inputs/DatePicker.d.ts +54 -0
- package/dist/Components/Inputs/DatePicker.js +246 -0
- package/dist/Components/Inputs/DateRangePicker.d.ts +62 -0
- package/dist/Components/Inputs/DateRangePicker.js +283 -0
- package/dist/Components/Inputs/DateTimePicker.d.ts +75 -0
- package/dist/Components/Inputs/DateTimePicker.js +399 -0
- package/dist/Components/Inputs/Dropdown.d.ts +117 -0
- package/dist/Components/Inputs/Dropdown.js +839 -0
- package/dist/Components/Inputs/DropdownParts.d.ts +84 -0
- package/dist/Components/Inputs/DropdownParts.js +102 -0
- package/dist/Components/Inputs/EditorShell.d.ts +45 -0
- package/dist/Components/Inputs/EditorShell.js +214 -0
- package/dist/Components/Inputs/Field.d.ts +53 -0
- package/dist/Components/Inputs/Field.js +16 -0
- package/dist/Components/Inputs/GroupForm.d.ts +20 -0
- package/dist/Components/Inputs/GroupForm.js +170 -0
- package/dist/Components/Inputs/InputField.d.ts +59 -0
- package/dist/Components/Inputs/InputField.js +169 -0
- package/dist/Components/Inputs/InputRenderer.d.ts +35 -0
- package/dist/Components/Inputs/InputRenderer.js +99 -0
- package/dist/Components/Inputs/MarkdownEditor.d.ts +67 -0
- package/dist/Components/Inputs/MarkdownEditor.js +289 -0
- package/dist/Components/Inputs/NumberBox.d.ts +53 -0
- package/dist/Components/Inputs/NumberBox.js +228 -0
- package/dist/Components/Inputs/RadioGroup.d.ts +20 -0
- package/dist/Components/Inputs/RadioGroup.js +43 -0
- package/dist/Components/Inputs/ReadOnlyNotice.d.ts +36 -0
- package/dist/Components/Inputs/ReadOnlyNotice.js +116 -0
- package/dist/Components/Inputs/RichTextEditor.d.ts +103 -0
- package/dist/Components/Inputs/RichTextEditor.js +1170 -0
- package/dist/Components/Inputs/RichTextParts.d.ts +63 -0
- package/dist/Components/Inputs/RichTextParts.js +157 -0
- package/dist/Components/Inputs/RowsInput.d.ts +104 -0
- package/dist/Components/Inputs/RowsInput.js +259 -0
- package/dist/Components/Inputs/Slider.d.ts +87 -0
- package/dist/Components/Inputs/Slider.js +216 -0
- package/dist/Components/Inputs/TabularForm.d.ts +23 -0
- package/dist/Components/Inputs/TabularForm.js +283 -0
- package/dist/Components/Inputs/TextArea.d.ts +43 -0
- package/dist/Components/Inputs/TextArea.js +31 -0
- package/dist/Components/Inputs/TextBox.d.ts +52 -0
- package/dist/Components/Inputs/TextBox.js +58 -0
- package/dist/Components/Inputs/TextField.d.ts +31 -0
- package/dist/Components/Inputs/TextField.js +35 -0
- package/dist/Components/Inputs/TimePanel.d.ts +34 -0
- package/dist/Components/Inputs/TimePanel.js +288 -0
- package/dist/Components/Inputs/TimePicker.d.ts +54 -0
- package/dist/Components/Inputs/TimePicker.js +239 -0
- package/dist/Components/Inputs/TreeView.d.ts +160 -0
- package/dist/Components/Inputs/TreeView.js +947 -0
- package/dist/Components/Layouts/MessageBox.d.ts +71 -0
- package/dist/Components/Layouts/MessageBox.js +104 -0
- package/dist/Components/Layouts/Popup.d.ts +92 -0
- package/dist/Components/Layouts/Popup.js +376 -0
- package/dist/Components/Layouts/PopupContext.d.ts +2 -0
- package/dist/Components/Layouts/PopupContext.js +4 -0
- package/dist/Components/Layouts/PopupDock.d.ts +22 -0
- package/dist/Components/Layouts/PopupDock.js +121 -0
- package/dist/Components/Layouts/PopupHost.d.ts +56 -0
- package/dist/Components/Layouts/PopupHost.js +163 -0
- package/dist/Components/Layouts/PopupRules.d.ts +30 -0
- package/dist/Components/Layouts/PopupRules.js +63 -0
- package/dist/Components/Layouts/ThemePicker.d.ts +13 -0
- package/dist/Components/Layouts/ThemePicker.js +22 -0
- package/dist/Components/Layouts/ThemeScript.d.ts +14 -0
- package/dist/Components/Layouts/ThemeScript.js +13 -0
- package/dist/Components/Layouts/ThemeSwitcher.d.ts +13 -0
- package/dist/Components/Layouts/ThemeSwitcher.js +29 -0
- package/dist/Components/Layouts/Toaster.d.ts +76 -0
- package/dist/Components/Layouts/Toaster.js +172 -0
- package/dist/Components/Viewers/DataTable.d.ts +25 -0
- package/dist/Components/Viewers/DataTable.js +542 -0
- package/dist/Components/Viewers/DataTableParts.d.ts +134 -0
- package/dist/Components/Viewers/DataTableParts.js +145 -0
- package/dist/Components/Viewers/MarkdownView.d.ts +26 -0
- package/dist/Components/Viewers/MarkdownView.js +79 -0
- package/dist/Components/Viewers/RichTextView.d.ts +18 -0
- package/dist/Components/Viewers/RichTextView.js +17 -0
- package/dist/Helpers/AnimationHelpers.d.ts +6 -0
- package/dist/Helpers/AnimationHelpers.js +35 -0
- package/dist/Helpers/ApiClient.d.ts +74 -0
- package/dist/Helpers/ApiClient.js +197 -0
- package/dist/Helpers/ApiFormHelpers.d.ts +26 -0
- package/dist/Helpers/ApiFormHelpers.js +56 -0
- package/dist/Helpers/ApiModules.d.ts +135 -0
- package/dist/Helpers/ApiModules.js +194 -0
- package/dist/Helpers/ApiResponses.d.ts +41 -0
- package/dist/Helpers/ApiResponses.js +159 -0
- package/dist/Helpers/ApiRoutes.d.ts +150 -0
- package/dist/Helpers/ApiRoutes.js +191 -0
- package/dist/Helpers/ApiToasts.d.ts +21 -0
- package/dist/Helpers/ApiToasts.js +22 -0
- package/dist/Helpers/ClassHelpers.d.ts +3 -0
- package/dist/Helpers/ClassHelpers.js +4 -0
- package/dist/Helpers/CrudBackend.d.ts +56 -0
- package/dist/Helpers/CrudBackend.js +119 -0
- package/dist/Helpers/CrudHelpers.d.ts +76 -0
- package/dist/Helpers/CrudHelpers.js +202 -0
- package/dist/Helpers/DateHelpers.d.ts +88 -0
- package/dist/Helpers/DateHelpers.js +363 -0
- package/dist/Helpers/DatePreferences.d.ts +14 -0
- package/dist/Helpers/DatePreferences.js +94 -0
- package/dist/Helpers/DateTimeHelpers.d.ts +87 -0
- package/dist/Helpers/DateTimeHelpers.js +286 -0
- package/dist/Helpers/DragHelpers.d.ts +129 -0
- package/dist/Helpers/DragHelpers.js +281 -0
- package/dist/Helpers/DropdownHelpers.d.ts +56 -0
- package/dist/Helpers/DropdownHelpers.js +180 -0
- package/dist/Helpers/FormDrafts.d.ts +18 -0
- package/dist/Helpers/FormDrafts.js +76 -0
- package/dist/Helpers/FormStore.d.ts +231 -0
- package/dist/Helpers/FormStore.js +734 -0
- package/dist/Helpers/InputParamsHelpers.d.ts +79 -0
- package/dist/Helpers/InputParamsHelpers.js +388 -0
- package/dist/Helpers/MarkdownEditing.d.ts +27 -0
- package/dist/Helpers/MarkdownEditing.js +250 -0
- package/dist/Helpers/MarkdownHelpers.d.ts +82 -0
- package/dist/Helpers/MarkdownHelpers.js +775 -0
- package/dist/Helpers/MessageBuilder.d.ts +74 -0
- package/dist/Helpers/MessageBuilder.js +254 -0
- package/dist/Helpers/NepaliCalendar.d.ts +25 -0
- package/dist/Helpers/NepaliCalendar.js +107 -0
- package/dist/Helpers/NumberHelpers.d.ts +71 -0
- package/dist/Helpers/NumberHelpers.js +202 -0
- package/dist/Helpers/Permissions.d.ts +62 -0
- package/dist/Helpers/Permissions.js +133 -0
- package/dist/Helpers/PopoverHelpers.d.ts +43 -0
- package/dist/Helpers/PopoverHelpers.js +91 -0
- package/dist/Helpers/RichTextHelpers.d.ts +77 -0
- package/dist/Helpers/RichTextHelpers.js +1072 -0
- package/dist/Helpers/RowsStore.d.ts +160 -0
- package/dist/Helpers/RowsStore.js +584 -0
- package/dist/Helpers/TableColumns.d.ts +40 -0
- package/dist/Helpers/TableColumns.js +254 -0
- package/dist/Helpers/TableExport.d.ts +30 -0
- package/dist/Helpers/TableExport.js +97 -0
- package/dist/Helpers/TableHelpers.d.ts +102 -0
- package/dist/Helpers/TableHelpers.js +281 -0
- package/dist/Helpers/TableStore.d.ts +142 -0
- package/dist/Helpers/TableStore.js +530 -0
- package/dist/Helpers/ThemeHelpers.d.ts +15 -0
- package/dist/Helpers/ThemeHelpers.js +16 -0
- package/dist/Helpers/TimeHelpers.d.ts +73 -0
- package/dist/Helpers/TimeHelpers.js +253 -0
- package/dist/Helpers/TreeStore.d.ts +188 -0
- package/dist/Helpers/TreeStore.js +577 -0
- package/dist/Helpers/ValidationHelpers.d.ts +124 -0
- package/dist/Helpers/ValidationHelpers.js +301 -0
- package/dist/Interfaces/ApiInterfaces.d.ts +149 -0
- package/dist/Interfaces/ApiInterfaces.js +24 -0
- package/dist/Interfaces/CrudInterfaces.d.ts +171 -0
- package/{src/sso-client.tsx → dist/Interfaces/CrudInterfaces.js} +1 -1
- package/dist/Interfaces/DateInterfaces.d.ts +154 -0
- package/dist/Interfaces/DateInterfaces.js +29 -0
- package/dist/Interfaces/FormInterfaces.d.ts +352 -0
- package/dist/Interfaces/FormInterfaces.js +1 -0
- package/dist/Interfaces/InputInterfaces.d.ts +141 -0
- package/dist/Interfaces/InputInterfaces.js +1 -0
- package/dist/Interfaces/MessageInterfaces.d.ts +111 -0
- package/dist/Interfaces/MessageInterfaces.js +5 -0
- package/dist/Interfaces/TableInterfaces.d.ts +291 -0
- package/dist/Interfaces/TableInterfaces.js +1 -0
- package/dist/Interfaces/ThemeInterfaces.d.ts +17 -0
- package/dist/Interfaces/ThemeInterfaces.js +17 -0
- package/dist/Interfaces/TypeInterfaces.d.ts +4 -0
- package/dist/Interfaces/TypeInterfaces.js +1 -0
- package/dist/Server.Index.d.ts +2 -0
- package/dist/Server.Index.js +5 -0
- package/dist/Services/ApiProxy.d.ts +32 -0
- package/dist/Services/ApiProxy.js +111 -0
- package/dist/Services/BrowserApi.d.ts +78 -0
- package/dist/Services/BrowserApi.js +111 -0
- package/dist/Services/ServerApi.d.ts +49 -0
- package/dist/Services/ServerApi.js +75 -0
- package/dist/Services/ThemeService.d.ts +17 -0
- package/dist/Services/ThemeService.js +83 -0
- package/dist/Shared.Index.d.ts +43 -0
- package/dist/Shared.Index.js +47 -0
- package/package.json +82 -33
- package/src/Styles/Nexus.Base.css +70 -0
- package/src/Styles/Nexus.Button.css +151 -0
- package/src/Styles/Nexus.Calendar.css +275 -0
- package/src/Styles/Nexus.Choice.css +345 -0
- package/src/Styles/Nexus.Crud.css +84 -0
- package/src/Styles/Nexus.DateTime.css +81 -0
- package/src/Styles/Nexus.Dropdown.css +774 -0
- package/src/Styles/Nexus.Editor.css +297 -0
- package/src/Styles/Nexus.Form.css +298 -0
- package/src/Styles/Nexus.Index.css +34 -0
- package/src/Styles/Nexus.Input.css +637 -0
- package/src/Styles/Nexus.Markdown.css +265 -0
- package/src/Styles/Nexus.Menu.css +249 -0
- package/src/Styles/Nexus.Popup.css +731 -0
- package/src/Styles/Nexus.RichText.css +463 -0
- package/src/Styles/Nexus.Rows.css +566 -0
- package/src/Styles/Nexus.Slider.css +278 -0
- package/src/Styles/Nexus.Tab.Buttons.css +364 -0
- package/src/Styles/Nexus.Table.css +1014 -0
- package/src/Styles/Nexus.Theme.Picker.css +159 -0
- package/src/Styles/Nexus.Themes.css +194 -0
- package/src/Styles/Nexus.Time.css +147 -0
- package/src/Styles/Nexus.Toast.css +265 -0
- package/src/Styles/Nexus.Tokens.css +72 -0
- package/src/Styles/Nexus.Tree.css +435 -0
- package/src/api-services/authentication-service.tsx +0 -23
- package/src/api-services/preference-service.tsx +0 -5
- package/src/api-services/system-service.tsx +0 -34
- package/src/client.ts +0 -31
- package/src/components/documents/button.tsx +0 -142
- package/src/components/documents/icon-box.tsx +0 -93
- package/src/components/documents/page-title.tsx +0 -7
- package/src/components/documents/tab-button.tsx +0 -172
- package/src/components/documents/tag.tsx +0 -30
- package/src/components/index.js +0 -0
- package/src/components/inputs/checkbox-input.tsx +0 -66
- package/src/components/inputs/input-box.tsx +0 -48
- package/src/components/inputs/input-element.tsx +0 -65
- package/src/components/inputs/input-form.tsx +0 -164
- package/src/components/inputs/input.tsx +0 -181
- package/src/components/inputs/number-input.tsx +0 -108
- package/src/components/inputs/radiobox-input.tsx +0 -53
- package/src/components/inputs/textarea-input.tsx +0 -47
- package/src/components/inputs/textbox-input.tsx +0 -45
- package/src/components/layouts/global-dialogbox.tsx +0 -431
- package/src/components/layouts/global-layout.tsx +0 -59
- package/src/components/layouts/layout-helpers.tsx +0 -20
- package/src/components/layouts/panels/user-panel.tsx +0 -112
- package/src/components/layouts/utility-menu.tsx +0 -49
- package/src/components/panels/theme-panel.tsx +0 -46
- package/src/helpers/bitwise-helpers.tsx +0 -11
- package/src/helpers/browser-helpers.tsx +0 -156
- package/src/helpers/datasource-helpers.tsx +0 -99
- package/src/helpers/element-helpers.tsx +0 -57
- package/src/helpers/input-helpers.tsx +0 -74
- package/src/helpers/string-helpers.tsx +0 -28
- package/src/helpers/utility-helpers.tsx +0 -45
- package/src/helpers/validation-helpers.tsx +0 -302
- package/src/index.ts +0 -26
- package/src/interface.ts +0 -19
- package/src/interfaces/auth-token-interfaces.tsx +0 -6
- package/src/interfaces/browser-interfaces.tsx +0 -36
- package/src/interfaces/button-interfaces.tsx +0 -63
- package/src/interfaces/datasource-interfaces.tsx +0 -22
- package/src/interfaces/datatable-interfaces.tsx +0 -25
- package/src/interfaces/dialogbox-interfaces.tsx +0 -5
- package/src/interfaces/exception-interfaces.tsx +0 -99
- package/src/interfaces/http-interfaces.tsx +0 -129
- package/src/interfaces/icon-interfaces.tsx +0 -129
- package/src/interfaces/input-interfaces.tsx +0 -410
- package/src/interfaces/layout-interfaces.tsx +0 -191
- package/src/interfaces/menu-interfaces.tsx +0 -49
- package/src/interfaces/message-interfaces.tsx +0 -32
- package/src/interfaces/permission-interfaces.tsx +0 -9
- package/src/interfaces/storage-interfaces.tsx +0 -5
- package/src/interfaces/system-interfaces.tsx +0 -22
- package/src/interfaces/theme-interfaces.tsx +0 -123
- package/src/interfaces/type-interfaces.tsx +0 -28
- package/src/interfaces/user-interfaces.tsx +0 -47
- package/src/nexus-client.tsx +0 -21
- package/src/nexus.environments.tsx +0 -66
- package/src/proxy-api/proxy-backend.tsx +0 -60
- package/src/proxy-api/proxy-constants.tsx +0 -28
- package/src/proxy-api/proxy-helpers.tsx +0 -91
- package/src/proxy-api-client.tsx +0 -1
- package/src/proxy-api-server.tsx +0 -2
- package/src/services/http-client-services.tsx +0 -75
- package/src/services/http-services.tsx +0 -158
- package/src/services/loader-service.tsx +0 -185
- package/src/services/localstorage-service.tsx +0 -119
- package/src/services/message-services.tsx +0 -383
- package/src/services/theme-service.tsx +0 -161
- package/src/services/user-services.tsx +0 -10
- package/src/sso-config/auth-token-validation.ts +0 -16
- package/src/sso-config/callback-route.tsx +0 -35
- package/src/sso-config/cookie-encryption.ts +0 -51
- package/src/sso-config/cookie-helpers.tsx +0 -102
- package/src/sso-config/forgot-password-route.tsx +0 -36
- package/src/sso-config/oauth-callback-state.ts +0 -61
- package/src/sso-config/pkce-helpers.ts +0 -9
- package/src/sso-config/provider-complete-route.tsx +0 -18
- package/src/sso-config/redirect-context-actions.tsx +0 -45
- package/src/sso-config/redirect-context-helpers.tsx +0 -159
- package/src/sso-config/redirect-context-persist.tsx +0 -18
- package/src/sso-config/redirect-context-route.tsx +0 -45
- package/src/sso-config/refresh-route.tsx +0 -23
- package/src/sso-config/sign-in-route.tsx +0 -74
- package/src/sso-config/sign-out-route.tsx +0 -57
- package/src/sso-config/sign-up-route.tsx +0 -57
- package/src/sso-config/sso-backend-flow.ts +0 -38
- package/src/sso-config/sso-complete-route.tsx +0 -50
- package/src/sso-config/sso-config.ts +0 -59
- package/src/sso-config/sso-interfaces.tsx +0 -99
- package/src/sso-config/sso-response-helpers.tsx +0 -20
- package/src/sso-server.tsx +0 -21
- package/src/styles/nexus.animation.css +0 -269
- package/src/styles/nexus.core.css +0 -119
- package/src/styles/nexus.dialog.css +0 -144
- package/src/styles/nexus.icon.css +0 -51
- package/src/styles/nexus.input.css +0 -207
- package/src/styles/nexus.loader.css +0 -11
- package/src/styles/nexus.logic.css +0 -47
- package/src/styles/nexus.utility.css +0 -347
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { iconArchive, iconArchiveOff, iconEye, iconFlag, iconFlagOff, iconList, iconLock, iconNotes, iconPencil, iconPin, iconPinnedOff, iconPlus, iconRestore, iconTrash, iconTrashX, InlineIcon, } from "nexus-icons";
|
|
4
|
+
import { useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
5
|
+
import { abortError } from "../../Helpers/ApiFormHelpers.js";
|
|
6
|
+
import { unwrapApiResult } from "../../Helpers/ApiResponses.js";
|
|
7
|
+
import { getCrudBackend } from "../../Helpers/CrudBackend.js";
|
|
8
|
+
import { actionAllowed, availableModes, CRUD_CACHE_LIFETIME, CRUD_MODES, CRUD_ROW_ACTIONS, CrudCache, crudListEndpoint, crudMassActions, crudMessageAction, crudRowActions, crudRowEndpoint, getCrudSettings, leavesList, massActionAllowed, pinRoom, } from "../../Helpers/CrudHelpers.js";
|
|
9
|
+
import { cx } from "../../Helpers/ClassHelpers.js";
|
|
10
|
+
import { FormStore } from "../../Helpers/FormStore.js";
|
|
11
|
+
import { pluralize } from "../../Helpers/MessageBuilder.js";
|
|
12
|
+
import { getPermissions, permissionsVersion, subscribePermissions } from "../../Helpers/Permissions.js";
|
|
13
|
+
import { columnValue, formatCellText, readPath } from "../../Helpers/TableHelpers.js";
|
|
14
|
+
import { api as browserApi } from "../../Services/BrowserApi.js";
|
|
15
|
+
import { Button } from "../Documents/Button.js";
|
|
16
|
+
import { TabButtons } from "../Documents/TabButtons.js";
|
|
17
|
+
import { openPopup } from "../Layouts/PopupHost.js";
|
|
18
|
+
import { toast } from "../Layouts/Toaster.js";
|
|
19
|
+
import { DataTable, useTable } from "../Viewers/DataTable.js";
|
|
20
|
+
import { ApiForm } from "./ApiForm.js";
|
|
21
|
+
import { CrudDetails } from "./CrudParts.js";
|
|
22
|
+
import { confirmDiscard, FormButtons } from "./SubmitForm.js";
|
|
23
|
+
/*
|
|
24
|
+
* Crud: a module's records in one component. It takes a controller's endpoints, the table's columns, and the form's
|
|
25
|
+
* inputs, and handles the rest: the lists (all, flagged, archived, trash, deleted) the user may see, the actions they
|
|
26
|
+
* may take, the records the user pinned in a section above the list, Add and Edit popups built on ApiForm, a details
|
|
27
|
+
* popup, actions on many rows, a cache of lists while the page is open, and rows updated from each answer instead of
|
|
28
|
+
* reloading.
|
|
29
|
+
*/
|
|
30
|
+
const ALL_ACTIONS = ["create", "view", "edit", "flag", "pin", "archive", "restore", "trash", "recover", "delete", "trashFromDelete", "description"];
|
|
31
|
+
const MODE_INFO = {
|
|
32
|
+
normal: { label: "All", icon: iconList, empty: plural => `No ${plural} yet.` },
|
|
33
|
+
flag: { label: "Flagged", icon: iconFlag, empty: plural => `No flagged ${plural}.` },
|
|
34
|
+
archive: { label: "Archived", icon: iconArchive, empty: () => "The archive is empty." },
|
|
35
|
+
trash: { label: "Trash", icon: iconTrash, empty: () => "The trash is empty." },
|
|
36
|
+
deleted: { label: "Deleted", icon: iconTrashX, empty: plural => `No deleted ${plural}.` },
|
|
37
|
+
};
|
|
38
|
+
/** Actions that take a record out of reach are asked about first; flags, pins, restore, and recover are undone by a click. */
|
|
39
|
+
const ASKED = new Set(["archive", "trash", "delete", "trashFromDelete"]);
|
|
40
|
+
/** Actions that take a record out of the active lists, so it is no longer among the pinned records either. */
|
|
41
|
+
const LEFT_ACTIVE = new Set(["archive", "trash", "delete", "trashFromDelete"]);
|
|
42
|
+
/** The words of an action that flags some records and unflags others (a mass toggle of a mixed selection). */
|
|
43
|
+
const TOGGLED_FLAG = { verb: "change the flags of {object}", done: "updated", doing: "Changing the flags of {object}", object: "the records" };
|
|
44
|
+
const capitalize = (text) => (text ? text[0].toUpperCase() + text.slice(1) : text);
|
|
45
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
46
|
+
function isInputList(inputs) {
|
|
47
|
+
return Array.isArray(inputs);
|
|
48
|
+
}
|
|
49
|
+
/** The subject the endpoints were given: `apiController(module, "Guest", "guest")` → "guest". */
|
|
50
|
+
function endpointSubject(api) {
|
|
51
|
+
const endpoints = [api.create, api.update, api.details, ...Object.values(api.lists ?? {}), ...Object.values(api.pages ?? {})];
|
|
52
|
+
return endpoints.find(endpoint => endpoint?.subject)?.subject;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A CRUD page in one component: a data table of a controller's records with its lists, Add and Edit popups, details,
|
|
56
|
+
* and actions on one record or many, each shown only when its endpoint exists and the user may call it.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* const GUESTS = MODULES.controller(NexusModule.Reservation, "Guest", "guest");
|
|
60
|
+
*
|
|
61
|
+
* <Crud<Guest>
|
|
62
|
+
* api={GUESTS}
|
|
63
|
+
* columns={[{ key: "fullName", label: "Name" }, { key: "email", label: "Email" }, { key: "visits", label: "Visits", format: "number" }]}
|
|
64
|
+
* inputs={GUEST_INPUTS}
|
|
65
|
+
* paging="server"
|
|
66
|
+
* />
|
|
67
|
+
*/
|
|
68
|
+
export function Crud(props) {
|
|
69
|
+
const { api, columns, rowKey = "id", paging = "client", table: tableOptions, id, className, style, ref } = props;
|
|
70
|
+
const latest = useRef(props);
|
|
71
|
+
latest.current = props;
|
|
72
|
+
const subject = props.subject ?? endpointSubject(api) ?? "record";
|
|
73
|
+
const plural = props.subjectPlural ?? pluralize(subject);
|
|
74
|
+
const name = props.name ?? subject;
|
|
75
|
+
const backend = getCrudBackend();
|
|
76
|
+
// Permissions: every list and action asks, again whenever the user's rights change.
|
|
77
|
+
const permissionsStamp = useSyncExternalStore(subscribePermissions, permissionsVersion, permissionsVersion);
|
|
78
|
+
const permissions = getPermissions();
|
|
79
|
+
const ready = permissions.ready;
|
|
80
|
+
const wantedModes = props.modes ?? CRUD_MODES;
|
|
81
|
+
const wantedActions = props.actions ?? ALL_ACTIONS;
|
|
82
|
+
const can = useCallback((endpoint) => permissions.can(endpoint), [permissions, permissionsStamp]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
83
|
+
const modes = useMemo(() => (ready ? availableModes(api, paging, wantedModes, can) : []), [ready, api, paging, wantedModes, can]);
|
|
84
|
+
const allowed = useMemo(() => {
|
|
85
|
+
const map = {};
|
|
86
|
+
for (const action of ALL_ACTIONS)
|
|
87
|
+
map[action] = ready && actionAllowed(action, api, wantedActions, can);
|
|
88
|
+
return map;
|
|
89
|
+
}, [ready, api, wantedActions, can]);
|
|
90
|
+
const massAllowed = useMemo(() => {
|
|
91
|
+
const map = {};
|
|
92
|
+
for (const action of ["flag", "pin", "archive", "restore", "trash", "recover", "delete", "trashFromDelete", "description"])
|
|
93
|
+
map[action] = ready && massActionAllowed(action, api, wantedActions, can);
|
|
94
|
+
return map;
|
|
95
|
+
}, [ready, api, wantedActions, can]);
|
|
96
|
+
// A controller without a trash deletes from its lists; a trash the user may not use never becomes Delete.
|
|
97
|
+
const trashless = !api.trash || !wantedActions.includes("trash");
|
|
98
|
+
const massTrashless = !api.mass?.trash || !wantedActions.includes("trash");
|
|
99
|
+
const [chosenMode, setChosenMode] = useState(props.defaultMode ?? null);
|
|
100
|
+
const mode = chosenMode && modes.includes(chosenMode) ? chosenMode : modes[0];
|
|
101
|
+
const modeRef = useRef(mode);
|
|
102
|
+
modeRef.current = mode;
|
|
103
|
+
const table = useTable();
|
|
104
|
+
const lifetime = props.cache === false ? 0 : (props.cache?.lifetime ?? CRUD_CACHE_LIFETIME);
|
|
105
|
+
const [cache] = useState(() => new CrudCache(lifetime));
|
|
106
|
+
// The cache lives while the page is open, as the old CRUD's did.
|
|
107
|
+
useEffect(() => () => cache.clear(), [cache]);
|
|
108
|
+
const rawKey = (row) => readPath(row, rowKey);
|
|
109
|
+
const labelOf = useCallback((row) => {
|
|
110
|
+
const custom = latest.current.rowLabel;
|
|
111
|
+
if (custom)
|
|
112
|
+
return custom(row);
|
|
113
|
+
const first = latest.current.columns[0];
|
|
114
|
+
const text = first ? formatCellText(first, columnValue(first, row)) : "";
|
|
115
|
+
return text || `${capitalize(subject)} ${table.keyOf(row)}`;
|
|
116
|
+
}, [subject, table]);
|
|
117
|
+
/* The records the user pinned: their own endpoint, a section above the list, and a limit every CRUD shares */
|
|
118
|
+
const maxPins = getCrudSettings().maxPins;
|
|
119
|
+
const pinsEndpoint = api.pins;
|
|
120
|
+
const pinsOn = ready && Boolean(pinsEndpoint) && wantedActions.includes("pin") && can(pinsEndpoint);
|
|
121
|
+
const [pinned, setPinned] = useState(null);
|
|
122
|
+
const [pinsLoading, setPinsLoading] = useState(false);
|
|
123
|
+
const [pinsStamp, setPinsStamp] = useState(0);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (!pinsOn || !pinsEndpoint) {
|
|
126
|
+
setPinned(null);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const controller = new AbortController();
|
|
130
|
+
setPinsLoading(true);
|
|
131
|
+
const done = (rows) => {
|
|
132
|
+
if (controller.signal.aborted)
|
|
133
|
+
return;
|
|
134
|
+
if (rows)
|
|
135
|
+
setPinned(rows);
|
|
136
|
+
setPinsLoading(false);
|
|
137
|
+
};
|
|
138
|
+
void browserApi.send(pinsEndpoint, { signal: controller.signal, toast: false, subject: plural }).then(answer => {
|
|
139
|
+
if (answer.aborted)
|
|
140
|
+
return;
|
|
141
|
+
// The rows come from the pinned list, so they are pinned whatever the backend sends on each one.
|
|
142
|
+
done(answer.ok ? backend.readList(answer.result).map(row => backend.withPin(row, true)) : []);
|
|
143
|
+
},
|
|
144
|
+
// The section is a shortcut to records the list holds as well, so a failed load leaves it out quietly.
|
|
145
|
+
() => done([]));
|
|
146
|
+
return () => controller.abort();
|
|
147
|
+
}, [pinsOn, pinsEndpoint, backend, plural, pinsStamp]);
|
|
148
|
+
const reloadPins = useCallback(() => setPinsStamp(stamp => stamp + 1), []);
|
|
149
|
+
/** The section after a pin or an unpin: records just pinned go on top, unpinned ones go. */
|
|
150
|
+
function applyPins(rows) {
|
|
151
|
+
setPinned(current => {
|
|
152
|
+
if (!current)
|
|
153
|
+
return current;
|
|
154
|
+
const changed = new Map(rows.map(row => [table.keyOf(row), row]));
|
|
155
|
+
const kept = current.map(row => changed.get(table.keyOf(row)) ?? row).filter(row => backend.isPinned(row));
|
|
156
|
+
const keys = new Set(kept.map(row => table.keyOf(row)));
|
|
157
|
+
return [...rows.filter(row => backend.isPinned(row) && !keys.has(table.keyOf(row))), ...kept];
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** A pinned record's copy in the section, after a change that was not a pin: a flag, a description, a save. */
|
|
161
|
+
function patchPins(rows) {
|
|
162
|
+
setPinned(current => {
|
|
163
|
+
if (!current)
|
|
164
|
+
return current;
|
|
165
|
+
const changed = new Map(rows.map(row => [table.keyOf(row), row]));
|
|
166
|
+
if (!current.some(row => changed.has(table.keyOf(row))))
|
|
167
|
+
return current;
|
|
168
|
+
// The record is still pinned: only its other values changed, and a list may not send the pin at all.
|
|
169
|
+
return current.map(row => {
|
|
170
|
+
const next = changed.get(table.keyOf(row));
|
|
171
|
+
return next ? backend.withPin(next, true) : row;
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/** Records that left the active lists are no longer among the pinned ones. */
|
|
176
|
+
function dropPins(keys) {
|
|
177
|
+
if (!keys.length)
|
|
178
|
+
return;
|
|
179
|
+
const gone = new Set(keys);
|
|
180
|
+
setPinned(current => (current && current.some(row => gone.has(table.keyOf(row))) ? current.filter(row => !gone.has(table.keyOf(row))) : current));
|
|
181
|
+
}
|
|
182
|
+
/** Nothing can be pinned while the limit is reached: lowering the limit never unpins, it only waits to be met. */
|
|
183
|
+
const pinsFull = () => `You can pin up to ${maxPins} ${plural}. Unpin one to pin another.`;
|
|
184
|
+
/* Loading */
|
|
185
|
+
const load = useCallback((query, signal) => {
|
|
186
|
+
if (!mode)
|
|
187
|
+
return new Promise(() => undefined);
|
|
188
|
+
const endpoint = crudListEndpoint(api, mode, paging);
|
|
189
|
+
const sortColumn = query.sort ? latest.current.columns.find(column => column.key === query.sort.key) : undefined;
|
|
190
|
+
const key = paging === "server" ? `${mode}|${query.search}|${query.sort ? `${query.sort.key}:${query.sort.direction}` : ""}|${query.page}|${query.pageSize}` : mode;
|
|
191
|
+
const kept = cache.get(key);
|
|
192
|
+
if (kept)
|
|
193
|
+
return kept;
|
|
194
|
+
const options = { signal, toast: false, subject: plural };
|
|
195
|
+
if (paging === "server")
|
|
196
|
+
options.body = backend.pageRequest(query, sortColumn?.sortKey ?? sortColumn?.key);
|
|
197
|
+
return browserApi.send(endpoint, options).then(answer => {
|
|
198
|
+
if (answer.aborted)
|
|
199
|
+
throw abortError();
|
|
200
|
+
if (!answer.ok)
|
|
201
|
+
throw new Error(answer.message ?? `Could not load the ${plural}.`);
|
|
202
|
+
const result = paging === "server" ? backend.readPage(answer.result) : backend.readList(answer.result);
|
|
203
|
+
cache.set(key, result);
|
|
204
|
+
return result;
|
|
205
|
+
});
|
|
206
|
+
}, [mode, api, paging, cache, backend, plural]);
|
|
207
|
+
/** After a change: the list shown keeps its rows in the cache, the other lists load again when shown. */
|
|
208
|
+
function afterChange(action, rows) {
|
|
209
|
+
const shown = modeRef.current;
|
|
210
|
+
if (paging === "client" && shown) {
|
|
211
|
+
cache.drop(key => key !== shown);
|
|
212
|
+
cache.update(shown, table.getRows());
|
|
213
|
+
}
|
|
214
|
+
else
|
|
215
|
+
cache.clear();
|
|
216
|
+
if (shown)
|
|
217
|
+
latest.current.onChange?.({ action, rows, mode: shown });
|
|
218
|
+
}
|
|
219
|
+
/* Changing rows after an answer */
|
|
220
|
+
function applyChange(action, rows, result, markOn, values) {
|
|
221
|
+
const shown = modeRef.current;
|
|
222
|
+
if (!shown)
|
|
223
|
+
return;
|
|
224
|
+
const answered = rows.length > 1 ? backend.answeredKeys(result, rowKey) : null;
|
|
225
|
+
const done = answered ? rows.filter(row => answered.has(table.keyOf(row))) : rows;
|
|
226
|
+
if (action === "flag") {
|
|
227
|
+
const changes = backend.answeredChanges(result, rowKey);
|
|
228
|
+
const leaving = [];
|
|
229
|
+
const updated = [];
|
|
230
|
+
for (const row of done) {
|
|
231
|
+
const key = table.keyOf(row);
|
|
232
|
+
const current = table.getRow(key) ?? row;
|
|
233
|
+
const on = markOn ?? !backend.isFlagged(current);
|
|
234
|
+
let next = backend.withFlag(current, on);
|
|
235
|
+
const change = changes.get(key);
|
|
236
|
+
if (change)
|
|
237
|
+
next = { ...next, ...change };
|
|
238
|
+
if (leavesList(action, shown, backend.isFlagged(next)))
|
|
239
|
+
leaving.push(key);
|
|
240
|
+
else
|
|
241
|
+
table.upsert(next, { flash: false });
|
|
242
|
+
updated.push(next);
|
|
243
|
+
}
|
|
244
|
+
table.remove(leaving);
|
|
245
|
+
patchPins(updated);
|
|
246
|
+
afterChange(action, updated);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (action === "pin") {
|
|
250
|
+
const changes = backend.answeredChanges(result, rowKey);
|
|
251
|
+
const updated = [];
|
|
252
|
+
for (const row of done) {
|
|
253
|
+
const key = table.keyOf(row);
|
|
254
|
+
const current = table.getRow(key) ?? row;
|
|
255
|
+
const on = markOn ?? !backend.isPinned(current);
|
|
256
|
+
let next = backend.withPin(current, on);
|
|
257
|
+
const change = changes.get(key);
|
|
258
|
+
if (change)
|
|
259
|
+
next = { ...next, ...change };
|
|
260
|
+
// A pin changes the section above the list, never the list itself: the record stays where it is, on its page.
|
|
261
|
+
if (table.getRow(key))
|
|
262
|
+
table.upsert(next, { flash: false });
|
|
263
|
+
updated.push(next);
|
|
264
|
+
}
|
|
265
|
+
applyPins(updated);
|
|
266
|
+
afterChange(action, updated);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (action === "description") {
|
|
270
|
+
const updated = done.map(row => ({ ...(table.getRow(table.keyOf(row)) ?? row), ...values }));
|
|
271
|
+
for (const row of updated)
|
|
272
|
+
table.upsert(row);
|
|
273
|
+
patchPins(updated);
|
|
274
|
+
afterChange(action, updated);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const keys = done.map(row => table.keyOf(row));
|
|
278
|
+
if (leavesList(action, shown))
|
|
279
|
+
table.remove(keys);
|
|
280
|
+
if (LEFT_ACTIVE.has(action))
|
|
281
|
+
dropPins(keys);
|
|
282
|
+
afterChange(action, done);
|
|
283
|
+
}
|
|
284
|
+
/* Row actions */
|
|
285
|
+
async function runRowAction(action, row) {
|
|
286
|
+
const endpoint = crudRowEndpoint(api, action);
|
|
287
|
+
if (!endpoint)
|
|
288
|
+
return;
|
|
289
|
+
const key = rawKey(row);
|
|
290
|
+
const markOn = action === "flag" ? !backend.isFlagged(row) : action === "pin" ? !backend.isPinned(row) : undefined;
|
|
291
|
+
if (action === "pin" && markOn && pinRoom(pinned?.length) < 1) {
|
|
292
|
+
toast.warning(pinsFull());
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const options = { params: { id: key, [rowKey]: key }, subject: labelOf(row), action: crudMessageAction(action, markOn) };
|
|
296
|
+
const answer = ASKED.has(action) ? await browserApi.confirm(endpoint, options) : await browserApi.send(endpoint, options);
|
|
297
|
+
if (answer?.ok)
|
|
298
|
+
applyChange(action, [row], answer.result, markOn);
|
|
299
|
+
}
|
|
300
|
+
async function runMassAction(action, rows) {
|
|
301
|
+
if (action === "description") {
|
|
302
|
+
openDescription(rows);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
const endpoint = api.mass?.[action];
|
|
306
|
+
if (!endpoint)
|
|
307
|
+
return;
|
|
308
|
+
let markOn;
|
|
309
|
+
let sent = rows;
|
|
310
|
+
let over = 0;
|
|
311
|
+
if (action === "flag") {
|
|
312
|
+
const on = rows.filter(row => backend.isFlagged(row)).length;
|
|
313
|
+
markOn = on === 0 ? true : on === rows.length ? false : undefined;
|
|
314
|
+
}
|
|
315
|
+
if (action === "pin") {
|
|
316
|
+
// Every selected record pinned means Unpin; otherwise the ones that are not pinned yet are pinned, as many as
|
|
317
|
+
// the limit leaves room for, in the order the user picked them. The rest stay as they are.
|
|
318
|
+
const adding = rows.filter(row => !backend.isPinned(row));
|
|
319
|
+
markOn = adding.length > 0;
|
|
320
|
+
if (markOn) {
|
|
321
|
+
const room = pinRoom(pinned?.length);
|
|
322
|
+
if (room < 1) {
|
|
323
|
+
toast.warning(pinsFull());
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
sent = adding.slice(0, room);
|
|
327
|
+
over = adding.length - sent.length;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const messageAction = action === "flag" && markOn === undefined ? TOGGLED_FLAG : crudMessageAction(action, markOn);
|
|
331
|
+
const options = { body: backend.massRequest(action, sent.map(rawKey)), subject, plural: props.subjectPlural, count: sent.length, action: messageAction };
|
|
332
|
+
const answer = ASKED.has(action) ? await browserApi.confirm(endpoint, options) : await browserApi.send(endpoint, options);
|
|
333
|
+
if (!answer?.ok)
|
|
334
|
+
return;
|
|
335
|
+
applyChange(action, sent, answer.result, markOn);
|
|
336
|
+
table.clearSelection();
|
|
337
|
+
if (over > 0)
|
|
338
|
+
toast.warning(`${sent.length} of ${sent.length + over} ${plural} pinned: you can pin up to ${maxPins}.`);
|
|
339
|
+
}
|
|
340
|
+
/* Popups */
|
|
341
|
+
const addPopup = useRef(null);
|
|
342
|
+
async function inputsFor(action, row) {
|
|
343
|
+
const given = latest.current.inputs;
|
|
344
|
+
if (typeof given === "function")
|
|
345
|
+
return given(action, row);
|
|
346
|
+
if (isInputList(given))
|
|
347
|
+
return given;
|
|
348
|
+
return (action === "create" ? (given.create ?? given.edit) : (given.edit ?? given.create)) ?? [];
|
|
349
|
+
}
|
|
350
|
+
async function openForm(action, row, values) {
|
|
351
|
+
const endpoint = action === "create" ? api.create : api.update;
|
|
352
|
+
if (!endpoint)
|
|
353
|
+
return;
|
|
354
|
+
// An Add form with changes comes back as it was; an untouched one is replaced by a fresh form.
|
|
355
|
+
if (action === "create" && addPopup.current?.form.isChanged()) {
|
|
356
|
+
addPopup.current.handle.restore();
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const inputs = await inputsFor(action, row);
|
|
360
|
+
const options = latest.current.form ?? {};
|
|
361
|
+
const form = new FormStore();
|
|
362
|
+
const key = row ? table.keyOf(row) : "";
|
|
363
|
+
const label = row ? labelOf(row) : undefined;
|
|
364
|
+
const newValues = typeof options.newValues === "function" ? options.newValues() : options.newValues;
|
|
365
|
+
const loader = options.load;
|
|
366
|
+
const loadValue = action === "edit" && row && loader && loader !== "row"
|
|
367
|
+
? loader === "details"
|
|
368
|
+
? api.details
|
|
369
|
+
? (signal) => browserApi.get(api.details, { params: { id: rawKey(row), [rowKey]: rawKey(row) }, signal, toast: false }).then(answer => ({ ...row, ...unwrapApiResult(answer) }))
|
|
370
|
+
: undefined
|
|
371
|
+
: (signal) => loader(row, signal).then(full => ({ ...row, ...full }))
|
|
372
|
+
: undefined;
|
|
373
|
+
const handle = openPopup({
|
|
374
|
+
key: action === "create" ? `${name}-new` : `${name}-edit-${key}`,
|
|
375
|
+
reopen: action === "create" ? "replace" : "restore",
|
|
376
|
+
title: action === "create" ? `Add ${subject}` : `Edit ${subject}`,
|
|
377
|
+
description: label,
|
|
378
|
+
icon: _jsx(InlineIcon, { icon: action === "create" ? iconPlus : iconPencil }),
|
|
379
|
+
size: options.size ?? (inputs.length > 8 ? "lg" : "md"),
|
|
380
|
+
closeOnBackdrop: false,
|
|
381
|
+
beforeClose: async () => {
|
|
382
|
+
const discard = await confirmDiscard(form);
|
|
383
|
+
if (discard)
|
|
384
|
+
form.dropDraft();
|
|
385
|
+
return discard;
|
|
386
|
+
},
|
|
387
|
+
content: popup => (_jsx(ApiForm, { form: form, inputs: inputs, buttons: false, defaultValue: (action === "create" ? { ...newValues, ...values } : (row ?? undefined)), loadValue: loadValue, api: endpoint, transform: options.transform ? formValues => options.transform(formValues, { action, row }) : undefined, validate: options.validate, onFieldChange: options.onFieldChange, design: options.design, draftKey: options.draftKey ? `${options.draftKey}:${name}:${action === "create" ? "new" : key}` : undefined, "aria-label": action === "create" ? `Add ${subject}` : `Edit ${label}`, onSuccess: (result, saved) => {
|
|
388
|
+
onSaved(action, row, result, saved);
|
|
389
|
+
popup.close();
|
|
390
|
+
} })),
|
|
391
|
+
footer: popup => _jsx(FormButtons, { form: form, submitLabel: action === "create" ? `Add ${subject}` : "Save", onCancel: () => popup.close() }),
|
|
392
|
+
});
|
|
393
|
+
if (action === "create") {
|
|
394
|
+
const entry = { handle, form };
|
|
395
|
+
addPopup.current = entry;
|
|
396
|
+
void handle.closed.then(() => {
|
|
397
|
+
if (addPopup.current === entry)
|
|
398
|
+
addPopup.current = null;
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function onSaved(action, row, result, values) {
|
|
403
|
+
const record = { ...row, ...values, ...(isRecord(result) ? result : {}) };
|
|
404
|
+
const shown = modeRef.current;
|
|
405
|
+
if (action === "edit") {
|
|
406
|
+
table.upsert(record);
|
|
407
|
+
patchPins([record]);
|
|
408
|
+
afterChange("edit", [record]);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
// A new record belongs to the list of every record; without its key from the server, the list loads again.
|
|
412
|
+
if (shown === "normal") {
|
|
413
|
+
if (table.keyOf(record))
|
|
414
|
+
table.upsert(record);
|
|
415
|
+
else
|
|
416
|
+
void table.reload({ quiet: true });
|
|
417
|
+
}
|
|
418
|
+
afterChange("create", [record]);
|
|
419
|
+
}
|
|
420
|
+
function openDetails(row) {
|
|
421
|
+
const details = latest.current.details;
|
|
422
|
+
if (details === false)
|
|
423
|
+
return;
|
|
424
|
+
const key = table.keyOf(row);
|
|
425
|
+
const canEdit = allowed.edit && modeRef.current !== undefined && CRUD_ROW_ACTIONS[modeRef.current].includes("edit");
|
|
426
|
+
const endpoint = api.details;
|
|
427
|
+
openPopup({
|
|
428
|
+
key: `${name}-view-${key}`,
|
|
429
|
+
reopen: "restore",
|
|
430
|
+
title: `${capitalize(subject)} details`,
|
|
431
|
+
description: labelOf(row),
|
|
432
|
+
icon: _jsx(InlineIcon, { icon: iconEye }),
|
|
433
|
+
size: details?.size ?? "md",
|
|
434
|
+
content: (_jsx(CrudDetails, { row: row, columns: details?.columns ?? latest.current.columns, render: details?.render, load: endpoint ? signal => browserApi.get(endpoint, { params: { id: rawKey(row), [rowKey]: rawKey(row) }, signal, toast: false }).then(unwrapApiResult) : undefined })),
|
|
435
|
+
footer: popup => (_jsxs(_Fragment, { children: [canEdit && (_jsx(Button, { icon: _jsx(InlineIcon, { icon: iconPencil }), onClick: () => {
|
|
436
|
+
popup.close();
|
|
437
|
+
void openForm("edit", table.getRow(key) ?? row);
|
|
438
|
+
}, children: "Edit" })), _jsx(Button, { variant: "primary", onClick: () => popup.close(), children: "Close" })] })),
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
function openDescription(rows) {
|
|
442
|
+
const endpoint = api.mass?.description;
|
|
443
|
+
if (!endpoint)
|
|
444
|
+
return;
|
|
445
|
+
const form = new FormStore();
|
|
446
|
+
const count = rows.length;
|
|
447
|
+
openPopup({
|
|
448
|
+
key: `${name}-description`,
|
|
449
|
+
title: "Description",
|
|
450
|
+
description: `${count} ${count === 1 ? subject : plural}`,
|
|
451
|
+
icon: _jsx(InlineIcon, { icon: iconNotes }),
|
|
452
|
+
size: "sm",
|
|
453
|
+
closeOnBackdrop: false,
|
|
454
|
+
beforeClose: () => confirmDiscard(form),
|
|
455
|
+
content: popup => (_jsx(ApiForm, { form: form, buttons: false, inputs: [{ type: "textarea", name: "description", label: "Description", maxLength: 500, rows: 4, showCount: true }], api: endpoint, transform: values => backend.massRequest("description", rows.map(rawKey), values), "aria-label": `Description of ${count} ${count === 1 ? subject : plural}`, onSuccess: (result, values) => {
|
|
456
|
+
applyChange("description", rows, result, undefined, values);
|
|
457
|
+
table.clearSelection();
|
|
458
|
+
popup.close();
|
|
459
|
+
} })),
|
|
460
|
+
footer: popup => _jsx(FormButtons, { form: form, submitLabel: "Save", onCancel: () => popup.close() }),
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
// Row actions and the handle call these through a ref, so the table's callbacks stay the same between renders.
|
|
464
|
+
const run = useRef({ runRowAction, runMassAction, openForm, openDetails });
|
|
465
|
+
run.current = { runRowAction, runMassAction, openForm, openDetails };
|
|
466
|
+
/* The table's callbacks */
|
|
467
|
+
const rowActions = useCallback((row) => {
|
|
468
|
+
if (!mode)
|
|
469
|
+
return [];
|
|
470
|
+
const list = [];
|
|
471
|
+
const primary = mode === "normal" || mode === "flag";
|
|
472
|
+
const flagged = backend.isFlagged(row);
|
|
473
|
+
const isPinned = backend.isPinned(row);
|
|
474
|
+
const info = {
|
|
475
|
+
view: { label: "View details", icon: _jsx(InlineIcon, { icon: iconEye }) },
|
|
476
|
+
edit: { label: "Edit", icon: _jsx(InlineIcon, { icon: iconPencil }), inline: true },
|
|
477
|
+
flag: flagged ? { label: "Unflag", icon: _jsx(InlineIcon, { icon: iconFlagOff }) } : { label: "Flag", icon: _jsx(InlineIcon, { icon: iconFlag }) },
|
|
478
|
+
pin: isPinned ? { label: "Unpin", icon: _jsx(InlineIcon, { icon: iconPinnedOff }) } : { label: "Pin", icon: _jsx(InlineIcon, { icon: iconPin }) },
|
|
479
|
+
archive: { label: "Archive", icon: _jsx(InlineIcon, { icon: iconArchive }) },
|
|
480
|
+
restore: { label: "Restore", description: "Back to the active list", icon: _jsx(InlineIcon, { icon: iconArchiveOff }), inline: true },
|
|
481
|
+
trash: { label: "Move to trash", icon: _jsx(InlineIcon, { icon: iconTrash }), tone: "danger" },
|
|
482
|
+
recover: { label: "Recover", description: "Back to the active list", icon: _jsx(InlineIcon, { icon: iconRestore }), inline: true },
|
|
483
|
+
delete: { label: "Delete permanently", icon: _jsx(InlineIcon, { icon: iconTrashX }), tone: "danger" },
|
|
484
|
+
trashFromDelete: { label: "Move back to trash", icon: _jsx(InlineIcon, { icon: iconRestore }), inline: true },
|
|
485
|
+
};
|
|
486
|
+
for (const action of crudRowActions(mode, candidate => allowed[candidate], trashless)) {
|
|
487
|
+
if (action === "view" && latest.current.details === false)
|
|
488
|
+
continue;
|
|
489
|
+
const onSelect = action === "view" ? () => run.current.openDetails(row) : action === "edit" ? () => void run.current.openForm("edit", row) : () => run.current.runRowAction(action, row);
|
|
490
|
+
const entry = info[action];
|
|
491
|
+
list.push({ ...entry, id: action, inline: entry.inline && (primary ? action === "edit" : true), onSelect });
|
|
492
|
+
}
|
|
493
|
+
const extra = latest.current.rowActions?.(row, mode) ?? [];
|
|
494
|
+
return [...list, ...extra];
|
|
495
|
+
}, [mode, allowed, backend, trashless]);
|
|
496
|
+
const massActionsFor = useCallback((rows) => {
|
|
497
|
+
if (!mode)
|
|
498
|
+
return [];
|
|
499
|
+
const list = [];
|
|
500
|
+
const flagged = rows.filter(row => backend.isFlagged(row)).length;
|
|
501
|
+
const unpinned = rows.filter(row => !backend.isPinned(row)).length;
|
|
502
|
+
for (const action of crudMassActions(mode, candidate => massAllowed[candidate], massTrashless)) {
|
|
503
|
+
const onSelect = (selected) => run.current.runMassAction(action, selected);
|
|
504
|
+
const info = {
|
|
505
|
+
flag: flagged === rows.length ? { label: "Unflag", icon: _jsx(InlineIcon, { icon: iconFlagOff }) } : { label: flagged ? "Flag / unflag" : "Flag", icon: _jsx(InlineIcon, { icon: iconFlag }) },
|
|
506
|
+
pin: unpinned === 0 ? { label: "Unpin", icon: _jsx(InlineIcon, { icon: iconPinnedOff }) } : { label: "Pin", icon: _jsx(InlineIcon, { icon: iconPin }) },
|
|
507
|
+
archive: { label: "Archive", icon: _jsx(InlineIcon, { icon: iconArchive }) },
|
|
508
|
+
restore: { label: "Restore", icon: _jsx(InlineIcon, { icon: iconArchiveOff }) },
|
|
509
|
+
trash: { label: "Move to trash", icon: _jsx(InlineIcon, { icon: iconTrash }), tone: "danger" },
|
|
510
|
+
recover: { label: "Recover", icon: _jsx(InlineIcon, { icon: iconRestore }) },
|
|
511
|
+
delete: { label: "Delete permanently", icon: _jsx(InlineIcon, { icon: iconTrashX }), tone: "danger" },
|
|
512
|
+
trashFromDelete: { label: "Move back to trash", icon: _jsx(InlineIcon, { icon: iconRestore }) },
|
|
513
|
+
description: { label: "Description", icon: _jsx(InlineIcon, { icon: iconNotes }) },
|
|
514
|
+
};
|
|
515
|
+
list.push({ ...info[action], id: action, onSelect });
|
|
516
|
+
}
|
|
517
|
+
const extra = latest.current.massActions ? latest.current.massActions(rows, mode) : [];
|
|
518
|
+
return [...list, ...extra];
|
|
519
|
+
}, [mode, massAllowed, backend, massTrashless]);
|
|
520
|
+
const onRowOpen = useCallback((row) => {
|
|
521
|
+
if (allowed.view && latest.current.details !== false)
|
|
522
|
+
run.current.openDetails(row);
|
|
523
|
+
else if (allowed.edit && mode && CRUD_ROW_ACTIONS[mode].includes("edit"))
|
|
524
|
+
void run.current.openForm("edit", row);
|
|
525
|
+
}, [allowed, mode]);
|
|
526
|
+
useImperativeHandle(ref, () => ({
|
|
527
|
+
table,
|
|
528
|
+
get mode() {
|
|
529
|
+
return modeRef.current ?? "normal";
|
|
530
|
+
},
|
|
531
|
+
setMode: next => setChosenMode(next),
|
|
532
|
+
reload: () => {
|
|
533
|
+
cache.clear();
|
|
534
|
+
reloadPins();
|
|
535
|
+
return table.reload({ quiet: true });
|
|
536
|
+
},
|
|
537
|
+
create: values => void run.current.openForm("create", null, values),
|
|
538
|
+
edit: row => void run.current.openForm("edit", row),
|
|
539
|
+
view: row => run.current.openDetails(row),
|
|
540
|
+
}), [table, cache, reloadPins]);
|
|
541
|
+
const tableLabel = props["aria-label"] ?? (props["aria-labelledby"] ? undefined : capitalize(plural));
|
|
542
|
+
const configured = CRUD_MODES.some(value => wantedModes.includes(value) && crudListEndpoint(api, value, paging));
|
|
543
|
+
if (!configured) {
|
|
544
|
+
return (_jsx("div", { id: id, className: cx("nx-crud", "nx-crud--denied", className), style: style, role: "alert", children: _jsxs("p", { className: "nx-crud__denied-title", children: ["Nothing to show: no ", paging === "server" ? "`pages`" : "`lists`", " endpoint for the ", plural, "."] }) }));
|
|
545
|
+
}
|
|
546
|
+
if (ready && !mode) {
|
|
547
|
+
return (_jsxs("div", { id: id, className: cx("nx-crud", "nx-crud--denied", className), style: style, role: "status", children: [_jsx(InlineIcon, { icon: iconLock, className: "nx-crud__denied-icon" }), _jsxs("p", { className: "nx-crud__denied-title", children: ["You do not have permission to see ", plural, "."] }), _jsx("p", { className: "nx-crud__denied-detail", children: "Ask an administrator for access." })] }));
|
|
548
|
+
}
|
|
549
|
+
const modeOptions = modes.map(value => ({ value, label: MODE_INFO[value].label, icon: _jsx(InlineIcon, { icon: MODE_INFO[value].icon }) }));
|
|
550
|
+
const addButton = allowed.create ? (_jsxs(Button, { variant: "primary", size: "sm", icon: _jsx(InlineIcon, { icon: iconPlus }), onClick: () => void run.current.openForm("create", null), children: ["Add ", subject] })) : null;
|
|
551
|
+
const selectable = props.massActions !== false && Boolean(mode) && (crudMassActions(mode, action => massAllowed[action], massTrashless).length > 0 || Boolean(props.massActions));
|
|
552
|
+
return (_jsx("div", { id: id, className: cx("nx-crud", className), style: style, "data-mode": mode, children: _jsx(DataTable, { table: table, columns: columns, load: load, loadKey: mode ?? "waiting", paging: paging, rowKey: rowKey, rowLabel: labelOf, "aria-label": tableLabel, "aria-labelledby": props["aria-labelledby"], toolbar: modes.length > 1 && mode ? (_jsx(TabButtons, { size: "sm", labels: "selected", options: modeOptions, value: mode, onChange: setChosenMode, "aria-label": `${capitalize(plural)}: lists` })) : undefined, actions: _jsxs(_Fragment, { children: [props.toolbar, addButton] }), selectable: selectable, massActions: massActionsFor, rowActions: rowActions, pinnedRows: mode === "normal" && pinsOn ? (pinned ?? undefined) : undefined, pinnedLoading: pinsLoading, pinnedLabel: `Pinned ${plural}`, pinnedMax: maxPins, onRowOpen: onRowOpen, onReload: () => {
|
|
553
|
+
cache.clear();
|
|
554
|
+
reloadPins();
|
|
555
|
+
}, emptyMessage: tableOptions?.emptyMessage ?? (mode ? MODE_INFO[mode].empty(plural) : undefined), emptyAction: mode === "normal" ? addButton : undefined, searchPlaceholder: tableOptions?.searchPlaceholder ?? `Search ${plural}…`, stateKey: tableOptions?.stateKey ?? `crud:${name}`, pageSize: tableOptions?.pageSize, pageSizes: tableOptions?.pageSizes, defaultSort: tableOptions?.defaultSort, numbered: tableOptions?.numbered, maxHeight: tableOptions?.maxHeight, size: tableOptions?.size, maxSelection: tableOptions?.maxSelection, columnsMenu: tableOptions?.columnsMenu, searchDelay: tableOptions?.searchDelay, reorderable: tableOptions?.reorderable, resizable: tableOptions?.resizable, pinnable: tableOptions?.pinnable, moreActions: tableOptions?.moreActions, exportable: tableOptions?.exportable, printable: tableOptions?.printable, exportName: tableOptions?.exportName, onImport: tableOptions?.onImport }) }));
|
|
556
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { FormValues } from "../../Interfaces/FormInterfaces.ts";
|
|
3
|
+
import type { TableColumn } from "../../Interfaces/TableInterfaces.ts";
|
|
4
|
+
export interface CrudDetailsProps<TRow extends FormValues = FormValues> {
|
|
5
|
+
/** The record as the list has it, shown at once. */
|
|
6
|
+
row: TRow;
|
|
7
|
+
/** The fields, as the table's columns describe them; hidden columns included. */
|
|
8
|
+
columns: readonly TableColumn<TRow>[];
|
|
9
|
+
/** Loads the full record (fields the list leaves out); its values replace the row's when they come. */
|
|
10
|
+
load?: (signal: AbortSignal) => Promise<Partial<TRow> | null | undefined>;
|
|
11
|
+
/** Draws the details instead of the list of fields. */
|
|
12
|
+
render?: (row: TRow) => ReactNode;
|
|
13
|
+
}
|
|
14
|
+
/** A record's fields as label and value, two to a line where there is room, in the formats of the table's columns. */
|
|
15
|
+
export declare function CrudDetails<TRow extends FormValues = FormValues>({ row, columns, load, render }: CrudDetailsProps<TRow>): import("react").JSX.Element;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { iconInfoCircle, iconLoader2, InlineIcon } from "nexus-icons";
|
|
4
|
+
import { useEffect, useMemo, useState } from "react";
|
|
5
|
+
import { isAbortError } from "../../Helpers/ApiResponses.js";
|
|
6
|
+
import { cx } from "../../Helpers/ClassHelpers.js";
|
|
7
|
+
import { useBrowserTimeZone, useDatePreferences } from "../../Helpers/DatePreferences.js";
|
|
8
|
+
import { TableCellValue } from "../Viewers/DataTableParts.js";
|
|
9
|
+
/** A record's fields as label and value, two to a line where there is room, in the formats of the table's columns. */
|
|
10
|
+
export function CrudDetails({ row, columns, load, render }) {
|
|
11
|
+
const [record, setRecord] = useState(row);
|
|
12
|
+
const [state, setState] = useState(load ? "loading" : "done");
|
|
13
|
+
const preferences = useDatePreferences();
|
|
14
|
+
const timeZone = useBrowserTimeZone();
|
|
15
|
+
const options = useMemo(() => ({ preferences, timeZone: timeZone ?? undefined }), [preferences, timeZone]);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!load)
|
|
18
|
+
return;
|
|
19
|
+
const controller = new AbortController();
|
|
20
|
+
load(controller.signal).then(full => {
|
|
21
|
+
if (controller.signal.aborted)
|
|
22
|
+
return;
|
|
23
|
+
if (full)
|
|
24
|
+
setRecord(current => ({ ...current, ...full }));
|
|
25
|
+
setState("done");
|
|
26
|
+
}, error => {
|
|
27
|
+
if (controller.signal.aborted || isAbortError(error))
|
|
28
|
+
return;
|
|
29
|
+
setState("failed");
|
|
30
|
+
});
|
|
31
|
+
return () => controller.abort();
|
|
32
|
+
// The record loads once per popup.
|
|
33
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
+
}, []);
|
|
35
|
+
return (_jsxs("div", { className: "nx-crud-details", "aria-busy": state === "loading" || undefined, children: [state === "loading" && (_jsxs("p", { className: "nx-crud-details__note", role: "status", children: [_jsx(InlineIcon, { icon: iconLoader2, className: "nx-spin" }), "Loading the full record\u2026"] })), state === "failed" && (_jsxs("p", { className: "nx-crud-details__note", role: "status", children: [_jsx(InlineIcon, { icon: iconInfoCircle }), "The full record could not be loaded; these are the values from the list."] })), render ? (render(record)) : (_jsx("dl", { className: "nx-crud-details__list", children: columns.map(column => (_jsxs("div", { className: cx("nx-crud-details__item", column.wrap && "nx-crud-details__item--wide"), children: [_jsx("dt", { className: "nx-crud-details__label", children: column.label }), _jsx("dd", { className: "nx-crud-details__value", children: _jsx(TableCellValue, { column: column, row: record, options: options }) })] }, column.key))) }))] }));
|
|
36
|
+
}
|