Skip to content

Commit 5760cc1

Browse files
authored
Merge pull request #919 from PAIR-code/alpha-conditional-survey
Move conditional survey feature out of alpha mode
2 parents 3e4f323 + 9975a1c commit 5760cc1

File tree

6 files changed

+60
-55
lines changed

6 files changed

+60
-55
lines changed

frontend/src/components/experiment_builder/structured_prompt_editor.ts

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ import {CSSResultGroup, html, nothing, TemplateResult} from 'lit';
88
import {customElement, property} from 'lit/decorators.js';
99

1010
import {core} from '../../core/core';
11-
import {AuthService} from '../../services/auth.service';
1211
import {ExperimentEditor} from '../../services/experiment.editor';
1312
import {renderConditionEditor} from '../../shared/condition_editor.utils';
1413

1514
import {
1615
Condition,
16+
ConditionOperator,
1717
ConditionTarget,
18+
createConditionGroup,
19+
createDefaultPromptItemGroup,
20+
createDefaultStageContextPromptItem,
21+
createShuffleConfig,
22+
getConditionTargetsFromStages,
1823
PromptItem,
19-
PromptItemType,
2024
PromptItemGroup,
25+
PromptItemType,
26+
SeedStrategy,
27+
ShuffleConfig,
2128
StageContextPromptItem,
2229
StageKind,
2330
TextPromptItem,
24-
createDefaultStageContextPromptItem,
25-
createDefaultPromptItemGroup,
26-
getConditionTargetsFromStages,
27-
ShuffleConfig,
28-
SeedStrategy,
29-
createShuffleConfig,
3031
} from '@deliberation-lab/utils';
3132

