Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

settings: Handle invalid section values in URL hash.

Sahil Batrae957920cc0ea
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 8de00c5c6b9..011856f4a52 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -6,11 +6,13 @@ import * as z from "zod/mini";
import * as blueslip from "./blueslip.ts";
import * as channel_folders from "./channel_folders.ts";
import * as filter_util from "./filter_util.ts";
+import * as hash_parser from "./hash_parser.ts";
import * as internal_url from "./internal_url.ts";
import type {Message} from "./message_store.ts";
import * as people from "./people.ts";
import {web_channel_default_view_values} from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
+import type {SettingsPanelMenu} from "./settings_panel_menu.ts";
import {
current_user,
narrow_canonical_term_schema,
@@ -370,6 +372,119 @@ export function validate_group_settings_hash(hash: string): string {
return hash;
}
+function handle_invalid_settings_tab(
+ base: string,
+ section: "bots" | "users",
+ settings_tab: string,
+): string {
+ const valid_tab_values = {
+ users: new Set(["active", "imported", "deactivated", "invitations"]),
+ bots: new Set(["all-bots", "your-bots"]),
+ };
+
+ if (!valid_tab_values[section].has(settings_tab)) {
+ let default_tab = [...valid_tab_values[section]][0]!;
+ if (section === "bots" && base === "settings") {
+ // For bots panel in "Personal" tab we open "Your bots"
+ // tab by default.
+ default_tab = "your-bots";
+ }
+ return default_tab;
+ }
+ return settings_tab;
+}
+
+function get_settings_tab(base: string, section: string): string | undefined {
+ if (section === "users" || section === "bots") {
+ const current_settings_tab = hash_parser.get_current_nth_hash_section(2);
+ return handle_invalid_settings_tab(base, section, current_settings_tab);
+ }
+ return undefined;
+}
+
+export function validate_settings_hash(
+ hash: string,
+ settings_panel_object: SettingsPanelMenu,
+): string {
+ const hash_components = hash.slice(1).split(/\//);
+ const base = hash_components[0]!;
+ let section = hash_components[1];
+
+ if (!section) {
+ const settings_tab = settings_panel_object.get_settings_tab(
+ settings_panel_object.current_tab,
+ );
+ if (settings_tab) {
+ return `#${base}/${settings_panel_object.current_tab}/${settings_tab}`;
+ }
+ return `#${base}/${settings_panel_object.current_tab}`;
+ }
+
+ let settings_tab = get_settings_tab(base, section);
+
+ if (base === "settings" && section === "display-settings") {
+ // Since display-settings was deprecated and replaced with preferences
+ // #settings/display-settings is being redirected to #settings/preferences.
+ section = "preferences";
+ }
+ if (base === "organization" && section === "bot-list-admin") {
+ // #organization/bot-list-admin is being redirected to #organization/bots/all-bots.
+ section = "bots";
+ settings_tab = "all-bots";
+ }
+ if (base === "organization" && section === "user-list-admin") {
+ // #organization/user-list-admin is being redirected to #organization/users/active
+ // after it was renamed.
+ section = "users";
+ settings_tab = "active";
+ }
+ if (base === "settings" && section === "your-bots") {
+ // #settings/your-bots is being redirected to #settings/bots/your-bots.
+ section = "bots";
+ settings_tab = "your-bots";
+ }
+
+ const valid_personal_section_values = new Set([
+ "profile",
+ "account-and-privacy",
+ "preferences",
+ "notifications",
+ "bots",
+ "alert-words",
+ "uploaded-files",
+ "topics",
+ "muted-users",
+ ]);
+ const valid_organization_section_values = new Set([
+ "organization-profile",
+ "organization-settings",
+ "organization-permissions",
+ "emoji-settings",
+ "linkifier-settings",
+ "playground-settings",
+ "users",
+ "bots",
+ "profile-field-settings",
+ "organization-level-user-defaults",
+ "channel-folders",
+ "default-channels-list",
+ "auth-methods",
+ "data-exports-admin",
+ ]);
+
+ if (base === "settings" && !valid_personal_section_values.has(section)) {
+ return "#settings/profile";
+ }
+ if (base === "organization" && !valid_organization_section_values.has(section)) {
+ return "#organization/organization-profile";
+ }
+
+ if (settings_tab) {
+ return `#${base}/${section}/${settings_tab}`;
+ }
+ return `#${base}/${section}`;
+}
+
export function decode_dm_recipient_user_ids_from_narrow_url(narrow_url: string): number[] | null {
try {
const url = new URL(narrow_url, window.location.origin);
diff --git a/web/src/hashchange.ts b/web/src/hashchange.ts
index 5c27fcc28f2..e04fda7c1d2 100644
--- a/web/src/hashchange.ts
+++ b/web/src/hashchange.ts
@@ -72,34 +72,12 @@ function is_somebody_else_profile_open(): boolean {
);
}
-function handle_invalid_settings_tab(
- base: string,
- section: "bots" | "users",
- settings_tab: string,
-): string {
- const valid_tab_values = {
- users: new Set(["active", "imported", "deactivated", "invitations"]),
- bots: new Set(["all-bots", "your-bots"]),
- };
-
- if (!valid_tab_values[section].has(settings_tab)) {
- let default_tab = [...valid_tab_values[section]][0]!;
- if (section === "bots" && base === "settings") {
- // For bots panel in "Personal" tab we open "Your bots"
- // tab by default.
- default_tab = "your-bots";
- }
- const valid_section_url = `#${base}/${section}/${default_tab}`;
- window.history.replaceState(null, "", browser_history.get_full_url(valid_section_url));
- return default_tab;
- }
- return settings_tab;
-}
-
-function get_settings_tab(base: string, section: string): string | undefined {
+function get_settings_tab(section: string): string | undefined {
if (section === "users" || section === "bots") {
const current_settings_tab = hash_parser.get_current_nth_hash_section(2);
- return handle_invalid_settings_tab(base, section, current_settings_tab);
+ // URL will be updated in hash_util.validate_settings_hash to contain
+ // the correct tab value.
+ return current_settings_tab;
}
return undefined;
}
@@ -299,50 +277,20 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
}
const coming_from_overlay = hash_parser.is_overlay_hash(old_hash);
- if (base === "settings" && section === "display-settings") {
- // Since display-settings was deprecated and replaced with preferences
- // #settings/display-settings is being redirected to #settings/preferences.
- section = "preferences";
- window.history.replaceState(
- null,
- "",
- browser_history.get_full_url("#settings/preferences"),
- );
- }
- if (base === "organization" && section === "bot-list-admin") {
- // #organization/bot-list-admin is being redirected to #organization/bots/all-bots.
- section = "bots";
- window.history.replaceState(
- null,
- "",
- browser_history.get_full_url("#organization/bots/all-bots"),
- );
- }
- if (base === "organization" && section === "user-list-admin") {
- // #organization/user-list-admin is being redirected to #organization/users/active
- // after it was renamed.
- section = "users";
- window.history.replaceState(
- null,
- "",
- browser_history.get_full_url("#organization/users/active"),
- );
- }
- if (base === "settings" && section === "your-bots") {
- // #settings/your-bots is being redirected to #settings/bots/your-bots.
- section = "bots";
- window.history.replaceState(null, "", "#settings/bots/your-bots");
- }
- if ((base === "settings" || base === "organization") && !section) {
+ if (base === "settings" || base === "organization") {
let settings_panel_object = settings_panel_menu.normal_settings;
if (base === "organization") {
settings_panel_object = settings_panel_menu.org_settings;
}
- window.history.replaceState(
- null,
- "",
- browser_history.get_full_url(base + "/" + settings_panel_object.current_tab),
+ const valid_hash = hash_util.validate_settings_hash(
+ window.location.hash,
+ settings_panel_object,
);
+
+ if (valid_hash !== window.location.hash) {
+ window.history.replaceState(null, "", browser_history.get_full_url(valid_hash));
+ section = hash_parser.get_hash_section(valid_hash);
+ }
}
// In 2024, stream was renamed to channel in the Zulip API and UI.
@@ -408,7 +356,7 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
}
settings_panel_menu.normal_settings.activate_section_or_default(
section,
- get_settings_tab(base, section),
+ get_settings_tab(section),
);
return;
}
@@ -421,7 +369,7 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
}
settings_panel_menu.org_settings.activate_section_or_default(
section,
- get_settings_tab(base, section),
+ get_settings_tab(section),
);
return;
}
@@ -443,19 +391,17 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
settings_panel_menu.normal_settings.set_current_tab(section);
if (section === "bots") {
settings_panel_menu.normal_settings.set_bot_settings_tab(
- get_settings_tab(base, section)!,
+ get_settings_tab(section)!,
"personal",
);
}
} else {
settings_panel_menu.org_settings.set_current_tab(section);
if (section === "users") {
- settings_panel_menu.org_settings.set_user_settings_tab(
- get_settings_tab(base, section),
- );
+ settings_panel_menu.org_settings.set_user_settings_tab(get_settings_tab(section));
} else if (section === "bots") {
settings_panel_menu.org_settings.set_bot_settings_tab(
- get_settings_tab(base, section)!,
+ get_settings_tab(section)!,
"org",
);
}
@@ -525,14 +471,14 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
if (base === "settings") {
settings.build_page();
admin.build_page();
- settings.launch(section, get_settings_tab(base, section));
+ settings.launch(section, get_settings_tab(section));
return;
}
if (base === "organization") {
settings.build_page();
admin.build_page();
- admin.launch(section, get_settings_tab(base, section));
+ admin.launch(section, get_settings_tab(section));
return;
}
diff --git a/web/tests/hashchange.test.cjs b/web/tests/hashchange.test.cjs
index e665e0434e6..de213bf0b3d 100644
--- a/web/tests/hashchange.test.cjs
+++ b/web/tests/hashchange.test.cjs
@@ -377,6 +377,7 @@ run_test("hash_interactions", ({override, override_rewire}) => {
let fixed_url;
override(history, "replaceState", (_state, _title, url) => {
fixed_url = url;
+ window.location.hash = new URL(url).hash;
});
window.location.hash = "#organization/user-list-admin";