Skip to content

[SIEM] Replace WithSource with useWithSource hook#68722

Merged
patrykkopycinski merged 13 commits intoelastic:masterfrom
patrykkopycinski:chore/refactor-with-source-hook
Jun 25, 2020
Merged

[SIEM] Replace WithSource with useWithSource hook#68722
patrykkopycinski merged 13 commits intoelastic:masterfrom
patrykkopycinski:chore/refactor-with-source-hook

Conversation

@patrykkopycinski
Copy link
Copy Markdown
Contributor

@patrykkopycinski patrykkopycinski commented Jun 9, 2020

Summary

Refactor WithSource that is using render props pattern to useWithSource hooks based solution to make the code cleaner and avoid unnecessary re-renders.

Checklist

@patrykkopycinski patrykkopycinski added Team:SIEM v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.9.0 labels Jun 9, 2020
@patrykkopycinski patrykkopycinski self-assigned this Jun 9, 2020
…r-with-source-hook

# Conflicts:
#	x-pack/plugins/security_solution/public/alerts/pages/detection_engine/rules/details/index.tsx
@patrykkopycinski patrykkopycinski marked this pull request as ready for review June 10, 2020 22:39
@patrykkopycinski patrykkopycinski requested review from a team as code owners June 10, 2020 22:39
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/siem (Team:SIEM)

…r-with-source-hook

# Conflicts:
#	x-pack/plugins/security_solution/public/alerts/pages/detection_engine/rules/details/index.tsx
@patrykkopycinski
Copy link
Copy Markdown
Contributor Author

@elasticmachine merge upstream

elasticmachine and others added 3 commits June 16, 2020 11:30
…r-with-source-hook

# Conflicts:
#	x-pack/plugins/security_solution/public/alerts/pages/detection_engine/detection_engine.test.tsx
#	x-pack/plugins/security_solution/public/alerts/pages/detection_engine/detection_engine.tsx
#	x-pack/plugins/security_solution/public/alerts/pages/detection_engine/rules/details/index.tsx
#	x-pack/plugins/security_solution/public/app/home/index.tsx
#	x-pack/plugins/security_solution/public/common/components/header_global/__snapshots__/index.test.tsx.snap
#	x-pack/plugins/security_solution/public/common/components/header_global/index.test.tsx
#	x-pack/plugins/security_solution/public/common/components/header_global/index.tsx
#	x-pack/plugins/security_solution/public/network/pages/ip_details/__snapshots__/index.test.tsx.snap
#	x-pack/plugins/security_solution/public/network/pages/ip_details/index.test.tsx
#	x-pack/plugins/security_solution/public/network/pages/ip_details/index.tsx
#	x-pack/plugins/security_solution/public/overview/pages/overview.tsx
@XavierM
Copy link
Copy Markdown
Contributor

XavierM commented Jun 24, 2020

@elasticmachine merge upstream

Copy link
Copy Markdown
Contributor

@XavierM XavierM left a comment

Choose a reason for hiding this comment

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

Let's talk about my two comments, I am not 100% sure anymore about my first comment but I am pretty sure that's why we did it like that but it might not be valid anymore.

After the implementation looks good to me, I will fully test and ask someone in the alert team to test it to make sure we are not missing something over there.

);

