A platform for community discussion. Free, open, simple.
UX: warn when theme component isn't added to themes (#44339)
Kris810d6ff29edd
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 1650d9d445f..43618b3e3fb 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -9203,8 +9203,9 @@ en:upload: "Upload"select_component: "Select a component…"unsaved_changes_alert: "You haven't saved your changes yet, do you want to discard them and move on?"- unsaved_parent_themes: "You haven't assigned the component to themes, do you want to move on?"+ unsaved_parent_themes: "You haven't added this component to any themes, so it won't appear anywhere. Do you still want to leave?"discard: "Discard"+ leave: "Leave"stay: "Stay"css_html: "Custom Code"edit_css_html: "Edit Code"diff --git a/frontend/discourse/admin/components/site-setting.gjs b/frontend/discourse/admin/components/site-setting.gjsindex 90952e05852..0141a1f4cb4 100644--- a/frontend/discourse/admin/components/site-setting.gjs+++ b/frontend/discourse/admin/components/site-setting.gjs@@ -11,7 +11,7 @@ import { LinkTo } from "@ember/routing";import { scheduleOnce } from "@ember/runloop";import { service } from "@ember/service";import { trustHTML } from "@ember/template";-import { isEmpty, isNone } from "@ember/utils";+import { isEmpty } from "@ember/utils";import SettingValidationMessage from "discourse/admin/components/setting-validation-message";import SettingBool from "discourse/admin/components/site-settings/bool";import SettingCategory from "discourse/admin/components/site-settings/category";@@ -476,22 +476,7 @@ export default class SiteSettingComponent extends Component {}settingIsDirty(setting) {- let bufferVal = this.buffered.get("value");- let settingVal = setting?.value;-- if (setting !== this.setting) {- bufferVal = setting.buffered.get("value");- }-- if (isNone(bufferVal)) {- bufferVal = "";- }-- if (isNone(settingVal)) {- settingVal = "";- }-- const dirty = !this.#valuesEqual(bufferVal, settingVal, setting);+ const dirty = setting.hasPendingChanges;if (this.trackChanges) {if (dirty) {diff --git a/frontend/discourse/admin/components/theme-setting-relatives-selector.js b/frontend/discourse/admin/components/theme-setting-relatives-selector.jsindex af3d6599090..2ed33d10163 100644--- a/frontend/discourse/admin/components/theme-setting-relatives-selector.js+++ b/frontend/discourse/admin/components/theme-setting-relatives-selector.js@@ -18,9 +18,10 @@ export default class ThemeSettingRelativesSelectorComponent extends SiteSettingC});}- _save() {- return this.args.model.save({+ async _save() {+ await this.args.model.save({[this.args.setting.setting]: this.convertNamesToIds(),});+ this.args.onSave?.();}}diff --git a/frontend/discourse/admin/controllers/admin-customize-themes/show.js b/frontend/discourse/admin/controllers/admin-customize-themes/show.jsindex 26aac098426..c1cb34d5b51 100644--- a/frontend/discourse/admin/controllers/admin-customize-themes/show.js+++ b/frontend/discourse/admin/controllers/admin-customize-themes/show.js@@ -95,7 +95,6 @@ export default class AdminCustomizeThemesShowController extends Controller {}),didConfirm: () => {const model = this.model;- model.setProperties({ recentlyInstalled: false });model.destroyRecord().then(() => {removeValueFromArray(this.allThemes, model);this.router.transitionTo("adminConfig.customize.themes");diff --git a/frontend/discourse/admin/controllers/admin-customize-themes/show/index.js b/frontend/discourse/admin/controllers/admin-customize-themes/show/index.jsindex 3c312dab70a..81510870356 100644--- a/frontend/discourse/admin/controllers/admin-customize-themes/show/index.js+++ b/frontend/discourse/admin/controllers/admin-customize-themes/show/index.js@@ -27,6 +27,7 @@ export default class AdminCustomizeThemesShowIndexController extends Controller@service toasts;editRouteName = "adminCustomizeThemes.edit";+ parentThemesSaved = false;@computed("model.id")get downloadUrl() {@@ -123,6 +124,17 @@ export default class AdminCustomizeThemesShowIndexController extends Controllerreturn this.model?.themeable_site_settings;}+ get pendingSettings() {+ return [+ ...(this.settings ?? []),+ ...(this.themeSiteSettings ?? []),+ ...(this.translations ?? []),+ this.model.component+ ? this.relativesSelectorSettingsForComponent+ : this.relativesSelectorSettingsForTheme,+ ].filter((setting) => setting.hasPendingChanges);+ }+@computed("model.component", "model.remote_theme")get showCheckboxes() {return !this.model.component || this.model.remote_theme;@@ -537,6 +549,11 @@ export default class AdminCustomizeThemesShowIndexController extends Controller});}+ @action+ markParentThemesSaved() {+ this.parentThemesSaved = true;+ }+@actionremoveChildTheme(theme) {this.model.removeChildTheme(theme).then(() => this.store.findAll("theme"));@@ -550,7 +567,6 @@ export default class AdminCustomizeThemesShowIndexController extends Controller}),didConfirm: () => {const model = this.model;- model.setProperties({ recentlyInstalled: false });model.destroyRecord().then(() => {removeValueFromArray(this.allThemes, model);diff --git a/frontend/discourse/admin/models/site-setting.js b/frontend/discourse/admin/models/site-setting.jsindex d57fa59b1f4..3737a00e001 100644--- a/frontend/discourse/admin/models/site-setting.js+++ b/frontend/discourse/admin/models/site-setting.js@@ -9,6 +9,7 @@ import SettingObjectHelper from "discourse/admin/lib/setting-object-helper";import { ajax } from "discourse/lib/ajax";import BufferedProxy from "discourse/lib/buffered-proxy";import { bind } from "discourse/lib/decorators";+import { deepEqual } from "discourse/lib/object";import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";@@ -199,6 +200,16 @@ export default class SiteSetting extends EmberObject {return this.buffered.get("value");}+ get hasPendingChanges() {+ const pending = this.pendingValue ?? "";+ const saved = this.value ?? "";++ if (this.json_schema || this.schema || this.objects_schema) {+ return !deepEqual(pending, saved);+ }+ return pending.toString() !== saved.toString();+ }+get requiresConfirmation() {switch (this.requires_confirmation) {case SITE_SETTING_REQUIRES_CONFIRMATION_TYPES.simple:diff --git a/frontend/discourse/admin/models/theme.js b/frontend/discourse/admin/models/theme.jsindex bc21982d9a1..0d411e179eb 100644--- a/frontend/discourse/admin/models/theme.js+++ b/frontend/discourse/admin/models/theme.js@@ -182,9 +182,9 @@ class Theme extends RestModel {}}- @computed("recentlyInstalled", "component", "hasParents")+ @computed("component", "enabled", "hasParents")get warnUnassignedComponent() {- return this.recentlyInstalled && this.component && !this.hasParents;+ return this.component && this.enabled && !this.hasParents;}getKey(field) {diff --git a/frontend/discourse/admin/routes/admin-customize-themes/edit.js b/frontend/discourse/admin/routes/admin-customize-themes/edit.jsindex 497c805a8a9..923fde4dbb0 100644--- a/frontend/discourse/admin/routes/admin-customize-themes/edit.js+++ b/frontend/discourse/admin/routes/admin-customize-themes/edit.js@@ -67,6 +67,7 @@ export default class AdminCustomizeThemesEditRoute extends Route {this.dialog.confirm({message: i18n("admin.customize.theme.unsaved_changes_alert"),+ confirmButtonClass: "btn-danger",confirmButtonLabel: "admin.customize.theme.discard",cancelButtonLabel: "admin.customize.theme.stay",didConfirm: () => {diff --git a/frontend/discourse/admin/routes/admin-customize-themes/show/index.js b/frontend/discourse/admin/routes/admin-customize-themes/show/index.jsindex 454b05c2beb..4be95d58d4e 100644--- a/frontend/discourse/admin/routes/admin-customize-themes/show/index.js+++ b/frontend/discourse/admin/routes/admin-customize-themes/show/index.js@@ -1,4 +1,5 @@import { action } from "@ember/object";+import { schedule } from "@ember/runloop";import { service } from "@ember/service";import { scrollTop } from "discourse/lib/scroll-top";import DiscourseRoute from "discourse/routes/discourse";@@ -24,6 +25,7 @@ export default class AdminCustomizeThemesShowIndexRoute extends DiscourseRoute {darkColorSchemeId: model.get("dark_color_scheme_id"),colorSchemes: parentController.get("model.extras.color_schemes"),editingName: false,+ parentThemesSaved: false,userLocale: parentController.get("model.extras.locale"),});}@@ -36,17 +38,66 @@ export default class AdminCustomizeThemesShowIndexRoute extends DiscourseRoute {@actionwillTransition(transition) {const model = this.controller.model;- if (model.warnUnassignedComponent) {+ if (+ transition.data.skipLeaveWarnings ||+ this.#isDeleted(model) ||+ this.#staysOnTheme(transition, model)+ ) {+ return;+ }++ const pendingSettings = this.controller.pendingSettings;+ if (pendingSettings.length > 0) {transition.abort();- this.dialog.yesNoConfirm({- message: i18n("admin.customize.theme.unsaved_parent_themes"),+ this.dialog.confirm({+ message: i18n("admin.customize.theme.unsaved_changes_alert"),+ confirmButtonClass: "btn-danger",+ confirmButtonLabel: "admin.customize.theme.discard",+ cancelButtonLabel: "admin.customize.theme.stay",didConfirm: () => {- model.set("recentlyInstalled", false);- transition.retry();+ // Settings outlive the page, so unsaved values would reappear on return+ pendingSettings.forEach((setting) => setting.rollback());+ this.#leave(transition);+ },+ });+ } else if (+ model.warnUnassignedComponent &&+ // Saving the theme selection, even to empty, is a deliberate choice+ !this.controller.parentThemesSaved+ ) {+ transition.abort();++ this.dialog.confirm({+ message: i18n("admin.customize.theme.unsaved_parent_themes"),+ confirmButtonLabel: "admin.customize.theme.leave",+ cancelButtonLabel: "admin.customize.theme.stay",+ didConfirm: () => this.#leave(transition),+ didCancel: () => {+ // After render, so we win over the dialog restoring focus on close+ schedule("afterRender", () =>+ document+ .querySelector(".parent-themes-setting .select-kit-header")+ ?.focus()+ );},- didCancel: () => model.set("recentlyInstalled", false),});}}++ #leave(transition) {+ // `data` is carried over to the retried transition+ transition.data.skipLeaveWarnings = true;+ transition.retry();+ }++ #isDeleted(model) {+ return !this.modelFor("adminCustomizeThemes").content.includes(model);+ }++ #staysOnTheme(transition, model) {+ const themeId = transition.to?.find((info) => info.params?.theme_id)?.params+ .theme_id;+ return parseInt(themeId, 10) === model.id;+ }}diff --git a/frontend/discourse/admin/templates/admin-customize-themes/show/index.gjs b/frontend/discourse/admin/templates/admin-customize-themes/show/index.gjsindex 4106cee1ac2..ba0fb473d22 100644--- a/frontend/discourse/admin/templates/admin-customize-themes/show/index.gjs+++ b/frontend/discourse/admin/templates/admin-customize-themes/show/index.gjs@@ -270,6 +270,7 @@ export default <template><ThemeSettingRelativesSelectorclass="theme-setting"@model={{@controller.model}}+ @onSave={{@controller.markParentThemesSaved}}@setting={{@controller.relativesSelectorSettingsForComponent}}/></div>diff --git a/frontend/discourse/app/instance-initializers/live-development.js b/frontend/discourse/app/instance-initializers/live-development.jsindex 192c4587378..777e90053ee 100644--- a/frontend/discourse/app/instance-initializers/live-development.js+++ b/frontend/discourse/app/instance-initializers/live-development.js@@ -54,9 +54,13 @@ class LiveDevelopmentInit {} else if (me === "development-mode-theme-changed") {if (window.location.pathname.startsWith("/admin/customize/themes") ||+ window.location.pathname.startsWith("/admin/customize/components") ||window.location.pathname.startsWith("/admin/config/customize/themes") ||+ window.location.pathname.startsWith(+ "/admin/config/customize/components"+ ) ||window.location.pathname.startsWith("/admin/config/look-and-feel")) {// Don't refresh users on routes which make theme changes - would be very inconvenient.diff --git a/spec/system/admin_customize_components_config_area_spec.rb b/spec/system/admin_customize_components_config_area_spec.rbindex 8547cf8c717..5368fdfa1aa 100644--- a/spec/system/admin_customize_components_config_area_spec.rb+++ b/spec/system/admin_customize_components_config_area_spec.rb@@ -362,4 +362,17 @@ describe "Admin Customize Themes Config Area Page" doexpect(page).to have_current_path(%r{/admin/customize/components/\d+})end++ it "warns when leaving a newly created component that isn't assigned to any themes" do+ theme_page = PageObjects::Pages::AdminCustomizeThemes.new++ config_area.visit.click_install_button.create_new_theme(+ name: "some new component",+ component: true,+ )+ expect(page).to have_current_path(%r{/admin/customize/components/\d+})++ theme_page.click_back_to_components+ expect(dialog).to have_content(I18n.t("admin_js.admin.customize.theme.unsaved_parent_themes"))+ endenddiff --git a/spec/system/admin_customize_themes_spec.rb b/spec/system/admin_customize_themes_spec.rbindex 0ab5e5161dd..2e75707824f 100644--- a/spec/system/admin_customize_themes_spec.rb+++ b/spec/system/admin_customize_themes_spec.rb@@ -298,6 +298,123 @@ describe "Admin Customize Themes" do"#{theme.name}, Foundation, Horizon",)end++ describe "when leaving the page" do+ let(:unassigned_warning) { I18n.t("admin_js.admin.customize.theme.unsaved_parent_themes") }++ it "warns when the component is enabled but not used by any themes" do+ theme_page.visit(component)+ theme_page.click_back_to_components+ expect(dialog).to have_content(unassigned_warning)++ dialog.click_no+ expect(page).to have_current_path("/admin/customize/themes/#{component.id}")+ expect(page).to have_css(".parent-themes-setting .select-kit-header:focus")++ theme_page.click_back_to_components+ expect(dialog).to have_content(unassigned_warning)++ dialog.click_yes+ expect(page).to have_current_path("/admin/config/customize/components")+ end++ it "doesn't warn after the admin saves an empty theme selection" do+ theme.add_relative_theme!(:child, component)++ theme_page.visit(component)+ theme_page.parent_themes_selector.expand+ theme_page.parent_themes_selector.unselect_by_name(theme.name)+ theme_page.parent_themes_selector.collapse+ find(".parent-themes-setting .setting-controls .ok").click+ expect(page).to have_no_css(".parent-themes-setting .setting-controls")+ expect(component.reload.parent_themes).to be_empty++ theme_page.click_back_to_components++ expect(page).to have_current_path("/admin/config/customize/components")+ expect(dialog).to be_closed+ end++ it "doesn't warn when the component is disabled" do+ component.update!(enabled: false)++ theme_page.visit(component)+ theme_page.click_back_to_components++ expect(page).to have_current_path("/admin/config/customize/components")+ expect(dialog).to be_closed+ end++ it "doesn't warn when the component is used by a theme" do+ theme.add_relative_theme!(:child, component)++ theme_page.visit(component)+ theme_page.click_back_to_components++ expect(page).to have_current_path("/admin/config/customize/components")+ expect(dialog).to be_closed+ end++ it "doesn't warn when opening the component's code editor" do+ theme_page.visit(component)+ theme_page.click_edit_code++ expect(page).to have_current_path(%r{/#{component.id}/common/scss/edit})+ expect(dialog).to be_closed+ end++ context "with unsaved setting changes" do+ let(:unsaved_warning) { I18n.t("admin_js.admin.customize.theme.unsaved_changes_alert") }++ before do+ theme.add_relative_theme!(:child, component)+ component.set_field(target: :settings, name: "yaml", value: "greeting: hello")+ component.save!+ end++ it "lets the admin stay on the page" do+ theme_page.visit(component).fill_in_setting("greeting", "howdy")+ theme_page.click_back_to_components+ expect(dialog).to have_content(unsaved_warning)++ dialog.click_no+ expect(page).to have_current_path("/admin/customize/themes/#{component.id}")+ expect(theme_page).to have_setting_value("greeting", "howdy")+ end++ it "discards the changes when leaving" do+ theme_page.visit(component).fill_in_setting("greeting", "howdy")+ theme_page.click_back_to_components+ expect(dialog).to have_content(unsaved_warning)++ dialog.click_danger+ expect(page).to have_current_path("/admin/config/customize/components")++ page.go_back+ expect(theme_page).to have_setting_value("greeting", "hello")+ end+ end++ it "warns about an unsaved theme selection instead of the component being unused" do+ theme_page.visit(component)+ theme_page.parent_themes_selector.expand+ theme_page.parent_themes_selector.select_row_by_index(0)+ theme_page.parent_themes_selector.collapse+ theme_page.click_back_to_components++ expect(dialog).to have_content(+ I18n.t("admin_js.admin.customize.theme.unsaved_changes_alert"),+ )+ end++ it "doesn't warn after deleting the component" do+ theme_page.visit(component)+ theme_page.click_delete_button_and_confirm++ expect(page).to have_current_path("/admin/config/customize/themes")+ expect(dialog).to be_closed+ end+ endenddescribe "changing theme source" dodiff --git a/spec/system/page_objects/pages/admin_customize_themes.rb b/spec/system/page_objects/pages/admin_customize_themes.rbindex d8cefa61cad..5c4f649cb55 100644--- a/spec/system/page_objects/pages/admin_customize_themes.rb+++ b/spec/system/page_objects/pages/admin_customize_themes.rb@@ -178,6 +178,25 @@ module PageObjects)end+ def click_back_to_components+ find('.back-to-themes-and-components a[href="/admin/config/customize/components"]').click+ end++ def fill_in_setting(setting_name, value)+ find("#{setting_selector(setting_name)} input.input-setting-string").fill_in(with: value)+ self+ end++ def has_setting_value?(setting_name, value)+ has_css?("#{setting_selector(setting_name)} input.input-setting-string") do |input|+ input.value == value+ end+ end++ def click_edit_code+ find(".edit-code").click+ end+def click_add_all_themes_buttonfind(".relative-theme-selector .setting-label .btn-link").clickfind(".setting-controls .ok").click@@ -257,6 +276,10 @@ module PageObjectsfind(".dialog-footer .btn-danger").clickend+ def parent_themes_selector+ PageObjects::Components::SelectKit.new(".parent-themes-setting .select-kit")+ end+def included_components_selectorPageObjects::Components::SelectKit.new(".included-components-setting .select-kit")end