Skip to content

Commit 3e5b5fc

Browse files
authored
DEV: Replace findBy with find in category lookups (#217)
Refactor category lookups across `discourse-docs` plugin by replacing `findBy` with the native `find`, simplifying the logic and ensuring safe fallback with optional chaining (`?.name`). This improves code clarity and reduces reliance on Ember's custom utilities. Changes cover controllers, components, and routes where category references are used. No functionality impact is expected.
1 parent 32a7d03 commit 3e5b5fc

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

assets/javascripts/discourse/components/docs-category.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import discourseComputed from "discourse/lib/decorators";
88
export default class DocsCategory extends Component {
99
@discourseComputed("category")
1010
categoryName(category) {
11-
return this.site.categories.findBy("id", category.id).name;
11+
return this.site.categories.find((item) => item.id === category.id)?.name;
1212
}
1313

1414
<template>

assets/javascripts/discourse/controllers/docs-index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ export default class DocsIndexController extends Controller {
8181
} else {
8282
categories = categories.sort((a, b) => {
8383
const first = this.site.categories
84-
.findBy("id", a.id)
84+
.find((item) => item.id === a.id)
8585
.name.toLowerCase(),
86-
second = this.site.categories.findBy("id", b.id).name.toLowerCase();
86+
second = this.site.categories
87+
.find((item) => item.id === b.id)
88+
?.name.toLowerCase();
8789
return first.localeCompare(second);
8890
});
8991
}
@@ -94,7 +96,9 @@ export default class DocsIndexController extends Controller {
9496

9597
if (this.showCategoryFilter) {
9698
return categories.filter((category) => {
97-
let categoryData = this.site.categories.findBy("id", category.id);
99+
let categoryData = this.site.categories.find(
100+
(item) => item.id === category.id
101+
);
98102
return (
99103
categoryData.name.toLowerCase().indexOf(filter.toLowerCase()) > -1 ||
100104
(categoryData.description_excerpt &&
@@ -278,7 +282,10 @@ export default class DocsIndexController extends Controller {
278282

279283
return this.siteSettings.docs_categories
280284
.split("|")
281-
.map((c) => this.site.categories.findBy("id", parseInt(c, 10))?.name)
285+
.map(
286+
(c) =>
287+
this.site.categories.find((item) => item.id === parseInt(c, 10))?.name
288+
)
282289
.filter(Boolean);
283290
}
284291

assets/javascripts/discourse/routes/docs-index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ export default class DocsIndex extends DiscourseRoute {
2929
const pageTitle = i18n("docs.title");
3030
if (model.topic.title && model.topic.category_id) {
3131
const title = model.topic.unicode_title || model.topic.title;
32-
const categoryName = this.site.categories.findBy(
33-
"id",
34-
model.topic.category_id
35-
).name;
32+
const categoryName = this.site.categories.find(
33+
(item) => item.id === model.topic.category_id
34+
)?.name;
3635
return `${title} - ${categoryName} - ${pageTitle}`;
3736
} else {
3837
return pageTitle;

0 commit comments

Comments
 (0)