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.
[Security Solution] Disable update button when editing field in rule …
…update flyout (elastic#205999) **Resolves: elastic#203912** ## Summary When editing a field in Rule Update flyout, the `Update` button should be temporarily disabled. Currently it is enabled all the time which is counterintuitive and may lead to errors. ## BEFORE https://github.com/user-attachments/assets/ef2c7580-247f-4eb1-96aa-43c9454e0e94 ## AFTER https://github.com/user-attachments/assets/02c6a3a8-861c-4d41-8dbd-52defb63f642 --------- Co-authored-by: Nikita Indik <[email protected]>
- Loading branch information
1 parent
802fdf0
commit 9891bbd
Showing
5 changed files
with
169 additions
and
44 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
70 changes: 70 additions & 0 deletions
70
...anagement_ui/components/rules_table/upgrade_prebuilt_rules_table/rule_preview_context.tsx
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,70 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { invariant } from '@formatjs/intl-utils'; | ||
import useSet from 'react-use/lib/useSet'; | ||
import React, { createContext, useContext, useEffect, useMemo } from 'react'; | ||
import usePrevious from 'react-use/lib/usePrevious'; | ||
|
||
export interface RulePreviewContextType { | ||
/** | ||
* Sets the rule is being edited in the rule upgrade flyout | ||
*/ | ||
setFieldEditing: (fieldName: string) => void; | ||
|
||
/** | ||
* Sets the rule is not being edited in the rule upgrade flyout | ||
*/ | ||
setFieldReadonly: (fieldName: string) => void; | ||
|
||
/** | ||
* Returns whether the rule is being edited in the rule upgrade flyout | ||
*/ | ||
isEditingRule: boolean; | ||
} | ||
|
||
const RulePreviewContext = createContext<RulePreviewContextType | null>(null); | ||
|
||
interface RulePreviewContextProviderProps { | ||
children: React.ReactNode; | ||
ruleId: string | undefined; | ||
} | ||
|
||
export function RulePreviewContextProvider({ children, ruleId }: RulePreviewContextProviderProps) { | ||
const [editedFields, { add, remove, reset }] = useSet<string>(); | ||
const prevRuleId = usePrevious(ruleId); | ||
|
||
useEffect(() => { | ||
if (ruleId !== prevRuleId) { | ||
reset(); | ||
} | ||
}, [reset, ruleId, prevRuleId]); | ||
|
||
const isEditingRule = editedFields.size > 0; | ||
|
||
const contextValue: RulePreviewContextType = useMemo( | ||
() => ({ | ||
isEditingRule, | ||
setFieldEditing: add, | ||
setFieldReadonly: remove, | ||
}), | ||
[isEditingRule, add, remove] | ||
); | ||
|
||
return <RulePreviewContext.Provider value={contextValue}>{children}</RulePreviewContext.Provider>; | ||
} | ||
|
||
export function useRulePreviewContext() { | ||
const context = useContext(RulePreviewContext); | ||
|
||
invariant( | ||
context !== null, | ||
'useRulePreviewContext must be used inside a RulePreviewContextProvider' | ||
); | ||
|
||
return context; | ||
} |
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