Skip to content

Commit

Permalink
REFACTOR: Update from widget to glimmer component (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomerobot authored Apr 21, 2023
1 parent 2960a74 commit 28fa1c6
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 118 deletions.
6 changes: 3 additions & 3 deletions about.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discourse-tag-banners",
"name": "Tag Banners",
"component": true,
"about_url": "",
"license_url": ""
"about_url": "https://meta.discourse.org/t/tag-banners/124240",
"license_url": "https://github.com/discourse/discourse-tag-banners/blob/main/LICENSE"
}
3 changes: 1 addition & 2 deletions common/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
width: 100%;
justify-content: center;
background: var(--primary-very-low);
min-height: 10em; // prevents hop when tag name loads

.tag-title-contents {
max-width: 700px;
max-width: 46em;
padding: 3.5em 3em 3em;

@media screen and (max-width: 450px) {
Expand Down
30 changes: 30 additions & 0 deletions javascripts/discourse/components/discourse-tag-banners.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{#if (not (and this.site.mobileView (not (theme-setting "show_on_mobile"))))}}
{{#if this.shouldRender}}
<div
{{did-insert this.getTagInfo}}
{{did-update this.getTagInfo this.shouldRender}}
{{will-destroy this.resetTag}}
class={{concat-class
"tag-title-header"
(concat "tag-banner-" this.formattedTagName)
this.additionalClass
}}
>
<div class="tag-title-contents">
<h1>
<span>{{this.formattedTagName}}</span>
{{#if this.formattedAdditionalTagNames}}
&amp;
{{this.formattedAdditionalTagNames}}
{{/if}}
</h1>
{{#unless this.isIntersection}}
{{! hide descriptions on tag intersections}}
{{#if (theme-setting "show_tag_description")}}
<p>{{this.tag.description}}</p>
{{/if}}
{{/unless}}
</div>
</div>
{{/if}}
{{/if}}
78 changes: 78 additions & 0 deletions javascripts/discourse/components/discourse-tag-banners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";

export default class DiscourseTagBanners extends Component {
@service store;
@service router;
@service site;
@tracked tag = null;
@tracked keepDuringLoadingRoute = false;
@tracked isIntersection = false;

#formatTagName(tagName = "") {
// for intersections: tag1/tag2 => tag1 & tag2
tagName = tagName.replace(/\//g, " & ");

if (settings.remove_tag_hyphen) {
tagName = tagName.replace(/-/g, " ");
}
if (settings.remove_tag_underscore) {
tagName = tagName.replace(/_/g, " ");
}
return tagName;
}

get currentRouteParams() {
return this.router?.currentRoute?.params;
}

get shouldRender() {
return (
(this.currentRouteParams.tag_id !== "none" &&
this.currentRouteParams?.tag_id) ||
(this.keepDuringLoadingRoute &&
this.router.currentRoute.name.includes("loading"))
);
}

get formattedTagName() {
return this.#formatTagName(this.tag?.name);
}

get formattedAdditionalTagNames() {
const additionalTags = this.currentRouteParams.additional_tags;
return additionalTags ? this.#formatTagName(additionalTags) : "";
}

get additionalClass() {
if (this.formattedAdditionalTagNames === "") {
return "single-tag";
}

let tagList = this.formattedAdditionalTagNames.split(" & ");
return tagList.map((e) => `tag-banner-${e}`).join(" ");
}

@action
async getTagInfo() {
const tag = this.currentRouteParams?.tag_id;
if (tag) {
const result = await this.store.find("tag-info", tag);
this.tag = result;
this.isIntersection = this.currentRouteParams.additional_tags;
this.keepDuringLoadingRoute = true;
} else {
if (!this.router.currentRoute.name.includes("loading")) {
this.keepDuringLoadingRoute = false;
}
}
}

@action
resetTag() {
this.keepDuringLoadingRoute = false;
this.tag = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#if (theme-setting "show_above_site_header")}}
{{mount-widget widget="tag-header-widget"}}
<DiscourseTagBanners />
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#if (theme-setting "show_below_site_header")}}
{{mount-widget widget="tag-header-widget"}}
<DiscourseTagBanners />
{{/if}}
15 changes: 0 additions & 15 deletions javascripts/discourse/initializers/discourse-tag-banners.js

This file was deleted.

96 changes: 0 additions & 96 deletions javascripts/discourse/widgets/tag-header-widget.js

This file was deleted.

0 comments on commit 28fa1c6

Please sign in to comment.