Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Jan 9, 2025
1 parent 0e0adc0 commit 59db7b7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
24 changes: 15 additions & 9 deletions src/components/compose-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@ export function ComposePost({ onSubmit }) {
// Converting single post to multiple
const text = posts[0].text;
// Only split if there's actual content and it contains ---
if (text.trim() && text.includes('---')) {
const threads = text.split('---')
.map(t => t.trim())
.filter(t => t)
.map(text => ({ text, image: null }));
if (text.trim() && text.includes("---")) {
const threads = text
.split("---")
.map((t) => t.trim())
.filter((t) => t)
.map((text) => ({ text, image: null }));
setPosts(threads.length > 0 ? threads : [{ text: "", image: null }]);
}
} else {
// Converting multiple posts to single
const combinedText = posts.map(p => p.text).filter(t => t.trim()).join('\n---\n');
const combinedText = posts
.map((p) => p.text)
.filter((t) => t.trim())
.join("\n---\n");
setPosts([{ text: combinedText, image: null }]);
}
};

const handleSubmit = async () => {
const nonEmptyPosts = posts.filter(p => p.text.trim());
const nonEmptyPosts = posts.filter((p) => p.text.trim());
if (nonEmptyPosts.length === 0) {
setError("Please enter your post text");
return;
Expand Down Expand Up @@ -123,11 +127,13 @@ export function ComposePost({ onSubmit }) {

<div className="flex justify-between items-center">
<span className="text-sm text-gray-500">
{isThreadMode ? `${posts.length} parts` : `${posts[0].text.length} characters`}
{isThreadMode
? `${posts.length} parts`
: `${posts[0].text.length} characters`}
</span>
<button
onClick={handleSubmit}
disabled={posts.every(p => !p.text.trim())}
disabled={posts.every((p) => !p.text.trim())}
className="flex items-center gap-2 px-6 py-2 border-2 border-gray-800 hover:bg-gray-100 shadow-[2px_2px_0_rgba(0,0,0,1)] disabled:opacity-50 disabled:cursor-not-allowed"
>
Post
Expand Down
16 changes: 12 additions & 4 deletions src/pages/api/twitter/tweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default async function handler(req, res) {
}

const { posts } = req.body;
if (!Array.isArray(posts) || posts.length === 0 || !posts.every(p => p.text?.trim())) {
if (
!Array.isArray(posts) ||
posts.length === 0 ||
!posts.every((p) => p.text?.trim())
) {
return res.status(400).json({ error: "Valid posts array is required" });
}

Expand All @@ -20,10 +24,14 @@ export default async function handler(req, res) {

try {
const twitterService = await TwitterService.initialize();
const response = await twitterService.tweet(accessToken, accessSecret, posts);
res.status(200).json({
const response = await twitterService.tweet(
accessToken,
accessSecret,
posts,
);
res.status(200).json({
success: true,
data: Array.isArray(response) ? response : [response]
data: Array.isArray(response) ? response : [response],
});
} catch (error) {
console.error("Tweet error:", error);
Expand Down
4 changes: 2 additions & 2 deletions src/services/near-social.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class NearSocialService {

try {
// Combine all posts into a single content, joining with newlines
const combinedText = posts.map(p => p.text).join('\n\n');
const combinedText = posts.map((p) => p.text).join("\n\n");

const content = {
type: "md",
text: combinedText,
Expand Down
12 changes: 6 additions & 6 deletions src/services/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ export class TwitterService {

// Handle array of post objects
if (!Array.isArray(posts)) {
throw new Error('Posts must be an array');
throw new Error("Posts must be an array");
}

if (posts.length === 1) {
// Single tweet
return userClient.v2.tweet(posts[0].text);
} else {
// Thread implementation
let lastTweetId = null;
const responses = [];

for (const post of posts) {
const tweetData = lastTweetId
const tweetData = lastTweetId
? { text: post.text, reply: { in_reply_to_tweet_id: lastTweetId } }
: { text: post.text };

const response = await userClient.v2.tweet(tweetData);
responses.push(response);
lastTweetId = response.data.id;
}

return responses;
}
}
Expand Down

0 comments on commit 59db7b7

Please sign in to comment.