-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Conversation
1fbe20e
to
0f2432a
Compare
Pinging @elastic/security-threat-hunting-cases (Team:Threat Hunting:Cases) |
x-pack/plugins/cases/public/components/cases_context/use_cases_features.tsx
Outdated
Show resolved
Hide resolved
@elasticmachine merge upstream |
x-pack/plugins/cases/public/components/case_action_bar/index.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
x-pack/plugins/cases/public/components/case_action_bar/index.tsx
Outdated
Show resolved
Hide resolved
/** | ||
* 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, |
There was a problem hiding this comment.
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:
/** | |
* 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), | |
})); |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 👍
There was a problem hiding this 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 🚀
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Async chunks
Page load bundle
History
To update your PR or re-run it, just comment with: cc @cnasikas |
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
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 theCasesContext
. 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