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.

3.1 KB
Raw Blame
import {$} from "jquery";
import * as z from "zod/mini";

import render_topic_summary from "../templates/topic_summary.hbs";

import * as channel from "./channel.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {Filter} from "./filter.ts";
import {$t} from "./i18n.ts";
import * as message_fetch from "./message_fetch.ts";
import * as rendered_markdown from "./rendered_markdown.ts";
import * as unread from "./unread.ts";
import * as unread_ops from "./unread_ops.ts";
import * as util from "./util.ts";

export function get_narrow_summary(channel_id: number, topic_name: string): void {
    const filter = new Filter([
        {operator: "channel", operand: `${channel_id}`},
        {operator: "topic", operand: topic_name},
    ]);
    const data = {narrow: message_fetch.get_narrow_for_message_fetch(filter)};
    const display_topic_name = util.get_final_topic_display_name(topic_name);
    const unread_topic_params = {
        modal_submit_button_text: $t({defaultMessage: "Mark topic as read"}),
        modal_exit_button_text: $t({defaultMessage: "Close"}),
        on_click() {
            unread_ops.mark_topic_as_read(channel_id, topic_name);
        },
        single_footer_button: false,
    };

    let params = {
        modal_submit_button_text: $t({defaultMessage: "Close"}),
        on_click() {
            // Just close the modal, there is nothing else to do.
        },
        single_footer_button: true,
    };
    if (unread.topic_has_any_unread(channel_id, topic_name)) {
        params = {
            ...params,
            ...unread_topic_params,
        };
    }
    dialog_widget.launch({
        modal_title_text: display_topic_name,
        modal_content_html: "<div></div>", // TODO: Add a loading indicator here instead of a placeholder.
        close_on_submit: true,
        id: "topic-summary-modal",
        footer_minor_text: $t({defaultMessage: "AI summaries may have errors."}),
        ...params,
        on_show() {
            $("#topic-summary-modal .modal__content").addClass("hide");
        },
        post_render() {
            const close_on_success = false;
            dialog_widget.submit_api_request(
                channel.get,
                "/json/messages/summary",
                data,
                {
                    success_continuation(response_data) {
                        const data = z.object({summary: z.string()}).parse(response_data);
                        const summary_markdown = data.summary;
                        const summary_html = render_topic_summary({
                            summary_markdown,
                        });
                        $("#topic-summary-modal .modal__content")
                            .removeClass("hide")
                            .addClass("rendered_markdown");
                        $("#topic-summary-modal .modal__content").html(summary_html);
                        rendered_markdown.update_elements(
                            $("#topic-summary-modal .modal__content"),
                        );
                    },
                },
                close_on_success,
            );
        },
    });
}