@vuu-ui/vuu-data-react 0.8.23 → 0.8.24-debug

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/esm/index.js CHANGED
@@ -1,2 +1,561 @@
1
- import{getFullRange as M,metadataKeys as U,WindowRange as W}from"@vuu-ui/vuu-utils";import{useCallback as h,useEffect as R,useMemo as b,useRef as g,useState as N}from"react";var{SELECTED:C}=U;function Ve({dataSource:t,renderBufferSize:e=10}){let[,n]=N(null),r=g(!0),u=g(!1),c=g(null),s=g([]),i=g({from:0,to:10}),o=b(()=>new w(M(i.current,e)),[e]),l=h(a=>{for(let p of a)o.add(p);s.current=o.data.slice(),u.current=!0},[o]),m=h(a=>{a.type==="viewport-update"&&(a.size!==void 0&&o.setRowCount(a.size),a.rows?(l(a.rows),n({})):a.size!==void 0&&(s.current=o.data.slice(),u.current=!0))},[o,l]);R(()=>()=>{c.current&&(cancelAnimationFrame(c.current),c.current=null),r.current=!1},[]);let f=h(a=>{i.current=a;let p=M(i.current,e);t.range=p,o.setRange(p.from,p.to)},[t,o,e]);return b(()=>{let{from:a,to:p}=i.current,d=M({from:a,to:p},e);t.range=d,o.setRange(d.from,d.to)},[t,o,e]),R(()=>{let{from:a,to:p}=M(i.current,e);t.subscribe({range:{from:a,to:p}},m)},[t,m,e]),R(()=>()=>{t.unsubscribe()},[t]),[s.current,o.rowCount,M(i.current,e),f]}var w=class{constructor({from:e,to:n}){this.rowCount=0;this.setRowCount=e=>{e<this.data.length&&(this.data.length=e),this.rowCount=e};this.range=new W(e,n),this.data=new Array(n-e)}add(e){let[n]=e;if(this.isWithinRange(n)){let r=n-this.range.from;this.data[r]=e,this.data[r-1]&&this.data[r-1][C]===1&&e[C]===0&&(e[C]=2),n===this.rowCount-1&&(this.data.length=r+1)}}getAtIndex(e){return this.range.isWithin(e)&&this.data[e-this.range.from]!=null?this.data[e-this.range.from]:void 0}isWithinRange(e){return this.range.isWithin(e)}setRange(e,n){if(e!==this.range.from||n!==this.range.to){let[r,u]=this.range.overlap(e,n),c=new Array(n-e);for(let s=r;s<u;s++){let i=this.getAtIndex(s);if(i){let o=s-e;c[o]=i}}this.data=c,this.range.from=e,this.range.to=n}}};import{VuuDataSource as F}from"@vuu-ui/vuu-data-remote";import{useShellContext as H}from"@vuu-ui/vuu-shell";import{buildColumnMap as K,isLookupRenderer as G,isTypeDescriptor as q}from"@vuu-ui/vuu-utils";import{useMemo as V,useState as $}from"react";var S=[],T=new Map,j=({labelColumn:t,table:e,valueColumn:n})=>{let r=`${e.module}:${e.table}`,u=T.get(r);if(u)return u;{let c=new Promise(s=>{let i=[n,t],o=K(i),l=new F({bufferSize:0,table:e});l.subscribe({columns:i,range:{from:0,to:100}},m=>{if(m.type==="viewport-update"&&m.rows){let f=m.rows.map(a=>({value:a[o[n]],label:a[o[t]]}));s(f),l.unsubscribe()}})});return T.set(r,c),c}},v=(t,e)=>{var n;return e===void 0?null:(n=t.find(r=>r.value===e))!=null?n:null},Y=({name:t,type:e})=>{if(q(e)&&G(e.renderer))return e.renderer.lookup;throw Error(`useLookupValues column ${t} is not configured to use lookup values`)},Ie=(t,e)=>{let n=Y(t),{getLookupValues:r}=H(),u=V(()=>{var l;let o=(l=r==null?void 0:r(n.table))!=null?l:S;return{initialValue:v(o,e),values:o}},[r,e,n.table]),[{initialValue:c,values:s},i]=$(u);return V(()=>{s===S&&j(n).then(o=>i({initialValue:v(o,e),values:o}))},[s,n,e]),{initialValue:c,values:s}};import{useCallback as Q,useEffect as J,useState as X}from"react";import{ConnectionManager as L}from"@vuu-ui/vuu-data-remote";var Pe=()=>{let[t,e]=X("disconnected"),n=Q(({status:r})=>{e(r)},[]);return J(()=>(L.on("connection-status",n),()=>{L.removeListener("connection-status",n)}),[n]),t};import{useCallback as Z,useEffect as B,useState as z}from"react";import{ConnectionManager as y}from"@vuu-ui/vuu-data-remote";var _e=()=>{let[t,e]=z(0),n=Z(({messages:r})=>{e(r.messagesLength)},[]);return B(()=>(y.on("connection-metrics",n),()=>{y.removeListener("connection-metrics",n)}),[n]),t};import{makeRpcCall as ee}from"@vuu-ui/vuu-data-remote";import{useCallback as te}from"react";var I={type:"RPC_CALL",service:"TypeAheadRpcHandler"},Fe=(t,e,n="",r=[])=>n!==""&&!r.includes(n.toLowerCase())?[t,e,n]:[t,e],He=()=>te(async t=>{let e=t.length===2?{method:"getUniqueFieldValues",params:t,...I}:{method:"getUniqueFieldValuesStartingWith",params:t,...I};return ee(e)},[]);import{getFilterPredicate as k}from"@vuu-ui/vuu-filter-parser";import{getRowRecord as ne,isGroupMenuItemDescriptor as re,metadataKeys as oe}from"@vuu-ui/vuu-utils";import{useCallback as D}from"react";var Ye="addRowsFromInstruments",{KEY:x}=oe,ue={},se=t=>"rpcName"in t,ae=t=>"menus"in t,ie=t=>t.name==="ROOT",P=t=>t.context==="cell",ce=t=>t.context==="row",le=t=>t.context==="selected-rows",pe=(t,e,n=0)=>{switch(t){case"grid":return e==="selected-rows"?n>0:!0;case"header":return e==="grid";default:return!1}},me=(t,e,n,r,u)=>{if(t==="cell"||t==="row")return k(u,r)(e);if(t==="selected-rows"){if(n.length===0)return!1;{let c=k(u,r);return n.every(c)}}return!0},fe=t=>{let{rpcName:e}=t;return P(t)?{field:t.field,rowKey:t.rowKey,rpcName:e,value:t.value,type:"VIEW_PORT_MENU_CELL_RPC"}:ce(t)?{rowKey:t.rowKey,row:t.row,rpcName:e,type:"VIEW_PORT_MENU_ROW_RPC"}:le(t)?{rpcName:e,type:"VIEW_PORT_MENUS_SELECT_RPC"}:{rpcName:e,type:"VIEW_PORT_MENU_TABLE_RPC"}},de=t=>["grid","header","filter"].includes(t),Me=({filter:t})=>typeof t=="string"&&t.length>0,ge=(t,e)=>{switch(t.context){case"cell":return{...t,field:e.columnName,rowKey:e.row[x],value:e.row[e.columnMap[e.columnName]]};case"row":return{...t,row:ne(e.row,e.columnMap),rowKey:e.row[x]};default:return t}},E=(t,e,n)=>{var r;return ae(t)?t.menus.some(u=>E(u,e,n)):pe(e,t.context,(r=n.selectedRows)==null?void 0:r.length)?e==="grid"&&Me(t)?me(t.context,n.row,n.selectedRows,t.filter,n.columnMap):P(t)&&t.field!=="*"?t.field===n.columnName:!0:!1},O=(t,e,n)=>{if(E(t,e,n)){if(se(t))return{label:t.name,action:"MENU_RPC_CALL",options:ge(t,n)};{let r=t.menus.map(u=>O(u,e,n)).filter(u=>u!==void 0);if(r.length>0)return{label:t.name,children:r}}}},Qe=({clientSideMenuActionHandler:t,dataSource:e,menuActionConfig:n=ue,onRpcResponse:r})=>{let u=D((s,i)=>{let{links:o,menu:l}=e,{visualLink:m}=n,f=[];if(s==="grid"&&o&&!m&&o.forEach(a=>{let{link:p,label:d}=a,_=d||p.toTable;f.push({label:`Link to ${_}`,action:"link-table",options:a})}),l&&de(s)){let a=O(l,s,i);ie(l)&&re(a)?f.push(...a.children):a&&f.push(a)}return f},[e,n]),c=D(({menuId:s,options:i})=>{if(t!=null&&t(s,i))return!0;if(s==="MENU_RPC_CALL"){let o=fe(i);return e.menuRpcCall(o).then(l=>{r&&l&&r&&r(l)}),!0}else{if(s==="link-table")return e.visualLink=i,!0;console.log(`useViewServer handleMenuAction, can't handle action type ${s}`)}return!1},[t,e,r]);return{buildViewserverMenuOptions:u,handleMenuAction:c}};import{getServerAPI as A}from"@vuu-ui/vuu-data-remote";import{useCallback as he,useEffect as Re,useState as Ce}from"react";var Be=()=>{let[t,e]=Ce(),n=he(r=>{let u=new Map;return r.forEach(c=>{u.set(c.table.table,c)}),u},[]);return Re(()=>{async function r(){console.log("GET TABLE LIST");let u=await A(),{tables:c}=await u.getTableList(),s=n(await Promise.all(c.map(i=>u.getTableSchema(i))));e(s)}r()},[n]),t},ze=t=>A().then(e=>e.getTableSchema(t));export{w as MovingWindow,Ye as addRowsFromInstruments,Fe as getTypeaheadParams,ze as getVuuTableSchema,Ve as useDataSource,Ie as useLookupValues,_e as useServerConnectionQuality,Pe as useServerConnectionStatus,He as useTypeaheadSuggestions,Qe as useVuuMenuActions,Be as useVuuTables};
1
+ // src/hooks/useDataSource.ts
2
+ import { getFullRange, metadataKeys, WindowRange } from "@vuu-ui/vuu-utils";
3
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4
+ var { SELECTED } = metadataKeys;
5
+ function useDataSource({
6
+ dataSource,
7
+ renderBufferSize = 10
8
+ }) {
9
+ const [, forceUpdate] = useState(null);
10
+ const isMounted = useRef(true);
11
+ const hasUpdated = useRef(false);
12
+ const rafHandle = useRef(null);
13
+ const data = useRef([]);
14
+ const rangeRef = useRef({ from: 0, to: 10 });
15
+ const dataWindow = useMemo(
16
+ () => new MovingWindow(getFullRange(rangeRef.current, renderBufferSize)),
17
+ [renderBufferSize]
18
+ );
19
+ const setData = useCallback(
20
+ (updates) => {
21
+ for (const row of updates) {
22
+ dataWindow.add(row);
23
+ }
24
+ data.current = dataWindow.data.slice();
25
+ hasUpdated.current = true;
26
+ },
27
+ [dataWindow]
28
+ );
29
+ const datasourceMessageHandler = useCallback(
30
+ (message) => {
31
+ if (message.type === "viewport-update") {
32
+ if (message.size !== void 0) {
33
+ dataWindow.setRowCount(message.size);
34
+ }
35
+ if (message.rows) {
36
+ setData(message.rows);
37
+ forceUpdate({});
38
+ } else if (message.size !== void 0) {
39
+ data.current = dataWindow.data.slice();
40
+ hasUpdated.current = true;
41
+ }
42
+ }
43
+ },
44
+ [dataWindow, setData]
45
+ );
46
+ useEffect(
47
+ () => () => {
48
+ if (rafHandle.current) {
49
+ cancelAnimationFrame(rafHandle.current);
50
+ rafHandle.current = null;
51
+ }
52
+ isMounted.current = false;
53
+ },
54
+ []
55
+ );
56
+ const setRange = useCallback(
57
+ (range) => {
58
+ rangeRef.current = range;
59
+ const fullRange = getFullRange(rangeRef.current, renderBufferSize);
60
+ dataSource.range = fullRange;
61
+ dataWindow.setRange(fullRange.from, fullRange.to);
62
+ },
63
+ [dataSource, dataWindow, renderBufferSize]
64
+ );
65
+ useMemo(() => {
66
+ const { from, to } = rangeRef.current;
67
+ const fullRange = getFullRange({ from, to }, renderBufferSize);
68
+ dataSource.range = fullRange;
69
+ dataWindow.setRange(fullRange.from, fullRange.to);
70
+ }, [dataSource, dataWindow, renderBufferSize]);
71
+ useEffect(() => {
72
+ const { from, to } = getFullRange(rangeRef.current, renderBufferSize);
73
+ dataSource.subscribe(
74
+ {
75
+ range: { from, to }
76
+ },
77
+ datasourceMessageHandler
78
+ );
79
+ }, [dataSource, datasourceMessageHandler, renderBufferSize]);
80
+ useEffect(
81
+ () => () => {
82
+ dataSource.unsubscribe();
83
+ },
84
+ [dataSource]
85
+ );
86
+ return [
87
+ data.current,
88
+ dataWindow.rowCount,
89
+ getFullRange(rangeRef.current, renderBufferSize),
90
+ setRange
91
+ ];
92
+ }
93
+ var MovingWindow = class {
94
+ constructor({ from, to }) {
95
+ this.rowCount = 0;
96
+ this.setRowCount = (rowCount) => {
97
+ if (rowCount < this.data.length) {
98
+ this.data.length = rowCount;
99
+ }
100
+ this.rowCount = rowCount;
101
+ };
102
+ this.range = new WindowRange(from, to);
103
+ this.data = new Array(to - from);
104
+ }
105
+ add(data) {
106
+ const [index] = data;
107
+ if (this.isWithinRange(index)) {
108
+ const internalIndex = index - this.range.from;
109
+ this.data[internalIndex] = data;
110
+ if (this.data[internalIndex - 1]) {
111
+ if (this.data[internalIndex - 1][SELECTED] === 1 && data[SELECTED] === 0) {
112
+ data[SELECTED] = 2;
113
+ }
114
+ }
115
+ if (index === this.rowCount - 1) {
116
+ this.data.length = internalIndex + 1;
117
+ }
118
+ }
119
+ }
120
+ getAtIndex(index) {
121
+ return this.range.isWithin(index) && this.data[index - this.range.from] != null ? this.data[index - this.range.from] : void 0;
122
+ }
123
+ isWithinRange(index) {
124
+ return this.range.isWithin(index);
125
+ }
126
+ setRange(from, to) {
127
+ if (from !== this.range.from || to !== this.range.to) {
128
+ const [overlapFrom, overlapTo] = this.range.overlap(from, to);
129
+ const newData = new Array(to - from);
130
+ for (let i = overlapFrom; i < overlapTo; i++) {
131
+ const data = this.getAtIndex(i);
132
+ if (data) {
133
+ const index = i - from;
134
+ newData[index] = data;
135
+ }
136
+ }
137
+ this.data = newData;
138
+ this.range.from = from;
139
+ this.range.to = to;
140
+ }
141
+ }
142
+ };
143
+
144
+ // src/hooks/useLookupValues.ts
145
+ import { VuuDataSource } from "@vuu-ui/vuu-data-remote";
146
+ import { useShellContext } from "@vuu-ui/vuu-shell";
147
+ import {
148
+ buildColumnMap,
149
+ isLookupRenderer,
150
+ isTypeDescriptor
151
+ } from "@vuu-ui/vuu-utils";
152
+ import { useMemo as useMemo2, useState as useState2 } from "react";
153
+ var NO_VALUES = [];
154
+ var lookupValueMap = /* @__PURE__ */ new Map();
155
+ var loadLookupValues = ({
156
+ labelColumn,
157
+ table,
158
+ valueColumn
159
+ }) => {
160
+ const tableKey = `${table.module}:${table.table}`;
161
+ const lookupValues = lookupValueMap.get(tableKey);
162
+ if (lookupValues) {
163
+ return lookupValues;
164
+ } else {
165
+ const promise = new Promise((resolve) => {
166
+ const columns = [valueColumn, labelColumn];
167
+ const columnMap = buildColumnMap(columns);
168
+ const dataSource = new VuuDataSource({
169
+ bufferSize: 0,
170
+ table
171
+ });
172
+ dataSource.subscribe(
173
+ {
174
+ columns,
175
+ range: { from: 0, to: 100 }
176
+ },
177
+ (message) => {
178
+ if (message.type === "viewport-update") {
179
+ if (message.rows) {
180
+ const listOptions = message.rows.map((row) => ({
181
+ value: row[columnMap[valueColumn]],
182
+ label: row[columnMap[labelColumn]]
183
+ }));
184
+ resolve(listOptions);
185
+ dataSource.unsubscribe();
186
+ }
187
+ }
188
+ }
189
+ );
190
+ });
191
+ lookupValueMap.set(tableKey, promise);
192
+ return promise;
193
+ }
194
+ };
195
+ var getSelectedOption = (values, selectedValue) => {
196
+ var _a;
197
+ if (selectedValue === void 0) {
198
+ return null;
199
+ }
200
+ return (_a = values.find((option) => option.value === selectedValue)) != null ? _a : null;
201
+ };
202
+ var getLookupDetails = ({ name, type }) => {
203
+ if (isTypeDescriptor(type) && isLookupRenderer(type.renderer)) {
204
+ return type.renderer.lookup;
205
+ } else {
206
+ throw Error(
207
+ `useLookupValues column ${name} is not configured to use lookup values`
208
+ );
209
+ }
210
+ };
211
+ var useLookupValues = (column, initialValueProp) => {
212
+ const lookupDetails = getLookupDetails(column);
213
+ const { getLookupValues } = useShellContext();
214
+ const initialState = useMemo2(() => {
215
+ var _a;
216
+ const values2 = (_a = getLookupValues == null ? void 0 : getLookupValues(lookupDetails.table)) != null ? _a : NO_VALUES;
217
+ return {
218
+ initialValue: getSelectedOption(values2, initialValueProp),
219
+ values: values2
220
+ };
221
+ }, [getLookupValues, initialValueProp, lookupDetails.table]);
222
+ const [{ initialValue, values }, setLookupState] = useState2(initialState);
223
+ useMemo2(() => {
224
+ if (values === NO_VALUES) {
225
+ loadLookupValues(lookupDetails).then(
226
+ (values2) => setLookupState({
227
+ initialValue: getSelectedOption(values2, initialValueProp),
228
+ values: values2
229
+ })
230
+ );
231
+ }
232
+ }, [values, lookupDetails, initialValueProp]);
233
+ return {
234
+ initialValue,
235
+ values
236
+ };
237
+ };
238
+
239
+ // src/hooks/useServerConnectionStatus.ts
240
+ import { useCallback as useCallback2, useEffect as useEffect2, useState as useState3 } from "react";
241
+ import { ConnectionManager } from "@vuu-ui/vuu-data-remote";
242
+ var useServerConnectionStatus = () => {
243
+ const [connectionStatus, setConnectionStatus] = useState3("disconnected");
244
+ const handleStatusChange = useCallback2(
245
+ ({ status }) => {
246
+ setConnectionStatus(status);
247
+ },
248
+ []
249
+ );
250
+ useEffect2(() => {
251
+ ConnectionManager.on("connection-status", handleStatusChange);
252
+ return () => {
253
+ ConnectionManager.removeListener("connection-status", handleStatusChange);
254
+ };
255
+ }, [handleStatusChange]);
256
+ return connectionStatus;
257
+ };
258
+
259
+ // src/hooks/useServerConnectionQuality.ts
260
+ import { useCallback as useCallback3, useEffect as useEffect3, useState as useState4 } from "react";
261
+ import { ConnectionManager as ConnectionManager2 } from "@vuu-ui/vuu-data-remote";
262
+ var useServerConnectionQuality = () => {
263
+ const [messagesPerSecond, setMessagesPerSecond] = useState4(0);
264
+ const handleConnectivityMessage = useCallback3(({ messages }) => {
265
+ setMessagesPerSecond(messages.messagesLength);
266
+ }, []);
267
+ useEffect3(() => {
268
+ ConnectionManager2.on("connection-metrics", handleConnectivityMessage);
269
+ return () => {
270
+ ConnectionManager2.removeListener(
271
+ "connection-metrics",
272
+ handleConnectivityMessage
273
+ );
274
+ };
275
+ }, [handleConnectivityMessage]);
276
+ return messagesPerSecond;
277
+ };
278
+
279
+ // src/hooks/useTypeaheadSuggestions.ts
280
+ import { makeRpcCall } from "@vuu-ui/vuu-data-remote";
281
+ import { useCallback as useCallback4 } from "react";
282
+ var TYPEAHEAD_MESSAGE_CONSTANTS = {
283
+ type: "RPC_CALL",
284
+ service: "TypeAheadRpcHandler"
285
+ };
286
+ var getTypeaheadParams = (table, column, text = "", selectedValues = []) => {
287
+ if (text !== "" && !selectedValues.includes(text.toLowerCase())) {
288
+ return [table, column, text];
289
+ }
290
+ return [table, column];
291
+ };
292
+ var useTypeaheadSuggestions = () => useCallback4(async (params) => {
293
+ const rpcMessage = params.length === 2 ? {
294
+ method: "getUniqueFieldValues",
295
+ params,
296
+ ...TYPEAHEAD_MESSAGE_CONSTANTS
297
+ } : {
298
+ method: "getUniqueFieldValuesStartingWith",
299
+ params,
300
+ ...TYPEAHEAD_MESSAGE_CONSTANTS
301
+ };
302
+ return makeRpcCall(rpcMessage);
303
+ }, []);
304
+
305
+ // src/hooks/useVuuMenuActions.ts
306
+ import { getFilterPredicate } from "@vuu-ui/vuu-filter-parser";
307
+ import {
308
+ getRowRecord,
309
+ isGroupMenuItemDescriptor,
310
+ metadataKeys as metadataKeys2
311
+ } from "@vuu-ui/vuu-utils";
312
+ import { useCallback as useCallback5 } from "react";
313
+ var addRowsFromInstruments = "addRowsFromInstruments";
314
+ var { KEY } = metadataKeys2;
315
+ var NO_CONFIG = {};
316
+ var isMenuItem = (menu) => "rpcName" in menu;
317
+ var isGroupMenuItem = (menu) => "menus" in menu;
318
+ var isRoot = (menu) => menu.name === "ROOT";
319
+ var isCellMenu = (options) => options.context === "cell";
320
+ var isRowMenu = (options) => options.context === "row";
321
+ var isSelectionMenu = (options) => options.context === "selected-rows";
322
+ var vuuContextCompatibleWithTableLocation = (uiLocation, vuuContext, selectedRowCount = 0) => {
323
+ switch (uiLocation) {
324
+ case "grid":
325
+ if (vuuContext === "selected-rows") {
326
+ return selectedRowCount > 0;
327
+ } else {
328
+ return true;
329
+ }
330
+ case "header":
331
+ return vuuContext === "grid";
332
+ default:
333
+ return false;
334
+ }
335
+ };
336
+ var gridRowMeetsFilterCriteria = (context, row, selectedRows, filter, columnMap) => {
337
+ if (context === "cell" || context === "row") {
338
+ const filterPredicate = getFilterPredicate(columnMap, filter);
339
+ return filterPredicate(row);
340
+ } else if (context === "selected-rows") {
341
+ if (selectedRows.length === 0) {
342
+ return false;
343
+ } else {
344
+ const filterPredicate = getFilterPredicate(columnMap, filter);
345
+ return selectedRows.every(filterPredicate);
346
+ }
347
+ }
348
+ return true;
349
+ };
350
+ var getMenuRpcRequest = (options) => {
351
+ const { rpcName } = options;
352
+ if (isCellMenu(options)) {
353
+ return {
354
+ field: options.field,
355
+ rowKey: options.rowKey,
356
+ rpcName,
357
+ value: options.value,
358
+ type: "VIEW_PORT_MENU_CELL_RPC"
359
+ };
360
+ } else if (isRowMenu(options)) {
361
+ return {
362
+ rowKey: options.rowKey,
363
+ row: options.row,
364
+ rpcName,
365
+ type: "VIEW_PORT_MENU_ROW_RPC"
366
+ };
367
+ } else if (isSelectionMenu(options)) {
368
+ return {
369
+ rpcName,
370
+ type: "VIEW_PORT_MENUS_SELECT_RPC"
371
+ };
372
+ } else {
373
+ return {
374
+ rpcName,
375
+ type: "VIEW_PORT_MENU_TABLE_RPC"
376
+ };
377
+ }
378
+ };
379
+ var isTableLocation = (location) => ["grid", "header", "filter"].includes(location);
380
+ var hasFilter = ({ filter }) => typeof filter === "string" && filter.length > 0;
381
+ var getMenuItemOptions = (menu, options) => {
382
+ switch (menu.context) {
383
+ case "cell":
384
+ return {
385
+ ...menu,
386
+ field: options.columnName,
387
+ rowKey: options.row[KEY],
388
+ value: options.row[options.columnMap[options.columnName]]
389
+ };
390
+ case "row":
391
+ return {
392
+ ...menu,
393
+ row: getRowRecord(options.row, options.columnMap),
394
+ rowKey: options.row[KEY]
395
+ };
396
+ default:
397
+ return menu;
398
+ }
399
+ };
400
+ var menuShouldBeRenderedInThisContext = (menuItem, tableLocation, options) => {
401
+ var _a;
402
+ if (isGroupMenuItem(menuItem)) {
403
+ return menuItem.menus.some(
404
+ (childMenu) => menuShouldBeRenderedInThisContext(childMenu, tableLocation, options)
405
+ );
406
+ }
407
+ if (!vuuContextCompatibleWithTableLocation(
408
+ tableLocation,
409
+ menuItem.context,
410
+ (_a = options.selectedRows) == null ? void 0 : _a.length
411
+ )) {
412
+ return false;
413
+ }
414
+ if (tableLocation === "grid" && hasFilter(menuItem)) {
415
+ return gridRowMeetsFilterCriteria(
416
+ menuItem.context,
417
+ options.row,
418
+ options.selectedRows,
419
+ menuItem.filter,
420
+ options.columnMap
421
+ );
422
+ }
423
+ if (isCellMenu(menuItem) && menuItem.field !== "*") {
424
+ return menuItem.field === options.columnName;
425
+ }
426
+ return true;
427
+ };
428
+ var buildMenuDescriptor = (menu, tableLocation, options) => {
429
+ if (menuShouldBeRenderedInThisContext(menu, tableLocation, options)) {
430
+ if (isMenuItem(menu)) {
431
+ return {
432
+ label: menu.name,
433
+ action: "MENU_RPC_CALL",
434
+ options: getMenuItemOptions(menu, options)
435
+ };
436
+ } else {
437
+ const children = menu.menus.map(
438
+ (childMenu) => buildMenuDescriptor(childMenu, tableLocation, options)
439
+ ).filter(
440
+ (childMenu) => childMenu !== void 0
441
+ );
442
+ if (children.length > 0) {
443
+ return {
444
+ label: menu.name,
445
+ children
446
+ };
447
+ }
448
+ }
449
+ }
450
+ };
451
+ var useVuuMenuActions = ({
452
+ clientSideMenuActionHandler,
453
+ dataSource,
454
+ menuActionConfig = NO_CONFIG,
455
+ onRpcResponse
456
+ }) => {
457
+ const buildViewserverMenuOptions = useCallback5(
458
+ (location, options) => {
459
+ const { links, menu } = dataSource;
460
+ const { visualLink } = menuActionConfig;
461
+ const descriptors = [];
462
+ if (location === "grid" && links && !visualLink) {
463
+ links.forEach((linkDescriptor) => {
464
+ const { link, label: linkLabel } = linkDescriptor;
465
+ const label = linkLabel ? linkLabel : link.toTable;
466
+ descriptors.push({
467
+ label: `Link to ${label}`,
468
+ action: "link-table",
469
+ options: linkDescriptor
470
+ });
471
+ });
472
+ }
473
+ if (menu && isTableLocation(location)) {
474
+ const menuDescriptor = buildMenuDescriptor(
475
+ menu,
476
+ location,
477
+ options
478
+ );
479
+ if (isRoot(menu) && isGroupMenuItemDescriptor(menuDescriptor)) {
480
+ descriptors.push(...menuDescriptor.children);
481
+ } else if (menuDescriptor) {
482
+ descriptors.push(menuDescriptor);
483
+ }
484
+ }
485
+ return descriptors;
486
+ },
487
+ [dataSource, menuActionConfig]
488
+ );
489
+ const handleMenuAction = useCallback5(
490
+ ({ menuId, options }) => {
491
+ if (clientSideMenuActionHandler == null ? void 0 : clientSideMenuActionHandler(menuId, options)) {
492
+ return true;
493
+ } else if (menuId === "MENU_RPC_CALL") {
494
+ const rpcRequest = getMenuRpcRequest(options);
495
+ dataSource.menuRpcCall(rpcRequest).then((rpcResponse) => {
496
+ if (onRpcResponse && rpcResponse) {
497
+ onRpcResponse && onRpcResponse(rpcResponse);
498
+ }
499
+ });
500
+ return true;
501
+ } else if (menuId === "link-table") {
502
+ return dataSource.visualLink = options, true;
503
+ } else {
504
+ console.log(
505
+ `useViewServer handleMenuAction, can't handle action type ${menuId}`
506
+ );
507
+ }
508
+ return false;
509
+ },
510
+ [clientSideMenuActionHandler, dataSource, onRpcResponse]
511
+ );
512
+ return {
513
+ buildViewserverMenuOptions,
514
+ handleMenuAction
515
+ };
516
+ };
517
+
518
+ // src/hooks/useVuuTables.ts
519
+ import { getServerAPI } from "@vuu-ui/vuu-data-remote";
520
+ import { useCallback as useCallback6, useEffect as useEffect4, useState as useState5 } from "react";
521
+ var useVuuTables = () => {
522
+ const [tables, setTables] = useState5();
523
+ const buildTables = useCallback6((schemas) => {
524
+ const vuuTables = /* @__PURE__ */ new Map();
525
+ schemas.forEach((schema) => {
526
+ vuuTables.set(schema.table.table, schema);
527
+ });
528
+ return vuuTables;
529
+ }, []);
530
+ useEffect4(() => {
531
+ async function fetchTableMetadata() {
532
+ console.log("GET TABLE LIST");
533
+ const server = await getServerAPI();
534
+ const { tables: tables2 } = await server.getTableList();
535
+ const tableSchemas = buildTables(
536
+ await Promise.all(
537
+ tables2.map((vuuTable) => server.getTableSchema(vuuTable))
538
+ )
539
+ );
540
+ setTables(tableSchemas);
541
+ }
542
+ fetchTableMetadata();
543
+ }, [buildTables]);
544
+ return tables;
545
+ };
546
+ var getVuuTableSchema = (table) => getServerAPI().then((server) => server.getTableSchema(table));
547
+ export {
548
+ MovingWindow,
549
+ addRowsFromInstruments,
550
+ getSelectedOption,
551
+ getTypeaheadParams,
552
+ getVuuTableSchema,
553
+ useDataSource,
554
+ useLookupValues,
555
+ useServerConnectionQuality,
556
+ useServerConnectionStatus,
557
+ useTypeaheadSuggestions,
558
+ useVuuMenuActions,
559
+ useVuuTables
560
+ };
2
561
  //# sourceMappingURL=index.js.map