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] Convert remaining hooks to React Query #157254

Merged
merged 21 commits into from
May 12, 2023

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented May 10, 2023

Summary

This PR converts the remaining cases' hooks to React Query. It also adds the QueryProvider to the CasesProvider to be able to use the hooks when attaching data to a case from outside cases like ML.

Fixes: #134663

Testing

  • Verify you can add a comment successfully and the new comment markdown editor is empty after submission
  • Verify you can add attachments to a new case and an existing case
  • Add a file to a case. Verify that the entry in the user activity is being shown
  • Verify that you can push a case to an external service
  • Verify that you can push a case to an external service when creating a case
  • Verify that the loading spinner is being shown when you edit a comment in the actions of the comment
  • Verify that the loading spinner is being shown when you edit a title next to the title
  • Verify that the loading spinner is being shown when you edit a tag next to the tags section
  • Verify that the loading spinner is being shown when you edit severity next to the severity selectable
  • Verify that the status and "Sync alerts" is disabled when changing the status
  • Verify that the status and "Sync alerts" is disabled when changing the "Sync alerts"
  • Verify that the "edit pencil" is changed to a loading spinner when changing assignees
  • Verify you can update a comment
  • Verify you can update the fields of the case

Checklist

@cnasikas cnasikas added Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.9.0 labels May 10, 2023
@cnasikas cnasikas self-assigned this May 10, 2023
@cnasikas cnasikas requested a review from a team as a code owner May 10, 2023 11:20
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@cnasikas cnasikas added the release_note:skip Skip the PR/issue when compiling release notes label May 10, 2023
@@ -34,7 +34,6 @@ import { triggersActionsUiMock } from '@kbn/triggers-actions-ui-plugin/public/mo
import { registerConnectorsToMockActionRegistry } from '../../common/mock/register_connectors';
import { createStartServicesMock } from '../../common/lib/kibana/kibana_react.mock';
import { waitForComponentToUpdate } from '../../common/test_utils';
import { useCreateAttachments } from '../../containers/use_create_attachments';
Copy link
Member Author

Choose a reason for hiding this comment

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

not used anymore by the component.

@cnasikas
Copy link
Member Author

@elasticmachine merge upstream

@cnasikas cnasikas requested review from a team as code owners May 11, 2023 06:50
@kibanamachine kibanamachine requested a review from a team as a code owner May 11, 2023 06:55
Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

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

Awesome work!! Code looks much cleaner and easy to understand 🎉

I found small issue regarding loading spinner of severity.
i.e. when I update tag or assignee, I can see respective loading spinners and severity loading spinner as well.

loading_severity.mov

commentUpdate: content,
version,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

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

since we moved handling loadingCommentIds logic out of use_update_comment hook, Will we have to handle loadingCommentIds everywhere we use patchComment?
I am just wondering what will be the impact if someone uses patchComment somewhere else and doesn't add or remove loadingCommentIds.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's an excellent question and observation. I think that the use_update_comment responsibility is to update a comment and that's it. The hooks provide the isLoading, isFetching, etc status variables which should be enough. It should not care if it is being used in the user activity. On the other hand, the useUserActionsHandler is responsible for providing which comment is loading at the moment as it is the one that puts all the pieces together. Does it make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, it makes sense. Currently it's action handler's responsibility to handle comments. And if it changes in future, we can also take care of loadingCommentIds accordingly.

