Fix input state action button going to set-up mode after stopping input. (#25058)

* Fix inpit state action button going to set-up mode after stopping input.

* update test

---------

Co-authored-by: Laura Bergenthal-Grotlüschen <197286649+laura-b-g@users.noreply.github.com>
This commit is contained in:
Ousmane SAMBA
2026-02-26 16:43:51 +01:00
committed by GitHub
parent 41f67ba651
commit 9fcc0f7641
3 changed files with 32 additions and 86 deletions

View File

@@ -64,9 +64,34 @@ const renderSUT = (inputStates: InputStates, featureEnabled = true) => {
return render(<InputStateControl input={baseInput} inputStates={inputStates} openWizard={jest.fn()} />);
};
const messageInput = {
title: baseInput.title,
global: baseInput.global,
name: baseInput.name,
content_pack: '',
id: baseInput.id,
created_at: baseInput.created_at,
type: baseInput.type,
creator_user_id: baseInput.creator_user_id,
attributes: baseInput.attributes,
static_fields: baseInput.static_fields,
node: baseInput.node,
};
describe('InputStateControl', () => {
it('shows setup state when feature is enabled and input state is not loaded yet', async () => {
renderSUT({});
it('shows setup when feature is enabled and input is in setup mode', async () => {
const setupStates: InputStates = {
[baseInput.id]: {
node1: {
id: baseInput.id,
state: 'SETUP',
detailed_message: null,
message_input: messageInput,
},
},
};
renderSUT(setupStates);
expect(await screen.findByRole('button', { name: /set-up input/i })).toBeInTheDocument();
});
@@ -84,19 +109,7 @@ describe('InputStateControl', () => {
id: baseInput.id,
state: 'RUNNING',
detailed_message: null,
message_input: {
title: baseInput.title,
global: baseInput.global,
name: baseInput.name,
content_pack: '',
id: baseInput.id,
created_at: baseInput.created_at,
type: baseInput.type,
creator_user_id: baseInput.creator_user_id,
attributes: baseInput.attributes,
static_fields: baseInput.static_fields,
node: baseInput.node,
},
message_input: messageInput,
},
},
};
@@ -106,35 +119,8 @@ describe('InputStateControl', () => {
expect(await screen.findByRole('button', { name: /stop input/i })).toBeInTheDocument();
});
it('shows start after stopping an input instead of setup', async () => {
const runningStates: InputStates = {
[baseInput.id]: {
node1: {
id: baseInput.id,
state: 'RUNNING',
detailed_message: null,
message_input: {
title: baseInput.title,
global: baseInput.global,
name: baseInput.name,
content_pack: '',
id: baseInput.id,
created_at: baseInput.created_at,
type: baseInput.type,
creator_user_id: baseInput.creator_user_id,
attributes: baseInput.attributes,
static_fields: baseInput.static_fields,
node: baseInput.node,
},
},
},
};
const { rerender } = renderSUT(runningStates);
expect(await screen.findByRole('button', { name: /stop input/i })).toBeInTheDocument();
rerender(<InputStateControl input={baseInput} inputStates={{}} openWizard={jest.fn()} />);
it('shows start instead of setup when input has no state after page reload', async () => {
renderSUT({});
expect(await screen.findByRole('button', { name: /start input/i })).toBeInTheDocument();
});

View File

@@ -28,7 +28,6 @@ import { TELEMETRY_EVENT_TYPE } from 'logic/telemetry/Constants';
import { Button } from 'components/bootstrap';
import { INPUT_SETUP_MODE_FEATURE_FLAG } from 'components/inputs/InputSetupWizard';
import type { InputStates } from 'hooks/useInputsStates';
import useIsInitialUnknownInputState from 'components/inputs/hooks/useIsInitialUnknownInputState';
type Props = {
input: Input;
@@ -41,7 +40,7 @@ const InputStateControl = ({ input, openWizard, inputStates }: Props) => {
const { pathname } = useLocation();
const [isLoading, setIsLoading] = useState<boolean>(false);
const inputSetupFeatureFlagIsEnabled = useFeature(INPUT_SETUP_MODE_FEATURE_FLAG);
const isInitialUnknownState = useIsInitialUnknownInputState(inputStates, input.id);
const startInput = () => {
setIsLoading(true);
@@ -77,7 +76,7 @@ const InputStateControl = ({ input, openWizard, inputStates }: Props) => {
openWizard();
};
if (inputSetupFeatureFlagIsEnabled && (isInputInSetupMode(inputStates, input.id) || isInitialUnknownState)) {
if (inputSetupFeatureFlagIsEnabled && isInputInSetupMode(inputStates, input.id)) {
return (
<Button bsStyle="warning" bsSize="xsmall" onClick={setupInput}>
Set-up Input

View File

@@ -1,39 +0,0 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import { useEffect, useMemo } from 'react';
import type { InputStates } from 'hooks/useInputsStates';
const useIsInitialUnknownInputState = (inputStates: InputStates, inputId: string) => {
const seenInputIds = useMemo(() => new Set<string>(), []);
useEffect(() => {
if (!inputStates) {
return;
}
Object.keys(inputStates).forEach((id) => {
seenInputIds.add(id);
});
}, [inputStates, seenInputIds]);
const hasKnownState = !!inputStates?.[inputId];
return !hasKnownState && !seenInputIds.has(inputId);
};
export default useIsInitialUnknownInputState;