Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Move disabling features to the cases context #119864

Merged
merged 7 commits into from
Dec 2, 2021

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 29, 2021

Summary

Solutions that use Cases may want to disable some features cases provide. For example in Observability the sync of alerts is disabled. This is achieved with prop drilling. This PR enhances the code by creating a features object that is available through the CasesContext. This way in the future we can disable/enable more futures without having to pass props down to the components.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas added technical debt Improvement of the software architecture and operational architecture v8.1.0 Team:Threat Hunting:Cases labels Nov 29, 2021
@cnasikas cnasikas self-assigned this Nov 29, 2021
@cnasikas cnasikas added the release_note:skip Skip the PR/issue when compiling release notes label Nov 29, 2021
@cnasikas cnasikas force-pushed the cases_features_conf branch from 1fbe20e to 0f2432a Compare November 30, 2021 10:27
@cnasikas cnasikas requested review from semd and a team November 30, 2021 10:33
@cnasikas cnasikas marked this pull request as ready for review November 30, 2021 10:33
@cnasikas cnasikas requested a review from a team as a code owner November 30, 2021 10:33
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-threat-hunting-cases (Team:Threat Hunting:Cases)

@cnasikas
Copy link
Member Author

@elasticmachine merge upstream

Copy link
Contributor

@jonathan-buttner jonathan-buttner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sooo much cleaning than the prop drilling nice work! I left a few questions and suggestions.

Comment on lines 32 to 41
/**
* The empty object at the beginning avoids the mutation
* of the DEFAULT_FEATURES object
*/
const featuresOptions = merge({}, DEFAULT_FEATURES, features);
const [value, setValue] = useState<CasesContextStateValue>({
owner,
userCanCrud,
basePath,
features: featuresOptions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I would only suggest moving the initialization of the state inside a function so we prevent calling merge when it is not needed. Right now the merge result is only used the first render but it is computed every time, if we move it inside a function in the useState parameter React will only call it once:

Suggested change
/**
* The empty object at the beginning avoids the mutation
* of the DEFAULT_FEATURES object
*/
const featuresOptions = merge({}, DEFAULT_FEATURES, features);
const [value, setValue] = useState<CasesContextStateValue>({
owner,
userCanCrud,
basePath,
features: featuresOptions,
const [value, setValue] = useState<CasesContextStateValue>(() => ({
owner,
userCanCrud,
basePath,
features: merge({}, DEFAULT_FEATURES, features),
}));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if that is correct. The JS engine will execute the merge function but React will not gonna reset the value after the first render. I change it because it is more readable :)

Copy link
Contributor

@semd semd Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JS engine will execute the merge function but React will not gonna reset the value after the first render

exactly https://reactjs.org/docs/hooks-reference.html#lazy-initial-state

Copy link
Contributor

@semd semd Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though the last change did no difference because the initialValue parameter is still an object. The change I was suggesting is to replace the object for a function returning the object, this way the merge will only be executed in the first render.
Anyway, the merged features configs are pretty small by now, the optimization will not have a big impact. It is okay as it is 👍

Copy link
Contributor

@semd semd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in general LGTM! I left a couple of suggestions, but it is up to you @cnasikas to do it or not, I am good with how the code is now too. Thanks for the improvement 🚀

@cnasikas cnasikas enabled auto-merge (squash) December 2, 2021 15:29
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
cases 353 354 +1

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
cases 414 421 +7

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cases 300.1KB 300.0KB -120.0B
observability 360.6KB 360.5KB -7.0B
timelines 202.9KB 202.9KB +12.0B
total -115.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 85.3KB 85.4KB +112.0B
Unknown metric groups

API count

id before after diff
cases 455 463 +8

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @cnasikas

@cnasikas cnasikas merged commit 319fc9f into elastic:main Dec 2, 2021
@cnasikas cnasikas deleted the cases_features_conf branch December 2, 2021 17:11
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Dec 2, 2021
TinLe pushed a commit to TinLe/kibana that referenced this pull request Dec 22, 2021
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes technical debt Improvement of the software architecture and operational architecture v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants