Skip to content

Commit ae99639

Browse files
xyin96Xiaoyupeternandersson
authored
[plugin] add support for usePaginatedElementData (#16)
Co-authored-by: Xiaoyu <[email protected]> Co-authored-by: peternandersson <[email protected]>
1 parent 4258d9b commit ae99639

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ interface WorkbookElementColumns {
615615
616616
#### useElementData()
617617
618-
Provides the latest data values from corresponding sheet
618+
Provides the latest data values from corresponding sheet, up to 25000 values.
619619
620620
```ts
621621
function useElementData(elementId: string): WorkbookElementData;
@@ -633,6 +633,28 @@ interface WorkbookElementData {
633633
}
634634
```
635635
636+
#### usePaginatedElementData()
637+
638+
Provides the latest data values from the corresponding sheet (initially 25000), and provides a
639+
callback for fetching more data in chunks of 25000 values.
640+
641+
```ts
642+
function useElementData(elementId: string): [WorkbookElementData, () => void];
643+
```
644+
645+
Arguments
646+
647+
- `elementId : string` - A workbook element’s unique identifier.
648+
649+
Returns the row data from the specified element, and a callback for fetching
650+
more data.
651+
652+
```ts
653+
interface WorkbookElementData {
654+
[colId: string]: any[];
655+
}
656+
```
657+
636658
#### useVariable()
637659
638660
Returns a given variable's value and a setter to update that variable

src/client/initialize.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ export function initialize<T = {}>(): PluginInstance<T> {
190190
void execPromise('wb:plugin:element:unsubscribe:data', id);
191191
};
192192
},
193+
fetchMoreElementData(id) {
194+
void execPromise('wb:plugin:element:fetch-more', id);
195+
}
193196
},
194197
destroy() {
195198
Object.keys(listeners).forEach(event => delete listeners[event]);

src/react/hooks.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { useContext, useEffect, useCallback, useRef, useState } from 'react';
1+
import {
2+
useContext,
3+
useEffect,
4+
useCallback,
5+
useRef,
6+
useMemo,
7+
useState,
8+
} from 'react';
29

310
import { PluginContext } from './Context';
411
import {
@@ -81,7 +88,7 @@ export function useElementColumns(id: string): WorkbookElementColumns {
8188
}
8289

8390
/**
84-
* Provides the latest data values from corresponding sheet
91+
* Provides the latest data values from corresponding sheet (max 25_000)
8592
* @param {string} id Sheet ID to get element data from
8693
* @returns {WorkbookElementData} Element Data for corresponding sheet, if any
8794
*/
@@ -98,6 +105,33 @@ export function useElementData(id: string): WorkbookElementData {
98105
return data;
99106
}
100107

108+
/**
109+
* Provides the latest data values from corresponding sheet with a callback to
110+
* fetch more in chunks of 25_000 data points
111+
* @param {string} id Sheet ID to get element data from
112+
* @returns {WorkbookElementData} Element Data for corresponding sheet, if any
113+
*/
114+
export function usePaginatedElementData(
115+
id: string,
116+
): [WorkbookElementData, () => void] {
117+
const client = usePlugin();
118+
const [data, setData] = useState<WorkbookElementData>({});
119+
120+
const loadMore = useCallback(() => {
121+
if (id) {
122+
client.elements.fetchMoreElementData(id);
123+
}
124+
}, [id]);
125+
126+
useEffect(() => {
127+
if (id) {
128+
return client.elements.subscribeToElementData(id, setData);
129+
}
130+
}, [client, id]);
131+
132+
return [data, loadMore];
133+
}
134+
101135
/**
102136
* Provides the latest value for entire config or certain key within the config
103137
* @param {string} key Key within Plugin Config, optional

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ export interface PluginInstance<T = any> {
313313
id: string,
314314
callback: (data: WorkbookElementData) => void,
315315
): Unsubscriber;
316+
317+
/**
318+
* Ask sigma to load more data
319+
* @param {string} id Sheet ID to load more data
320+
*/
321+
fetchMoreElementData(id: string): void;
316322
};
317323

318324
/**

0 commit comments

Comments
 (0)