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.

integrations: Convert module to TypeScript.

Anders Kaseorgdd3ed187f1c9
diff --git a/web/src/portico/integrations.js b/web/src/portico/integrations.ts
similarity index 78%
rename from web/src/portico/integrations.js
rename to web/src/portico/integrations.ts
index a69477b8c7f..c954ac4a156 100644
--- a/web/src/portico/integrations.js
+++ b/web/src/portico/integrations.ts
@@ -1,5 +1,7 @@
import $ from "jquery";
import _ from "lodash";
+import assert from "minimalistic-assert";
+import {z} from "zod";
import * as blueslip from "../blueslip.ts";
import * as common from "../common.ts";
@@ -10,10 +12,10 @@ import {path_parts} from "./landing-page.ts";
// these constants are populated immediately with data from the DOM on page load
// name -> display name
-const INTEGRATIONS = new Map();
-const CATEGORIES = new Map();
+const INTEGRATIONS = new Map<string, string>();
+const CATEGORIES = new Map<string, string>();
-function load_data() {
+function load_data(): void {
for (const integration of $(".integration-lozenge")) {
const name = $(integration).attr("data-name");
const display_name = $(integration).find(".integration-name").text().trim();
@@ -33,7 +35,9 @@ function load_data() {
}
}
-const INITIAL_STATE = {
+type State = {category: string; integration: string | null; query: string};
+
+const INITIAL_STATE: State = {
category: "all",
integration: null,
query: "",
@@ -41,29 +45,29 @@ const INITIAL_STATE = {
let state = {...INITIAL_STATE};
-function adjust_font_sizing() {
+function adjust_font_sizing(): void {
for (const integration of $(".integration-lozenge")) {
const $integration_name = $(integration).find(".integration-name");
const $integration_category = $(integration).find(".integration-category");
// if the text has wrapped to two lines, decrease font-size
- if ($integration_name.height() > 30) {
+ if (($integration_name.height() ?? 0) > 30) {
$integration_name.css("font-size", "1em");
- if ($integration_name.height() > 30) {
+ if (($integration_name.height() ?? 0) > 30) {
$integration_name.css("font-size", ".95em");
}
}
- if ($integration_category.height() > 30) {
+ if (($integration_category.height() ?? 0) > 30) {
$integration_category.css("font-size", ".8em");
- if ($integration_category.height() > 30) {
+ if (($integration_category.height() ?? 0) > 30) {
$integration_category.css("font-size", ".75em");
}
}
}
}
-function update_path() {
+function update_path(): void {
let next_path;
if (state.integration) {
next_path = $(`.integration-lozenge[data-name="${CSS.escape(state.integration)}"]`)
@@ -81,7 +85,7 @@ function update_path() {
google_analytics.config({page_path: next_path});
}
-function update_categories() {
+function update_categories(): void {
$(".integration-lozenges").css("opacity", 0);
$(".integration-category").removeClass("selected");
@@ -91,7 +95,7 @@ function update_categories() {
if (state.category === INITIAL_STATE.category) {
$dropdown_label.text($t({defaultMessage: "Filter by category"}));
} else {
- $dropdown_label.text(CATEGORIES.get(state.category));
+ $dropdown_label.text(CATEGORIES.get(state.category) ?? "");
}
$(".integration-lozenges").animate({opacity: 1}, {duration: 400});
@@ -99,6 +103,8 @@ function update_categories() {
adjust_font_sizing();
}
+const categories_schema = z.array(z.string());
+
const update_integrations = _.debounce(() => {
const max_scrollY = window.scrollY;
@@ -115,12 +121,13 @@ const update_integrations = _.debounce(() => {
}
if (!$integration.hasClass("integration-create-your-own")) {
- const display_name = INTEGRATIONS.get($integration.attr("data-name"));
+ const display_name = INTEGRATIONS.get($integration.attr("data-name")!) ?? "";
+ const integration_categories = categories_schema.parse(
+ JSON.parse($integration.attr("data-categories")!),
+ );
const display =
common.phrase_match(state.query, display_name) &&
- (JSON.parse($integration.attr("data-categories")).includes(
- CATEGORIES.get(state.category),
- ) ||
+ (integration_categories.includes(CATEGORIES.get(state.category) ?? "") ||
state.category === "all");
if (display) {
@@ -136,21 +143,23 @@ const update_integrations = _.debounce(() => {
adjust_font_sizing();
}, 50);
-function hide_catalog_show_integration() {
+function hide_catalog_show_integration(): void {
+ assert(state.integration !== null);
const $lozenge_icon = $(
`.integration-lozenge.integration-${CSS.escape(state.integration)}`,
).clone(false);
$lozenge_icon.removeClass("legacy");
- const categories = JSON.parse(
- $(`.integration-${CSS.escape(state.integration)}`).attr("data-categories"),
+ const categories = categories_schema.parse(
+ JSON.parse($(`.integration-${CSS.escape(state.integration)}`).attr("data-categories")!),
);
- function show_integration(doc) {
- $("#integration-instructions-group .name").text(INTEGRATIONS.get(state.integration));
+ function show_integration(doc: string): void {
+ assert(state.integration !== null);
+ $("#integration-instructions-group .name").text(INTEGRATIONS.get(state.integration) ?? "");
$("#integration-instructions-group .categories .integration-category").remove();
for (const category of categories) {
- let link;
+ let link = "";
for (const [name, display_name] of CATEGORIES) {
if (display_name === category) {
link = name;
@@ -182,7 +191,7 @@ function hide_catalog_show_integration() {
adjust_font_sizing();
}
- function hide_catalog(doc) {
+ function hide_catalog(doc: string): void {
$(".integration-categories-dropdown").css("display", "none");
$(".integrations .catalog").addClass("hide");
$(".extra, .integration-main-text, #integration-search").css("display", "none");
@@ -207,8 +216,8 @@ function hide_catalog_show_integration() {
});
}
-function hide_integration_show_catalog() {
- function show_catalog() {
+function hide_integration_show_catalog(): void {
+ function show_catalog(): void {
$("html, body").animate({scrollTop: 0}, {duration: 200});
$(".integration-categories-dropdown").css("display", "");
@@ -217,7 +226,7 @@ function hide_integration_show_catalog() {
adjust_font_sizing();
}
- function hide_integration() {
+ function hide_integration(): void {
$("#integration-instruction-block").css("display", "none");
$("#integration-instructions-group").css("display", "none");
$(".inner-content").css({padding: ""});
@@ -228,21 +237,21 @@ function hide_integration_show_catalog() {
hide_integration();
}
-function get_state_from_path() {
+function get_state_from_path(): State {
const result = {...INITIAL_STATE};
result.query = state.query;
const parts = path_parts();
- if (parts[1] === "doc" && INTEGRATIONS.get(parts[2])) {
+ if (parts[1] === "doc" && parts[2] !== undefined && INTEGRATIONS.get(parts[2])) {
result.integration = parts[2];
- } else if (CATEGORIES.has(parts[1])) {
+ } else if (parts[1] !== undefined && CATEGORIES.has(parts[1])) {
result.category = parts[1];
}
return result;
}
-function render(next_state) {
+function render(next_state: State): void {
const previous_state = {...state};
state = next_state;
@@ -266,15 +275,15 @@ function render(next_state) {
}
}
-function toggle_categories_dropdown() {
+function toggle_categories_dropdown(): void {
const $dropdown_list = $(".integration-categories-dropdown .dropdown-list");
$dropdown_list.slideToggle(250);
}
-function integration_events() {
- $('#integration-search input[type="text"]').on("keypress", (e) => {
- if (e.key === "Enter" && e.target.value !== "") {
- $(".integration-lozenges .integration-lozenge:visible")[0]?.closest("a").click();
+function integration_events(): void {
+ $<HTMLInputElement>('#integration-search input[type="text"]').on("keypress", function (e) {
+ if (e.key === "Enter" && this.value !== "") {
+ $(".integration-lozenges .integration-lozenge:visible")[0]?.closest("a")?.click();
}
});
@@ -285,25 +294,25 @@ function integration_events() {
},
);
- $(".integration-instruction-block").on("click", "a .integration-category", (e) => {
+ $(".integration-instruction-block").on("click", "a .integration-category", function (e) {
e.preventDefault();
- const category = $(e.target).attr("data-category");
+ const category = $(this).attr("data-category")!;
render({...state, integration: null, category});
update_path();
});
- $(".integrations a .integration-category").on("click", (e) => {
+ $(".integrations a .integration-category").on("click", function (e) {
e.preventDefault();
- const category = $(e.target).attr("data-category");
+ const category = $(this).attr("data-category")!;
render({...state, category});
update_path();
toggle_categories_dropdown();
});
- $(".integrations a .integration-lozenge").on("click", (e) => {
- if (!$(e.target).closest(".integration-lozenge").hasClass("integration-create-your-own")) {
+ $(".integrations a .integration-lozenge").on("click", function (e) {
+ if (!$(this).hasClass("integration-create-your-own")) {
e.preventDefault();
- const integration = $(e.target).closest(".integration-lozenge").attr("data-name");
+ const integration = $(this).attr("data-name")!;
render({...state, integration});
update_path();
}
@@ -317,8 +326,8 @@ function integration_events() {
// combine selector use for both focusing the integrations searchbar and adding
// the input event.
- $(".integrations .searchbar input[type='text']").on("input", (e) => {
- render({...state, query: e.target.value.toLowerCase()});
+ $<HTMLInputElement>(".integrations .searchbar input[type='text']").on("input", function () {
+ render({...state, query: this.value.toLowerCase()});
});
$(window).on("scroll", () => {
@@ -338,7 +347,7 @@ function integration_events() {
render(get_state_from_path());
google_analytics.config({page_path: window.location.pathname});
} else {
- window.location = window.location.href;
+ window.location.replace(window.location.href);
}
});
}
diff --git a/web/webpack.assets.json b/web/webpack.assets.json
index d103120ccb7..de576bac50a 100644
--- a/web/webpack.assets.json
+++ b/web/webpack.assets.json
@@ -86,7 +86,7 @@
],
"integrations": [
"./src/bundles/portico.ts",
- "./src/portico/integrations.js",
+ "./src/portico/integrations.ts",
"./styles/portico/landing_page.css",
"./styles/portico/integrations.css"
],