export const getBrowserFields = memoizeOne(
(_title: string, fields: IndexField[]): BrowserFields =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think _title arguments was there to make the memoization simpler and faster than an object with a different reference


export const useWithSource = (sourceId = 'default', indexToAdd?: string[] | null) => {
const [configIndex] = useUiSetting$<string[]>(DEFAULT_INDEX_KEY);
const [loading, updateLoading] = useState(false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

something that we learned, it is better to create a complex state object than multiple states. With only one state, we will avoid a lot of re-render. We discover that in the detection development and we refactor most of api hooks to manage with one state.

}

useEffect(() => {
const abortCtrl = new AbortController();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we are missing something fundamental here, to avoid to update the state if the promise has been canceled.

facebook/react#14326 (comment) or like here in our code https://github.com/elastic/kibana/blob/master/x-pack/plugins/security_solution/public/alerts/containers/detection_engine/alerts/use_privilege_user.tsx#L40

},
(error) => {
updateLoading(false);
setState((prevState) => ({ ...prevState, errorMessage: error.message }));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!


return { indicesExist, browserFields, indexPattern, loading, errorMessage };
async function fetchSource() {
updateLoading(true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit -> I think loading can belong to UseWithSourceState

Copy link
Copy Markdown
Contributor

@XavierM XavierM left a comment

Choose a reason for hiding this comment

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

I tested locally and I did not see issues with our browser fields and/or KQL bar. I really do like what you did for us.

Please make sure to resolve https://github.com/elastic/kibana/pull/68722/files#r445265906 before merging.

@kibanamachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Build metrics

✅ unchanged

History

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

@patrykkopycinski patrykkopycinski merged commit ef496ff into elastic:master Jun 25, 2020
@patrykkopycinski patrykkopycinski deleted the chore/refactor-with-source-hook branch June 25, 2020 16:08
patrykkopycinski added a commit to patrykkopycinski/kibana that referenced this pull request Jun 25, 2020
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jun 25, 2020
* master: (90 commits)
  [Encrypted Saved Objects] Adds support for migrations in ESO (elastic#69513)
  [SIEM] Replace WithSource with useWithSource hook (elastic#68722)
  [Endpoint]EMT-451: add ability to filter endpoint metadata based on presence of unenrolled events (elastic#69708)
  rename old siem kibana config to securitySolution (elastic#69874)
  Remove unused Resolver code (elastic#69914)
  [Observability] Fixing dynamic return type based on the appName (elastic#69894)
  [SECURITY SOLUTION][INGEST] Task/endpoint list tests (elastic#69419)
  Fixes special clicks and 3rd party icon sizes in nav (elastic#69767)
  [APM] Catch annotations index permission error and log warning (elastic#69881)
  [Endpoint][Ingest Manager] minor code cleanup (elastic#69844)
  [Logs UI] Logs ui context menu (elastic#69915)
  Index pattern serialize and de-serialize (elastic#68844)
  [QA] Unskip functional tests (elastic#69760)
  [SIEM][Detection Engine] - Update DE to work with new exceptions schema (elastic#69715)
  Fixes elastic#69639: Ignore url.url fields above 2048 characters (elastic#69863)
  PR: Provide limit warnings to user when API limits are reached. (elastic#69590)
  [Maps] Remove broken button (elastic#69853)
  Makes usage collection methods available on start (elastic#69836)
  [SIEM][CASE] Improve Jira's labelling (elastic#69892)
  [Logs UI] Access ML via the programmatic plugin API (elastic#68905)
  ...
gmmorris added a commit to gmmorris/kibana that referenced this pull request Jun 26, 2020
* master:
  [Encrypted Saved Objects] Adds support for migrations in ESO (elastic#69513)
  [SIEM] Replace WithSource with useWithSource hook (elastic#68722)
  [Endpoint]EMT-451: add ability to filter endpoint metadata based on presence of unenrolled events (elastic#69708)
  rename old siem kibana config to securitySolution (elastic#69874)
  Remove unused Resolver code (elastic#69914)
  [Observability] Fixing dynamic return type based on the appName (elastic#69894)
  [SECURITY SOLUTION][INGEST] Task/endpoint list tests (elastic#69419)
  Fixes special clicks and 3rd party icon sizes in nav (elastic#69767)
  [APM] Catch annotations index permission error and log warning (elastic#69881)
  [Endpoint][Ingest Manager] minor code cleanup (elastic#69844)
  [Logs UI] Logs ui context menu (elastic#69915)
  Index pattern serialize and de-serialize (elastic#68844)
@MindyRS MindyRS added the Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. label Sep 23, 2021
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release_note:skip Skip the PR/issue when compiling release notes Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:SIEM v7.9.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants