kibana/packages
Davis McPhee d536f85005
[Discover] Support Lens fetches across tabs (#218506)
## Summary

This PR implements support for Lens chart fetches across Discover tabs,
and restoring chart state when returning to a tab.

The Lens embeddable does not currently support continuing data fetching
after it's been unmounted, or fully support restoring chart state using
previously fetched data. The Vis team is aware of this request, but in
the meantime we're using an alternative approach that keeps Lens charts
rendered in memory for each tab that's been visited at least once. This
allows fetches to run in the background and displays the resulting chart
when switching back to tabs. Doing this involved some refactoring to
both Discover and Unified Histogram:
- Create a `ChartPortalsRenderer` wrapper component in Discover to lift
chart rendering high up in the React tree and render charts into
[reverse portals](https://github.com/httptoolkit/react-reverse-portal),
ensuring charts are not remounted when switching tabs and continue to be
rendered after switching away from a tab.
- Refactor Unified Histogram from a single "container" component into
three pieces: a `UnifiedHistogramLayout` component, a
`UnifiedHistogramChart` component, and a `useUnifiedHistogram` hook to
tie things together. This approach allows us to render the chart and
hook separately (in a reverse portal) from the layout, making it
possible to separate the lifecycle of both components without keeping
the rest of Discover's components in memory after switching tabs.
- **Important note:** This change had the side effect of bloating the
Unified Histogram page load bundle since we're now exporting a static
hook that isn't dynamically imported. We could have worked around this
by getting creative with dynamic imports, but doing that seemed hacky.
Instead I decided now was a good time to split Unified Histogram out
into a package in order to support tree shaking, which also has the
added benefits of one fewer plugins to load on startup, and simplifying
the Discover async bundles.

There is one flaw with this approach: the `useDiscoverHistogram` hook
currently depends on global services for retrieving the current query
and filters (including global filters) through the `useQuerySubscriber`
hook. This means the values can become out of sync in inactive tabs when
the user modifies them in the current tab. In practice this doesn't seem
to have an effect in most cases since we don't trigger new fetches in
inactive tabs, and sync the the current tab values to the global
services when switching back to a tab. However, we should still fix this
for the MVP with an approach that doesn't leak state since the current
approach is brittle. I avoided doing that in this PR since it would
likely cause more conflicts with #217706, and that PR may introduce a
solution to the issue anyway (syncing global state to the redux store,
which we can rely on in the hook instead).

Resolves #216475.

### Checklist

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2025-05-02 13:39:25 -03:00
..
kbn-babel-preset [Security Solution] Add rule upgrade preview FE integration tests (Rule Upgrade Flyout) (#210377) 2025-05-01 11:39:17 +02:00
kbn-bazel-runner Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-capture-oas-snapshot-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-check-mappings-update-cli [Incident Management] Investigation Guide backend (#216377) 2025-05-02 10:16:50 -04:00
kbn-check-prod-native-modules-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-ci-stats-performance-metrics Disable allowAbsoluteUrls for axios (#215138) 2025-03-25 09:52:36 +01:00
kbn-ci-stats-shipper-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-cli-dev-mode Sustainable Kibana Architecture: Move modules owned by @elastic/kibana-presentation (#204843) 2024-12-23 12:32:11 -06:00
kbn-dependency-ownership Add check to fail CI if any dependencies are unowned (#206679) 2025-01-16 09:59:04 -05:00
kbn-dependency-usage SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-docs-utils Sustainable Kibana Architecture: Move modules owned by @elastic/kibana-visualizations (#202754) 2025-01-08 12:19:24 +01:00
kbn-eslint-config chore: update eslint-plugin-eui to 0.1.1 (#210082) 2025-04-02 14:06:17 +02:00
kbn-eslint-plugin-disable Add ESLINT constraints to detect inter-group dependencies (#194810) 2024-10-22 06:34:19 -05:00
kbn-eslint-plugin-eslint [Hardening] Kibana Feature API Privileges Names (#208067) 2025-02-03 15:22:29 +01:00
kbn-eslint-plugin-eui-a11y Add support for wrapping elements in eslint-plugin-eui-a11y plugin (#216339) 2025-04-09 18:16:00 +02:00
kbn-eslint-plugin-i18n Sustainable Kibana Architecture: Move modules owned by @elastic/kibana-visualizations (#202754) 2025-01-08 12:19:24 +01:00
kbn-eslint-plugin-imports SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-eslint-plugin-telemetry [chore] Reuse bundled lodash and lodash/fp dependency (#217467) 2025-04-17 14:54:25 +02:00
kbn-failed-test-reporter-cli Disable allowAbsoluteUrls for axios (#215138) 2025-03-25 09:52:36 +01:00
kbn-find-used-node-modules Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-generate SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-generate-console-definitions SKA: Categorise remaining packages (#205875) 2025-01-08 22:20:14 +01:00
kbn-import-locator Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-json-ast Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-kibana-manifest-schema SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-lint-packages-cli [codeowners] Filter kibanamachine (#199404) 2024-11-12 12:39:17 -06:00
kbn-lint-ts-projects-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-lock-manager [LockManager] Expose as package (#219220) 2025-04-29 18:42:45 +02:00
kbn-managed-vscode-config SKA: Misc cleanup and enhancements (#212207) 2025-02-24 13:31:00 +00:00
kbn-managed-vscode-config-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-manifest Sustainable Kibana Architecture: Categorise straightforward packages (#199630) 2024-11-22 10:33:25 +01:00
kbn-mock-idp-plugin [workchat] Initial FTR test setup (#216828) 2025-04-09 10:31:12 +02:00
kbn-optimizer [Discover] Support Lens fetches across tabs (#218506) 2025-05-02 13:39:25 -03:00
kbn-peggy-loader SKA: Relocate "platform" packages that remain on /packages (#208704) 2025-02-24 11:03:30 +00:00
kbn-performance-testing-dataset-extractor [CI tools] Use ES 8.x (#213056) 2025-03-05 20:26:57 +01:00
kbn-picomatcher Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-plugin-check SKA: Categorise remaining packages (#205875) 2025-01-08 22:20:14 +01:00
kbn-plugin-generator SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-plugin-helpers Clean up REACT_18=true variable (#213246) 2025-03-05 17:40:05 +01:00
kbn-relocate SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-repo-file-maps Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-repo-linter Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-repo-source-classifier SKA: Extract list of Kibana solutions into a dedicated package (#213353) 2025-03-20 10:20:07 +01:00
kbn-repo-source-classifier-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-set-map SKA: Categorise remaining packages (#205875) 2025-01-08 22:20:14 +01:00
kbn-sort-package-json Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-styled-components-mapping-cli SKA: Relocate "platform" packages that remain on /packages (#208704) 2025-02-24 11:03:30 +00:00
kbn-ts-projects Dependency usage CLI (#198920) 2024-11-25 14:07:40 +01:00
kbn-ts-type-check-cli [CI] Only emit .d.ts when running typecheck (#209259) 2025-03-18 11:48:14 +01:00
kbn-validate-next-docs-cli [ci] Work around docosaurus errors (#206097) 2025-01-09 16:33:30 -06:00
kbn-web-worker-stub Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-whereis-pkg-cli Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00
kbn-yarn-lock-validator Adds AGPL 3.0 license (#192025) 2024-09-06 19:02:41 -06:00