FEATURE: Let users collapse the topic inline summary (#22604)

This commit is contained in:
Roman Rizzi 2023-07-13 18:21:50 -03:00 committed by GitHub
parent ac6f2f0d96
commit dc547e39aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 20 deletions

View File

@ -26,20 +26,16 @@ export default createWidget("summary-box", {
tagName: "article.summary-box", tagName: "article.summary-box",
buildKey: (attrs) => `summary-box-${attrs.topicId}`, buildKey: (attrs) => `summary-box-${attrs.topicId}`,
defaultState() { html(attrs) {
return { summary: "" };
},
html(attrs, state) {
const html = []; const html = [];
if (state.summary) { if (attrs.summary) {
html.push(new RawHtml({ html: `<div>${state.summary}</div>` })); html.push(new RawHtml({ html: `<div>${attrs.summary}</div>` }));
html.push( html.push(
h( h(
"div.summarized-on", "div.summarized-on",
{}, {},
I18n.t("summary.summarized_on", { date: state.summarized_on }) I18n.t("summary.summarized_on", { date: attrs.summarizedOn })
) )
); );
} else { } else {
@ -53,11 +49,12 @@ export default createWidget("summary-box", {
fetchSummary(topicId) { fetchSummary(topicId) {
ajax(`/t/${topicId}/strategy-summary`) ajax(`/t/${topicId}/strategy-summary`)
.then((data) => { .then((data) => {
this.state.summarized_on = shortDateNoYear(data.summarized_on);
cookAsync(data.summary).then((cooked) => { cookAsync(data.summary).then((cooked) => {
this.state.summary = cooked.string; // We store the summary in the parent so we can re-render it without doing a new request.
this.scheduleRerender(); this.sendWidgetEvent("summaryUpdated", {
summary: cooked.string,
summarizedOn: shortDateNoYear(data.summarized_on),
});
}); });
}) })
.catch(popupAjaxError); .catch(popupAjaxError);

View File

@ -37,7 +37,12 @@ export default createWidget("toggle-topic-summary", {
buildKey: (attrs) => `toggle-topic-summary-${attrs.topicId}`, buildKey: (attrs) => `toggle-topic-summary-${attrs.topicId}`,
defaultState() { defaultState() {
return { expandSummaryBox: false }; return {
expandSummaryBox: false,
summaryBoxHidden: true,
summary: "",
summarizedOn: null,
};
}, },
html(attrs, state) { html(attrs, state) {
@ -45,16 +50,22 @@ export default createWidget("toggle-topic-summary", {
const summarizationButtons = []; const summarizationButtons = [];
if (attrs.summarizable) { if (attrs.summarizable) {
const title = I18n.t("summary.strategy.button_title"); const expandTitle = I18n.t("summary.strategy.button_title");
const collapseTitle = I18n.t("summary.strategy.hide_button_title");
summarizationButtons.push( summarizationButtons.push(
this.attach("button", { this.attach("button", {
className: "btn btn-primary topic-strategy-summarization", className: "btn btn-primary topic-strategy-summarization",
icon: "magic", icon: this.summaryBoxVisble() ? "chevron-up" : "magic",
translatedTitle: title, translatedTitle: this.summaryBoxVisble()
translatedLabel: title, ? collapseTitle
action: "expandSummaryBox", : expandTitle,
disabled: state.expandSummaryBox, translatedLabel: this.summaryBoxVisble()
? collapseTitle
: expandTitle,
action: state.expandSummaryBox
? "toggleSummaryBox"
: "expandSummaryBox",
}) })
); );
} }
@ -78,14 +89,30 @@ export default createWidget("toggle-topic-summary", {
html.push(h("div.summarization-buttons", summarizationButtons)); html.push(h("div.summarization-buttons", summarizationButtons));
} }
if (state.expandSummaryBox) { if (this.summaryBoxVisble()) {
attrs.summary = this.state.summary;
attrs.summarizedOn = this.state.summarizedOn;
html.push(this.attach("summary-box", attrs)); html.push(this.attach("summary-box", attrs));
} }
return html; return html;
}, },
summaryUpdatedEvent(update) {
this.state.summary = update.summary;
this.state.summarizedOn = update.summarizedOn;
},
summaryBoxVisble() {
return this.state.expandSummaryBox && !this.state.summaryBoxHidden;
},
expandSummaryBox() { expandSummaryBox() {
this.state.expandSummaryBox = true; this.state.expandSummaryBox = true;
this.state.summaryBoxHidden = false;
},
toggleSummaryBox() {
this.state.summaryBoxHidden = !this.state.summaryBoxHidden;
}, },
}); });

View File

@ -2062,6 +2062,7 @@ en:
short_label: "Top replies" short_label: "Top replies"
short_title: "Show this topic top replies: the most interesting posts as determined by the community" short_title: "Show this topic top replies: the most interesting posts as determined by the community"
strategy: strategy:
hide_button_title: "Hide summary"
button_title: "Summarize this topic" button_title: "Summarize this topic"
title: "Topic summary" title: "Topic summary"