Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
import {$} from "jquery";
import assert from "minimalistic-assert";
import type * as z from "zod/mini";
import render_navigation_tour_video_modal from "../templates/navigation_tour_video_modal.hbs";
import * as browser_history from "./browser_history.ts";
import * as channel from "./channel.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import type * as message_view from "./message_view.ts";
import * as people from "./people.ts";
import {get_retry_backoff_seconds} from "./retry_backoff.ts";
import type {StateData, onboarding_step_schema} from "./state_data.ts";
import * as util from "./util.ts";
export type OnboardingStep = z.output<typeof onboarding_step_schema>;
export const ONE_TIME_NOTICES_TO_DISPLAY = new Set<string>();
const MAX_RETRIES = 5;
export function post_onboarding_step_as_read(
onboarding_step_name: string,
schedule_navigation_tour_video_reminder_delay?: number,
attempt = 1,
): void {
if (attempt > MAX_RETRIES) {
return;
}
const data: {onboarding_step: string; schedule_navigation_tour_video_reminder_delay?: number} =
{
onboarding_step: onboarding_step_name,
};
if (schedule_navigation_tour_video_reminder_delay !== undefined) {
assert(onboarding_step_name === "navigation_tour_video");
data.schedule_navigation_tour_video_reminder_delay =
schedule_navigation_tour_video_reminder_delay;
}
void channel.post({
url: "/json/users/me/onboarding_steps",
data,
error(xhr) {
if (xhr.status === 400) {
// Bad request: Codepath calling this function supplied
// invalid 'onboarding_step_name' (almost negligible chance
// because it's not user input) - but no point of retrying
// in that case.
return;
}
const retry_delay_secs = get_retry_backoff_seconds(xhr, attempt);
setTimeout(() => {
post_onboarding_step_as_read(
onboarding_step_name,
schedule_navigation_tour_video_reminder_delay,
attempt + 1,
);
}, retry_delay_secs * 1000);
},
});
}
export function update_onboarding_steps_to_display(onboarding_steps: OnboardingStep[]): void {
ONE_TIME_NOTICES_TO_DISPLAY.clear();
for (const onboarding_step of onboarding_steps) {
if (onboarding_step.type === "one_time_notice") {
ONE_TIME_NOTICES_TO_DISPLAY.add(onboarding_step.name);
}
}
}
function narrow_to_dm_with_welcome_bot_new_user(
onboarding_steps: OnboardingStep[],
show_message_view: typeof message_view.show,
): void {
if (
onboarding_steps.some(
(onboarding_step) => onboarding_step.name === "narrow_to_dm_with_welcome_bot_new_user",
)
) {
post_onboarding_step_as_read("narrow_to_dm_with_welcome_bot_new_user");
if (!browser_history.is_current_hash_home_view()) {
// If this account was created with a `next` parameter to
// take the user to a specific Zulip view, that takes
// precedence over sending the user to the welcome bot DM
// view. The user will hopefully still make their way
// there, as it is still an unread DM conversation.
return;
}
show_message_view(
[
{
operator: "dm",
operand: [people.WELCOME_BOT.user_id],
},
],
{trigger: "sidebar"},
);
}
}
function show_navigation_tour_video(
navigation_tour_video_url: string | null,
update_recipient_row_attention_level: () => void,
): void {
if (ONE_TIME_NOTICES_TO_DISPLAY.has("navigation_tour_video")) {
assert(navigation_tour_video_url !== null);
const modal_content_html = render_navigation_tour_video_modal({
video_src: navigation_tour_video_url,
poster_src: "/static/images/navigation-tour-video-thumbnail.png",
});
let watch_later_clicked = false;
dialog_widget.launch({
modal_title_html: $t_html({defaultMessage: "Welcome to Zulip!"}),
modal_content_html,
on_click() {
// Do nothing
},
modal_submit_button_text: $t({defaultMessage: "Skip video — I'm familiar with Zulip"}),
modal_exit_button_text: $t({defaultMessage: "Watch later"}),
close_on_submit: true,
id: "navigation-tour-video-modal",
footer_minor_text: $t({defaultMessage: "Tip: You can watch this video without sound."}),
close_on_overlay_click: false,
post_render() {
const $watch_later_button = $("#navigation-tour-video-modal .dialog_exit_button");
$watch_later_button.on("click", (e) => {
e.preventDefault();
e.stopPropagation();
// Schedule a reminder message for a few hours from now.
const reminder_delay_seconds = 2 * 60 * 60;
post_onboarding_step_as_read("navigation_tour_video", reminder_delay_seconds);
watch_later_clicked = true;
dialog_widget.close();
});
const $skip_video_button = $("#navigation-tour-video-modal .dialog_submit_button");
$skip_video_button
.removeClass("dialog_submit_button")
.addClass("dialog_exit_button");
$skip_video_button.css({"margin-left": "12px"});
const $video = $<HTMLVideoElement>("#navigation-tour-video");
$video.on("play", () => {
// Remove the custom play button overlaying the video.
$("#navigation-tour-video-wrapper").addClass("hide-play-button");
});
let skip_video_button_text_updated = false;
let video_ended_button_visible = false;
$video.on("timeupdate", () => {
const $video_elem = util.the($video);
const current_time = $video_elem.currentTime;
if (!skip_video_button_text_updated && current_time >= 30) {
$skip_video_button.text($t({defaultMessage: "Skip the rest"}));
skip_video_button_text_updated = true;
}
if (video_ended_button_visible && current_time < $video_elem.duration) {
$("#navigation-tour-video-ended-button-wrapper").css(
"visibility",
"hidden",
);
video_ended_button_visible = false;
$video.removeClass("dimmed-background");
}
});
$video.on("ended", () => {
$("#navigation-tour-video-ended-button-wrapper").css("visibility", "visible");
video_ended_button_visible = true;
$video.addClass("dimmed-background");
$skip_video_button.css("visibility", "hidden");
$watch_later_button.css("visibility", "hidden");
// Exit fullscreen to make the 'video-ended-button-wrapper' button visible.
const $video_elem = util.the($video);
if (document.fullscreenElement === $video_elem) {
void document.exitFullscreen();
}
});
$("#navigation-tour-video-ended-button").on("click", () => {
dialog_widget.close();
});
},
on_hide() {
// `narrow_to_dm_with_welcome_bot_new_user` triggers a focus change from
// #compose-channel-recipient to #compose-textarea (see `compose_actions.show_compose_box`
// with `opts.defer_focus = true`). We start initializing this modal while the
// focus transition is in progress, resulting in a flaky behaviour of the
// element that will be in focus when modal is closed.
//
// We explicitly set the focus to #compose-textarea to avoid flaky nature.
$("textarea#compose-textarea").trigger("focus");
update_recipient_row_attention_level();
if (!watch_later_clicked) {
// $watch_later_button click handler already calls this function.
post_onboarding_step_as_read("navigation_tour_video");
}
},
});
}
}
export function initialize(
params: StateData["onboarding_steps"],
{
show_message_view,
update_recipient_row_attention_level,
}: {
show_message_view: typeof message_view.show;
update_recipient_row_attention_level: () => void;
},
): void {
update_onboarding_steps_to_display(params.onboarding_steps);
narrow_to_dm_with_welcome_bot_new_user(params.onboarding_steps, show_message_view);
show_navigation_tour_video(
params.navigation_tour_video_url,
update_recipient_row_attention_level,
);
}