Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow consumers to lock and intercept category visibility changes (#41006)

Keegan Georged97ec2c1ad9b
diff --git a/app/assets/stylesheets/common/form-kit/_conditional-display.scss b/app/assets/stylesheets/common/form-kit/_conditional-display.scss
index 4714c3d6d8e..1f1db2ff774 100644
--- a/app/assets/stylesheets/common/form-kit/_conditional-display.scss
+++ b/app/assets/stylesheets/common/form-kit/_conditional-display.scss
@@ -57,6 +57,17 @@
cursor: not-allowed;
}
+ // Like --disabled, but the option stays clickable.
+ &.--locked {
+ color: var(--primary-medium);
+ background: var(--d-input-bg-color--disabled);
+
+ .d-icon {
+ color: currentcolor;
+ display: inline-block;
+ }
+ }
+
.d-icon {
display: none;
}
diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjs
index f9253d84834..4aafb7d8383 100644
--- a/frontend/discourse/admin/components/upsert-category/general.gjs
+++ b/frontend/discourse/admin/components/upsert-category/general.gjs
@@ -23,6 +23,10 @@ import { AUTO_GROUPS, CATEGORY_TEXT_COLORS } from "discourse/lib/constants";
import { bind } from "discourse/lib/decorators";
import getURL from "discourse/lib/get-url";
import { runOnBeforeCategoryTypesChange } from "discourse/lib/on-before-category-types-change";
+import {
+ applyBehaviorTransformer,
+ applyValueTransformer,
+} from "discourse/lib/transformer";
import Category from "discourse/models/category";
import Composer from "discourse/models/composer";
import PermissionType from "discourse/models/permission-type";
@@ -314,6 +318,14 @@ export default class UpsertCategoryGeneral extends Component {
: "category.visibility.public";
}
+ get privateVisibilityLocked() {
+ return applyValueTransformer("category-visibility-private-locked", false, {
+ category: this.args.category,
+ form: this.args.form,
+ transientData: this.args.transientData,
+ });
+ }
+
get showWarning() {
return this.args.category.isUncategorizedCategory;
}
@@ -338,6 +350,21 @@ export default class UpsertCategoryGeneral extends Component {
@action
onChangeVisibility(value) {
+ // Wrapped so consumers can intercept the change; skipping `next` vetoes it.
+ return applyBehaviorTransformer(
+ "category-visibility-change",
+ () => this.#applyVisibilityChange(value),
+ {
+ nextVisibility: value,
+ previousVisibility: this.categoryVisibility,
+ category: this.args.category,
+ form: this.args.form,
+ transientData: this.args.transientData,
+ }
+ );
+ }
+
+ #applyVisibilityChange(value) {
// Save current permissions before switching to public
if (value === "public" && this.isPrivateCategory) {
this.#previousPermissions = (this.permissions || []).map((p) => ({
@@ -912,8 +939,15 @@ export default class UpsertCategoryGeneral extends Component {
{{i18n this.publicVisibilityLabel}}
</Condition>
{{/if}}
- <Condition @name="group_restricted">
- {{dIcon "check"}}
+ <Condition
+ @name="group_restricted"
+ @locked={{this.privateVisibilityLocked}}
+ >
+ {{#if this.privateVisibilityLocked}}
+ {{dIcon "lock"}}
+ {{else}}
+ {{dIcon "check"}}
+ {{/if}}
{{i18n "category.visibility.group_restricted"}}
</Condition>
</cc.Conditions>
diff --git a/frontend/discourse/app/form-kit/components/fk/control/conditional-content.gjs b/frontend/discourse/app/form-kit/components/fk/control/conditional-content.gjs
index 227864e9af0..66a559337ba 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/conditional-content.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/conditional-content.gjs
@@ -12,6 +12,7 @@ const Conditions = <template>
FKControlConditionalDisplayCondition
activeName=@activeName
setCondition=@setCondition
+ resyncToken=@resyncToken
)
}}
</div>
@@ -25,6 +26,9 @@ const Contents = <template>
export default class FKControlConditionalContent extends Component {
@tracked manuallySetName = null;
+ // Bumped after `onChange` settles to re-sync the radios with `activeName`,
+ // so a rejected change doesn't leave the clicked radio selected.
+ @tracked resyncToken = 0;
get activeName() {
// If onChange is provided, parent controls state - always use @activeName
@@ -37,7 +41,12 @@ export default class FKControlConditionalContent extends Component {
@action
setCondition(name) {
this.manuallySetName = name;
- this.args.onChange?.(name);
+
+ if (this.args.onChange) {
+ Promise.resolve(this.args.onChange(name)).finally(() => {
+ this.resyncToken++;
+ });
+ }
}
<template>
@@ -45,7 +54,10 @@ export default class FKControlConditionalContent extends Component {
{{yield
(hash
Conditions=(component
- Conditions activeName=this.activeName setCondition=this.setCondition
+ Conditions
+ activeName=this.activeName
+ setCondition=this.setCondition
+ resyncToken=this.resyncToken
)
Contents=(component Contents activeName=this.activeName)
)
diff --git a/frontend/discourse/app/form-kit/components/fk/control/conditional-content/condition.gjs b/frontend/discourse/app/form-kit/components/fk/control/conditional-content/condition.gjs
index a28ee2138e9..06f4896b53c 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/conditional-content/condition.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/conditional-content/condition.gjs
@@ -1,10 +1,18 @@
import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import FKLabel from "discourse/form-kit/components/fk/label";
import { eq } from "discourse/truth-helpers";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
import dUniqueId from "discourse/ui-kit/helpers/d-unique-id";
+// A native radio click mutates the DOM, so a rejected change (where
+// `activeName` is unchanged) won't reset the one-way `checked` binding on its
+// own. Re-assert it when `resyncToken` bumps.
+function syncChecked(element, [name, activeName]) {
+ element.checked = name === activeName;
+}
+
const FKControlConditionalContentOption = <template>
{{#let (dUniqueId) as |uuid|}}
<FKLabel
@@ -12,6 +20,7 @@ const FKControlConditionalContentOption = <template>
class={{dConcatClass
"form-kit__control-radio-label"
(if @disabled "--disabled")
+ (if @locked "--locked")
}}
>
<input
@@ -22,6 +31,7 @@ const FKControlConditionalContentOption = <template>
disabled={{@disabled}}
class="form-kit__control-radio"
{{on "change" (fn @setCondition @name)}}
+ {{didUpdate syncChecked @name @activeName @resyncToken}}
/>
<span>{{yield}}</span>
diff --git a/frontend/discourse/app/lib/registry/transformers.js b/frontend/discourse/app/lib/registry/transformers.js
index b00316f9dcf..25ced86fc6c 100644
--- a/frontend/discourse/app/lib/registry/transformers.js
+++ b/frontend/discourse/app/lib/registry/transformers.js
@@ -9,6 +9,7 @@
*/
// eslint-discourse keep-array-sorted
export const BEHAVIOR_TRANSFORMERS = Object.freeze([
+ "category-visibility-change",
"composer-actions-on-select",
"composer-position:correct-scroll-position",
"composer-position:editor-touch-move",
@@ -46,6 +47,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"category-display-name",
"category-sort-orders",
"category-text-color",
+ "category-visibility-private-locked",
"composer-actions-content",
"composer-editor-quoted-post-avatar-template",
"composer-editor-reply-placeholder",
diff --git a/frontend/discourse/tests/integration/components/form-kit/controls/conditional-content-test.gjs b/frontend/discourse/tests/integration/components/form-kit/controls/conditional-content-test.gjs
new file mode 100644
index 00000000000..92c724727cc
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/form-kit/controls/conditional-content-test.gjs
@@ -0,0 +1,41 @@
+import { click, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import Form from "discourse/components/form";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+module(
+ "Integration | Component | FormKit | Controls | ConditionalContent",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("reverts the selection when a controlled change is rejected", async function (assert) {
+ const reject = () => {};
+
+ await render(
+ <template>
+ <Form as |form|>
+ <form.ConditionalContent
+ @activeName="one"
+ @onChange={{reject}}
+ as |cc|
+ >
+ <cc.Conditions as |Condition|>
+ <Condition @name="one">One</Condition>
+ <Condition @name="two">Two</Condition>
+ </cc.Conditions>
+ </form.ConditionalContent>
+ </Form>
+ </template>
+ );
+
+ assert.dom("input[value='one']").isChecked();
+ assert.dom("input[value='two']").isNotChecked();
+
+ await click("input[value='two']");
+
+ // `onChange` did not update `@activeName`, so the radio must snap back.
+ assert.dom("input[value='one']").isChecked();
+ assert.dom("input[value='two']").isNotChecked();
+ });
+ }
+);