Skip to content

Commit 55a6d48

Browse files
authored
fix: align channel data structure with backend API specification (#1543)
* fix: align channel data structure with backend API specification * fix: lint * fix: improve typing on channels prop
1 parent 3c106a6 commit 55a6d48

File tree

21 files changed

+108
-139
lines changed

21 files changed

+108
-139
lines changed

src/components/CheckEditor/CheckProbes/ProbesList.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,9 @@ export const ProbesList = ({
2727
const { isEnabled: isVersionManagementEnabled } = useFeatureFlag(FeatureName.VersionManagement);
2828
const { getValues } = useFormContext<CheckFormValues>();
2929

30-
const checkType = getValues('checkType');
3130
const selectedChannel = useMemo(() => {
32-
if (checkType === 'scripted') {
33-
return getValues('settings.scripted.channel');
34-
}
35-
if (checkType === 'browser') {
36-
return getValues('settings.browser.channel');
37-
}
38-
return undefined;
39-
}, [checkType, getValues]);
31+
return getValues('channels.k6.id');
32+
}, [getValues]);
4033

4134
const isProbeCompatible = (probe: ProbeWithMetadata): boolean => {
4235
if (!isVersionManagementEnabled || !selectedChannel || !probe.k6Versions) {

src/components/CheckEditor/FormComponents/K6ChannelSelect.test.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,17 @@ describe('K6ChannelSelect', () => {
196196
settings: {
197197
browser: {
198198
script: 'test script',
199-
channel: 'deprecated'
200199
}
201-
}
200+
},
201+
channels: {
202+
k6: {
203+
id: 'deprecated',
204+
name: 'deprecated.x',
205+
default: false,
206+
deprecatedAfter: '2020-01-01T00:00:00Z',
207+
manifest: 'k6>=0.5,k6<1',
208+
}
209+
}
202210
};
203211

204212
server.use(

src/components/CheckEditor/FormComponents/K6ChannelSelect.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function K6ChannelSelectContent({ disabled }: K6ChannelSelectProps) {
4242

4343
const { field, fieldState } = useController({
4444
control,
45-
name: `settings.${checkType === 'scripted' ? 'scripted' : 'browser'}.channel`,
45+
name: 'channels.k6',
4646
});
4747

4848
const {
@@ -56,9 +56,12 @@ function K6ChannelSelectContent({ disabled }: K6ChannelSelectProps) {
5656
// Initialize with default channel when no channel is set (new checks or existing checks without channel)
5757
useEffect(() => {
5858
if (!field.value && defaultChannelId && !isLoadingChannels) {
59-
field.onChange(defaultChannelId);
59+
const defaultChannel = channels.find((channel) => channel.id === defaultChannelId);
60+
if (defaultChannel) {
61+
field.onChange(defaultChannel);
62+
}
6063
}
61-
}, [field, defaultChannelId, isLoadingChannels]);
64+
}, [field, defaultChannelId, isLoadingChannels, channels]);
6265

6366
// Throw error to be caught by QueryErrorBoundary if there's an error
6467
if (hasChannelError && channelError) {
@@ -90,17 +93,17 @@ function K6ChannelSelectContent({ disabled }: K6ChannelSelectProps) {
9093
<Stack direction="column" gap={2}>
9194
<Combobox
9295
{...field}
93-
value={field.value || defaultChannelId}
96+
value={field.value?.id || defaultChannelId}
9497
disabled={disabled || isLoadingChannels}
9598
options={channelOptions}
9699
id={id}
97100
createCustomValue={false}
98101
onChange={(value) => {
99-
const channelValue = typeof value === 'string' ? value : value?.value || '';
100-
field.onChange(channelValue);
101-
102-
const selectedChannel = channels.find((channel) => channel.id === channelValue);
102+
const channelId = typeof value === 'string' ? value : value?.value || '';
103+
const selectedChannel = channels.find((channel) => channel.id === channelId);
104+
103105
if (selectedChannel) {
106+
field.onChange(selectedChannel);
104107
trackK6ChannelSelected({
105108
checkType,
106109
channelName: selectedChannel.name,
@@ -112,7 +115,7 @@ function K6ChannelSelectContent({ disabled }: K6ChannelSelectProps) {
112115
/>
113116
</Stack>
114117
</Field>
115-
<ChannelDetails channelId={field.value || defaultChannelId || null} channels={channels} />
118+
<ChannelDetails channelId={field.value?.id || defaultChannelId || null} channels={channels} />
116119
</div>
117120
);
118121
}

src/components/Checkster/components/form/generic/GenericScriptField.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import { FieldValidationMessage, useTheme2 } from '@grafana/ui';
44
import { css, cx } from '@emotion/css';
55

66
import { CheckFormFieldPath } from '../../../types';
7-
import { CheckFormValues } from 'types';
7+
import { CheckFormValues, K6Channel } from 'types';
88
import { CodeEditor } from 'components/CodeEditor';
99

1010
import { getFieldErrorProps } from '../../../utils/form';
1111
import { Column } from '../../ui/Column';
1212

1313
interface GenericScriptFieldProps {
1414
field: CheckFormFieldPath;
15-
channelField?: CheckFormFieldPath;
1615
}
1716

1817
// FIXME: Not actually a Field (no label, no description), but it has errors!
19-
export function GenericScriptField({ field, channelField }: GenericScriptFieldProps) {
18+
export function GenericScriptField({ field }: GenericScriptFieldProps) {
2019
const {
2120
control,
2221
getValues,
@@ -27,7 +26,7 @@ export function GenericScriptField({ field, channelField }: GenericScriptFieldPr
2726

2827
const theme = useTheme2();
2928

30-
const selectedChannel = channelField ? getValues(channelField) : undefined;
29+
const k6ChannelId = (getValues('channels.k6') as K6Channel | undefined)?.id;
3130
return (
3231
<Column
3332
grow
@@ -53,7 +52,7 @@ export function GenericScriptField({ field, channelField }: GenericScriptFieldPr
5352
readOnly={disabled}
5453
data-form-name={field}
5554
data-form-element-selector="textarea"
56-
k6Channel={selectedChannel}
55+
k6Channel={k6ChannelId}
5756
/>
5857
);
5958
}}

src/components/Checkster/components/form/layouts/BrowserCheckContent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import { BROWSER_EXAMPLES } from 'components/WelcomeTabs/constants';
44

55
import { ScriptedCheckContent } from './ScriptedCheckContent';
66

7-
export const BROWSER_CHECK_FIELDS = ['job', 'instance', 'settings.browser.channel', 'settings.browser.script'];
7+
export const BROWSER_CHECK_FIELDS = ['job', 'instance', 'channels.k6', 'settings.browser.script'];
88

99
export function BrowserCheckContent() {
1010
return (
1111
<ScriptedCheckContent
1212
scriptField="settings.browser.script"
13-
channelField="settings.browser.channel"
1413
examples={BROWSER_EXAMPLES}
1514
/>
1615
);

src/components/Checkster/components/form/layouts/ScriptedCheckContent.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ import { GenericScriptField } from '../generic/GenericScriptField';
2222

2323
interface ScriptedCheckSectionProps {
2424
scriptField?: `settings.${CheckType.Scripted | CheckType.Browser}.script`;
25-
channelField?: `settings.${CheckType.Scripted | CheckType.Browser}.channel`;
2625
examples?: ExampleScript[];
2726
}
2827

29-
export const SCRIPTED_CHECK_FIELDS = ['job', 'target', 'settings.scripted.channel', 'settings.scripted.script'];
28+
export const SCRIPTED_CHECK_FIELDS = ['job', 'target', 'channels.k6', 'settings.scripted.script'];
3029

3130
// Don't set label here, set it explicitly, where the component is used (for readability)
3231
export function ScriptedCheckContent({
3332
examples = SCRIPT_EXAMPLES,
3433
scriptField = 'settings.scripted.script',
35-
channelField = 'settings.scripted.channel',
3634
}: ScriptedCheckSectionProps) {
3735
const theme = useTheme2();
3836
const hasExamples = examples && examples?.length > 0;
@@ -48,7 +46,7 @@ export function ScriptedCheckContent({
4846
<Column fill>
4947
<FormTabs actions={<HelpButton />}>
5048
<FormTabContent label="Script" fillVertical vanilla>
51-
<GenericScriptField field={scriptField} channelField={channelField} />
49+
<GenericScriptField field={scriptField} />
5250
</FormTabContent>
5351
{hasExamples && (
5452
<FormTabContent label="Examples" fillVertical vanilla className={styles.codeSnippetWrapper}>

src/components/Checkster/transformations/toFormValues.browser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export function getBrowserCheckFormValues(check: BrowserCheck): CheckFormValuesB
1313
settings: {
1414
browser: {
1515
script: decode(check.settings?.browser?.script),
16-
channel: check.settings?.browser?.channel || null,
1716
},
1817
},
1918
};

src/components/Checkster/transformations/toFormValues.scripted.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export function getScriptedCheckFormValues(check: ScriptedCheck): CheckFormValue
1313
settings: {
1414
scripted: {
1515
script: decode(check.settings?.scripted?.script),
16-
channel: check.settings?.scripted?.channel || null,
1716
},
1817
},
1918
};

src/components/Checkster/transformations/toFormValues.utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export function getBaseFormValuesFromCheck(check: Check): Omit<CheckFormValues,
4646
target: check.target,
4747
timeout: check.timeout,
4848
alerts: predefinedAlertsToFormValues(GLOBAL_PREDEFINED_ALERTS, check.alerts || []),
49+
channels: {
50+
k6: check.channels?.k6,
51+
},
4952
};
5053
}
5154

src/components/Checkster/transformations/toPayload.browser.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ import { getBasePayloadValuesFromForm } from './toPayload.utils';
77
export function getBrowserPayload(formValues: CheckFormValuesBrowser): BrowserCheck {
88
const base = getBasePayloadValuesFromForm(formValues);
99

10-
const browserSettings: { script: string; channel?: string } = {
11-
script: encode(formValues.settings.browser.script),
12-
};
13-
14-
// Only include channel if it has a value
15-
if (formValues.settings.browser.channel) {
16-
browserSettings.channel = formValues.settings.browser.channel;
17-
}
18-
1910
return {
2011
...base,
2112
settings: {
22-
browser: browserSettings,
13+
browser: {
14+
script: encode(formValues.settings.browser.script),
15+
},
2316
},
2417
};
2518
}

0 commit comments

Comments
 (0)