Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit c92d995

Browse files
committed
DEV: Added compatibility with the Glimmer Post Menu
1 parent 7fcde6c commit c92d995

File tree

2 files changed

+152
-40
lines changed

2 files changed

+152
-40
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Component from "@glimmer/component";
2+
import { action } from "@ember/object";
3+
import { inject as service } from "@ember/service";
4+
import DButton from "discourse/components/d-button";
5+
6+
export default class AssignButton extends Component {
7+
static shouldRender(args) {
8+
return !args.post.firstPost;
9+
}
10+
11+
static hidden(args) {
12+
return args.post.assigned_to_user?.id !== args.state.currentUser.id;
13+
}
14+
15+
@service taskActions;
16+
17+
get icon() {
18+
return this.isAssigned ? "user-times" : "user-plus";
19+
}
20+
21+
get isAssigned() {
22+
return this.args.post.assigned_to_user || this.args.post.assigned_to_group;
23+
}
24+
25+
get title() {
26+
return this.isAssigned
27+
? "discourse_assign.unassign_post.title"
28+
: "discourse_assign.assign_post.title";
29+
}
30+
31+
@action
32+
acceptAnswer() {
33+
if (this.isAssigned) {
34+
unassignPost(this.args.post, this.taskActions);
35+
} else {
36+
assignPost(this.args.post, this.taskActions);
37+
}
38+
}
39+
40+
<template>
41+
<DButton
42+
class={{if
43+
this.isAssigned
44+
"post-action-menu__assign-post assign-post"
45+
"post-action-menu__unassign-post unassign-post"
46+
}}
47+
...attributes
48+
@action={{this.acceptAnswer}}
49+
@icon={{this.icon}}
50+
@title={{this.title}}
51+
/>
52+
</template>
53+
}
54+
55+
// TODO (glimmer-post-menu): Remove these exported functions and move the code into the button action after the widget code is removed
56+
export function assignPost(post, taskActions) {
57+
taskActions.showAssignModal(post, {
58+
isAssigned: false,
59+
targetType: "Post",
60+
});
61+
}
62+
63+
export async function unassignPost(post, taskActions) {
64+
await taskActions.unassign(post.id, "Post");
65+
delete post.topic.indirectly_assigned_to[post.id];
66+
}

assets/javascripts/discourse/initializers/extend-for-assigns.js

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ import { htmlSafe } from "@ember/template";
33
import { isEmpty } from "@ember/utils";
44
import { hbs } from "ember-cli-htmlbars";
55
import { h } from "virtual-dom";
6+
import {
7+
POST_MENU_ADMIN_BUTTON_KEY,
8+
POST_MENU_COPY_LINK_BUTTON_KEY,
9+
POST_MENU_DELETE_BUTTON_KEY,
10+
POST_MENU_LIKE_BUTTON_KEY,
11+
POST_MENU_SHARE_BUTTON_KEY,
12+
POST_MENU_SHOW_MORE_BUTTON_KEY,
13+
} from "discourse/components/post/menu";
614
import SearchAdvancedOptions from "discourse/components/search-advanced-options";
715
import { renderAvatar } from "discourse/helpers/user-avatar";
816
import { withPluginApi } from "discourse/lib/plugin-api";
917
import { registerTopicFooterDropdown } from "discourse/lib/register-topic-footer-dropdown";
1018
import { escapeExpression } from "discourse/lib/utilities";
1119
import RawHtml from "discourse/widgets/raw-html";
1220
import RenderGlimmer from "discourse/widgets/render-glimmer";
21+
import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
1322
import getURL from "discourse-common/lib/get-url";
1423
import { iconHTML, iconNode } from "discourse-common/lib/icon-library";
1524
import discourseComputed from "discourse-common/utils/decorators";
1625
import I18n from "I18n";
26+
import AssignButton, {
27+
assignPost,
28+
unassignPost,
29+
} from "../components/assign-button";
1730
import BulkActionsAssignUser from "../components/bulk-actions/bulk-assign-user";
1831
import EditTopicAssignments from "../components/modal/edit-topic-assignments";
1932
import TopicLevelAssignMenu from "../components/topic-level-assign-menu";
@@ -314,47 +327,9 @@ function initialize(api) {
314327
},
315328
before: "top",
316329
});
317-
if (api.getCurrentUser()?.can_assign) {
318-
api.addPostMenuButton("assign", (post) => {
319-
if (post.firstPost) {
320-
return;
321-
}
322-
if (post.assigned_to_user || post.assigned_to_group) {
323-
return {
324-
action: "unassignPost",
325-
icon: "user-times",
326-
className: "unassign-post",
327-
title: "discourse_assign.unassign_post.title",
328-
position:
329-
post.assigned_to_user?.id === api.getCurrentUser().id
330-
? "first"
331-
: "second-last-hidden",
332-
};
333-
} else {
334-
return {
335-
action: "assignPost",
336-
icon: "user-plus",
337-
className: "assign-post",
338-
title: "discourse_assign.assign_post.title",
339-
position: "second-last-hidden",
340-
};
341-
}
342-
});
343330

344-
api.attachWidgetAction("post", "assignPost", function () {
345-
const taskActions = getOwner(this).lookup("service:task-actions");
346-
taskActions.showAssignModal(this.model, {
347-
isAssigned: false,
348-
targetType: "Post",
349-
});
350-
});
351-
352-
api.attachWidgetAction("post", "unassignPost", function () {
353-
const taskActions = getOwner(this).lookup("service:task-actions");
354-
taskActions.unassign(this.model.id, "Post").then(() => {
355-
delete this.model.topic.indirectly_assigned_to[this.model.id];
356-
});
357-
});
331+
if (api.getCurrentUser()?.can_assign) {
332+
customizePostMenu(api);
358333
}
359334
}
360335

