grafana / scenes

Build Grafana dashboards directly in your Grafana app plugins.
https://grafana.com/developers/scenes
Apache License 2.0
133 stars 20 forks source link

UrlSync: Major refactoring to simplify and make it work better across page routes (for scenes-react use case) #765

Closed torkelo closed 3 months ago

torkelo commented 3 months ago

To me merged after main scenes-react PR is merged, https://github.com/grafana/scenes/pull/734

This is not easy but I think I found a pretty good solution to some very tricky problems

Problems solved

Breaking changes

TODO

Release notes

EmbbedScene no longer has a function named initUrlSync as URL sync is now initialized via a hook exported from @grafana/scenes called useUrlSync. So to use EmbeddedScene in isolation (outside a SceneAppPage) you need to initialize URL sync yourself.

   const scene = useMemo(() => buildMyScene(), [...]);
   const isUrlSyncInitialized = useUrlSync(scene);

   if (!isUrlSyncInitialized) {
     return null;
   }

   return <scene.Component model={scene} />

A simpler way to do the above is to use the new component, UrlSyncContextProvider that is exported from the main scenes lib:

<UrlSyncContextProvider scene={scene}>
   <scene.Component model={scene} />
</UrlSyncContextProvider>  

This is only if you are using EmbeddedScene in isolation, if you are using EmbeddedScene as part of a SceneAppPage this is all handled for you by the default rendering of SceneAppPage so this change should not impact SceneAppPage usage. If trying out the the new scenes-react library the SceneContextProvider will make sure the root SceneContext is wrapped in an UrlSyncContextProvider.

📦 Published PR as canary version: 4.29.1--canary.765.9462062093.0
:sparkles: Test out this PR locally via: ```bash npm install @grafana/scenes-react@4.29.1--canary.765.9462062093.0 npm install @grafana/scenes@4.29.1--canary.765.9462062093.0 # or yarn add @grafana/scenes-react@4.29.1--canary.765.9462062093.0 yarn add @grafana/scenes@4.29.1--canary.765.9462062093.0 ```
ananasovich commented 3 months ago

Hi Torkel! I think you may have added me as a reviewer by mistake. I am a backend developer and cannot help here. Sorry for the late notice.

dprokop commented 3 months ago

~@torkelo I think I found a regression in the core Grafana. Navigating to dashboard settings does not work. Url is updated correctly, hovewer the settings view is not shown. It does work tho when the page is refreshed. The same problem appears when trying to navigate to panel edit. Url update, no effect in the ui unless refreshed.~

~Moreover, navigating between settings pages does not work, and back/forward browser actions.~

My bad, tested agains a wrong branch.

grafanabot commented 3 months ago

:rocket: PR was released in v5.0.0 :rocket: