Skip to content

Commit 12d576b

Browse files
authored
Merge pull request #60 from thoughtspot/SCAL-314115-filters
SCAL-314115 filters
2 parents 29d77c5 + edc8250 commit 12d576b

19 files changed

Lines changed: 2515 additions & 0 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
VITE_THOUGHTSPOT_HOST=https://training.thoughtspot.cloud
2+
VITE_THOUGHTSPOT_USERNAME=code-sandbox
3+
VITE_THOUGHTSPOT_PASSWORD="3mbed+#3xplz"
4+
VITE_THOUGHTSPOT_LIVEBOARD_ID=6dabda7b-5392-4d47-ba26-17e8244cbcaf
5+
VITE_THOUGHTSPOT_VIZ_ID=8d2e93ad-cae8-4c8e-a364-e7966a69a41e
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
!.env
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"installDependencies": true,
3+
"startCommand": "npm run dev",
4+
"env": {
5+
"VITE_THOUGHTSPOT_HOST": "https://training.thoughtspot.cloud",
6+
"VITE_THOUGHTSPOT_USERNAME": "code-sandbox",
7+
"VITE_THOUGHTSPOT_PASSWORD": "3mbed+#3xplz",
8+
"VITE_THOUGHTSPOT_LIVEBOARD_ID": "6dabda7b-5392-4d47-ba26-17e8244cbcaf",
9+
"VITE_THOUGHTSPOT_VIZ_ID": "8d2e93ad-cae8-4c8e-a364-e7966a69a41e"
10+
}
11+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<!-- search-meta
2+
tags: [filters, parameters, runtime-filters, runtime-parameters, host-events, useHostEventsV2, subscribed-event, timing, UpdateFilters, UpdateRuntimeFilters, UpdateParameters, React, TypeScript]
3+
apis: [runtimeFilters, runtimeParameters, HostEvent, UpdateRuntimeFilters, UpdateFilters, UpdateParameters, RuntimeFilterOp, RuntimeParameter, subscribedEvent, useHostEventsV2, LiveboardEmbed, SearchEmbed, useEmbedRef, trigger, on]
4+
questions:
5+
- How do I apply filters programmatically on an embedded Liveboard or Search?
6+
- How do I update filters on a ThoughtSpot embed after it has loaded?
7+
- How do I pass runtime parameters when embedding a Liveboard?
8+
- How do I update runtime parameters on an embedded Liveboard from my app?
9+
- What payload does HostEvent.UpdateParameters expect?
10+
- Should I use runtimeFilters or HostEvent.UpdateFilters to set filters?
11+
- What is the difference between UpdateRuntimeFilters and UpdateFilters?
12+
- Why is HostEvent.UpdateFilters not firing or not working in my embed?
13+
- When is it safe to trigger a host event after the embed loads?
14+
- How do I use subscribedEvent to know a host event is ready?
15+
- Do I need useHostEventsV2 for the Subscribed embed event to fire?
16+
- How do I update runtime filters from a button in my app?
17+
-->
18+
19+
# update-filters-and-params
20+
21+
Working patterns and a decision guide for applying **filters and parameters** to embedded ThoughtSpot content programmatically — at init via `runtimeFilters` / `runtimeParameters`, and after load via the `UpdateRuntimeFilters`, `UpdateFilters`, and `UpdateParameters` host events, including the `subscribedEvent` readiness pattern (requires `useHostEventsV2: true`).
22+
23+
## Which mechanism to use
24+
25+
| Your situation | Use | Since |
26+
|---|---|---|
27+
| Filters/parameters are known when you render the embed | **`runtimeFilters`** / **`runtimeParameters`** view-config options — simplest, no events, no timing concerns | SDK 1.9.0 |
28+
| Filters change after load (button, external UI) | **`HostEvent.UpdateRuntimeFilters`** — array of `{ columnName, operator, values }` | SDK 1.9.0 / TS 8.1.0.cl |
29+
| Updating values of a filter that already exists on the Liveboard | **`HostEvent.UpdateFilters`** — `{ filters: [{ column, oper, values }] }` | SDK 1.23.0 / TS 9.4.0.cl |
30+
| Parameter values change after load | **`HostEvent.UpdateParameters`** — array of `{ name, value, isVisibleToUser? }` | SDK 1.29.0 / TS 10.1.0.cl |
31+
| You need the exact moment a host event is safe to trigger | **`embed.subscribedEvent(HostEvent.X)`** listener — **only fires when `useHostEventsV2: true` is set in the embed view config** | SDK 1.45.2+ / TS 26.3.0.cl+ |
32+
33+
Good to know:
34+
35+
- **`UpdateRuntimeFilters` resets the embedded object to its original state** before applying the new conditions — user changes such as drill-downs are cleared.
36+
- If multiple columns share a name, disambiguate with the `WORKSHEET_NAME::COLUMN_NAME` format in `UpdateFilters` (e.g. `"(Sample) Retail - Apparel::city"`).
37+
- **Common pitfall:** wiring the `Subscribed` readiness listener **without** enabling `useHostEventsV2` — the listener silently never fires. On clusters older than 26.3.0.cl, trigger from `EmbedEvent.Load`/`EmbedEvent.Data` instead, guarded so it runs once.
38+
39+
## Key Usage
40+
41+
```typescript
42+
import {
43+
AuthType,
44+
HostEvent,
45+
RuntimeFilterOp,
46+
init,
47+
type EmbedEvent,
48+
} from "@thoughtspot/visual-embed-sdk";
49+
import { LiveboardEmbed, useEmbedRef } from "@thoughtspot/visual-embed-sdk/react";
50+
import { useEffect } from "react";
51+
52+
// init() must run BEFORE any embed component mounts — call it at module scope.
53+
// (The useInit hook runs in a parent effect, which fires AFTER child effects,
54+
// so the embed can construct before init and fail with "Please provide a valid URL".)
55+
init({
56+
thoughtSpotHost: "https://your-instance.thoughtspot.cloud",
57+
authType: AuthType.None,
58+
});
59+
60+
const App = () => {
61+
const embedRef = useEmbedRef<typeof LiveboardEmbed>();
62+
63+
// AFTER LOAD — update runtime filters from your app's UI.
64+
// Note: resets the object to its original state (drills are cleared) before applying.
65+
const updateRuntimeFilters = () => {
66+
embedRef.current?.trigger(HostEvent.UpdateRuntimeFilters, [
67+
{ columnName: "state", operator: RuntimeFilterOp.EQ, values: ["michigan"] },
68+
]);
69+
};
70+
71+
// AFTER LOAD — update values of filters that already exist on the Liveboard.
72+
// Disambiguate duplicate column names with "WORKSHEET_NAME::COLUMN_NAME".
73+
const updateExistingFilters = () => {
74+
embedRef.current?.trigger(HostEvent.UpdateFilters, {
75+
filters: [{ column: "item type", oper: "IN", values: ["shoes", "boots"] }],
76+
});
77+
};
78+
79+
// AFTER LOAD — update Parameter values (SDK 1.29.0+ / TS 10.1.0.cl+).
80+
const updateParameters = () => {
81+
embedRef.current?.trigger(HostEvent.UpdateParameters, [
82+
{ name: "Discount", value: 0.25, isVisibleToUser: true },
83+
]);
84+
};
85+
86+
// READINESS — fires the moment the embedded app has registered its handler for
87+
// the host event, so triggering is guaranteed safe.
88+
// REQUIRES useHostEventsV2: true on the embed (see props below) — without the
89+
// flag this "<Event> Subscribed" signal NEVER fires.
90+
useEffect(() => {
91+
const embed = embedRef.current;
92+
if (!embed) return;
93+
const subscribed = embed.subscribedEvent(HostEvent.UpdateFilters) as EmbedEvent;
94+
const onReady = () => {
95+
embed.trigger(HostEvent.UpdateFilters, {
96+
filters: [{ column: "date", oper: "EQ", values: ["JULY", "2023"], type: "MONTH_YEAR" }],
97+
});
98+
};
99+
embed.on(subscribed, onReady);
100+
return () => {
101+
embed.off(subscribed, onReady);
102+
};
103+
}, []);
104+
105+
return (
106+
<>
107+
<button onClick={updateRuntimeFilters}>Update runtime filters</button>
108+
<button onClick={updateExistingFilters}>Update existing filters</button>
109+
<button onClick={updateParameters}>Update parameters</button>
110+
<LiveboardEmbed
111+
ref={embedRef}
112+
liveboardId="your-liveboard-id"
113+
// AT INIT — filters/parameters known upfront: pass them in the view config.
114+
// Prefer this whenever possible; no events or timing involved.
115+
runtimeFilters={[
116+
{ columnName: "state", operator: RuntimeFilterOp.EQ, values: ["california"] },
117+
]}
118+
runtimeParameters={[{ name: "Discount", value: 0.1 }]}
119+
// Required for the subscribedEvent readiness pattern (SDK 1.45.2+ / TS 26.3.0.cl+).
120+
useHostEventsV2
121+
/>
122+
</>
123+
);
124+
};
125+
```
126+
127+
## Documentation
128+
129+
- [Runtime filters](https://developers.thoughtspot.com/docs/runtime-filters)
130+
- [Runtime parameters](https://developers.thoughtspot.com/docs/runtime-parameters)
131+
- [Filter types and application layers](https://developers.thoughtspot.com/docs/filters-overview)
132+
- [Using host events](https://developers.thoughtspot.com/docs/host-events)
133+
- [HostEvent reference](https://developers.thoughtspot.com/docs/Enumeration_HostEvent)
134+
135+
## Demo
136+
137+
Open in [StackBlitz](https://stackblitz.com/github/thoughtspot/developer-examples/tree/main/visual-embed/update-filters-and-params)
138+
139+
## Run locally
140+
141+
```
142+
$ git clone https://github.com/thoughtspot/developer-examples
143+
$ cd visual-embed/update-filters-and-params
144+
```
145+
```
146+
$ npm i
147+
```
148+
```
149+
$ npm run dev
150+
```
151+
152+
### Technology labels
153+
154+
- React
155+
- Typescript
156+
- Web
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/ts-logo.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>ThoughtSpot React + Update filters and parameters</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)