Skip to content

Adds Notion plugin#38

Merged
elliotBraem merged 4 commits intomainfrom
feat/notion-plugin
Feb 5, 2025
Merged

Adds Notion plugin#38
elliotBraem merged 4 commits intomainfrom
feat/notion-plugin

Conversation

@elliotBraem
Copy link
Collaborator

@elliotBraem elliotBraem commented Feb 5, 2025

Summary by CodeRabbit

  • New Features

    • Introduced Notion integration to distribute submissions directly into a Notion database.
    • Updated bot configuration to support new Telegram and Notion settings.
    • Enhanced moderation actions with dedicated buttons for approving and rejecting submissions.
  • Documentation

    • Added a detailed guide on setting up and using the new Notion integration.

@vercel
Copy link

vercel bot commented Feb 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
curatedotfun-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2025 5:38am
curatedotfun-docs 🛑 Canceled (Inspect) Feb 5, 2025 5:38am
curatedotfun-landing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2025 5:38am

@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2025

Walkthrough

This pull request introduces several modifications across the project. The distribution services and their tests now use an updated parameter structure, replacing simple IDs with full submission objects. A configuration file handling environment variables has been removed in favor of a centralized configuration service. A new Notion integration has been added along with its documentation and related configuration updates. Additional improvements include enhanced Twitter client validations, changes to submission handling, and frontend moderation actions using button clicks with environment-specific logic. Finally, unused dependencies have been removed from the package manifest.

Changes

File(s) Change Summary
backend/.../mocks/distribution-service.mock.ts
backend/.../services/distribution/distribution.service.ts
backend/.../types/plugin.ts
Distribution Processing Changes: Updated method signatures for processStreamOutput and distributeContent to accept a TwitterSubmission object instead of separate IDs or strings; internal logic now extracts submission details (e.g. tweetId) accordingly, and the interface definition in DistributorPlugin has been updated.
backend/.../config/config.ts
backend/.../index.ts
Configuration & Environment Changes: Deleted the deprecated config file that performed environment validation; updated index.ts to use ConfigService for loading configuration and replaced dotenv.config() with an import of "dotenv/config".
backend/.../external/notion/index.ts
docs/.../notion.md
curate.config.test.json
External Integrations & Plugin Configurations: Introduced a new NotionPlugin implementing the DistributorPlugin interface, added corresponding documentation, and modified the configuration file to replace the supabase plugin with telegram and add the notion plugin, including required credentials.
backend/.../services/submissions/submission.service.ts
backend/.../services/twitter/client.ts
Submission & Twitter Client Enhancements: Updated submission handling by referencing existing submissions and ensuring tweet IDs are non-null; added Twitter credential validations in the client initialization along with improved error handling.
frontend/.../components/FeedItem.tsx
frontend/.../lib/twitter.ts
Frontend Moderation Updates: Replaced anchor tags with buttons for approving/rejecting Twitter submissions; added new functions (handleApprove, handleReject, isDev, generateTweetId) to manage tweet actions based on development or production environments.
package.json Package Cleanup: Removed unused dependencies (@curatedotfun/supabase and elysia-rate-limit), streamlining the project’s dependency management.

Sequence Diagram(s)

sequenceDiagram
  participant DS as DistributionService
  participant NP as NotionPlugin
  participant NAPI as NotionAPI

  DS->>NP: distribute(feedId, submission)
  NP->>NP: Validate configuration (token, databaseId)
  NP->>NAPI: Create database row with submission data
  NAPI-->>NP: Success/Error response
  NP-->>DS: Return result
Loading
sequenceDiagram
  participant User
  participant FI as FeedItem
  participant TL as TwitterLib
  participant API as MockAPI/TwitterIntent

  User->>FI: Click "Approve" button
  FI->>TL: handleApprove(submission, botId)
  alt Development Mode
    TL->>API: POST tweet approval details
    API-->>TL: Response
  else Production Mode
    TL->>Browser: Open Twitter intent URL for approval
  end
Loading

Possibly related PRs

Poem

