Skip to content

Commit 092c614

Browse files
committed
first commit
1 parent c603b75 commit 092c614

File tree

9 files changed

+196
-6
lines changed

9 files changed

+196
-6
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
# discourse-sidebar-new-topic-button
2-
3-
**Theme Summary**
4-
5-
For more information, please see: **url to meta topic**

common/common.scss

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.navigation-controls {
2+
.topic-drafts-menu-trigger,
3+
.fk-d-button-tooltip {
4+
display: none;
5+
}
6+
}
7+
8+
.sidebar-new-topic-button {
9+
flex: 1 1 auto;
10+
11+
&__wrapper {
12+
box-sizing: border-box;
13+
display: flex;
14+
margin: 1.5rem 1rem 0;
15+
16+
.mobile-view & {
17+
margin: 0 0 1rem;
18+
}
19+
20+
&:has(.topic-drafts-menu-trigger) {
21+
.sidebar-new-topic-button {
22+
border-radius: var(--d-button-border-radius) 0 0
23+
var(--d-button-border-radius);
24+
border-right: 1px solid var(--primary-300);
25+
}
26+
}
27+
28+
.fk-d-button-tooltip {
29+
flex: 1 1 auto;
30+
}
31+
32+
.topic-drafts-menu-trigger {
33+
flex: 0 1 auto;
34+
margin: 0;
35+
border-radius: 0 var(--d-button-border-radius)
36+
var(--d-button-border-radius) 0;
37+
}
38+
}
39+
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { apiInitializer } from "discourse/lib/api";
2+
import SidebarNewTopicButton from "../components/sidebar-new-topic-button";
3+
4+
export default apiInitializer("1.8.0", (api) => {
5+
api.renderInOutlet("before-sidebar-sections", SidebarNewTopicButton);
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import Component from "@glimmer/component";
2+
import { tracked } from "@glimmer/tracking";
3+
import { action } from "@ember/object";
4+
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
5+
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
6+
import { service } from "@ember/service";
7+
import { gt } from "truth-helpers";
8+
import CreateTopicButton from "discourse/components/create-topic-button";
9+
import not from "truth-helpers/helpers/not";
10+
11+
export default class SidebarNewTopicButton extends Component {
12+
@service composer;
13+
@service currentUser;
14+
@service siteSettings;
15+
@service router;
16+
17+
@tracked category;
18+
@tracked tag;
19+
20+
get shouldRender() {
21+
return this.currentUser;
22+
}
23+
24+
get canCreateTopic() {
25+
return this.currentUser?.can_create_topic;
26+
}
27+
28+
get draftCount() {
29+
return this.currentUser?.get("draft_count");
30+
}
31+
32+
get createTopicTargetCategory() {
33+
if (this.category?.canCreateTopic) {
34+
return this.category;
35+
}
36+
37+
if (this.siteSettings.default_subcategory_on_read_only_category) {
38+
return this.category?.subcategoryWithCreateTopicPermission;
39+
}
40+
}
41+
42+
get tagRestricted() {
43+
return this.tag?.staff;
44+
}
45+
46+
get createTopicDisabled() {
47+
return (
48+
(this.category && !this.createTopicTargetCategory) || this.tagRestricted
49+
);
50+
}
51+
52+
get categoryReadOnlyBanner() {
53+
if (this.category && this.currentUser && this.createTopicDisabled) {
54+
return this.category.read_only_banner;
55+
}
56+
}
57+
58+
get createTopicClass() {
59+
const baseClasses = "btn-default sidebar-new-topic-button";
60+
return this.categoryReadOnlyBanner
61+
? `${baseClasses} disabled`
62+
: baseClasses;
63+
}
64+
65+
@action
66+
createNewTopic() {
67+
this.composer.openNewTopic({ category: this.category, tags: this.tag?.id });
68+
}
69+
70+
@action
71+
getCategoryandTag() {
72+
this.category = this.router.currentRoute.attributes.category || null;
73+
this.tag = this.router.currentRoute.attributes.tag || null;
74+
}
75+
76+
<template>
77+
{{#if this.shouldRender}}
78+
<div
79+
class="sidebar-new-topic-button__wrapper"
80+
{{didInsert this.getCategoryandTag}}
81+
{{didUpdate this.getCategoryandTag this.router.currentRoute}}
82+
>
83+
<CreateTopicButton
84+
@canCreateTopic={{this.canCreateTopic}}
85+
@action={{this.createNewTopic}}
86+
@disabled={{this.createTopicDisabled}}
87+
@label="topic.create"
88+
@btnClass={{this.createTopicClass}}
89+
@canCreateTopicOnTag={{not this.tagRestricted}}
90+
@showDrafts={{gt this.draftCount 0}}
91+
/>
92+
</div>
93+
{{/if}}
94+
</template>
95+
}

locales/en.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
en:
22
theme_metadata:
33
description: Adds a new topic button at the top of the navigation sidebar
4-
settings:
5-
example_setting: A description of a setting.

spec/system/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Sidebar New Topic Button", system: true do
4+
fab!(:theme) { upload_theme_component }
5+
6+
it "does not render the sidebar button for anons" do
7+
visit("/latest")
8+
expect(page).not_to have_css(".sidebar-new-topic-button__wrapper")
9+
expect(page).not_to have_css(".sidebar-new-topic-button:not(.disabled)")
10+
end
11+
end
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Sidebar New Topic Button", system: true do
4+
fab!(:theme) { upload_theme_component }
5+
fab!(:group)
6+
fab!(:user) { Fabricate(:user, trust_level: 3, groups: [group]) }
7+
fab!(:category)
8+
fab!(:private_category) do
9+
c = Fabricate(:category_with_definition)
10+
c.set_permissions(group => :readonly)
11+
c.save
12+
c
13+
end
14+
15+
before { sign_in(user) }
16+
17+
it "renders the new topic button in the sidebar" do
18+
visit("/latest")
19+
expect(page).to have_css(".sidebar-new-topic-button__wrapper")
20+
expect(page).to have_css(".sidebar-new-topic-button:not(.disabled)")
21+
end
22+
23+
it "opens the composer when clicked" do
24+
visit("/")
25+
find(".sidebar-new-topic-button").click
26+
expect(page).to have_css("#reply-title")
27+
end
28+
29+
it "shows draft menu when drafts exist" do
30+
Draft.create!(user: user, draft_key: "topic_1", data: {})
31+
32+
visit("/")
33+
expect(page).to have_css(".sidebar-new-topic-button__wrapper .topic-drafts-menu-trigger")
34+
end
35+
36+
it "disables button when visiting read-only category" do
37+
visit("/c/#{private_category.slug}/#{private_category.id}")
38+
39+
expect(page).to have_css(".sidebar-new-topic-button[disabled]")
40+
41+
visit("/c/#{category.slug}/#{category.id}")
42+
43+
expect(page).not_to have_css(".sidebar-new-topic-button[disabled]")
44+
end
45+
end

0 commit comments

Comments
 (0)