Skip to content

Commit 35f34ea

Browse files
committed
Rework twitter share button
Consolidate stimulus tweet functionality to link and extract as Phlex component for toolbar
1 parent b4262e2 commit 35f34ea

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

app/javascript/controllers/snippets/tweet.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,31 @@ const WINDOW_OPTIONS = {
2121
const WINDOW_OPTIONS_ARGUMENT = Object.entries(WINDOW_OPTIONS)
2222
.map(([key, value]) => `${key}=${value}`)
2323
.join(',');
24-
export default class extends Controller {
24+
export default class extends Controller<HTMLAnchorElement> {
2525
static values = {
26-
url: String,
2726
auto: Boolean,
27+
url: String,
2828
};
2929

30-
declare urlValue: string;
31-
declare autoValue: boolean;
30+
declare readonly autoValue: boolean;
31+
declare readonly urlValue: string;
3232

3333
connect() {
3434
console.log('Connect!');
3535

3636
if (this.autoValue) {
37-
this.tweet();
37+
this.element.click();
3838
Turbo.visit(this.urlValue);
3939
}
4040
}
4141

42-
tweet() {
43-
console.log('Tweet!');
44-
45-
const url = this.urlValue;
46-
47-
const tweetText = encodeURIComponent(`Created with @joyofrails ${url}`);
42+
tweet = (event: Event) => {
43+
if (event) {
44+
event.preventDefault();
45+
}
4846

49-
const tweetUrl = `https://x.com/intent/post?text=${tweetText}`;
47+
const tweetUrl = (this.element as HTMLAnchorElement).href;
5048

5149
window.open(tweetUrl, '_blank', WINDOW_OPTIONS_ARGUMENT);
52-
}
50+
};
5351
}
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class Share::SnippetTweets::Tweet < ApplicationComponent
2-
include Phlex::Rails::Helpers::ButtonTag
3-
include Phlex::Rails::Helpers::ClassNames
42
include PhlexConcerns::FlexBlock
53

64
def initialize(snippet, auto: false)
@@ -9,30 +7,17 @@ def initialize(snippet, auto: false)
97
end
108

119
def view_template
12-
div(
13-
class: "snippet-tweet grid-content",
14-
data: {
15-
controller: "snippet-tweet",
16-
snippet_tweet_url_value: tweet_url,
17-
snippet_tweet_auto_value: auto?.to_s
18-
}
19-
) do
10+
div(class: "snippet-tweet grid-content") do
2011
render CodeBlock::Snippet.new(
2112
@snippet,
2213
screenshot: true
2314
)
2415

2516
flex_block do
26-
button_tag "Share",
27-
class: class_names("button", "primary"),
28-
data: {action: "click->snippet-tweet#tweet"}
17+
render Share::SnippetTweets::TweetButton.new(@snippet, auto: auto?)
2918
end
3019
end
3120
end
3221

33-
def tweet_url
34-
share_snippet_url(@snippet)
35-
end
36-
3722
def auto? = !!@auto
3823
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Share::SnippetTweets::TweetButton < ApplicationComponent
2+
def initialize(snippet, auto: false)
3+
@snippet = snippet
4+
@auto = auto
5+
end
6+
7+
def view_template
8+
a(
9+
href: tweet_url,
10+
class: "button primary",
11+
data: {
12+
controller: "snippet-tweet",
13+
action: "click->snippet-tweet#tweet",
14+
snippet_tweet_auto_value: auto?.to_s,
15+
snippet_tweet_url_value: share_url
16+
}
17+
) do
18+
"Share"
19+
end
20+
end
21+
22+
def tweet_url
23+
"https://x.com/intent/post?text=#{encode(tweet_text)}"
24+
end
25+
26+
def share_url
27+
share_snippet_url(@snippet)
28+
end
29+
30+
def tweet_text
31+
"Created with @joyofrails #{share_url}"
32+
end
33+
34+
def encode(text)
35+
URI.encode_www_form_component(text)
36+
end
37+
38+
def auto? = !!@auto
39+
end

app/views/share/snippets/toolbar.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def initialize(snippet, current_user: nil)
1010

1111
def view_template
1212
flex_block do
13-
link_to "Share", share_url, class: "button primary"
13+
render Share::SnippetTweets::TweetButton.new(@snippet)
14+
1415
if @current_user&.can_edit?(@snippet)
1516
link_to "Edit this snippet", edit_share_snippet_path(@snippet),
1617
class: "button secondary",

0 commit comments

Comments
 (0)