Skip to content

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

FEATURE: Add `routeHistory` service, SessionStore service and make `back to forum` button go back to previous page (#32260)

Gabriel Grubba84659cd2343d
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/back-to-forum.gjs b/app/assets/javascripts/discourse/app/components/sidebar/back-to-forum.gjs
index 011c4605329..c29a3271811 100644
--- a/app/assets/javascripts/discourse/app/components/sidebar/back-to-forum.gjs
+++ b/app/assets/javascripts/discourse/app/components/sidebar/back-to-forum.gjs
@@ -1,13 +1,27 @@
+import Component from "@glimmer/component";
+import { service } from "@ember/service";
import icon from "discourse/helpers/d-icon";
import getURL from "discourse/lib/get-url";
import { i18n } from "discourse-i18n";
-const BackToForum = <template>
- <a href={{getURL "/"}} class="sidebar-sections__back-to-forum">
- {{icon "arrow-left"}}
+export default class BackToForum extends Component {
+ @service routeHistory;
- <span>{{i18n "sidebar.back_to_forum"}}</span>
- </a>
-</template>;
+ get href() {
+ const lastNonAdminUrl = this.routeHistory.history.find(
+ (url) => !url.startsWith("/admin")
+ );
+ if (lastNonAdminUrl) {
+ return getURL(lastNonAdminUrl);
+ }
+ return getURL("/");
+ }
-export default BackToForum;
+ <template>
+ <a href={{this.href}} class="sidebar-sections__back-to-forum">
+ {{icon "arrow-left"}}
+
+ <span>{{i18n "sidebar.back_to_forum"}}</span>
+ </a>
+ </template>
+}
diff --git a/app/assets/javascripts/discourse/app/instance-initializers/init-route-history.js b/app/assets/javascripts/discourse/app/instance-initializers/init-route-history.js
new file mode 100644
index 00000000000..53723361de7
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/instance-initializers/init-route-history.js
@@ -0,0 +1,5 @@
+export default {
+ initialize(owner) {
+ owner.lookup("service:route-history");
+ },
+};
diff --git a/app/assets/javascripts/discourse/app/services/route-history.js b/app/assets/javascripts/discourse/app/services/route-history.js
new file mode 100644
index 00000000000..5fe7b4216f5
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/services/route-history.js
@@ -0,0 +1,50 @@
+import Service, { service } from "@ember/service";
+import { bind } from "discourse/lib/decorators";
+
+const HISTORY_THRESHOLD = 1000;
+// This service is responsible for managing the route history
+// mainly used by the `BackButton` component
+export default class RouteHistory extends Service {
+ @service router;
+ @service sessionStore;
+
+ init() {
+ super.init(...arguments);
+ this.router.on("routeWillChange", this.routeWillChange);
+ }
+
+ willDestroy() {
+ this.router.off("routeWillChange", this.routeWillChange);
+ }
+
+ get history() {
+ const history = this.sessionStore.getObject("routeHistory");
+ if (history === null) {
+ return [];
+ }
+ return history;
+ }
+
+ addToHistory(url) {
+ const history = this.history || [];
+ history.unshift(url);
+ if (history.length > HISTORY_THRESHOLD) {
+ history.pop();
+ }
+ this.sessionStore.setObject({ key: "routeHistory", value: history });
+ }
+
+ @bind
+ routeWillChange() {
+ if (
+ this.router.currentURL !== null &&
+ this.router.currentURL !== this.lastURL // don't add the same URL twice
+ ) {
+ this.addToHistory(this.router.currentURL);
+ }
+ }
+
+ get lastURL() {
+ return this.history[0];
+ }
+}
diff --git a/app/assets/javascripts/discourse/app/services/session-store.js b/app/assets/javascripts/discourse/app/services/session-store.js
new file mode 100644
index 00000000000..d52a59adfb8
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/services/session-store.js
@@ -0,0 +1,121 @@
+import Service from "@ember/service";
+import { isTesting } from "discourse/lib/environment";
+import { disableImplicitInjections } from "discourse/lib/implicit-injections";
+
+const TEST_KEY_PREFIX = "__test_";
+const DISCOURSE_PREFIX = "discourse_";
+
+@disableImplicitInjections
+export default class SessionStoreService extends Service {
+ context = null;
+
+ constructor(ctx = DISCOURSE_PREFIX) {
+ super(...arguments);
+ this.context = isTesting() ? `${TEST_KEY_PREFIX}${ctx}` : ctx;
+ }
+
+ abandonLocal() {
+ return this.removeKeys();
+ }
+
+ removeKeys(predicate = () => true) {
+ if (!sessionStorage) {
+ return;
+ }
+
+ let i = sessionStorage.length - 1;
+
+ while (i >= 0) {
+ let k = sessionStorage.key(i);
+ let v = sessionStorage.getItem(k);
+ try {
+ v = JSON.parse(v);
+ } catch {}
+
+ if (
+ k.substring(0, this.context.length) === this.context &&
+ predicate(k, v)
+ ) {
+ sessionStorage.removeItem(k);
+ }
+ i--;
+ }
+
+ return true;
+ }
+
+ remove(key) {
+ if (!sessionStorage) {
+ return;
+ }
+
+ return sessionStorage.removeItem(this.context + key);
+ }
+
+ get(key) {
+ if (!sessionStorage) {
+ return null;
+ }
+
+ return sessionStorage.getItem(this.context + key);
+ }
+
+ set(opts) {
+ if (!sessionStorage) {
+ return false;
+ }
+
+ sessionStorage.setItem(this.context + opts.key, opts.value);
+ }
+
+ setObject(opts) {
+ this.set({ key: opts.key, value: JSON.stringify(opts.value) });
+ }
+
+ getInt(key, def) {
+ if (!def) {
+ def = 0;
+ }
+
+ if (!sessionStorage) {
+ return def;
+ }
+
+ const result = parseInt(this.get(key), 10);
+ if (!isFinite(result)) {
+ return def;
+ }
+
+ return result;
+ }
+
+ getObject(key) {
+ if (!sessionStorage) {
+ return null;
+ }
+
+ let v = sessionStorage.getItem(this.context + key);
+ if (v === null) {
+ return null;
+ }
+
+ try {
+ v = JSON.parse(v);
+ } catch {}
+
+ return v;
+ }
+
+ // API compatibility with `sessionStorage`
+ getItem(key) {
+ return this.get(key);
+ }
+
+ removeItem(key) {
+ return this.remove(key);
+ }
+
+ setItem(key, value) {
+ this.set({ key, value });
+ }
+}
diff --git a/spec/system/admin_sidebar_navigation_spec.rb b/spec/system/admin_sidebar_navigation_spec.rb
index 2b5dd188dea..7f2b14eca3b 100644
--- a/spec/system/admin_sidebar_navigation_spec.rb
+++ b/spec/system/admin_sidebar_navigation_spec.rb
@@ -23,11 +23,21 @@ describe "Admin | Sidebar Navigation", type: :system do
expect(sidebar).to be_visible
expect(sidebar).to have_no_section("categories")
expect(page).to have_no_css(".admin-main-nav")
+
sidebar.click_back_to_forum
- expect(page).to have_current_path("/")
+ expect(page).to have_current_path("/latest")
expect(sidebar).to have_no_section("admin-root")
end
+ it "goes back to exactly the same page when clicking back to forum" do
+ visit("/hot")
+
+ sidebar.click_link_in_section("community", "admin")
+
+ sidebar.click_back_to_forum
+ expect(page).to have_current_path("/hot")
+ end
+
context "with subfolder" do
before { set_subfolder "/discuss" }
@@ -70,8 +80,10 @@ describe "Admin | Sidebar Navigation", type: :system do
sidebar_dropdown.click
expect(sidebar).to have_no_section("community")
expect(page).to have_no_css(".admin-main-nav")
+
sidebar.click_back_to_forum
- expect(page).to have_current_path("/")
+ expect(page).to have_current_path("/latest")
+
sidebar_dropdown.click
expect(sidebar).to have_no_section("admin-root")
end