forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dashboard Navigation] Simplify state management (elastic#171581)
Closes elastic#167577 ## Summary Previously, the Link embeddable used the whole redux embeddable package - however, the overall state that needs to be managed for this panel is very simple, so this ended up being overkill. This PR fixes that by adding a `useLinksAttributes` hook to replace the redux package that subscribes to changes made to the attributes instead. I also made two smaller changes in this PR: 1. Called the "Organize imports" command from VSCode on all of the touched files - this explains all of the seemingly unrelated import changes. 2. I fixed the React warning that was being thrown due to calling `setIsSaving` after the component was unmounted. ### How to Test To test number 2 above, create a by-reference Links panel and refresh the dashboard. Then, 1. Make some sort of change to the Links panel, such as re-arranging the links 2. Save the changes - note that, without the mount check, the following React error will be thrown: ![image](https://github.com/elastic/kibana/assets/8698078/88573c7b-8469-490d-83dd-5e335573aa75) 3. Now, with the mount check, this no longer happens 🎉 ### Checklist - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
a5d1810
commit be46cce
Showing
7 changed files
with
117 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
import { LinksAttributes } from '../../common/content_management'; | ||
import { LinksContext, LinksEmbeddable } from '../embeddable/links_embeddable'; | ||
|
||
export const useLinks = (): LinksEmbeddable => { | ||
const linksEmbeddable = useContext<LinksEmbeddable | null>(LinksContext); | ||
if (linksEmbeddable == null) { | ||
throw new Error('useLinks must be used inside LinksContext.'); | ||
} | ||
return linksEmbeddable!; | ||
}; | ||
|
||
export const useLinksAttributes = (): LinksAttributes | undefined => { | ||
const linksEmbeddable = useLinks(); | ||
const [attributes, setAttributes] = useState<LinksAttributes | undefined>( | ||
linksEmbeddable.attributes | ||
); | ||
|
||
useEffect(() => { | ||
const attributesSubscription = linksEmbeddable.attributes$.subscribe((newAttributes) => { | ||
setAttributes(newAttributes); | ||
}); | ||
return () => { | ||
attributesSubscription.unsubscribe(); | ||
}; | ||
}, [linksEmbeddable.attributes$]); | ||
|
||
return attributes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.