@aglyn/plugins-crm 1.10.0 → 1.11.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aglyn/plugins-crm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"homepage": "https://aglyn.com",
|
|
6
6
|
"repository": {
|
|
@@ -25,19 +25,19 @@
|
|
|
25
25
|
"./package.json": "./package.json"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aglyn/aglyn": "1.
|
|
29
|
-
"@aglyn/shared-data-enums": "1.
|
|
30
|
-
"@aglyn/shared-data-mdi": "1.
|
|
31
|
-
"@aglyn/shared-ui-jsx": "1.
|
|
32
|
-
"@aglyn/shared-ui-next": "1.
|
|
33
|
-
"@aglyn/shared-ui-snackstack": "1.
|
|
34
|
-
"@aglyn/shared-util-email": "1.
|
|
35
|
-
"@aglyn/shared-util-http": "1.
|
|
36
|
-
"@aglyn/shared-util-timestamp": "1.
|
|
37
|
-
"@aglyn/shared-util-tools": "1.
|
|
38
|
-
"@aglyn/tenant-data-admin": "1.
|
|
39
|
-
"@aglyn/tenant-feature-instance": "1.
|
|
40
|
-
"@aglyn/tenant-runtime": "1.
|
|
28
|
+
"@aglyn/aglyn": "1.11.0",
|
|
29
|
+
"@aglyn/shared-data-enums": "1.11.0",
|
|
30
|
+
"@aglyn/shared-data-mdi": "1.11.0",
|
|
31
|
+
"@aglyn/shared-ui-jsx": "1.11.0",
|
|
32
|
+
"@aglyn/shared-ui-next": "1.11.0",
|
|
33
|
+
"@aglyn/shared-ui-snackstack": "1.11.0",
|
|
34
|
+
"@aglyn/shared-util-email": "1.11.0",
|
|
35
|
+
"@aglyn/shared-util-http": "1.11.0",
|
|
36
|
+
"@aglyn/shared-util-timestamp": "1.11.0",
|
|
37
|
+
"@aglyn/shared-util-tools": "1.11.0",
|
|
38
|
+
"@aglyn/tenant-data-admin": "1.11.0",
|
|
39
|
+
"@aglyn/tenant-feature-instance": "1.11.0",
|
|
40
|
+
"@aglyn/tenant-runtime": "1.11.0",
|
|
41
41
|
"@dnd-kit/core": "^6.3.1",
|
|
42
42
|
"@dnd-kit/utilities": "^3.2.2",
|
|
43
43
|
"@swc/helpers": "0.5.23",
|
|
@@ -20,7 +20,7 @@ import { useFirestore, useFirestoreCollection } from "@aglyn/tenant-feature-inst
|
|
|
20
20
|
import { Autocomplete, TextField } from "@mui/material";
|
|
21
21
|
import { collection, limit, orderBy, query } from "firebase/firestore";
|
|
22
22
|
import { useMemo, useState } from "react";
|
|
23
|
-
import { CRM_RECORD_COLLECTIONS, crmRecordName } from "../hooks/use-crm-record-names.js";
|
|
23
|
+
import { CRM_RECORD_COLLECTIONS, crmRecordName, useCrmRecordNames } from "../hooks/use-crm-record-names.js";
|
|
24
24
|
import { crmVisibleToClause } from "../hooks/use-crm-scope.js";
|
|
25
25
|
import { contactPrimaryGroup } from "../model/contact-record.js";
|
|
26
26
|
/**
|
|
@@ -99,19 +99,35 @@ const PICKER_PLURALS = {
|
|
|
99
99
|
]);
|
|
100
100
|
/*
|
|
101
101
|
* The linked record may sit outside the window — an old contact a record
|
|
102
|
-
* page's button linked — so the value is a placeholder
|
|
103
|
-
*
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
* page's button linked — so the value is a placeholder until (unless) the
|
|
103
|
+
* window turns out to hold it. Its name is the caller's, or one read of the
|
|
104
|
+
* record itself (AGL-4163); the kind's name stands in while that read is in
|
|
105
|
+
* flight, never the id.
|
|
106
|
+
*/ const held = value ? options.find((option)=>option.id === value) : undefined;
|
|
107
|
+
const unnamed = value && !held && !valueLabel ? value : null;
|
|
108
|
+
const names = useCrmRecordNames({
|
|
109
|
+
orgId: scope == null ? void 0 : scope[1],
|
|
110
|
+
groupId,
|
|
111
|
+
org,
|
|
112
|
+
records: unnamed ? [
|
|
113
|
+
{
|
|
114
|
+
kind,
|
|
115
|
+
id: unnamed
|
|
116
|
+
}
|
|
117
|
+
] : []
|
|
118
|
+
});
|
|
119
|
+
// Read on every render: the lookup is stable and the read lands as a re-render.
|
|
120
|
+
const placeholder = valueLabel || unnamed && names(kind, unnamed) || PICKER_LABELS[kind];
|
|
121
|
+
const selected = useMemo(()=>{
|
|
106
122
|
if (!value) return null;
|
|
107
|
-
return
|
|
123
|
+
return held != null ? held : {
|
|
108
124
|
id: value,
|
|
109
|
-
label:
|
|
125
|
+
label: placeholder
|
|
110
126
|
};
|
|
111
127
|
}, [
|
|
112
|
-
|
|
128
|
+
held,
|
|
113
129
|
value,
|
|
114
|
-
|
|
130
|
+
placeholder
|
|
115
131
|
]);
|
|
116
132
|
return /*#__PURE__*/ _jsx(Autocomplete, {
|
|
117
133
|
options: options,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/components/crm-record-picker.tsx"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n'use client'\n\nimport {\n useFirestore,\n useFirestoreCollection,\n} from '@aglyn/tenant-feature-instance'\nimport { Autocomplete, TextField } from '@mui/material'\nimport { collection, limit, orderBy, query } from 'firebase/firestore'\nimport { useMemo, useState } from 'react'\nimport {\n CRM_RECORD_COLLECTIONS,\n type CrmRecordKind,\n crmRecordName,\n} from '../hooks/use-crm-record-names'\nimport { crmVisibleToClause } from '../hooks/use-crm-scope'\nimport { contactPrimaryGroup } from '../model/contact-record'\n\n/**\n * How many records a picker offers. The window is the hundred most recently\n * touched, which is the hundred a person is most likely to be making a task\n * about; the field says so, and the record pages' own \"New task\" buttons\n * arrive with the record already linked, which is how a task about an older\n * one gets made.\n */\nexport const CRM_RECORD_PICKER_LIMIT = 100\n\nconst PICKER_LABELS: Record<CrmRecordKind, string> = {\n contact: 'Contact',\n company: 'Company',\n deal: 'Deal',\n}\n\nconst PICKER_PLURALS: Record<CrmRecordKind, string> = {\n contact: 'contacts',\n company: 'companies',\n deal: 'deals',\n}\n\nexport interface CrmRecordPickerProps {\n kind: CrmRecordKind\n scope: readonly [string, string] | null\n /** The reader's tokens, or `null` at the organization level — no clause (AGL-2630). */\n readTokens: readonly string[] | null\n /**\n * The viewing group, for a contact's facet name — or `null` at the\n * organization level, where each contact is named through its own\n * primary holder (`contactPrimaryGroup`), resolved from {@link org}.\n */\n groupId: string | null\n /** The org document, for the per-contact holder when {@link groupId} is null. */\n org?: Record<string, unknown> | null\n value: string | null\n onChange: (id: string | null) => void\n disabled?: boolean\n /** The field's label; the record kind's name by default. */\n label?: string\n /** A record the picker never offers — the record being edited, say. */\n excludeId?: string\n /**\n * The current value's name, when the caller already resolved it — shown\n * instead of the id while the window does not hold the record.\n */\n valueLabel?: string\n /**\n * Open the listener only once the menu is first opened, for a picker that\n * sits on a page most readers never change it on.\n */\n lazy?: boolean\n helperText?: string\n /**\n * Records offered first, whatever the window holds, under their own\n * {@link pinnedLabel} heading: a deal's contact roles on an activity's\n * **Name** field. Each needs its name already resolved.\n */\n pinned?: ReadonlyArray<{ id: string; label: string }>\n /** The heading over {@link pinned}. */\n pinnedLabel?: string\n}\n\n/**\n * The hundred most recently updated records of a kind this reader may see,\n * the one window every record picker offers. `enabled` holds the listener\n * shut until the picker needs it.\n */\nexport function useCrmRecordWindow(\n kind: CrmRecordKind,\n scope: readonly [string, string] | null,\n readTokens: readonly string[] | null,\n enabled = true,\n) {\n const firestore = useFirestore()\n return useFirestoreCollection<Record<string, unknown> & { $id: string }>(\n () =>\n scope && enabled\n ? query(\n collection(firestore, scope[0], scope[1], CRM_RECORD_COLLECTIONS[kind]),\n ...crmVisibleToClause(readTokens),\n orderBy('updatedAt', 'desc'),\n limit(CRM_RECORD_PICKER_LIMIT),\n )\n : null,\n [firestore, scope, readTokens, kind, enabled],\n { idField: '$id' },\n )\n}\n\n/**\n * Link a task to a contact, company or deal by name (AGL-2599).\n *\n * One query shape for all three kinds — the most recently updated hundred\n * visible to this site, filtered as the person types — because that is the\n * one shape every CRM collection has an index for (`visibleTo, updatedAt`).\n * A prefix search on the stored name would need `(visibleTo, nameLower)`,\n * which companies have and contacts do not, and a picker that searched two\n * kinds properly and one kind not at all would be worse to explain than a\n * window. The listener opens with the drawer and closes with it.\n */\nexport function CrmRecordPicker(props: CrmRecordPickerProps) {\n const { kind, scope, readTokens, groupId, org, value, onChange, disabled } = props\n const { label, excludeId, valueLabel, lazy = false, helperText } = props\n const { pinned, pinnedLabel } = props\n const [opened, setOpened] = useState(!lazy)\n const { data, status } = useCrmRecordWindow(kind, scope, readTokens, opened)\n const windowOptions = useMemo(\n () =>\n (data ?? [])\n .filter((row) => row.$id !== excludeId)\n .map((row) => ({\n id: row.$id,\n label:\n crmRecordName(\n kind,\n row,\n groupId ?? contactPrimaryGroup(row, org).groupId,\n ) || row.$id,\n }))\n .sort((a, b) => a.label.localeCompare(b.label)),\n [data, kind, groupId, org, excludeId],\n )\n // Pinned first, and each record once: `groupBy` needs a group's rows adjacent.\n const options = useMemo(() => {\n if (!pinned?.length) return windowOptions\n const held = new Set(pinned.map((option) => option.id))\n return [...pinned, ...windowOptions.filter((option) => !held.has(option.id))]\n }, [pinned, windowOptions])\n const pinnedIds = useMemo(() => new Set(pinned?.map((option) => option.id)), [pinned])\n /*\n * The linked record may sit outside the window — an old contact a record\n * page's button linked — so the value is a placeholder carrying its id\n * until (unless) the window turns out to hold it.\n */\n const selected = useMemo(() => {\n if (!value) return null\n return (\n options.find((option) => option.id === value) ?? { id: value, label: valueLabel || value }\n )\n }, [options, value, valueLabel])\n\n return (\n <Autocomplete\n options={options}\n value={selected}\n onChange={(_event, next) => onChange(next?.id ?? null)}\n onOpen={() => setOpened(true)}\n getOptionLabel={(option) => option.label}\n isOptionEqualToValue={(option, current) => option.id === current.id}\n groupBy={\n pinned?.length\n ? (option) =>\n pinnedIds.has(option.id)\n ? (pinnedLabel ?? PICKER_LABELS[kind])\n : `All ${PICKER_PLURALS[kind]}`\n : undefined\n }\n loading={opened && status === 'loading'}\n disabled={disabled}\n size=\"small\"\n renderInput={(params) => (\n <TextField\n {...params}\n label={label ?? PICKER_LABELS[kind]}\n helperText={\n options.length >= CRM_RECORD_PICKER_LIMIT\n ? `The ${CRM_RECORD_PICKER_LIMIT} most recently updated — type to narrow.`\n : helperText\n }\n />\n )}\n />\n )\n}\nCrmRecordPicker.displayName = 'CrmRecordPicker'\n\nexport default CrmRecordPicker\n"],"names":["useFirestore","useFirestoreCollection","Autocomplete","TextField","collection","limit","orderBy","query","useMemo","useState","CRM_RECORD_COLLECTIONS","crmRecordName","crmVisibleToClause","contactPrimaryGroup","CRM_RECORD_PICKER_LIMIT","PICKER_LABELS","contact","company","deal","PICKER_PLURALS","useCrmRecordWindow","kind","scope","readTokens","enabled","firestore","idField","CrmRecordPicker","props","groupId","org","value","onChange","disabled","label","excludeId","valueLabel","lazy","helperText","pinned","pinnedLabel","opened","setOpened","data","status","windowOptions","filter","row","$id","map","id","sort","a","b","localeCompare","options","length","held","Set","option","has","pinnedIds","selected","find","_event","next","onOpen","getOptionLabel","isOptionEqualToValue","current","groupBy","undefined","loading","size","renderInput","params","displayName"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD;;;AAEA,SACEA,YAAY,EACZC,sBAAsB,QACjB,iCAAgC;AACvC,SAASC,YAAY,EAAEC,SAAS,QAAQ,gBAAe;AACvD,SAASC,UAAU,EAAEC,KAAK,EAAEC,OAAO,EAAEC,KAAK,QAAQ,qBAAoB;AACtE,SAASC,OAAO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AAI;;QACjDgB,QAAQ,IAAMxB,UAAU;QACxByB,gBAAgB,CAACR,SAAWA,OAAOzB,KAAK;QACxCkC,sBAAsB,CAACT,QAAQU,UAAYV,OAAOT,EAAE,KAAKmB,QAAQnB,EAAE;QACnEoB,SACE/B,CAAAA,0BAAAA,OAAQiB,MAAM,IACV,CAACG,SACCE,UAAUD,GAAG,CAACD,OAAOT,EAAE,IAClBV,sBAAAA,cAAezB,aAAa,CAACM,KAAK,GACnC,CAAC,IAAI,EAAEF,cAAc,CAACE,KAAK,EAAE,GACnCkD;QAENC,SAAS/B,UAAUG,WAAW;QAC9BX,UAAUA;QACVwC,MAAK;QACLC,aAAa,CAACC,uBACZ,KAACxE,wBACKwE;gBACJzC,KAAK,EAAEA,gBAAAA,QAASnB,aAAa,CAACM,KAAK;gBACnCiB,YACEiB,QAAQC,MAAM,IAAI1C,0BACd,CAAC,IAAI,EAAEA,wBAAwB,wCAAwC,CAAC,GACxEwB;;;AAMhB;AACAX,gBAAgBiD,WAAW,GAAG;AAE9B,eAAejD,gBAAe"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/components/crm-record-picker.tsx"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n'use client'\n\nimport {\n useFirestore,\n useFirestoreCollection,\n} from '@aglyn/tenant-feature-instance'\nimport { Autocomplete, TextField } from '@mui/material'\nimport { collection, limit, orderBy, query } from 'firebase/firestore'\nimport { useMemo, useState } from 'react'\nimport {\n CRM_RECORD_COLLECTIONS,\n type CrmRecordKind,\n crmRecordName,\n useCrmRecordNames,\n} from '../hooks/use-crm-record-names'\nimport { crmVisibleToClause } from '../hooks/use-crm-scope'\nimport { contactPrimaryGroup } from '../model/contact-record'\n\n/**\n * How many records a picker offers. The window is the hundred most recently\n * touched, which is the hundred a person is most likely to be making a task\n * about; the field says so, and the record pages' own \"New task\" buttons\n * arrive with the record already linked, which is how a task about an older\n * one gets made.\n */\nexport const CRM_RECORD_PICKER_LIMIT = 100\n\nconst PICKER_LABELS: Record<CrmRecordKind, string> = {\n contact: 'Contact',\n company: 'Company',\n deal: 'Deal',\n}\n\nconst PICKER_PLURALS: Record<CrmRecordKind, string> = {\n contact: 'contacts',\n company: 'companies',\n deal: 'deals',\n}\n\nexport interface CrmRecordPickerProps {\n kind: CrmRecordKind\n scope: readonly [string, string] | null\n /** The reader's tokens, or `null` at the organization level — no clause (AGL-2630). */\n readTokens: readonly string[] | null\n /**\n * The viewing group, for a contact's facet name — or `null` at the\n * organization level, where each contact is named through its own\n * primary holder (`contactPrimaryGroup`), resolved from {@link org}.\n */\n groupId: string | null\n /** The org document, for the per-contact holder when {@link groupId} is null. */\n org?: Record<string, unknown> | null\n value: string | null\n onChange: (id: string | null) => void\n disabled?: boolean\n /** The field's label; the record kind's name by default. */\n label?: string\n /** A record the picker never offers — the record being edited, say. */\n excludeId?: string\n /**\n * The current value's name, when the caller already resolved it — shown\n * instead of the id while the window does not hold the record.\n */\n valueLabel?: string\n /**\n * Open the listener only once the menu is first opened, for a picker that\n * sits on a page most readers never change it on.\n */\n lazy?: boolean\n helperText?: string\n /**\n * Records offered first, whatever the window holds, under their own\n * {@link pinnedLabel} heading: a deal's contact roles on an activity's\n * **Name** field. Each needs its name already resolved.\n */\n pinned?: ReadonlyArray<{ id: string; label: string }>\n /** The heading over {@link pinned}. */\n pinnedLabel?: string\n}\n\n/**\n * The hundred most recently updated records of a kind this reader may see,\n * the one window every record picker offers. `enabled` holds the listener\n * shut until the picker needs it.\n */\nexport function useCrmRecordWindow(\n kind: CrmRecordKind,\n scope: readonly [string, string] | null,\n readTokens: readonly string[] | null,\n enabled = true,\n) {\n const firestore = useFirestore()\n return useFirestoreCollection<Record<string, unknown> & { $id: string }>(\n () =>\n scope && enabled\n ? query(\n collection(firestore, scope[0], scope[1], CRM_RECORD_COLLECTIONS[kind]),\n ...crmVisibleToClause(readTokens),\n orderBy('updatedAt', 'desc'),\n limit(CRM_RECORD_PICKER_LIMIT),\n )\n : null,\n [firestore, scope, readTokens, kind, enabled],\n { idField: '$id' },\n )\n}\n\n/**\n * Link a task to a contact, company or deal by name (AGL-2599).\n *\n * One query shape for all three kinds — the most recently updated hundred\n * visible to this site, filtered as the person types — because that is the\n * one shape every CRM collection has an index for (`visibleTo, updatedAt`).\n * A prefix search on the stored name would need `(visibleTo, nameLower)`,\n * which companies have and contacts do not, and a picker that searched two\n * kinds properly and one kind not at all would be worse to explain than a\n * window. The listener opens with the drawer and closes with it.\n */\nexport function CrmRecordPicker(props: CrmRecordPickerProps) {\n const { kind, scope, readTokens, groupId, org, value, onChange, disabled } = props\n const { label, excludeId, valueLabel, lazy = false, helperText } = props\n const { pinned, pinnedLabel } = props\n const [opened, setOpened] = useState(!lazy)\n const { data, status } = useCrmRecordWindow(kind, scope, readTokens, opened)\n const windowOptions = useMemo(\n () =>\n (data ?? [])\n .filter((row) => row.$id !== excludeId)\n .map((row) => ({\n id: row.$id,\n label:\n crmRecordName(\n kind,\n row,\n groupId ?? contactPrimaryGroup(row, org).groupId,\n ) || row.$id,\n }))\n .sort((a, b) => a.label.localeCompare(b.label)),\n [data, kind, groupId, org, excludeId],\n )\n // Pinned first, and each record once: `groupBy` needs a group's rows adjacent.\n const options = useMemo(() => {\n if (!pinned?.length) return windowOptions\n const held = new Set(pinned.map((option) => option.id))\n return [...pinned, ...windowOptions.filter((option) => !held.has(option.id))]\n }, [pinned, windowOptions])\n const pinnedIds = useMemo(() => new Set(pinned?.map((option) => option.id)), [pinned])\n /*\n * The linked record may sit outside the window — an old contact a record\n * page's button linked — so the value is a placeholder until (unless) the\n * window turns out to hold it. Its name is the caller's, or one read of the\n * record itself (AGL-4163); the kind's name stands in while that read is in\n * flight, never the id.\n */\n const held = value ? options.find((option) => option.id === value) : undefined\n const unnamed = value && !held && !valueLabel ? value : null\n const names = useCrmRecordNames({\n orgId: scope?.[1],\n groupId,\n org,\n records: unnamed ? [{ kind, id: unnamed }] : [],\n })\n // Read on every render: the lookup is stable and the read lands as a re-render.\n const placeholder = valueLabel || (unnamed && names(kind, unnamed)) || PICKER_LABELS[kind]\n const selected = useMemo(() => {\n if (!value) return null\n return held ?? { id: value, label: placeholder }\n }, [held, value, placeholder])\n\n return (\n <Autocomplete\n options={options}\n value={selected}\n onChange={(_event, next) => onChange(next?.id ?? null)}\n onOpen={() => setOpened(true)}\n getOptionLabel={(option) => option.label}\n isOptionEqualToValue={(option, current) => option.id === current.id}\n groupBy={\n pinned?.length\n ? (option) =>\n pinnedIds.has(option.id)\n ? (pinnedLabel ?? PICKER_LABELS[kind])\n : `All ${PICKER_PLURALS[kind]}`\n : undefined\n }\n loading={opened && status === 'loading'}\n disabled={disabled}\n size=\"small\"\n renderInput={(params) => (\n <TextField\n {...params}\n label={label ?? PICKER_LABELS[kind]}\n helperText={\n options.length >= CRM_RECORD_PICKER_LIMIT\n ? `The ${CRM_RECORD_PICKER_LIMIT} most recently updated — type to narrow.`\n : helperText\n }\n />\n )}\n />\n )\n}\nCrmRecordPicker.displayName = 'CrmRecordPicker'\n\nexport default CrmRecordPicker\n"],"names":["useFirestore","useFirestoreCollection","Autocomplete","TextField","collection","limit","orderBy","query","useMemo","useState","CRM_RECORD_COLLECTIONS","crmRecordName","useCrmRecordNames","crmVisibleToClause","contactPrimaryGroup","CRM_RECORD_PICKER_LIMIT","PICKER_LABELS","contact","company","deal","PICKER_PLURALS","useCrmRecordWindow","kind","scope","readTokens","enabled","firestore","idField","CrmRecordPicker","props","groupId","org","value","onChange","disabled","label","excludeId","valueLabel","lazy","helperText","pinned","pinnedLabel","opened","setOpened","data","status","windowOptions","filter","row","$id","map","id","sort","a","b","localeCompare","options","length","held","Set","option","has","pinnedIds","find","undefined","unnamed","names","orgId","records","placeholder","selected","_event","next","onOpen","getOptionLabel","isOptionEqualToValue","current","groupBy","loading","size","renderInput","params","displayName"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GACD;;;AAEA,SACEA,YAAY,EACZC,sBAAsB,QACjB,iCAAgC;AACvC,SAASC,YAAY,EAAEC,SAAS,QAAQ,gBAAe;AACvD,SAASC,UAAU,EAAEC,KAAK,EAAEC,OAAO,EAAEC,KAAK,QAAQ,qBAAoB;AACtE,SAASC,OAAO,EAAEC,QAAQ,QAAQ,QAAO;AACzC,SACEC,sBAAsB,EAEtBC,aAAa,EACbC,iBAAiB,QACZ,mCAA+B;AACtC,SAASC,kBAAkB,QAAQ,4BAAwB;AAC3D,SAASC,mBAAmB,QAAQ,6BAAyB;AAE7D;;;;;;CAMC,GACD,OAAO,MAAMC,0BAA0B,IAAG;AAE1C,MAAMC,gBAA+C;IACnDC,SAAS;IACTC,SAAS;IACTC,MAAM;AACR;AAEA,MAAMC,iBAAgD;IACpDH,SAAS;IACTC,SAAS;IACTC,MAAM;AACR;AA2CA;;;;CAIC,GACD,OAAO,SAASE,mBACdC,IAAmB,EACnBC,KAAuC,EACvCC,UAAoC,EACpCC,UAAU,IAAI;IAEd,MAAMC,YAAY1B;IAClB,OAAOC,uBACL,IACEsB,SAASE,UACLlB,MACEH,WAAWsB,WAAWH,KAAK,CAAC,EAAE,EAAEA,KAAK,CAAC,EAAE,EAAEb,sBAAsB,CAACY,KAAK,MACnET,mBAAmBW,aACtBlB,QAAQ,aAAa,SACrBD,MAAMU,4BAER,MACN;QAACW;QAAWH;QAAOC;QAAYF;QAAMG;KAAQ,EAC7C;QAAEE,SAAS;IAAM;AAErB;AAEA;;;;;;;;;;CAUC,GACD,OAAO,SAASC,gBAAgBC,KAA2B;IACzD,MAAM,EAAEP,IAAI,EAAEC,KAAK,EAAEC,UAAU,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