I hopped through lines of code today,
Tail twitching at every refined display.
With submissions now wrapped in a fuller embrace,
And configs centralized with impeccable grace.
My rabbit paws tap to a joyful beat,
As Notion and Twitter now dance in sync so sweet.
Carrots and code—oh, what a treat!

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (7)
backend/src/external/notion/index.ts (2)

5-34: Enhance error reporting in the initialize process.
Capturing and logging the entire error is helpful, but consider adding more context, such as which feedId triggered the error. Also evaluate whether the raw error object might contain sensitive information that should be sanitized.


36-50: Consider implementing retry logic for Notion API calls.
Network or rate-limit errors on Notion’s side can cause transient failures. A retry mechanism would help improve resilience.

backend/src/__tests__/mocks/distribution-service.mock.ts (1)

7-11: Replace ‘any’ with a more specific type to ensure robust tests.
If submission is always a TwitterSubmission, strongly typing it will help catch future changes and potential usage errors (e.g., a missing tweetId).

frontend/src/lib/twitter.ts (1)

9-12: Consider using a more robust ID generation method.

The current ID generation might lead to collisions in high-concurrency scenarios.

Consider using a UUID library:

-const generateTweetId = () =>
-  `${Date.now()}${Math.floor(Math.random() * 1000)}`;
+import { v4 as uuidv4 } from 'uuid';
+const generateTweetId = () => uuidv4();
backend/src/services/distribution/distribution.service.ts (1)

168-185: Remove commented-out code.

The commented-out code block should be removed since it's no longer needed and the TODO comment above explains the future requirements.

Apply this diff:

-    // const content = "";
-
-    // // Transform content (required for recap)
-    // const processedContent = await this.transformContent(
-    //   transform.plugin,
-    //   content,
-    //   transform.config,
-    // );
-
-    // // Distribute to all configured outputs
-    // for (const dist of distribute) {
-    //   await this.distributeContent(
-    //     feedId,
-    //     dist.plugin,
-    //     processedContent,
-    //     dist.config,
-    //   );
-    // }
backend/src/services/twitter/client.ts (1)

207-207: Consider safer type handling for tweet IDs.

The non-null assertions (!.) could be replaced with safer type handling.

Apply this diff:

-        const tweetId = BigInt(tweet.id!);
+        const tweetId = tweet.id ? BigInt(tweet.id) : null;
+        if (!tweetId) {
+          logger.warn('Tweet found without ID:', tweet);
+          continue;
+        }

         // Sort chronologically (oldest to newest)
         allNewTweets.sort((a, b) => {
-          const aId = BigInt(a.id!);
-          const bId = BigInt(b.id!);
+          const aId = a.id ? BigInt(a.id) : null;
+          const bId = b.id ? BigInt(b.id) : null;
+          if (!aId || !bId) return 0;
           return aId > bId ? 1 : aId < bId ? -1 : 0;
         });

         // Use the first tweet from the batch since it's the newest
         const highestId = batch[0].id;
-        await this.setLastCheckedTweetId(highestId!);
+        if (highestId) {
+          await this.setLastCheckedTweetId(highestId);
+        } else {
+          logger.warn('Highest tweet found without ID:', batch[0]);
+        }

Also applies to: 215-216, 224-224

docs/docs/plugins/distributors/notion.md (1)

1-101: Documentation is comprehensive but could be enhanced.

The Notion plugin documentation is well-structured with clear setup instructions, security considerations, and configuration examples. Consider these enhancements:

  1. Add a hyphen in "full-page" when used as a compound adjective.
  2. Add a comma in the tip section: "If your stream had been disabled, and you have existing, approved submissions..."
  3. Consider adding troubleshooting section for common issues.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~36-~36: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...tegration: - Open your database as a full page - Click the "..." menu in the top ri...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[uncategorized] ~85-~85: This verb may not be in the correct tense. Consider changing the tense to fit the context better.
Context: ... new rows. :::tip If your stream had been disabled and you have existing, ap...

(AI_EN_LECTOR_REPLACEMENT_VERB_TENSE)


[uncategorized] ~85-~85: Use a comma before ‘and’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...:tip If your stream had been disabled and you have existing, approved submissions...