@@ -755,6 +730,77 @@ function initialize(api) {
755730
api.addKeyboardShortcut("g a", "", { path: "/my/activity/assigned" });
756731
}
757732

733+
function customizePostMenu(api) {
734+
const transformerRegistered = api.registerValueTransformer(
735+
"post-menu-buttons",
736+
({ value: dag, context: { post, state } }) => {
737+
dag.add(
738+
"assign",
739+
AssignButton,
740+
post.assigned_to_user?.id === state.currentUser.id
741+
? {
742+
before: [
743+
POST_MENU_LIKE_BUTTON_KEY,
744+
POST_MENU_COPY_LINK_BUTTON_KEY,
745+
POST_MENU_SHARE_BUTTON_KEY,
746+
POST_MENU_SHOW_MORE_BUTTON_KEY,
747+
],
748+
}
749+
: {
750+
before: [
751+
POST_MENU_ADMIN_BUTTON_KEY,
752+
POST_MENU_SHOW_MORE_BUTTON_KEY,
753+
],
754+
after: POST_MENU_DELETE_BUTTON_KEY,
755+
}
756+
);
757+
758+
return dag;
759+
}
760+
);
761+
762+
const silencedKey =
763+
transformerRegistered && "discourse.post-menu-widget-overrides";
764+
765+
withSilencedDeprecations(silencedKey, () => customizeWidgetPostMenu(api));
766+
}
767+
768+
function customizeWidgetPostMenu(api) {
769+
api.addPostMenuButton("assign", (post) => {
770+
if (post.firstPost) {
771+
return;
772+
}
773+
if (post.assigned_to_user || post.assigned_to_group) {
774+
return {
775+
action: "unassignPost",
776+
icon: "user-times",
777+
className: "unassign-post",
778+
title: "discourse_assign.unassign_post.title",
779+
position:
780+
post.assigned_to_user?.id === api.getCurrentUser().id
781+
? "first"
782+
: "second-last-hidden",
783+
};
784+
} else {
785+
return {
786+
action: "assignPost",
787+
icon: "user-plus",
788+
className: "assign-post",
789+
title: "discourse_assign.assign_post.title",
790+
position: "second-last-hidden",
791+
};
792+
}
793+
});
794+
795+
api.attachWidgetAction("post", "assignPost", function () {
796+
assignPost(this.model, getOwner(this).lookup("service:task-actions"));
797+
});
798+
799+
api.attachWidgetAction("post", "unassignPost", function () {
800+
unassignPost(this.model, getOwner(this).lookup("service:task-actions"));
801+
});
802+
}
803+
758804
const REGEXP_USERNAME_PREFIX = /^(assigned:)/gi;
759805

760806
export default {

0 commit comments

Comments
 (0)