FEATURE: Quick access panel for assignments. (#49)

FEATURE: Quick access panel for assignments. (#49)

  • FEATURE: Quick access panel for assignments.

  • Only enable quick access when core is compatiable.

This ensures that the plugin won’t break Discourse instances without the latest QuickAccessPanel feature.

diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
index 724afc8..eecbd6d 100644
--- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
+++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
@@ -3,6 +3,7 @@ import { default as computed } from "ember-addons/ember-computed-decorators";
 import { iconNode } from "discourse-common/lib/icon-library";
 import { h } from "virtual-dom";
 import { iconHTML } from "discourse-common/lib/icon-library";
+import { queryRegistry } from "discourse/widgets/widget";
 
 // TODO: This has to be removed when 2.3 becomes the new stable version.
 import { ListItemDefaults } from "discourse/components/topic-list-item";
@@ -152,12 +153,19 @@ function initialize(api) {
 
   api.addUserMenuGlyph(widget => {
     if (widget.currentUser && widget.currentUser.can_assign) {
-      return {
+      const glyph = {
         label: "discourse_assign.assigned",
         className: "assigned",
         icon: "user-plus",
         href: `${widget.currentUser.path}/activity/assigned`
       };
+
+      if (queryRegistry("quick-access-panel")) {
+        glyph["action"] = "quickAccess";
+        glyph["actionParam"] = "assignments";
+      }
+
+      return glyph;
     }
   });
 
diff --git a/assets/javascripts/discourse/widgets/quick-access-assignments.js.es6 b/assets/javascripts/discourse/widgets/quick-access-assignments.js.es6
new file mode 100644
index 0000000..da4d10c
--- /dev/null
+++ b/assets/javascripts/discourse/widgets/quick-access-assignments.js.es6
@@ -0,0 +1,48 @@
+import { createWidgetFrom, queryRegistry } from "discourse/widgets/widget";
+import { postUrl } from "discourse/lib/utilities";
+
+const ICON = "user-plus";
+
+const QuickAccessPanel = queryRegistry("quick-access-panel");
+
+if (QuickAccessPanel) {
+  createWidgetFrom(QuickAccessPanel, "quick-access-assignments", {
+    buildKey: () => "quick-access-assignments",
+    emptyStatePlaceholderItemKey: "choose_topic.none_found",
+
+    hasMore() {
+      // Always show the button to the assignments page. Users cannot
+      // unassign or reassign from the quick access panel.
+      return true;
+    },
+
+    showAllHref() {
+      return `${this.attrs.path}/activity/assigned`;
+    },
+
+    findNewItems() {
+      return this.store
+        .findFiltered("topicList", {
+          filter: `topics/messages-assigned/${this.currentUser.username_lower}`,
+          params: {
+            exclude_category_ids: [-1]
+          }
+        })
+        .then(({ topic_list }) => {
+          return topic_list.topics.slice(0, this.estimateItemLimit());
+        });
+    },
+
+    itemHtml(assignedTopic) {
+      return this.attach("quick-access-item", {
+        icon: ICON,
+        href: postUrl(
+          assignedTopic.slug,
+          assignedTopic.id,
+          assignedTopic.last_read_post_number + 1
+        ),
+        content: assignedTopic.fancy_title
+      });
+    }
+  });
+}

GitHub sha: d18c347c