Skip to content

Commit

Permalink
Sync status filtering with urlbar (#131523)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mgiota and kibanamachine authored May 5, 2022
1 parent 8942cba commit 3545f31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
22 changes: 13 additions & 9 deletions x-pack/plugins/observability/public/pages/rules/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { FormattedMessage } from '@kbn/i18n-react';
import {
deleteRules,
RuleTableItem,
RuleStatus,
enableRule,
disableRule,
snoozeRule,
useLoadRuleTypes,
RuleStatus,
unsnoozeRule,
} from '@kbn/triggers-actions-ui-plugin/public';
import { RuleExecutionStatus, ALERTS_FEATURE_ID } from '@kbn/alerting-plugin/common';
Expand Down Expand Up @@ -83,7 +83,6 @@ function RulesPage() {
application: { capabilities },
notifications: { toasts },
} = useKibana().services;
const { lastResponse, setLastResponse } = useRulesPageStateContainer();
const documentationLink = docLinks.links.observability.createAlerts;
const ruleTypeRegistry = triggersActionsUi.ruleTypeRegistry;
const canExecuteActions = hasExecuteActionsCapability(capabilities);
Expand All @@ -94,8 +93,9 @@ function RulesPage() {
});
const [inputText, setInputText] = useState<string | undefined>();
const [searchText, setSearchText] = useState<string | undefined>();
const [ruleStatusesFilter, setRuleStatusesFilter] = useState<RuleStatus[]>([]);
const [typesFilter, setTypesFilter] = useState<string[]>([]);
const { lastResponse, setLastResponse } = useRulesPageStateContainer();
const { status, setStatus } = useRulesPageStateContainer();
const [currentRuleToEdit, setCurrentRuleToEdit] = useState<RuleTableItem | null>(null);
const [rulesToDelete, setRulesToDelete] = useState<string[]>([]);
const [createRuleFlyoutVisibility, setCreateRuleFlyoutVisibility] = useState(false);
Expand All @@ -111,7 +111,7 @@ function RulesPage() {
const { rulesState, setRulesState, reload, noData, initialLoad } = useFetchRules({
searchText,
ruleLastResponseFilter: lastResponse,
ruleStatusesFilter,
ruleStatusesFilter: status,
typesFilter,
page,
setPage,
Expand Down Expand Up @@ -289,6 +289,13 @@ function RulesPage() {
[]
);

const setRuleStatusFilter = useCallback(
(ids: RuleStatus[]) => {
setStatus(ids);
},
[setStatus]
);

const setExecutionStatusFilter = useCallback(
(ids: string[]) => {
setLastResponse(ids);
Expand All @@ -311,9 +318,6 @@ function RulesPage() {
return <CenterJustifiedSpinner />;
}

// const nextSearchParams = new URLSearchParams(history.location.search);
// const xx = [...nextSearchParams.getAll('executionStatus')] || [];
// console.log(xx, '!!');
return (
<>
<EuiFlexGroup>
Expand Down Expand Up @@ -357,8 +361,8 @@ function RulesPage() {
</EuiFlexItem>
<EuiFlexItem grow={false}>
{triggersActionsUi.getRuleStatusFilter({
selectedStatuses: ruleStatusesFilter,
onChange: setRuleStatusesFilter,
selectedStatuses: status,
onChange: setRuleStatusFilter,
})}
</EuiFlexItem>
<EuiFlexItem grow={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import {
createStateContainer,
createStateContainerReactHelpers,
} from '@kbn/kibana-utils-plugin/public';
import { RuleStatus } from '@kbn/triggers-actions-ui-plugin/public';

interface RulesPageContainerState {
lastResponse: string[];
status: RuleStatus[];
}

const defaultState: RulesPageContainerState = {
lastResponse: [],
status: [],
};

interface RulesPageStateTransitions {
setLastResponse: (
state: RulesPageContainerState
) => (lastResponse: string[]) => RulesPageContainerState;
setStatus: (state: RulesPageContainerState) => (status: RuleStatus[]) => RulesPageContainerState;
}

const transitions: RulesPageStateTransitions = {
Expand All @@ -39,6 +43,20 @@ const transitions: RulesPageStateTransitions = {
});
return { ...state, lastResponse: filteredIds };
},
setStatus: (state) => (status) => {
const filteredIds = status;
status.forEach((id) => {
const isPreviouslyChecked = state.status.includes(id);
if (!isPreviouslyChecked) {
filteredIds.concat(id);
} else {
filteredIds.filter((val) => {
return val !== id;
});
}
});
return { ...state, status: filteredIds };
},
};

const rulesPageStateContainer = createStateContainer(defaultState, transitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export function useRulesPageStateContainer() {

useUrlStateSyncEffect(stateContainer);

const { setLastResponse } = stateContainer.transitions;
const { lastResponse } = useContainerSelector(stateContainer, (state) => state);
const { setLastResponse, setStatus } = stateContainer.transitions;
const { lastResponse, status } = useContainerSelector(stateContainer, (state) => state);

return {
lastResponse,
status,
setLastResponse,
setStatus,
};
}

Expand Down

0 comments on commit 3545f31

Please sign in to comment.