Skip to content

Commit d016087

Browse files
committed
Auto tweet only when going through screenshot flow
1 parent add7fbd commit d016087

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

app/controllers/share/snippet_screenshots_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def new
1414
def create
1515
@snippet = Snippet.find(params[:snippet_id])
1616
@snippet.attach_screenshot_from_base64(params[:screenshot])
17-
redirect_to new_share_snippet_tweet_path(@snippet), notice: "Screenshot attached".emojoy
17+
redirect_to new_share_snippet_tweet_path(@snippet, auto: true), notice: "Screenshot attached".emojoy
1818
end
1919
end

app/controllers/share/snippet_tweets_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ class Share::SnippetTweetsController < ApplicationController
33

44
def new
55
@snippet = Snippet.find(params[:snippet_id])
6+
@auto = params[:auto] == "true"
67
end
78
end

app/javascript/controllers/snippets/tweet.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ const console = debug('app:javascript:controllers:snippets:tweet');
77
export default class extends Controller {
88
static values = {
99
url: String,
10+
auto: Boolean,
1011
};
1112

1213
declare urlValue: string;
14+
declare autoValue: boolean;
1315

1416
connect() {
1517
console.log('Connect!');
1618

17-
this.tweet();
19+
if (this.autoValue) {
20+
this.tweet();
21+
}
1822
}
1923

2024
tweet() {
2125
console.log('Tweet!');
2226

23-
const tweetText = encodeURIComponent(this.urlValue);
27+
const url = this.urlValue;
28+
29+
const tweetText = encodeURIComponent(`Created with @joyofrails ${url}`);
30+
2431
const tweetUrl = `https://twitter.com/intent/tweet?text=${tweetText}`;
2532

2633
window.open(

app/views/share/snippet_tweets/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<%= render Pages::Header.new(title: "Share Snippet: Tweet") %>
22
<div class="section-content container py-gap">
33
<%= turbo_frame_tag :snippet_form, data: {turbo_action: "advance"} do %>
4-
<%= render Share::SnippetTweets::Tweet.new(@snippet) %>
4+
<%= render Share::SnippetTweets::Tweet.new(@snippet, auto: @auto) %>
55
<% end %>
66

77
<div>
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
class Share::SnippetTweets::Tweet < ApplicationComponent
2-
def initialize(snippet)
2+
include Phlex::Rails::Helpers::ButtonTag
3+
include Phlex::Rails::Helpers::ClassNames
4+
include PhlexConcerns::FlexBlock
5+
6+
def initialize(snippet, auto: false)
37
@snippet = snippet
8+
@auto = auto
49
end
510

611
def view_template
7-
render CodeBlock::Snippet.new(
8-
@snippet,
9-
screenshot: true,
12+
div(
13+
class: "snippet-tweet grid-content",
1014
data: {
1115
controller: "snippet-tweet",
12-
snippet_tweet_url_value: tweet_url
16+
snippet_tweet_url_value: tweet_url,
17+
snippet_tweet_auto_value: auto?.to_s
1318
}
14-
)
19+
) do
20+
render CodeBlock::Snippet.new(
21+
@snippet,
22+
screenshot: true
23+
)
24+
25+
flex_block do
26+
button_tag "Tweet",
27+
class: class_names("button", "primary", hidden: auto?),
28+
data: {action: "click->snippet-tweet#tweet"}
29+
end
30+
end
1531
end
1632

1733
def tweet_url
1834
@snippet.screenshot.attached? ? rails_storage_proxy_url(@snippet.screenshot) : share_snippet_url(@snippet)
1935
end
36+
37+
def auto? = !!@auto
2038
end

0 commit comments

Comments
 (0)