Skip to content

Commit

Permalink
[SIG-55948] Plugin Actions Support: Added custom hook for action regi…
Browse files Browse the repository at this point in the history
…stration and triggering (#10)

Co-authored-by: Alison Lai <[email protected]>
  • Loading branch information
alisonjlai and Alison Lai authored Jun 28, 2024
1 parent 4258d9b commit 4c3ca62
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ type CustomPluginConfigOptions =
type: 'interaction';
name: string;
label?: string;
}
| {
type: 'action-trigger';
name: string;
label?: string;
};
```
Expand Down Expand Up @@ -376,6 +381,10 @@ Additional Fields
A configurable workbook interaction to interact with other charts within your workbook
**Action Trigger**
A configurable action trigger to trigger actions in other elements within your workbook
#### PluginInstance
```ts
Expand Down Expand Up @@ -437,6 +446,11 @@ interface PluginInstance<T> {
selection: WorkbookSelection[],
): void;
/**
* Triggers an action based on the provided action trigger Id
*/
triggerAction(id: string): void;
/**
* Overrider function for Config Ready state
*/
Expand Down Expand Up @@ -677,6 +691,24 @@ The returned setter function accepts an array of workbook selection elements
function setVariableCallback(value: WorkbookSelection[]): void;
```
#### useActionTrigger()
Returns a callback function to trigger one or more action effects for a given action trigger
```ts
function useActionTrigger(triggerId: string);
```
Arguments
- `triggerId : string` - The ID of the action trigger
The function that can be called to trigger the action
```ts
function triggerActionCallback(): void;
```
#### useConfig()
Returns the workbook element’s current configuration. If a key is provided, only
Expand Down
12 changes: 12 additions & 0 deletions src/client/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function initialize<T = {}>(): PluginInstance<T> {

let subscribedInteractions: Record<string, WorkbookSelection[]> = {};
let subscribedWorkbookVars: Record<string, WorkbookVariable> = {};
const registeredActionTriggers = new Set<string>();

const listeners: {
[event: string]: Function[];
Expand Down Expand Up @@ -135,7 +136,18 @@ export function initialize<T = {}>(): PluginInstance<T> {
) {
void execPromise('wb:plugin:selection:set', id, elementId, selection);
},
triggerAction(id: string) {
if (!registeredActionTriggers.has(id)) {
throw new Error(`Invalid action trigger ID: ${id}`);
}
void execPromise('wb:plugin:action-trigger:invoke', id);
},
configureEditorPanel(options) {
registeredActionTriggers.clear();
for (const option of options) {
if (option.type !== 'action-trigger') continue;
registeredActionTriggers.add(option.name);
}
void execPromise('wb:plugin:config:inspector', options);
},
setLoadingState(loadingState) {
Expand Down
14 changes: 14 additions & 0 deletions src/react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,17 @@ export function useInteraction(

return [workbookInteraction, setInteraction];
}

/**
* React hook for returning a triggering callback function for the registered
* action trigger
* @param {string} id ID of action trigger
* @returns {Function} A callback function to trigger the action
*/
export function useActionTrigger(id: string) {
const client = usePlugin();

return useCallback(() => {
client.config.triggerAction(id);
}, [client, id]);
}
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ export type CustomPluginConfigOptions =
type: 'interaction';
name: string;
label?: string;
}
| {
type: 'action-trigger';
name: string;
label?: string;
};

/**
Expand Down Expand Up @@ -255,6 +260,12 @@ export interface PluginInstance<T = any> {
selection: WorkbookSelection[],
): void;

/**
* Triggers an action based on the provided action trigger Id
* @param {string} id ID from action-trigger type in Plugin Config
*/
triggerAction(id: string): void;

/**
* Overrider function for Config Ready state
* @param {boolean} loadingState Boolean representing if Plugin Config is still loading
Expand Down

0 comments on commit 4c3ca62

Please sign in to comment.