diff --git a/app/assets/javascripts/discourse/app/widgets/summary-box.js b/app/assets/javascripts/discourse/app/widgets/summary-box.js index ddb41e04dd5..6b31a3d4f20 100644 --- a/app/assets/javascripts/discourse/app/widgets/summary-box.js +++ b/app/assets/javascripts/discourse/app/widgets/summary-box.js @@ -26,20 +26,16 @@ export default createWidget("summary-box", { tagName: "article.summary-box", buildKey: (attrs) => `summary-box-${attrs.topicId}`, - defaultState() { - return { summary: "" }; - }, - - html(attrs, state) { + html(attrs) { const html = []; - if (state.summary) { - html.push(new RawHtml({ html: `
${state.summary}
` })); + if (attrs.summary) { + html.push(new RawHtml({ html: `
${attrs.summary}
` })); html.push( h( "div.summarized-on", {}, - I18n.t("summary.summarized_on", { date: state.summarized_on }) + I18n.t("summary.summarized_on", { date: attrs.summarizedOn }) ) ); } else { @@ -53,11 +49,12 @@ export default createWidget("summary-box", { fetchSummary(topicId) { ajax(`/t/${topicId}/strategy-summary`) .then((data) => { - this.state.summarized_on = shortDateNoYear(data.summarized_on); - cookAsync(data.summary).then((cooked) => { - this.state.summary = cooked.string; - this.scheduleRerender(); + // We store the summary in the parent so we can re-render it without doing a new request. + this.sendWidgetEvent("summaryUpdated", { + summary: cooked.string, + summarizedOn: shortDateNoYear(data.summarized_on), + }); }); }) .catch(popupAjaxError); diff --git a/app/assets/javascripts/discourse/app/widgets/toggle-topic-summary.js b/app/assets/javascripts/discourse/app/widgets/toggle-topic-summary.js index fbd84248f8b..9ceb69cf858 100644 --- a/app/assets/javascripts/discourse/app/widgets/toggle-topic-summary.js +++ b/app/assets/javascripts/discourse/app/widgets/toggle-topic-summary.js @@ -37,7 +37,12 @@ export default createWidget("toggle-topic-summary", { buildKey: (attrs) => `toggle-topic-summary-${attrs.topicId}`, defaultState() { - return { expandSummaryBox: false }; + return { + expandSummaryBox: false, + summaryBoxHidden: true, + summary: "", + summarizedOn: null, + }; }, html(attrs, state) { @@ -45,16 +50,22 @@ export default createWidget("toggle-topic-summary", { const summarizationButtons = []; 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( this.attach("button", { className: "btn btn-primary topic-strategy-summarization", - icon: "magic", - translatedTitle: title, - translatedLabel: title, - action: "expandSummaryBox", - disabled: state.expandSummaryBox, + icon: this.summaryBoxVisble() ? "chevron-up" : "magic", + translatedTitle: this.summaryBoxVisble() + ? collapseTitle + : expandTitle, + 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)); } - if (state.expandSummaryBox) { + if (this.summaryBoxVisble()) { + attrs.summary = this.state.summary; + attrs.summarizedOn = this.state.summarizedOn; html.push(this.attach("summary-box", attrs)); } return html; }, + summaryUpdatedEvent(update) { + this.state.summary = update.summary; + this.state.summarizedOn = update.summarizedOn; + }, + + summaryBoxVisble() { + return this.state.expandSummaryBox && !this.state.summaryBoxHidden; + }, + expandSummaryBox() { this.state.expandSummaryBox = true; + this.state.summaryBoxHidden = false; + }, + + toggleSummaryBox() { + this.state.summaryBoxHidden = !this.state.summaryBoxHidden; }, }); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index a33e4f4b881..dfffbfea12f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2062,6 +2062,7 @@ en: short_label: "Top replies" short_title: "Show this topic top replies: the most interesting posts as determined by the community" strategy: + hide_button_title: "Hide summary" button_title: "Summarize this topic" title: "Topic summary"