(COMMA_COMPOUND_SENTENCE)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 350cd52 and 7950440.

⛔ Files ignored due to path filters (1)
  • bun.lockb is excluded by !**/bun.lockb
📒 Files selected for processing (13)
  • backend/src/__tests__/mocks/distribution-service.mock.ts (1 hunks)
  • backend/src/config/config.ts (0 hunks)
  • backend/src/external/notion/index.ts (1 hunks)
  • backend/src/index.ts (3 hunks)
  • backend/src/services/distribution/distribution.service.ts (4 hunks)
  • backend/src/services/submissions/submission.service.ts (4 hunks)
  • backend/src/services/twitter/client.ts (2 hunks)
  • backend/src/types/plugin.ts (1 hunks)
  • curate.config.test.json (2 hunks)
  • docs/docs/plugins/distributors/notion.md (1 hunks)
  • frontend/src/components/FeedItem.tsx (2 hunks)
  • frontend/src/lib/twitter.ts (1 hunks)
  • package.json (0 hunks)
💤 Files with no reviewable changes (2)
  • package.json
  • backend/src/config/config.ts
🧰 Additional context used
🪛 LanguageTool
docs/docs/plugins/distributors/notion.md

[uncategorized] ~36-~36: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...tegration: - Open your database as a full page - Click the "..." menu in the top ri...

(EN_COMPOUND_ADJECTIVE_INTERNAL)


[uncategorized] ~85-~85: This verb may not be in the correct tense. Consider changing the tense to fit the context better.
Context: ... new rows. :::tip If your stream had been disabled and you have existing, ap...

(AI_EN_LECTOR_REPLACEMENT_VERB_TENSE)


[uncategorized] ~85-~85: Use a comma before ‘and’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...:tip If your stream had been disabled and you have existing, approved submissions...

(COMMA_COMPOUND_SENTENCE)

🔇 Additional comments (14)
backend/src/external/notion/index.ts (2)

1-4: Check for consistent type imports and references.
Your imports look good and match the interfaces from types/plugin and types/twitter. No issues identified.


52-101: Verify database schema requirements for Notion pages.
Many Notion databases require a title property to be set for new pages. The plugin currently omits a “title” property, which may lead to unexpected behavior.

Do you want me to generate a shell script to search for any references or usage instructions for a required Notion title property across the codebase?

backend/src/types/plugin.ts (1)

4-4: Well-aligned interface update.
Updating the distribute method signature to accept a TwitterSubmission object centralizes handling of submission data and ensures consistency across the application.

backend/src/services/distribution/distribution.service.ts (1)

65-69: LGTM! Parameter type change aligns with plugin interface.

The change from content string to TwitterSubmission object provides more context to plugins.

backend/src/services/twitter/client.ts (1)

114-122: LGTM! Early validation of required credentials.

Good practice to validate environment variables early in the initialization process.

backend/src/index.ts (3)

4-4: LGTM!

The import changes look good. Using dotenv/config is a cleaner way to load environment variables, and the ConfigService import aligns with the centralized configuration management approach.

Also applies to: 10-10


40-45: LGTM!

The configuration loading logic is well-structured and follows a clear sequence:

  1. Start spinner with loading message
  2. Get ConfigService instance
  3. Load configuration
  4. Get loaded config
  5. Update spinner with success message

322-322: LGTM!

The error handling update correctly includes "config" in the list of spinner keys to fail during initialization errors.

backend/src/services/submissions/submission.service.ts (3)

296-296: LGTM!

The changes correctly prioritize using the existing submission over creating a new one when processing stream outputs.

Also applies to: 334-334


353-353: LGTM!

The non-null assertion for tweet ID is safe here as it's already checked in the calling context.


365-365: LGTM!

The error message has been improved to be more descriptive by including both tweet ID and user ID validation.

curate.config.test.json (3)

3-3: LGTM!

The bot ID update to "test_bot" is consistent with the test environment naming convention.


11-18: LGTM!

The plugin configurations for Telegram and Notion are well-structured and correctly specify their types and URLs.


35-46: Verify the environment variable placeholders.