() => () => {
isCancelledRef.current = true;
abortCtrlRef.current.abort();
return createAttachments(attachments, request.caseId, abortCtrlRef.signal);
Copy link
Contributor

Choose a reason for hiding this comment

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

code looks so clean with react query 😍

Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

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

Awesome work!! Code looks much cleaner and easy to understand 🎉

I found small issue regarding loading spinner of severity.
i.e. when I update tag or assignee, I can see respective loading spinners and severity loading spinner as well.

loading_severity.mov

@js-jankisalvi
Copy link
Contributor

  • Verify you can add a comment successfully and the new comment markdown editor is empty after submission

I don't see same comment after submission anymore!! 😮 How did you fix this issue?

@cnasikas
Copy link
Member Author

I found small issue regarding loading spinner of severity.
i.e. when I update tag or assignee, I can see respective loading spinners and severity loading spinner as well.

Thank you @js-jankisalvi! I can reproduce the issue on main also. If you can also reproduce it on main, can you open an issue so we can address it on another PR?

@cnasikas
Copy link
Member Author

I don't see same comment after submission anymore!! 😮 How did you fix this issue?

Lol, I did not do anything. I don't know what is going on 🤷‍♀️. Maybe it needs more tries to reproduce it or our hook was buggy and somehow fixed it now.

@js-jankisalvi
Copy link
Contributor

Thank you @js-jankisalvi! I can reproduce the issue on main also. If you can also reproduce it on main, can you open an issue so we can address it on another PR?

Yes, I can reproduce the behaviour in main as well. created issue: #157364

Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

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

verified all mentioned scenarios locally, looks good 👍 🚀

@jonathan-buttner
Copy link
Contributor

  • Verify you can add a comment successfully and the new comment markdown editor is empty after submission ✅
  • Verify you can add attachments to a new case and an existing case ✅
  • Add a file to a case. Verify that the entry in the user activity is being shown ✅
  • Verify that you can push a case to an external service ✅
  • Verify that you can push a case to an external service when creating a case ✅
  • Verify that the loading spinner is being shown when you edit a comment in the actions of the comment ✅
  • Verify that the loading spinner is being shown when you edit a title next to the title ✅
  • Verify that the loading spinner is being shown when you edit a tag next to the tags section ✅
  • Verify that the loading spinner is being shown when you edit severity next to the severity selectable ✅
  • Verify that the status and "Sync alerts" is disabled when changing the status ✅
  • Verify that the status and "Sync alerts" is disabled when changing the "Sync alerts" ✅
  • Verify that the "edit pencil" is changed to a loading spinner when changing assignees ✅
  • Verify you can update a comment ✅
  • Verify you can update the fields of the case ✅

@@ -71,7 +71,7 @@ export const AddComment = React.memo(
const editorRef = useRef<EuiMarkdownEditorRef>(null);
const [focusOnContext, setFocusOnContext] = useState(false);
const { permissions, owner, appId } = useCasesContext();
const { isLoading, createAttachments } = useCreateAttachments();
const { isLoading, mutate: createAttachments } = useCreateAttachments();
Copy link
Contributor

Choose a reason for hiding this comment

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

This might have been how it was before these changes, but it seems like other hooks like useUpdateComment leverages an onError. Should the calls of createAttachments do the same?

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 think it depends on the scenario. Most of the calls to createAttachments call mutateAsync (which is of the form await createAttachments(...)) and are surrounded by a try/catch. In the scenario of adding a comment I think showing the error toaster (is being automatically by the hook) is enough. Do you have any scenarios in your mind where the onError is needed?

[]
{
mutationKey: casesMutationsKeys.pushCase,
onSuccess: (_, { connector }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to refresh anything once this succeeds?

Copy link
Member Author

Choose a reason for hiding this comment

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

The refreshing is done by the components that use the hook. But I agree, it should be done by the hook! I will change it.

},
[]
{
Copy link
Contributor

Choose a reason for hiding this comment

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

When this succeeds is the refresh of the comments done elsewhere so it gets displayed?

Copy link
Member Author

Choose a reason for hiding this comment

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

So far is being done outside of cases so navigating to cases will refresh the page. We may need to invalidate the caching keys for the case view page. I am not sure, what do you think?

Comment on lines -100 to -102
cy.get(TOASTER).should('have.text', "Created 'New connector'");
cy.get(TOASTER).should('have.text', 'Saved external connection settings');
cy.get(TOASTER).should('not.exist');
Copy link
Member Author

Choose a reason for hiding this comment

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

Once in a while, the test fails because the toaster disappears and Cypress can not find it. I removed it to reduce flakiness. We verify that the connector is selected properly which I think is enough.

@@ -86,6 +88,15 @@ describe('Cases connectors', () => {
});
});

after(() => {
cy.request({
Copy link
Member

Choose a reason for hiding this comment

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

NIT: Let's wrap this into a more readable method like deleteConnector and place it in x-pack/plugins/security_solution/cypress/tasks/common.ts :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in c046e0a

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.

Security changes LGTM. Thanks!

@@ -94,13 +100,10 @@ describe('Cases connectors', () => {

cy.wait('@createConnector').then(({ response }) => {
cy.wrap(response?.statusCode).should('eql', 200);
connectorId = response?.body.id ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: This connectorId does not seem to be used, can we remove it?

Copy link
Member Author

@cnasikas cnasikas May 12, 2023

Choose a reason for hiding this comment

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

Correct. I forgot about it. Done 6655997

@cnasikas cnasikas enabled auto-merge (squash) May 12, 2023 09:04
Copy link
Contributor

@rylnd rylnd left a comment

Choose a reason for hiding this comment

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

D&R changes LGTM

@@ -61,7 +61,8 @@ describe('timeline flyout button', () => {
cy.get(TIMELINE_BOTTOM_BAR_TOGGLE_BUTTON).should('have.focus');
});

it('the `(+)` button popover menu owns focus', () => {
// FLAKY: https://github.com/elastic/kibana/issues/153771
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the diligence here 👍

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

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 82 84 +2

Async chunks

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

id before after diff
cases 376.7KB 374.5KB -2.2KB

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
cases 31 30 -1

Page load bundle

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

id before after diff
cases 166.6KB 166.2KB -342.0B
Unknown metric groups

API count

id before after diff
cases 99 101 +2

ESLint disabled line counts

id before after diff
cases 57 55 -2
enterpriseSearch 19 21 +2
securitySolution 400 404 +4
total +4

Total ESLint disabled count

id before after diff
cases 75 73 -2
enterpriseSearch 20 22 +2
securitySolution 480 484 +4
total +4

History

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

cc @cnasikas

@cnasikas cnasikas merged commit 0bbef23 into elastic:main May 12, 2023
@cnasikas cnasikas deleted the react_query_hooks branch May 12, 2023 15:29
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label May 12, 2023
jasonrhodes pushed a commit that referenced this pull request May 17, 2023
## Summary

This PR converts the remaining cases' hooks to React Query. It also adds
the `QueryProvider` to the `CasesProvider` to be able to use the hooks
when attaching data to a case from outside cases like ML.

Fixes: #134663

## Testing

- Verify you can add a comment successfully and the new comment markdown
editor is empty after submission
- Verify you can add attachments to a new case and an existing case
- Add a file to a case. Verify that the entry in the user activity is
being shown
- Verify that you can push a case to an external service
- Verify that you can push a case to an external service when creating a
case
- Verify that the loading spinner is being shown when you edit a comment
in the actions of the comment
- Verify that the loading spinner is being shown when you edit a title
next to the title
- Verify that the loading spinner is being shown when you edit a tag
next to the tags section
- Verify that the loading spinner is being shown when you edit severity
next to the severity selectable
- Verify that the status and "Sync alerts" is disabled when changing the
status
- Verify that the status and "Sync alerts" is disabled when changing the
"Sync alerts"
- Verify that the "edit pencil" is changed to a loading spinner when
changing assignees
- Verify you can update a comment
- Verify you can update the fields of the case


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

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 Feature:Cases Cases feature release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cases] Switch all custom API hooks to React Query
9 participants