Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX: Lazy load categories support #20

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
04374ef
Initial commit by `discourse_theme` CLI
carsick May 29, 2024
c9d8594
init
carsick May 29, 2024
ef14f9b
init
carsick May 30, 2024
fffea4f
community guidelines
carsick Jun 4, 2024
6211e0c
init
carsick Jun 5, 2024
20cbd91
Merge remote-tracking branch 'origin/staging' into org-main
carsick Jun 5, 2024
5a0cf7c
lint
carsick Jun 5, 2024
006fb9b
lint
carsick Jun 5, 2024
27621be
Merge branch 'staging' into org-staging
carsick Jun 5, 2024
dc4c46d
refactor
carsick Jun 5, 2024
449abb0
refactor
carsick Jun 5, 2024
d994e02
license
carsick Jun 5, 2024
72ec014
lint
carsick Jun 5, 2024
1be83da
lint
carsick Jun 5, 2024
fb0f4f5
lint
carsick Jun 5, 2024
dfaf934
lint
carsick Jun 5, 2024
882cd94
lint
carsick Jun 5, 2024
73e811f
blocks
carsick Jun 10, 2024
acdbbe1
settings
carsick Jun 11, 2024
173a6d9
Merge branch 'org-staging' into org/main
carsick Jun 11, 2024
4139f3c
lint
carsick Jun 11, 2024
c7a5d87
Merge branch 'main' into banner-block
carsick Jun 12, 2024
05248a0
banner other css
carsick Jun 12, 2024
e6a003b
hide banner
carsick Jun 13, 2024
0bc029d
top contributors
carsick Jun 20, 2024
374a9d6
Merge remote-tracking branch 'org/main' into banner-block
carsick Jun 20, 2024
ff7d7ce
categories, navigations, components, settings, subcategories
carsick Jun 24, 2024
90469e7
lint
carsick Jun 24, 2024
a60588b
lint
carsick Jun 24, 2024
fc6c79c
lint
carsick Jun 24, 2024
c1d0883
lint
carsick Jun 24, 2024
f2d5270
lint
carsick Jun 24, 2024
9d61105
lint
carsick Jun 24, 2024
40d07fc
lint
carsick Jun 24, 2024
beb6f7b
lint
carsick Jun 24, 2024
414762c
{{setting}} to %{setting}
carsick Jun 24, 2024
027c2d3
i18n
carsick Jun 24, 2024
218bf9c
no mobile
carsick Jun 24, 2024
780aa23
block updates
carsick Jul 1, 2024
aaf03a9
badge
carsick Jul 1, 2024
81cc721
Merge branch 'main' into banner-block
carsick Jul 1, 2024
6fcdc8e
lint
carsick Jul 1, 2024
a654f9f
lint
carsick Jul 1, 2024
5a79aff
lazy load categories
carsick Jul 9, 2024
7bbed7b
Merge remote-tracking branch 'org/main' into lazy-load-categories
carsick Jul 9, 2024
e756ebd
lint
carsick Jul 9, 2024
6ab7836
prettier
carsick Jul 9, 2024
2a1c131
DEV: Review theme and refactor js (#21)
Grubba27 Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions javascripts/discourse/api-initializers/central.gjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { apiInitializer } from "discourse/lib/api";

export default apiInitializer("1.0", () => {
let css = "";
const categories = settings.category_icons;
if (categories) {
categories.forEach((category) => {
const id = category.id[0];
const emoji = category.emoji;
if (!categories) {
return;
}

css += `.badge-category__wrapper .badge-category[data-category-id="${id}"]:before { content: "${emoji}"; }\n`;
});
const css = categories.reduce((acc, category) => {
const id = category.id[0];
const emoji = category.emoji;

const styleElement = document.createElement("style");
styleElement.type = "text/css";
styleElement.appendChild(document.createTextNode(css));
document.head.appendChild(styleElement);
}
return `${acc}.badge-category__wrapper .badge-category[data-category-id="${id}"]:before { content: "${emoji}"; }\n`;
}, "");

const styleElement = document.createElement("style");
styleElement.type = "text/css";
styleElement.appendChild(document.createTextNode(css));
document.head.appendChild(styleElement);
});
6 changes: 1 addition & 5 deletions javascripts/discourse/api-initializers/icons.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ export default apiInitializer("1.0", (api) => {

api.headerIcons.add("c-create", <template>
<li class="c-create">
<DMenu
@placement="bottom-end"
{{!-- @modalForMobile={{true}} --}}
@identifier="c-create"
>
<DMenu @placement="bottom-end" @identifier="c-create">
<:trigger>
<div class="c-create__icon"></div>
</:trigger>
Expand Down
11 changes: 0 additions & 11 deletions javascripts/discourse/components/blocks/banner.gjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { concat } from "@ember/helper";
// import { eq, or } from "truth-helpers";

// import UserLink from "discourse/components/user-link";
// import avatar from "discourse/helpers/avatar";
// import { ajax } from "discourse/lib/ajax";
// import i18n from "discourse-common/helpers/i18n";

export default class BlockBanner extends Component {
@tracked title = this.args?.title;
@tracked description = this.args?.description;
@tracked ctas = this.args?.ctas;
@tracked image = this.args?.image;

constructor() {
super(...arguments);
}

<template>
<div class="block block-banner">
{{#if this.title}}
Expand All @@ -30,7 +20,6 @@ export default class BlockBanner extends Component {
{{this.description}}
</span>
{{/if}}
{{!log this.ctas}}
{{#if this.ctas}}
<div class="block-banner__ctas">
{{#each this.ctas as |cta|}}
Expand Down
2 changes: 1 addition & 1 deletion javascripts/discourse/components/blocks/birthday.gjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { concat } from "@ember/helper";
// import UserLink from "discourse/components/user-link";
import avatar from "discourse/helpers/avatar";
import { ajax } from "discourse/lib/ajax";
import i18n from "discourse-common/helpers/i18n";
Expand All @@ -13,6 +12,7 @@ export default class BlockBirthday extends Component {
super(...arguments);

ajax("/cakeday/birthdays/today.json").then((data) => {
// loading state?
const birthdays = data.birthdays;
this.randomBirthday =
birthdays[Math.floor(Math.random() * birthdays.length)];
Expand Down
14 changes: 0 additions & 14 deletions javascripts/discourse/components/blocks/cta.gjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
// eslint-disable-next-line no-unused-vars
import { concat } from "@ember/helper";
// eslint-disable-next-line no-unused-vars
import { inject as service } from "@ember/service";
// eslint-disable-next-line no-unused-vars
import { htmlSafe } from "@ember/template";
// import { eq, or } from "truth-helpers";
// import avatar from "discourse/helpers/avatar";
import concatClass from "discourse/helpers/concat-class";
// import { ajax } from "discourse/lib/ajax";

export default class BlockCta extends Component {
@tracked title = this.args?.title;
@tracked desc = this.args?.description;
@tracked cta = this.args?.cta;
@tracked url = this.args?.url;

constructor() {
super(...arguments);
}

get size() {
if (this.args.size) {
return `block--${this.args.size}`;
}
return;
}

<template>
Expand Down
1 change: 1 addition & 0 deletions javascripts/discourse/components/blocks/online.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class BlockOnline extends Component {
super(...arguments);

ajax("/about.json").then((data) => {
// loading state?
this.online = data.about.stats;
});
}
Expand Down
13 changes: 6 additions & 7 deletions javascripts/discourse/components/blocks/profile.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { concat } from "@ember/helper";
import { LinkTo } from "@ember/routing";
import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
// import { eq, or } from "truth-helpers";
import avatar from "discourse/helpers/avatar";
import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
Expand All @@ -21,13 +20,13 @@ export default class BlockProfile extends Component {
constructor() {
super(...arguments);

if (this.currentUser !== null) {
const currentUserUrl = "/u/" + this.currentUser.username + ".json";

ajax(currentUserUrl).then((data) => {
this.updateUserData(data.user);
});
if (this.currentUser === null) {
return;
}

ajax(`/u/${this.currentUser.username}.json`).then((data) => {
this.updateUserData(data.user);
});
}

get size() {
Expand Down
41 changes: 19 additions & 22 deletions javascripts/discourse/components/blocks/top-contributors.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { concat } from "@ember/helper";
import { action } from "@ember/object";
// import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import { eq } from "truth-helpers";
import avatar from "discourse/helpers/avatar";
Expand Down Expand Up @@ -50,24 +49,22 @@ export default class BlockTopContributors extends Component {
}
}

fetchTopContributors(period, count) {
ajax(`/leaderboard/7.json?period=${period}`)
.then((data) => {
this.topContributors = data.users.slice(0, count);
})
.catch(() => {
ajax(
`/directory_items.json?period=${period}&order=likes_received`
).then((data) => {
data.directory_items = data.directory_items.map((item) => {
let user = item.user;
delete item.user;
return { ...item, ...user };
});

this.topContributors = data.directory_items.slice(0, count);
});
async fetchTopContributors(period, count) {
try {
const response = await ajax(`/leaderboard/7.json?period=${period}`);
this.topContributors = response.users.slice(0, count);
} catch {
const response = await ajax(
`/directory_items.json?period=${period}&order=likes_received`
);
response.directory_items = response.directory_items.map((item) => {
let user = item.user;
delete item.user;
return { ...item, ...user };
});

this.topContributors = response.directory_items.slice(0, count);
}
}

@action
Expand Down Expand Up @@ -123,11 +120,11 @@ export default class BlockTopContributors extends Component {
href={{concat "/u/" topContributor.username}}
data-user-card={{topContributor.username}}
>
{{!-- {{#if topContributor.name}}
{{#if topContributor.name}}
{{topContributor.name}}
{{else}} --}}
{{htmlSafe topContributor.username}}
{{!-- {{/if}} --}}
{{else}}
{{htmlSafe topContributor.username}}
{{/if}}
</a>
</div>
<div class="block-chart__details">
Expand Down
20 changes: 8 additions & 12 deletions javascripts/discourse/components/blocks/top-topics.gjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/** eslint-disable no-unused-vars */
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { concat } from "@ember/helper";
import { action } from "@ember/object";
// import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import { eq } from "truth-helpers";
import UserLink from "discourse/components/user-link";
Expand Down Expand Up @@ -50,22 +48,20 @@ export default class BlockTopTopics extends Component {

@action
async fetchTopTopics(period, count) {
let data = await ajax(`/top.json?period=${period}`);
let topTopics = data.topic_list.topics.slice(0, count);
let categoryIds = topTopics.map((topic) => topic.category_id);
let categories = await Category.asyncFindByIds(categoryIds);
const data = await ajax(`/top.json?period=${period}`);
const topTopics = data.topic_list.topics.slice(0, count);
const categoryIds = topTopics.map((topic) => topic.category_id);
const categories = await Category.asyncFindByIds(categoryIds);

topTopics.forEach((topic) => {
topic["category"] = categories.find(
this.topTopics = topTopics.map((topic) => {
topic.category = categories.find(
(category) => topic.category_id === category.id
);

let author = data.users.find(
topic.author = data.users.find(
(user) => user.id === topic.posters[0].user_id
);
topic.author = author;
return topic;
});
this.topTopics = topTopics;
}

@action
Expand Down
35 changes: 14 additions & 21 deletions javascripts/discourse/components/like-toggle.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default class LikeToggle extends Component {
@action
toggleLikeDebounced(topic) {
if (this.loading) {
// console.log("Action is currently loading, please wait.");
return;
}

Expand All @@ -40,34 +39,28 @@ export default class LikeToggle extends Component {

async performToggleLike(topic) {
if (this.clickCounter % 2 === 0) {
// console.log("Skipping redundant like.");
this.clickCounter = 0;
return;
}
this.loading = true;

try {
const topicPosts = await ajax(`/t/${topic.id}/post_ids.json`);
if (topicPosts && topicPosts.post_ids.length) {
const firstPost = topicPosts.post_ids[0];
if (firstPost) {
if (!this.likeToggled) {
// Adjusted the logic here to match the updated state
await ajax(`/post_actions/${firstPost}`, {
type: "DELETE",
data: { post_action_type_id: 2 },
});
// console.log("UNLIKED");
} else {
await ajax(`/post_actions`, {
type: "POST",
data: { id: firstPost, post_action_type_id: 2 },
});
// console.log("LIKED");
}
const firstPost = topicPosts.post_ids[0];
if (topicPosts && firstPost) {
if (this.likeToggled) {
await ajax(`/post_actions`, {
type: "POST",
data: { id: firstPost, post_action_type_id: 2 },
});
} else {
await ajax(`/post_actions/${firstPost}`, {
type: "DELETE",
data: { post_action_type_id: 2 },
});
}
}
} catch (error) {
// console.error("Error toggling like:", error);
} catch {
// Rollback UI changes in case of an error
this.likeToggled = !this.likeToggled;
this.likeCount += this.likeToggled ? 1 : -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
// eslint-disable-next-line no-unused-vars
import { concat, fn } from "@ember/helper";
import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { htmlSafe } from "@ember/template";
// eslint-disable-next-line no-unused-vars
import { eq } from "truth-helpers";
import categoryColorVariable from "discourse/helpers/category-color-variable";
import categoryLink from "discourse/helpers/category-link";
import concatClass from "discourse/helpers/concat-class";
import formatDate from "discourse/helpers/format-date";
import i18n from "discourse-common/helpers/i18n";

export default class CentralCategories extends Component {
@service currentUser;
@service router;
@service discovery;
@service site;

@tracked categories = this.args.outletArgs.categories;

Expand All @@ -34,11 +28,9 @@ export default class CentralCategories extends Component {
}

<template>
{{!log this.categories}}
<div class="c-categories">
{{#each this.categories as |category|}}
{{! template-lint-disable no-invalid-interactive }}

<div
data-notification-level={{category.notificationLevelString}}
style={{categoryColorVariable category.color}}
Expand Down
13 changes: 0 additions & 13 deletions javascripts/discourse/connectors/above-main-container/admin.gjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import Component from "@glimmer/component";
// import { tracked } from "@glimmer/tracking";
// import { action } from "@ember/object";
import { LinkTo } from "@ember/routing";
import { service } from "@ember/service";
// import { eq } from "truth-helpers";
// import bodyClass from "discourse/helpers/body-class";
// import categoryLink from "discourse/helpers/category-link";
// import concatClass from "discourse/helpers/concat-class";
// import Category from "discourse/models/category";
import i18n from "discourse-common/helpers/i18n";

export default class CentralAdmin extends Component {
@service currentUser;
@service router;
@service site;
@service siteSettings;
@service discovery;

constructor() {
super(...arguments);
}

get isAdmin() {
return this.router.currentRouteName.startsWith("admin");
Expand Down
Loading