Skip to content

Commit fd343ca

Browse files
authored
refactor(ui): unify channel status and setup controls (#117499)
1 parent 3f9b242 commit fd343ca

15 files changed

Lines changed: 650 additions & 820 deletions

‎ui/src/components/wizard-step-controls.ts‎

Lines changed: 156 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,31 @@ type WizardStepControlsProps = {
1717
inputId: string;
1818
onValueChange: (value: unknown) => void;
1919
onAnswer: (value: unknown, includeValue?: boolean) => void;
20+
presentation?: "channels";
21+
answerLabel?: string;
2022
};
2123

22-
function renderMessage(step: WizardStep) {
23-
return step.message ? html`<div class="wizard-step__message">${step.message}</div>` : nothing;
24+
function stepClass(props: WizardStepControlsProps, name: string): string {
25+
return `${props.presentation === "channels" ? "channels-wizard" : "wizard-step"}__${name}`;
2426
}
2527

26-
function renderOptionBody(option: WizardStepOption) {
28+
function renderMessage(props: WizardStepControlsProps) {
29+
return props.step.message
30+
? html`<div class=${stepClass(props, "message")}>${props.step.message}</div>`
31+
: nothing;
32+
}
33+
34+
function renderOptionBody(option: WizardStepOption, presentation?: "channels", selected?: boolean) {
35+
if (presentation === "channels") {
36+
return html`
37+
<span class="channels-wizard__option-label">
38+
${selected === undefined ? nothing : selected ? "☑ " : "☐ "}${option.label}
39+
</span>
40+
${option.hint
41+
? html`<span class="channels-wizard__option-hint">${option.hint}</span>`
42+
: nothing}
43+
`;
44+
}
2745
return html`
2846
<span>
2947
<strong>${option.label}</strong>
@@ -59,32 +77,95 @@ function renderDeviceCode(step: WizardStep) {
5977
`;
6078
}
6179

80+
function renderAnswerButton(
81+
props: WizardStepControlsProps,
82+
label: string,
83+
onClick?: () => void,
84+
disabled = props.busy,
85+
) {
86+
const button = html`
87+
<button
88+
type=${onClick ? "button" : "submit"}
89+
class="btn primary"
90+
?disabled=${disabled}
91+
@click=${onClick}
92+
>
93+
${props.answerLabel ?? label}
94+
</button>
95+
`;
96+
return props.presentation === "channels"
97+
? html`<div class="channels-wizard__footer">${button}</div>`
98+
: button;
99+
}
100+
101+
function renderOption(
102+
props: WizardStepControlsProps,
103+
option: WizardStepOption,
104+
index: number,
105+
selected: unknown[],
106+
) {
107+
const checked = selected.some((value) => Object.is(value, option.value));
108+
if (props.presentation === "channels") {
109+
return props.step.type === "select"
110+
? html`<wa-radio
111+
class="channels-wizard__option"
112+
appearance="button"
113+
value=${String(index)}
114+
.checked=${checked}
115+
>
116+
${renderOptionBody(option, props.presentation)}
117+
</wa-radio>`
118+
: html`<button
119+
type="button"
120+
class="channels-wizard__option"
121+
aria-pressed=${checked ? "true" : "false"}
122+
?disabled=${props.busy}
123+
@click=${() => props.onValueChange(option.value)}
124+
>
125+
${renderOptionBody(option, props.presentation, checked)}
126+
</button>`;
127+
}
128+
return html`<label class="wizard-step__option">
129+
<input
130+
type=${props.step.type === "select" ? "radio" : "checkbox"}
131+
name=${props.step.type === "select" ? "wizard-option" : nothing}
132+
.checked=${checked}
133+
?disabled=${props.busy}
134+
@change=${(event: Event) => {
135+
const nextValue =
136+
props.step.type === "select"
137+
? option.value
138+
: (event.currentTarget as HTMLInputElement).checked
139+
? [...selected, option.value]
140+
: selected.filter((value) => !Object.is(value, option.value));
141+
props.onValueChange(nextValue);
142+
}}
143+
/>
144+
${renderOptionBody(option)}
145+
</label>`;
146+
}
147+
62148
function renderContinueStep(props: WizardStepControlsProps) {
63149
const step = props.step;
64150
return html`
65-
${renderMessage(step)}
151+
${renderMessage(props)}
66152
${step.externalUrl
67153
? html`<a class="btn btn--sm" href=${step.externalUrl} target="_blank" rel="noreferrer">
68154
${t("modelSetup.wizard.openSignIn")}
69155
</a>`
70156
: nothing}
71157
${renderDeviceCode(step)}
72-
<button
73-
type="button"
74-
class="btn primary"
75-
?disabled=${props.busy}
76-
@click=${() => props.onAnswer(undefined, false)}
77-
>
78-
${t("modelSetup.wizard.continue")}
79-
</button>
158+
${renderAnswerButton(props, t("modelSetup.wizard.continue"), () =>
159+
props.onAnswer(undefined, false),
160+
)}
80161
`;
81162
}
82163

83-
function renderProgressStep(step: WizardStep) {
164+
function renderProgressStep(props: WizardStepControlsProps) {
84165
return html`
85166
<div class="wizard-step__progress" role="status" aria-live="polite">
86167
<span class="wizard-step__spinner" aria-hidden="true"></span>
87-
${renderMessage(step)}
168+
${renderMessage(props)}
88169
</div>
89170
`;
90171
}
@@ -97,11 +178,14 @@ function renderTextStep(props: WizardStepControlsProps) {
97178
class="wizard-step__form"
98179
@submit=${(event: Event) => {
99180
event.preventDefault();
100-
props.onAnswer(value);
181+
const input = (event.currentTarget as HTMLFormElement).elements.namedItem(
182+
"wizard-text",
183+
) as HTMLInputElement | null;
184+
props.onAnswer(props.presentation === "channels" ? (input?.value ?? "") : value);
101185
}}
102186
>
103187
${step.message
104-
? html`<div class="wizard-step__message">
188+
? html`<div class=${stepClass(props, "message")}>
105189
<label for=${props.inputId}>${step.message}</label>
106190
</div>`
107191
: nothing}
@@ -115,101 +199,74 @@ function renderTextStep(props: WizardStepControlsProps) {
115199
.value=${value}
116200
?disabled=${props.busy}
117201
@input=${(event: Event) =>
202+
props.presentation !== "channels" &&
118203
props.onValueChange((event.currentTarget as HTMLInputElement).value)}
119204
/>
120-
<button type="submit" class="btn primary" ?disabled=${props.busy}>
121-
${t("modelSetup.wizard.submit")}
122-
</button>
205+
${renderAnswerButton(props, t("modelSetup.wizard.submit"))}
123206
</form>
124207
`;
125208
}
126209

127-
function renderSelectStep(props: WizardStepControlsProps) {
128-
return html`
129-
${renderMessage(props.step)}
130-
<div class="wizard-step__options" role="radiogroup">
131-
${(props.step.options ?? []).map(
132-
(option) => html`
133-
<label class="wizard-step__option">
134-
<input
135-
type="radio"
136-
name="wizard-option"
137-
.checked=${Object.is(props.value, option.value)}
138-
@change=${() => props.onValueChange(option.value)}
139-
/>
140-
${renderOptionBody(option)}
141-
</label>
142-
`,
143-
)}
144-
</div>
145-
<button
146-
type="button"
147-
class="btn primary"
148-
?disabled=${props.busy || props.value === undefined}
149-
@click=${() => props.onAnswer(props.value)}
150-
>
151-
${t("modelSetup.wizard.continue")}
152-
</button>
153-
`;
154-
}
155-
156-
function renderConfirmStep(props: WizardStepControlsProps) {
157-
return html`
158-
${renderMessage(props.step)}
159-
<div class="wizard-step__actions">
160-
<button
161-
type="button"
162-
class="btn"
210+
function renderOptionsStep(props: WizardStepControlsProps) {
211+
const options = props.step.options ?? [];
212+
const multiple = props.step.type === "multiselect";
213+
const selected = multiple ? (Array.isArray(props.value) ? props.value : []) : [props.value];
214+
if (props.presentation === "channels" && !multiple) {
215+
const selectedIndex = options.findIndex((option) => Object.is(option.value, props.value));
216+
return html`
217+
<wa-radio-group
218+
class="channels-wizard__options"
219+
label=${props.step.message ?? ""}
220+
orientation="vertical"
221+
.value=${selectedIndex >= 0 ? String(selectedIndex) : null}
163222
?disabled=${props.busy}
164-
@click=${() => props.onAnswer(false)}
223+
@change=${(event: Event) => {
224+
const index = (event.currentTarget as HTMLElement & { value?: string | number | null })
225+
.value;
226+
const option = options[Number(index)];
227+
if (option) {
228+
props.onAnswer(option.value);
229+
}
230+
}}
165231
>
166-
${t("common.no")}
167-
</button>
168-
<button
169-
type="button"
170-
class="btn primary"
171-
?disabled=${props.busy}
172-
@click=${() => props.onAnswer(true)}
173-
>
174-
${t("common.yes")}
175-
</button>
232+
${options.map((option, index) => renderOption(props, option, index, selected))}
233+
</wa-radio-group>
234+
`;
235+
}
236+
const answer = multiple
237+
? props.presentation === "channels"
238+
? [...selected]
239+
: selected
240+
: props.value;
241+
return html`
242+
${renderMessage(props)}
243+
<div class=${stepClass(props, "options")} role=${multiple ? nothing : "radiogroup"}>
244+
${options.map((option, index) => renderOption(props, option, index, selected))}
176245
</div>
246+
${renderAnswerButton(
247+
props,
248+
t("modelSetup.wizard.continue"),
249+
() => props.onAnswer(answer),
250+
props.busy || (!multiple && props.value === undefined),
251+
)}
177252
`;
178253
}
179254

180-
function renderMultiselectStep(props: WizardStepControlsProps) {
181-
const selected = Array.isArray(props.value) ? props.value : [];
255+
function renderConfirmStep(props: WizardStepControlsProps) {
182256
return html`
183-
${renderMessage(props.step)}
184-
<div class="wizard-step__options">
185-
${(props.step.options ?? []).map(
186-
(option) => html`
187-
<label class="wizard-step__option">
188-
<input
189-
type="checkbox"
190-
.checked=${selected.some((value) => Object.is(value, option.value))}
191-
@change=${(event: Event) => {
192-
const checked = (event.currentTarget as HTMLInputElement).checked;
193-
props.onValueChange(
194-
checked
195-
? [...selected, option.value]
196-
: selected.filter((value) => !Object.is(value, option.value)),
197-
);
198-
}}
199-
/>
200-
${renderOptionBody(option)}
201-
</label>
202-
`,
257+
${renderMessage(props)}
258+
<div class=${stepClass(props, props.presentation === "channels" ? "footer" : "actions")}>
259+
${[false, true].map(
260+
(answer) => html`<button
261+
type="button"
262+
class=${answer ? "btn primary" : "btn"}
263+
?disabled=${props.busy}
264+
@click=${() => props.onAnswer(answer)}
265+
>
266+
${t(answer ? "common.yes" : "common.no")}
267+
</button>`,
203268
)}
204269
</div>
205-
<button
206-
type="button"
207-
class="btn primary"
208-
?disabled=${props.busy}
209-
@click=${() => props.onAnswer(selected)}
210-
>
211-
${t("modelSetup.wizard.continue")}
212-
</button>
213270
`;
214271
}
215272

@@ -225,14 +282,13 @@ export function renderWizardStepControls(
225282
case "text":
226283
return renderTextStep(props);
227284
case "select":
228-
return renderSelectStep(props);
285+
case "multiselect":
286+
return renderOptionsStep(props);
229287
case "confirm":
230288
return renderConfirmStep(props);
231-
case "multiselect":
232-
return renderMultiselectStep(props);
233289
case "progress":
234290
return props.step.executor === "gateway"
235-
? renderProgressStep(props.step)
291+
? renderProgressStep(props)
236292
: renderContinueStep(props);
237293
// These show whatever the step supplies behind a single Continue.
238294
case "note":

0 commit comments

Comments
 (0)