The configuration uses environment variable placeholders for sensitive values:

  • {CRYPTOGRANTWIRE_NOTION_TOKEN}
  • {TELEGRAM_BOT_TOKEN}

Run this script to check if these environment variables are properly documented:

Comment on lines +14 to +51
export const handleApprove = async (
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!approve @${botId}`,
username: "test_admin",
userId: "mock-user-id-test_admin",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});

if (!response.ok) {
console.error("Failed to submit approval tweet");
return;
}

console.log("Development mode: Submitted approval tweet", { newTweetId });
return;
}

// In production, open Twitter intent
window.open(
getTwitterIntentUrl({
action: "approve",
submission,
botId,
}),
"_blank",
);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling in development mode.

The error handling could be improved to provide better feedback to users.

Apply this diff to enhance error handling:

   if (!response.ok) {
-    console.error("Failed to submit approval tweet");
+    const errorData = await response.json().catch(() => ({}));
+    console.error("Failed to submit approval tweet:", {
+      status: response.status,
+      statusText: response.statusText,
+      error: errorData
+    });
     return;
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const handleApprove = async (
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!approve @${botId}`,
username: "test_admin",
userId: "mock-user-id-test_admin",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});
if (!response.ok) {
console.error("Failed to submit approval tweet");
return;
}
console.log("Development mode: Submitted approval tweet", { newTweetId });
return;
}
// In production, open Twitter intent
window.open(
getTwitterIntentUrl({
action: "approve",
submission,
botId,
}),
"_blank",
);
};
export const handleApprove = async (
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!approve @${botId}`,
username: "test_admin",
userId: "mock-user-id-test_admin",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
console.error("Failed to submit approval tweet:", {
status: response.status,
statusText: response.statusText,
error: errorData
});
return;
}
console.log("Development mode: Submitted approval tweet", { newTweetId });
return;
}
// In production, open Twitter intent
window.open(
getTwitterIntentUrl({
action: "approve",
submission,
botId,
}),
"_blank",
);
};

Comment on lines +53 to +90
export const handleReject = async (
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!reject @${botId} spam`,
username: "test_admin",
userId: "test_admin_id",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});

if (!response.ok) {
console.error("Failed to submit rejection tweet");
return;
}

console.log("Development mode: Submitted rejection tweet", { newTweetId });
return;
}

// In production, open Twitter intent
window.open(
getTwitterIntentUrl({
action: "reject",
submission,
botId,
}),
"_blank",
);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Code duplication in handleReject function.

The handleReject function largely duplicates the handleApprove logic.

Extract the common logic into a shared function:

