From 74dfc39530201ea2afc9b7bb8e43d109e5752514 Mon Sep 17 00:00:00 2001
From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com>
Date: Tue, 13 Dec 2022 09:21:01 -0600
Subject: [PATCH] Upgrade query-row-content to Octane (#194)
* Upgrade query-row-content to octane
---
.../components/query-row-content.hbs | 5 +
.../discourse/components/query-row-content.js | 120 +++++++++---------
.../components/query-row-content.hbs | 1 -
.../templates/explorer-query-result.hbs | 25 +++-
4 files changed, 81 insertions(+), 70 deletions(-)
create mode 100644 assets/javascripts/discourse/components/query-row-content.hbs
delete mode 100644 assets/javascripts/discourse/templates/components/query-row-content.hbs
diff --git a/assets/javascripts/discourse/components/query-row-content.hbs b/assets/javascripts/discourse/components/query-row-content.hbs
new file mode 100644
index 0000000..cdabc4b
--- /dev/null
+++ b/assets/javascripts/discourse/components/query-row-content.hbs
@@ -0,0 +1,5 @@
+
+ {{#each this.results as |result|}}
+ {{result}} |
+ {{/each}}
+
diff --git a/assets/javascripts/discourse/components/query-row-content.js b/assets/javascripts/discourse/components/query-row-content.js
index d8a174f..297f211 100644
--- a/assets/javascripts/discourse/components/query-row-content.js
+++ b/assets/javascripts/discourse/components/query-row-content.js
@@ -1,4 +1,4 @@
-import Component from "@ember/component";
+import Component from "@glimmer/component";
import { categoryLinkHTML } from "discourse/helpers/category-link";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
import { convertIconClass, iconHTML } from "discourse-common/lib/icon-library";
@@ -8,6 +8,63 @@ import { htmlSafe } from "@ember/template";
import { get } from "@ember/object";
import { isEmpty } from "@ember/utils";
import { escapeExpression } from "discourse/lib/utilities";
+import { cached } from "@glimmer/tracking";
+
+export default class QueryRowContent extends Component {
+ @cached
+ get results() {
+ return this.args.columnTemplates.map((t, idx) => {
+ const value = this.args.row[idx],
+ id = parseInt(value, 10);
+
+ const ctx = {
+ value,
+ id,
+ baseuri: getURL(""),
+ };
+ const params = {};
+
+ if (this.args.row[idx] === null) {
+ return "NULL";
+ } else if (t.name === "text") {
+ return escapeExpression(this.args.row[idx]);
+ }
+ const lookupFunc = this.args[`lookup${capitalize(t.name)}`];
+ if (lookupFunc) {
+ ctx[t.name] = lookupFunc.call(this.args, id);
+ }
+
+ if (t.name === "url") {
+ let [url, name] = guessUrl(value);
+ ctx["href"] = url;
+ ctx["target"] = name;
+ }
+
+ if (t.name === "category" || t.name === "badge" || t.name === "reltime") {
+ // only replace helpers if needed
+ params.helpers = this.helpers;
+ }
+
+ try {
+ return htmlSafe(
+ (t.template || this.args.fallbackTemplate)(ctx, params)
+ );
+ } catch (e) {
+ return "error";
+ }
+ });
+ }
+
+ constructor() {
+ super(...arguments);
+
+ this.helpers = {
+ "icon-or-image": icon_or_image_replacement,
+ "category-link": category_badge_replacement,
+ reltime: bound_date_replacement,
+ };
+ }
+}
function icon_or_image_replacement(str, ctx) {
str = get(ctx.contexts[0], str);
@@ -35,7 +92,6 @@ function bound_date_replacement(str, ctx) {
return htmlSafe(autoUpdatingRelativeAge(new Date(value), { title: true }));
}
-// consider moving this elsewhere
function guessUrl(t) {
let [dest, name] = [t, t];
@@ -48,63 +104,3 @@ function guessUrl(t) {
return [dest, name];
}
-
-const QueryRowContentComponent = Component.extend({
- tagName: "tr",
- rowContents: null,
-
- didReceiveAttrs() {
- const row = this.row;
- const parentView = this.parentView;
- const fallback = this.fallbackTemplate;
- const helpers = {
- "icon-or-image": icon_or_image_replacement,
- "category-link": category_badge_replacement,
- reltime: bound_date_replacement,
- };
-
- const parts = this.columnTemplates.map((t, idx) => {
- const value = row[idx],
- id = parseInt(value, 10);
-
- const ctx = {
- value,
- id,
- baseuri: getURL(""),
- };
- const params = {};
-
- if (row[idx] === null) {
- return "NULL";
- } else if (t.name === "text") {
- return escapeExpression(row[idx]);
- }
-
- const lookupFunc = parentView[`lookup${capitalize(t.name)}`];
- if (lookupFunc) {
- ctx[t.name] = lookupFunc.call(parentView, id);
- }
-
- if (t.name === "url") {
- let [url, name] = guessUrl(value);
- ctx["href"] = url;
- ctx["target"] = name;
- }
-
- if (t.name === "category" || t.name === "badge" || t.name === "reltime") {
- // only replace helpers if needed
- params.helpers = helpers;
- }
-
- try {
- return htmlSafe((t.template || fallback)(ctx, params));
- } catch (e) {
- return "error";
- }
- });
-
- this.set("rowContents", htmlSafe(`${parts.join(" | ")} | `));
- },
-});
-
-export default QueryRowContentComponent;
diff --git a/assets/javascripts/discourse/templates/components/query-row-content.hbs b/assets/javascripts/discourse/templates/components/query-row-content.hbs
deleted file mode 100644
index 46e90d1..0000000
--- a/assets/javascripts/discourse/templates/components/query-row-content.hbs
+++ /dev/null
@@ -1 +0,0 @@
-{{rowContents}}
diff --git a/assets/javascripts/discourse/templates/explorer-query-result.hbs b/assets/javascripts/discourse/templates/explorer-query-result.hbs
index 455aba3..212e84e 100644
--- a/assets/javascripts/discourse/templates/explorer-query-result.hbs
+++ b/assets/javascripts/discourse/templates/explorer-query-result.hbs
@@ -39,7 +39,7 @@
{{duration}}
-
+
{{~#if hasExplain}}
@@ -47,7 +47,7 @@
{{~/if}}
-
+
@@ -68,11 +68,22 @@
{{#each rows as |row|}}
- {{query-row-content
- row=row
- fallbackTemplate=fallbackTemplate
- columnTemplates=columnTemplates
- }}
+
{{/each}}