3233
import {styles} from './structured_prompt_editor.scss';
@@ -36,7 +37,6 @@ import {styles} from './structured_prompt_editor.scss';
3637
export class EditorComponent extends MobxLitElement {
3738
static override styles: CSSResultGroup = [styles];
3839

39-
private readonly authService = core.getService(AuthService);
4040
private readonly experimentEditor = core.getService(ExperimentEditor);
4141

4242
@property() prompt: PromptItem[] = [];
@@ -213,13 +213,32 @@ export class EditorComponent extends MobxLitElement {
213213
}
214214

215215
const conditionTargets = this.getConditionTargets();
216+
const supportsConditions =
217+
this.supportsConditions() && conditionTargets.length > 0;
218+
219+
return items.map((item, index) => {
220+
const hasCondition = item.condition !== undefined;
216221

217-
return items.map(
218-
(item, index) => html`
222+
return html`
219223
<div class="prompt-item-wrapper ${isNested ? 'nested' : ''}">
220224
<div class="prompt-item-row">
221225
<div class="prompt-item-editor">${this.renderItemEditor(item)}</div>
222226
<div class="prompt-item-actions">
227+
${supportsConditions && item.type !== PromptItemType.GROUP
228+
? html`
229+
<pr-icon-button
230+
icon="rule"
231+
color=${hasCondition ? 'primary' : 'neutral'}
232+
variant="default"
233+
size="small"
234+
title=${hasCondition
235+
? 'Remove display condition'
236+
: 'Add display condition'}
237+
@click=${() => this.toggleCondition(item)}
238+
>
239+
</pr-icon-button>
240+
`
241+
: nothing}
223242
<pr-icon-button
224243
icon="arrow_upward"
225244
color="neutral"
@@ -246,43 +265,44 @@ export class EditorComponent extends MobxLitElement {
246265
</pr-icon-button>
247266
</div>
248267
</div>
249-
${this.renderPromptItemCondition(item, conditionTargets)}
268+
${hasCondition
269+
? this.renderPromptItemCondition(item, conditionTargets)
270+
: nothing}
250271
</div>
251-
`,
252-
);
272+
`;
273+
});
274+
}
275+
276+
private toggleCondition(item: PromptItem) {
277+
if (item.condition !== undefined) {
278+
// Remove condition
279+
this.updatePromptItem(item, {condition: undefined});
280+
} else {
281+
// Add an empty condition group - user will add conditions via the editor
282+
this.updatePromptItem(item, {
283+
condition: createConditionGroup(ConditionOperator.AND, []),
284+
});
285+
}
253286
}
254287

255288
private renderPromptItemCondition(
256289
item: PromptItem,
257290
conditionTargets: ConditionTarget[],
258291
) {
259-
// Don't show condition editor for GROUP items (they contain other items that can have conditions)
260-
if (item.type === PromptItemType.GROUP) {
261-
return nothing;
262-
}
263-
264-
// Conditions are only supported for private chat stages (not group chat)
265-
if (!this.supportsConditions()) {
266-
return nothing;
267-
}
268-
269292
const onConditionChange = (condition: Condition | undefined) => {
270293
this.updatePromptItem(item, {condition});
271294
};
272295

273-
const conditionEditor = renderConditionEditor({
274-
condition: item.condition,
275-
targets: conditionTargets,
276-
showAlphaFeatures: this.authService.showAlphaFeatures,
277-
canEdit: this.experimentEditor.canEditStages,
278-
onConditionChange,
279-
});
280-
281-
if (conditionEditor === nothing) {
282-
return nothing;
283-
}
284-
285-
return html` <div class="prompt-item-condition">${conditionEditor}</div> `;
296+
return html`
297+
<div class="prompt-item-condition">
298+
${renderConditionEditor({
299+
condition: item.condition,
300+
targets: conditionTargets,
301+
canEdit: this.experimentEditor.canEditStages,
302+
onConditionChange,
303+
})}
304+
</div>
305+
`;
286306
}
287307

288308
private renderTextPromptItemEditor(item: TextPromptItem) {

frontend/src/components/stages/condition_editor.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
display: block;
66
}
77

8-
.alpha {
9-
@include common.mode-tag;
10-
}
11-
128
.condition-editor {
139
padding: common.$spacing-small;
1410
border: 1px solid var(--md-sys-color-outline-variant);

frontend/src/components/stages/condition_editor.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ export class ConditionEditor extends MobxLitElement {
4242
return html`
4343
<div class="condition-editor ${this.disabled ? 'disabled' : ''}">
4444
<div class="header">
45-
<div class="title">
46-
Display Condition
47-
<span class="alpha">alpha</span>
48-
</div>
45+
<div class="title">Display Condition</div>
4946
${hasCondition
5047
? html`
5148
<md-text-button @click=${this.removeCondition}>

frontend/src/components/stages/survey_editor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ export class SurveyEditor extends MobxLitElement {
223223
return renderConditionEditor({
224224
condition: question.condition,
225225
targets,
226-
showAlphaFeatures: this.authService.showAlphaFeatures,
227226
canEdit: this.experimentEditor.canEditStages,
228227
onConditionChange,
229228
});

frontend/src/shared/condition_editor.utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ import '../components/stages/condition_editor';
77
export interface RenderConditionEditorOptions {
88
condition: Condition | undefined;
99
targets: ConditionTarget[];
10-
showAlphaFeatures: boolean | undefined;
1110
canEdit: boolean;
1211
onConditionChange: (condition: Condition | undefined) => void;
1312
}
1413

1514
/**
1615
* Render a condition editor with standard checks.
17-
* Returns nothing if alpha features are disabled or there are no valid targets.
16+
* Returns nothing if there are no valid targets.
1817
*/
1918
export function renderConditionEditor(options: RenderConditionEditorOptions) {
20-
const {condition, targets, showAlphaFeatures, canEdit, onConditionChange} =
21-
options;
19+
const {condition, targets, canEdit, onConditionChange} = options;
2220

23-
if (!showAlphaFeatures) return nothing; // Treats undefined as false
2421
if (targets.length === 0) return nothing;
2522

2623
return html`

utils/src/utils/condition.utils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,6 @@ export function getConditionTargetsFromStages(
313313
return targets;
314314
}
315315

316-
// ============================================================================
317-
// Condition Sanitization Utilities
318-
// ============================================================================
319-
320316
/**
321317
* Sanitize survey question conditions to ensure they only reference valid targets.
322318
*

0 commit comments

Comments
 (0)