+const handleModerationAction = async (
+  action: 'approve' | 'reject',
+  submission: TwitterSubmissionWithFeedData,
+  botId: string,
+) => {
+  if (isDev()) {
+    const newTweetId = generateTweetId();
+    const response = await fetch("/api/test/tweets", {
+      method: "POST",
+      headers: { "Content-Type": "application/json" },
+      body: JSON.stringify({
+        id: newTweetId,
+        text: `!${action} @${botId}${action === 'reject' ? ' spam' : ''}`,
+        username: "test_admin",
+        userId: "mock-user-id-test_admin",
+        timeParsed: new Date(),
+        inReplyToStatusId: submission.curatorTweetId,
+      }),
+    });
+
+    if (!response.ok) {
+      console.error(`Failed to submit ${action} tweet`);
+      return;
+    }
+
+    console.log(`Development mode: Submitted ${action} tweet`, { newTweetId });
+    return;
+  }
+
+  window.open(
+    getTwitterIntentUrl({
+      action,
+      submission,
+      botId,
+    }),
+    "_blank",
+  );
+};
+
+export const handleApprove = (submission: TwitterSubmissionWithFeedData, botId: string) =>
+  handleModerationAction('approve', submission, botId);
+
+export const handleReject = (submission: TwitterSubmissionWithFeedData, botId: string) =>
+  handleModerationAction('reject', submission, botId);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const handleReject = async (
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!reject @${botId} spam`,
username: "test_admin",
userId: "test_admin_id",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});
if (!response.ok) {
console.error("Failed to submit rejection tweet");
return;
}
console.log("Development mode: Submitted rejection tweet", { newTweetId });
return;
}
// In production, open Twitter intent
window.open(
getTwitterIntentUrl({
action: "reject",
submission,
botId,
}),
"_blank",
);
};
const handleModerationAction = async (
action: 'approve' | 'reject',
submission: TwitterSubmissionWithFeedData,
botId: string,
) => {
if (isDev()) {
const newTweetId = generateTweetId();
const response = await fetch("/api/test/tweets", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: newTweetId,
text: `!${action} @${botId}${action === 'reject' ? ' spam' : ''}`,
username: "test_admin",
userId: "mock-user-id-test_admin",
timeParsed: new Date(),
inReplyToStatusId: submission.curatorTweetId,
}),
});
if (!response.ok) {
console.error(`Failed to submit ${action} tweet`);
return;
}
console.log(`Development mode: Submitted ${action} tweet`, { newTweetId });
return;
}
window.open(
getTwitterIntentUrl({
action,
submission,
botId,
}),
"_blank",
);
};
export const handleApprove = (submission: TwitterSubmissionWithFeedData, botId: string) =>
handleModerationAction('approve', submission, botId);
export const handleReject = (submission: TwitterSubmissionWithFeedData, botId: string) =>
handleModerationAction('reject', submission, botId);

Comment on lines +100 to +105
<button
onClick={() => handleApprove(submission, botId)}
className="px-3 py-1.5 bg-green-200 hover:bg-green-300 text-black rounded-md border-2 border-black shadow-sharp hover:shadow-sharp-hover transition-all duration-200 translate-x-0 translate-y-0 hover:-translate-x-0.5 hover:-translate-y-0.5 text-sm font-medium"
>
approve
</a>
<a
href={getTwitterIntentUrl({ action: "reject", submission, botId })}
target="_blank"
rel="noopener noreferrer"
</button>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add loading states to moderation buttons.

The buttons should indicate when an action is in progress to prevent double-clicks.

Add loading states:

+import { useState } from 'react';

 const ModerationActions = ({
   submission,
 }: {
   submission: TwitterSubmissionWithFeedData;
 }) => {
   const botId = useBotId();
+  const [isLoading, setIsLoading] = useState<'approve' | 'reject' | null>(null);
+
+  const handleAction = async (action: 'approve' | 'reject') => {
+    setIsLoading(action);
+    try {
+      await (action === 'approve' 
+        ? handleApprove(submission, botId)
+        : handleReject(submission, botId));
+    } finally {
+      setIsLoading(null);
+    }
+  };

   return (
     <div className="flex flex-col gap-2 mt-4">
       <button
-        onClick={() => handleApprove(submission, botId)}
+        onClick={() => handleAction('approve')}
+        disabled={isLoading !== null}
         className="px-3 py-1.5 bg-green-200 hover:bg-green-300 text-black rounded-md border-2 border-black shadow-sharp hover:shadow-sharp-hover transition-all duration-200 translate-x-0 translate-y-0 hover:-translate-x-0.5 hover:-translate-y-0.5 text-sm font-medium"
       >
-        approve
+        {isLoading === 'approve' ? 'approving...' : 'approve'}
       </button>
       <button
-        onClick={() => handleReject(submission, botId)}
+        onClick={() => handleAction('reject')}
+        disabled={isLoading !== null}
         className="px-3 py-1.5 bg-red-200 hover:bg-red-300 text-black rounded-md border-2 border-black shadow-sharp hover:shadow-sharp-hover transition-all duration-200 translate-x-0 translate-y-0 hover:-translate-x-0.5 hover:-translate-y-0.5 text-sm font-medium"
       >
-        reject
+        {isLoading === 'reject' ? 'rejecting...' : 'reject'}
       </button>
     </div>
   );
 };

Also applies to: 106-111

@elliotBraem elliotBraem merged commit f4b5115 into main Feb 5, 2025
2 checks passed
@elliotBraem elliotBraem deleted the feat/notion-plugin branch February 5, 2025 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant