A platform for community discussion. Free, open, simple.
UX: add admin config area for welcome banner (#36022)
Kris878d23a1afbb
diff --git a/app/controllers/admin/config/welcome_banner_controller.rb b/app/controllers/admin/config/welcome_banner_controller.rbnew file mode 100644index 00000000000..5f36e99b0cf--- /dev/null+++ b/app/controllers/admin/config/welcome_banner_controller.rb@@ -0,0 +1,23 @@+# frozen_string_literal: true+class Admin::Config::WelcomeBannerController < Admin::AdminController+ def index+ end++ def themes_with_setting+ themes =+ Theme+ .not_components+ .where("themes.id = ? OR themes.user_selectable = ?", SiteSetting.default_theme_id, true)+ .includes(:theme_site_settings)++ themes_data =+ themes.map do |theme|+ setting = theme.theme_site_settings.find { |s| s.name == "enable_welcome_banner" }+ value = setting&.setting_rb_value || false++ { id: theme.id, name: theme.name, enable_welcome_banner: value }+ end++ render json: { themes: themes_data }+ end+enddiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 6abd5c97730..939a4603f20 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -5884,6 +5884,58 @@ en:modal_yes: "Yes"modal_no: "No, only apply going forward"+ welcome_banner:+ title: "Welcome banner"+ header_description: "Configure the banner containing a search bar displayed to visitors on your site."+ saved: "Welcome banner settings saved successfully."+ form:+ enabled_themes:+ label: "Enabled on themes..."+ description: "Select which themes should display the banner. Only active themes are shown."+ background_image:+ label: "Background image"+ description: "This image will be displayed behind the banner text."+ text_color:+ label: "Text color"+ page_visibility:+ label: "Page visibility"+ description: "Choose where the banner should be displayed."+ options:+ top_menu_pages: "Top menu pages"+ homepage: "Homepage only"+ discovery: "Discovery pages"+ all_pages: "All pages"+ location:+ label: "Location"+ description: "Choose where the banner should appear on the page."+ options:+ above_topic_content: "Above topic content"+ below_site_header: "Below site header"+ text_section:+ title: "Customizable text"+ variables: "You can use these variables in your text: <code>%{preferred_display_name}</code>, <code>%{site_name}</code>"+ header_new_members:+ label: "Header for new members"+ placeholder: "Welcome to %{site_name}, %{preferred_display_name}!"+ header_logged_in:+ label: "Header for logged-in members"+ placeholder: "Welcome back, %{preferred_display_name}!"+ header_anonymous:+ label: "Header for anonymous visitors"+ placeholder: "Welcome to %{site_name}!"+ subheader_logged_in:+ label: "Subheader for logged-in members"+ description: "Secondary text shown below the header for logged-in users."+ placeholder: "Check out what's new in the community"+ subheader_anonymous:+ label: "Subheader for anonymous visitors"+ description: "Secondary text shown below the header for anonymous visitors."+ placeholder: "Join our community to participate in discussions"+ search_placeholder:+ label: "Search placeholder"+ description: "Placeholder text for the search box in the welcome banner."+ placeholder: "Search topics..."+navigation:title: "Navigation"header_description: "Configure the navigation links and menu items for your site. This includes the location and behavior of the primary navigation menu, the quick links at the top of the homepage, as well as the admin sidebar"diff --git a/config/routes.rb b/config/routes.rbindex df898ee0613..cd1d819782b 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -443,6 +443,8 @@ Discourse::Application.routes.draw doget "group-permissions" => "site_settings#index"get "/logo" => "logo#index"get "/fonts" => "fonts#index"+ get "/welcome-banner/themes-with-setting" => "welcome_banner#themes_with_setting"+ get "/welcome-banner" => "welcome_banner#index"put "/logo" => "logo#update"put "/fonts" => "fonts#update"get "colors/:id" => "color_palettes#show"diff --git a/frontend/discourse/admin/components/admin-welcome-banner-form.gjs b/frontend/discourse/admin/components/admin-welcome-banner-form.gjsnew file mode 100644index 00000000000..6ddf8d70d23--- /dev/null+++ b/frontend/discourse/admin/components/admin-welcome-banner-form.gjs@@ -0,0 +1,514 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { fn } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import { htmlSafe } from "@ember/template";+import ColorInput from "discourse/admin/components/color-input";+import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";+import DMultiSelect from "discourse/components/d-multi-select";+import Form from "discourse/components/form";+import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import getURL from "discourse/lib/get-url";+import I18n, { i18n } from "discourse-i18n";++export default class AdminWelcomeBannerForm extends Component {+ @service siteSettings;+ @service siteSettingChangeTracker;+ @service toasts;++ @tracked formData = {};+ @tracked isLoading = true;+ @tracked allThemes = [];+ formApi;+ originalFormData = {};++ constructor() {+ super(...arguments);+ this.loadData();+ }++ @action+ registerApi(api) {+ this.formApi = api;+ }++ @action+ handleUpload(fieldName, upload, { set }) {+ if (upload) {+ set(fieldName, getURL(upload.url));+ } else {+ set(fieldName, undefined);+ }+ }++ async loadData() {+ await this.loadSettings();+ await this.loadThemesWithSettings();+ this.isLoading = false;+ }++ async loadSettings() {+ const data = {+ welcomeBannerImage: this.siteSettings.welcome_banner_image,+ welcomeBannerTextColor: this.siteSettings.welcome_banner_text_color,+ welcomeBannerLocation: this.siteSettings.welcome_banner_location,+ welcomeBannerPageVisibility:+ this.siteSettings.welcome_banner_page_visibility,+ enabledThemes: [],+ };++ try {+ const response = await ajax("/admin/customize/site_texts.json", {+ data: {+ q: "welcome_banner",+ locale: I18n.currentLocale(),+ },+ });++ const texts = response.site_texts;+ texts.forEach((text) => {+ switch (text.id) {+ case "js.welcome_banner.header.new_members":+ data.headerNewMembers = text.value;+ break;+ case "js.welcome_banner.header.logged_in_members":+ data.headerLoggedInMembers = text.value;+ break;+ case "js.welcome_banner.header.anonymous_members":+ data.headerAnonymousMembers = text.value;+ break;+ case "js.welcome_banner.subheader.logged_in_members":+ data.subheaderLoggedInMembers = text.value;+ break;+ case "js.welcome_banner.subheader.anonymous_members":+ data.subheaderAnonymousMembers = text.value;+ break;+ case "js.welcome_banner.search_placeholder":+ data.searchPlaceholder = text.value;+ break;+ }+ });+ } catch (error) {+ popupAjaxError(error);+ }++ this.formData = data;+ this.originalFormData = { ...data };+ }++ async loadThemesWithSettings() {+ try {+ const response = await ajax(+ "/admin/config/welcome-banner/themes-with-setting.json"+ );++ this.allThemes = response.themes.map((themeData) => ({+ id: themeData.id,+ name: themeData.name,+ enable_welcome_banner: themeData.enable_welcome_banner,+ }));++ const enabledThemes = this.allThemes.filter(+ (theme) => theme.enable_welcome_banner+ );++ this.formData = {+ ...this.formData,+ enabledThemes,+ };+ this.originalFormData = {+ ...this.originalFormData,+ enabledThemes,+ };+ } catch (error) {+ popupAjaxError(error);+ }+ }++ @action+ async loadThemes(filter) {+ if (!filter) {+ return this.allThemes;+ }+ return this.allThemes.filter((theme) =>+ theme.name.toLowerCase().includes(filter.toLowerCase())+ );+ }++ @action+ async save(data) {+ let siteTextsChanged = false;++ try {+ if (+ data.welcomeBannerImage !== this.originalFormData.welcomeBannerImage+ ) {+ await ajax("/admin/site_settings/welcome_banner_image", {+ type: "PUT",+ data: { welcome_banner_image: data.welcomeBannerImage || "" },+ });+ }++ if (+ data.welcomeBannerTextColor !==+ this.originalFormData.welcomeBannerTextColor+ ) {+ await ajax("/admin/site_settings/welcome_banner_text_color", {+ type: "PUT",+ data: {+ welcome_banner_text_color: data.welcomeBannerTextColor || "",+ },+ });+ }++ if (+ data.welcomeBannerLocation !==+ this.originalFormData.welcomeBannerLocation+ ) {+ await ajax("/admin/site_settings/welcome_banner_location", {+ type: "PUT",+ data: { welcome_banner_location: data.welcomeBannerLocation },+ });+ }++ if (+ data.welcomeBannerPageVisibility !==+ this.originalFormData.welcomeBannerPageVisibility+ ) {+ await ajax("/admin/site_settings/welcome_banner_page_visibility", {+ type: "PUT",+ data: {+ welcome_banner_page_visibility: data.welcomeBannerPageVisibility,+ },+ });+ }++ const locale = I18n.currentLocale();+ const siteTextUpdates = [+ {+ id: "js.welcome_banner.header.new_members",+ value: data.headerNewMembers,+ },+ {+ id: "js.welcome_banner.header.logged_in_members",+ value: data.headerLoggedInMembers,+ },+ {+ id: "js.welcome_banner.header.anonymous_members",+ value: data.headerAnonymousMembers,+ },+ {+ id: "js.welcome_banner.subheader.logged_in_members",+ value: data.subheaderLoggedInMembers,+ },+ {+ id: "js.welcome_banner.subheader.anonymous_members",+ value: data.subheaderAnonymousMembers,+ },+ {+ id: "js.welcome_banner.search_placeholder",+ value: data.searchPlaceholder,+ },+ ];++ const originalTextValues = {+ "js.welcome_banner.header.new_members":+ this.originalFormData.headerNewMembers,+ "js.welcome_banner.header.logged_in_members":+ this.originalFormData.headerLoggedInMembers,+ "js.welcome_banner.header.anonymous_members":+ this.originalFormData.headerAnonymousMembers,+ "js.welcome_banner.subheader.logged_in_members":+ this.originalFormData.subheaderLoggedInMembers,+ "js.welcome_banner.subheader.anonymous_members":+ this.originalFormData.subheaderAnonymousMembers,+ "js.welcome_banner.search_placeholder":+ this.originalFormData.searchPlaceholder,+ };++ for (const text of siteTextUpdates) {+ const originalValue = originalTextValues[text.id] || "";+ const newValue = text.value || "";++ if (newValue !== originalValue) {+ if (!newValue || newValue.trim() === "") {+ await ajax(+ `/admin/customize/site_texts/${text.id}?locale=${locale}`,+ {+ type: "DELETE",+ }+ );+ } else {+ await ajax(+ `/admin/customize/site_texts/${text.id}?locale=${locale}`,+ {+ type: "PUT",+ data: {+ site_text: {+ value: newValue,+ locale,+ },+ },+ }+ );+ }+ siteTextsChanged = true;+ }+ }++ await Promise.allSettled(+ this.allThemes.map((theme) => {+ const shouldBeEnabled = data.enabledThemes.some(+ (t) => t.id === theme.id+ );++ return ajax(`/admin/themes/${theme.id}/site-setting`, {+ type: "PUT",+ data: {+ name: "enable_welcome_banner",+ value: shouldBeEnabled,+ },+ });+ })+ );++ this.originalFormData = { ...data };++ this.toasts.success({+ duration: 3000,+ data: {+ message: i18n("admin.config.welcome_banner.saved"),+ },+ });++ if (siteTextsChanged) {+ this.siteSettingChangeTracker.refreshPage({+ welcome_banner_image: data.welcomeBannerImage,+ welcome_banner_text_color: data.welcomeBannerTextColor,+ welcome_banner_location: data.welcomeBannerLocation,+ welcome_banner_page_visibility: data.welcomeBannerPageVisibility,+ });+ }+ } catch (error) {+ popupAjaxError(error);+ }+ }++ <template>+ <ConditionalLoadingSpinner @condition={{this.isLoading}}>+ <Form+ @onSubmit={{this.save}}+ @data={{this.formData}}+ @onRegisterApi={{this.registerApi}}+ class="admin-welcome-banner-form"+ as |form|+ >+ <form.Field+ @name="enabledThemes"+ @title={{i18n+ "admin.config.welcome_banner.form.enabled_themes.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.enabled_themes.description"+ }}+ as |field|+ >+ <field.Custom>+ <DMultiSelect+ @loadFn={{this.loadThemes}}+ @selection={{field.value}}+ @onChange={{field.set}}+ >+ <:selection as |theme|>+ {{theme.name}}+ </:selection>+ <:result as |theme|>+ {{theme.name}}+ </:result>+ </DMultiSelect>+ </field.Custom>+ </form.Field>++ <form.Field+ @name="welcomeBannerImage"+ @title={{i18n+ "admin.config.welcome_banner.form.background_image.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.background_image.description"+ }}+ @onSet={{fn this.handleUpload "welcomeBannerImage"}}+ as |field|+ >+ <field.Image @type="site_setting" />+ </form.Field>++ <form.Field+ @name="welcomeBannerTextColor"+ @title={{i18n "admin.config.welcome_banner.form.text_color.label"}}+ @format="large"+ as |field|+ >+ <field.Custom>+ <ColorInput+ @hexValue={{readonly field.value}}+ @onlyHex={{false}}+ @styleSelection={{false}}+ @onChangeColor={{field.set}}+ />+ </field.Custom>+ </form.Field>++ <form.Field+ @name="welcomeBannerPageVisibility"+ @title={{i18n+ "admin.config.welcome_banner.form.page_visibility.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.page_visibility.description"+ }}+ as |field|+ >+ <field.Select @includeNone={{false}} as |select|>+ <select.Option @value="top_menu_pages">{{i18n+ "admin.config.welcome_banner.form.page_visibility.options.top_menu_pages"+ }}</select.Option>+ <select.Option @value="homepage">{{i18n+ "admin.config.welcome_banner.form.page_visibility.options.homepage"+ }}</select.Option>+ <select.Option @value="discovery">{{i18n+ "admin.config.welcome_banner.form.page_visibility.options.discovery"+ }}</select.Option>+ <select.Option @value="all_pages">{{i18n+ "admin.config.welcome_banner.form.page_visibility.options.all_pages"+ }}</select.Option>+ </field.Select>+ </form.Field>++ <form.Field+ @name="welcomeBannerLocation"+ @title={{i18n "admin.config.welcome_banner.form.location.label"}}+ @description={{i18n+ "admin.config.welcome_banner.form.location.description"+ }}+ as |field|+ >+ <field.Select @includeNone={{false}} as |select|>+ <select.Option @value="above_topic_content">{{i18n+ "admin.config.welcome_banner.form.location.options.above_topic_content"+ }}</select.Option>+ <select.Option @value="below_site_header">{{i18n+ "admin.config.welcome_banner.form.location.options.below_site_header"+ }}</select.Option>+ </field.Select>+ </form.Field>++ <form.Section+ @title={{i18n "admin.config.welcome_banner.form.text_section.title"}}+ @subtitle={{htmlSafe+ (i18n "admin.config.welcome_banner.form.text_section.variables")+ }}+ >++ <form.Field+ @name="headerNewMembers"+ @title={{i18n+ "admin.config.welcome_banner.form.header_new_members.label"+ }}+ @format="large"+ @validation="required"+ as |field|+ >+ <field.Input+ placeholder={{i18n+ "admin.config.welcome_banner.form.header_new_members.placeholder"+ }}+ />+ </form.Field>++ <form.Field+ @name="headerLoggedInMembers"+ @title={{i18n+ "admin.config.welcome_banner.form.header_logged_in.label"+ }}+ @format="large"+ @validation="required"+ as |field|+ >+ <field.Input+ placeholder={{i18n+ "admin.config.welcome_banner.form.header_logged_in.placeholder"+ }}+ />+ </form.Field>++ <form.Field+ @name="headerAnonymousMembers"+ @title={{i18n+ "admin.config.welcome_banner.form.header_anonymous.label"+ }}+ @format="large"+ @validation="required"+ as |field|+ >+ <field.Input+ placeholder={{i18n+ "admin.config.welcome_banner.form.header_anonymous.placeholder"+ }}+ />+ </form.Field>++ <form.Field+ @name="subheaderLoggedInMembers"+ @title={{i18n+ "admin.config.welcome_banner.form.subheader_logged_in.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.subheader_logged_in.description"+ }}+ @format="large"+ as |field|+ >+ <field.Textarea />+ </form.Field>++ <form.Field+ @name="subheaderAnonymousMembers"+ @title={{i18n+ "admin.config.welcome_banner.form.subheader_anonymous.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.subheader_anonymous.description"+ }}+ @format="large"+ as |field|+ >+ <field.Textarea />+ </form.Field>++ <form.Field+ @name="searchPlaceholder"+ @title={{i18n+ "admin.config.welcome_banner.form.search_placeholder.label"+ }}+ @description={{i18n+ "admin.config.welcome_banner.form.search_placeholder.description"+ }}+ @format="large"+ @validation="required"+ as |field|+ >+ <field.Input+ placeholder={{i18n+ "admin.config.welcome_banner.form.search_placeholder.placeholder"+ }}+ />+ </form.Field>+ </form.Section>+ <form.Submit />+ </Form>+ </ConditionalLoadingSpinner>+ </template>+}diff --git a/frontend/discourse/admin/routes/admin-config/welcome-banner.js b/frontend/discourse/admin/routes/admin-config/welcome-banner.jsnew file mode 100644index 00000000000..69a155229b5--- /dev/null+++ b/frontend/discourse/admin/routes/admin-config/welcome-banner.js@@ -0,0 +1,8 @@+import DiscourseRoute from "discourse/routes/discourse";+import { i18n } from "discourse-i18n";++export default class AdminConfigWelcomeBannerRoute extends DiscourseRoute {+ titleToken() {+ return i18n("admin.config.welcome_banner.title");+ }+}diff --git a/frontend/discourse/admin/routes/admin-route-map.js b/frontend/discourse/admin/routes/admin-route-map.jsindex 065fec74ac9..3bf771e4478 100644--- a/frontend/discourse/admin/routes/admin-route-map.js+++ b/frontend/discourse/admin/routes/admin-route-map.js@@ -369,6 +369,7 @@ export default function () {});this.route("logo");this.route("fonts");+ this.route("welcomeBanner", { path: "/welcome-banner" });this.route("navigation", function () {this.route("settings", { path: "/" });});diff --git a/frontend/discourse/admin/templates/admin-config/welcome-banner.gjs b/frontend/discourse/admin/templates/admin-config/welcome-banner.gjsnew file mode 100644index 00000000000..3a14055cbbd--- /dev/null+++ b/frontend/discourse/admin/templates/admin-config/welcome-banner.gjs@@ -0,0 +1,29 @@+import AdminWelcomeBannerForm from "discourse/admin/components/admin-welcome-banner-form";+import DBreadcrumbsItem from "discourse/components/d-breadcrumbs-item";+import DPageHeader from "discourse/components/d-page-header";+import { i18n } from "discourse-i18n";++export default <template>+ <div class="admin-config-page">+ <DPageHeader+ @hideTabs={{true}}+ @titleLabel={{i18n "admin.config.welcome_banner.title"}}+ @descriptionLabel={{i18n+ "admin.config.welcome_banner.header_description"+ }}+ >+ <:breadcrumbs>+ <DBreadcrumbsItem @path="/admin" @label={{i18n "admin_title"}} />+ <DBreadcrumbsItem+ @path="/admin/config/welcome-banner"+ @label={{i18n "admin.config.welcome_banner.title"}}+ />+ </:breadcrumbs>+ </DPageHeader>+ <div class="admin-config-area">+ <div class="admin-config-area__primary-content">+ <AdminWelcomeBannerForm />+ </div>+ </div>+ </div>+</template>diff --git a/frontend/discourse/app/lib/sidebar/admin-nav-map.js b/frontend/discourse/app/lib/sidebar/admin-nav-map.jsindex e946268eda5..426e7fb7ff7 100644--- a/frontend/discourse/app/lib/sidebar/admin-nav-map.js+++ b/frontend/discourse/app/lib/sidebar/admin-nav-map.js@@ -301,6 +301,13 @@ export const ADMIN_NAV_MAP = [},],},+ {+ name: "admin_welcome_banner",+ route: "adminConfig.welcomeBanner",+ label: "admin.config.welcome_banner.title",+ description: "admin.config.welcome_banner.header_description",+ icon: "sign-hanging",+ },{name: "admin_customize_site_texts",route: "adminSiteText",diff --git a/lib/svg_sprite.rb b/lib/svg_sprite.rbindex eae04227126..1704fe6f48a 100644--- a/lib/svg_sprite.rb+++ b/lib/svg_sprite.rb@@ -232,6 +232,7 @@ module SvgSpriteshield-halvedshufflesignal+ sign-hangingslidersspinnersquare-checkdiff --git a/spec/requests/admin/config/welcome_banner_controller_spec.rb b/spec/requests/admin/config/welcome_banner_controller_spec.rbnew file mode 100644index 00000000000..2c7231116ca--- /dev/null+++ b/spec/requests/admin/config/welcome_banner_controller_spec.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++RSpec.describe Admin::Config::WelcomeBannerController do+ fab!(:admin)+ fab!(:moderator)+ fab!(:user)++ describe "#themes_with_setting" do+ fab!(:default_theme) { Fabricate(:theme, user_selectable: false) }+ fab!(:user_selectable_theme) { Fabricate(:theme, user_selectable: true) }+ fab!(:non_selectable_theme) { Fabricate(:theme, user_selectable: false) }+ fab!(:component) { Fabricate(:theme, component: true) }++ before { SiteSetting.default_theme_id = default_theme.id }++ context "when logged in as an admin" do+ before { sign_in(admin) }++ it "returns user selectable themes and the default theme" do+ get "/admin/config/welcome-banner/themes-with-setting.json"++ expect(response.status).to eq(200)++ themes = response.parsed_body["themes"]+ theme_ids = themes.map { |t| t["id"] }++ expect(theme_ids).to include(default_theme.id)+ expect(theme_ids).to include(user_selectable_theme.id)+ expect(theme_ids).not_to include(non_selectable_theme.id)+ end++ it "excludes components" do+ get "/admin/config/welcome-banner/themes-with-setting.json"++ expect(response.status).to eq(200)++ theme_ids = response.parsed_body["themes"].map { |t| t["id"] }+ expect(theme_ids).not_to include(component.id)+ end++ it "returns enable_welcome_banner setting value when present" do+ Fabricate(+ :theme_site_setting,+ theme: user_selectable_theme,+ name: "enable_welcome_banner",+ value: true,+ )++ get "/admin/config/welcome-banner/themes-with-setting.json"++ expect(response.status).to eq(200)++ theme_data = response.parsed_body["themes"].find { |t| t["id"] == user_selectable_theme.id }+ expect(theme_data["enable_welcome_banner"]).to eq(true)+ end++ it "returns false for enable_welcome_banner when setting is not present" do+ get "/admin/config/welcome-banner/themes-with-setting.json"++ expect(response.status).to eq(200)++ theme_data = response.parsed_body["themes"].find { |t| t["id"] == user_selectable_theme.id }+ expect(theme_data["enable_welcome_banner"]).to eq(false)+ end+ end++ context "when logged in as a moderator" do+ before { sign_in(moderator) }++ it "denies access with a 403 response" do+ get "/admin/config/welcome-banner/themes-with-setting.json"+ expect(response.status).to eq(403)+ end+ end++ context "when logged in as a non-staff user" do+ before { sign_in(user) }++ it "denies access with a 404 response" do+ get "/admin/config/welcome-banner/themes-with-setting.json"+ expect(response.status).to eq(404)+ end+ end+ end+end