Skip to content

Commit 9629d87

Browse files
fix(site): fix disappearing preset selector when switching task template (coder#20514)
Closes coder#20465 Ensure we set `selectedPresetId` to `undefined` when we change `selectedTemplateId` to ensure we don't end up breaking the `<Select>` component by giving it an invalid preset id.
1 parent 643fe38 commit 9629d87

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

site/src/modules/tasks/TaskPrompt/TaskPrompt.stories.tsx

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,123 @@ export const CheckExternalAuthOnChangingVersions: Story = {
480480
});
481481
},
482482
};
483+
484+
export const CheckPresetsWhenChangingTemplate: Story = {
485+
args: {
486+
templates: [
487+
{
488+
...MockTemplate,
489+
id: "claude-code",
490+
name: "claude-code",
491+
display_name: "Claude Code",
492+
active_version_id: "claude-code-version",
493+
},
494+
{
495+
...MockTemplate,
496+
id: "codex",
497+
name: "codex",
498+
display_name: "Codex",
499+
active_version_id: "codex-version",
500+
},
501+
],
502+
},
503+
beforeEach: () => {
504+
spyOn(API, "getTemplateVersionPresets").mockImplementation((versionId) => {
505+
if (versionId === "claude-code-version") {
506+
return Promise.resolve([
507+
{
508+
...MockPresets[0],
509+
ID: "claude-code-preset-1",
510+
Name: "Claude Code Dev",
511+
},
512+
]);
513+
}
514+
if (versionId === "codex-version") {
515+
return Promise.resolve([
516+
{
517+
...MockPresets[0],
518+
ID: "codex-preset-1",
519+
Name: "Codex Dev",
520+
},
521+
]);
522+
}
523+
return Promise.resolve([]);
524+
});
525+
spyOn(API, "getTemplateVersions").mockImplementation((templateId) => {
526+
if (templateId === "claude-code") {
527+
return Promise.resolve([
528+
{
529+
...MockTemplateVersion,
530+
id: "claude-code-version",
531+
name: "claude-code-version",
532+
},
533+
]);
534+
}
535+
if (templateId === "codex") {
536+
return Promise.resolve([
537+
{
538+
...MockTemplateVersion,
539+
id: "codex-version",
540+
name: "codex-version",
541+
},
542+
]);
543+
}
544+
return Promise.resolve([]);
545+
});
546+
},
547+
play: async ({ canvasElement, step }) => {
548+
const canvas = within(canvasElement);
549+
const body = within(canvasElement.ownerDocument.body);
550+
551+
await step("Presets are initially present", async () => {
552+
const presetSelect = await canvas.findByLabelText(/preset/i);
553+
await userEvent.click(presetSelect);
554+
555+
const options = await body.findAllByRole("option");
556+
expect(options).toHaveLength(1);
557+
expect(options[0]).toContainHTML("Claude Code Dev");
558+
559+
await userEvent.click(options[0]);
560+
});
561+
562+
await step("Switch template", async () => {
563+
const templateSelect = await canvas.findByLabelText(/select template/i);
564+
await userEvent.click(templateSelect);
565+
566+
const codexTemplateOption = await body.findByRole("option", {
567+
name: /codex/i,
568+
});
569+
await userEvent.click(codexTemplateOption);
570+
});
571+
572+
await step("Presets are present in new template", async () => {
573+
const presetSelect = await canvas.findByLabelText(/preset/i);
574+
await userEvent.click(presetSelect);
575+
576+
const options = await body.findAllByRole("option");
577+
expect(options).toHaveLength(1);
578+
expect(options[0]).toContainHTML("Codex Dev");
579+
580+
await userEvent.click(options[0]);
581+
});
582+
583+
await step("Switch template back", async () => {
584+
const templateSelect = await canvas.findByLabelText(/select template/i);
585+
await userEvent.click(templateSelect);
586+
587+
const codexTemplateOption = await body.findByRole("option", {
588+
name: /claude code/i,
589+
});
590+
await userEvent.click(codexTemplateOption);
591+
});
592+
593+
await step("Presets are present in original template", async () => {
594+
const presetSelect = await canvas.findByLabelText(/preset/i);
595+
await userEvent.click(presetSelect);
596+
597+
const options = await body.findAllByRole("option");
598+
expect(options).toHaveLength(1);
599+
expect(options[0]).toContainHTML("Claude Code Dev");
600+
});
601+
},
602+
};

site/src/modules/tasks/TaskPrompt/TaskPrompt.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,12 @@ const CreateTaskForm: FC<CreateTaskFormProps> = ({ templates, onSuccess }) => {
269269
</label>
270270
<Select
271271
name="templateID"
272-
onValueChange={(value) => setSelectedTemplateId(value)}
272+
onValueChange={(value) => {
273+
setSelectedTemplateId(value);
274+
if (value !== selectedTemplateId) {
275+
setSelectedPresetId(undefined);
276+
}
277+
}}
273278
defaultValue={templates[0].id}
274279
required
275280
>

0 commit comments

Comments
 (0)