Skip to content

add reddit features to reviewdb#4216

Open
mantikafasi wants to merge 2 commits into
Vendicated:mainfrom
mantikafasi:aura
Open

add reddit features to reviewdb#4216
mantikafasi wants to merge 2 commits into
Vendicated:mainfrom
mantikafasi:aura

Conversation

@mantikafasi
Copy link
Copy Markdown
Contributor

No description provided.

@mantikafasi mantikafasi changed the title add tuff add reddit features to reviewdb May 13, 2026
@Vendicated
Copy link
Copy Markdown
Owner

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements a voting system for reviews, allowing users to upvote or downvote content. Key changes include the addition of a VoteButton component, new API endpoints for managing votes, and state management within the ReviewComponent to track scores and user interactions. The review feedback focuses on improving the robustness of the state management by using useEffect to synchronize local state with props, implementing a loading state to prevent race conditions during concurrent voting requests, and utilizing functional state updates to ensure score accuracy.

@@ -27,7 +27,7 @@ import { findCssClassesLazy } from "@webpack";
import { Alerts, IconUtils, Parser, Timestamp, useState } from "@webpack/common";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Add useEffect to the imports from @webpack/common to allow syncing the local state with props when they change (e.g., after a refetch).

Suggested change
import { Alerts, IconUtils, Parser, Timestamp, useState } from "@webpack/common";
import { Alerts, IconUtils, Parser, Timestamp, useEffect, useState } from "@webpack/common";

Comment on lines +43 to +44
const [localVote, setLocalVote] = useState<boolean | null>(review.userVote ?? null);
const [score, setScore] = useState(review.score ?? 0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Introduce a loading state to prevent concurrent voting requests and use useEffect to keep the local state in sync with the review prop. Since useState only initializes once, the local score and localVote would otherwise become stale if the parent component refetches data.

    const [localVote, setLocalVote] = useState<boolean | null>(review.userVote ?? null);
    const [score, setScore] = useState(review.score ?? 0);
    const [isVoting, setIsVoting] = useState(false);

    useEffect(() => {
        setLocalVote(review.userVote ?? null);
        setScore(review.score ?? 0);
    }, [review.userVote, review.score]);

Comment on lines +109 to +126
async function submitVote(isUpvote: boolean) {
if (review.sender.discordID === Auth.user?.discordID) {
return showToast("You cannot vote on your own review.");
}

if (localVote === isUpvote) {
return showToast(`You already ${isUpvote ? "upvoted" : "downvoted"} this review.`);
}

if (await voteReview(review.id, isUpvote)) {
const delta = localVote == null
? isUpvote ? 1 : -1
: isUpvote ? 2 : -2;

setLocalVote(isUpvote);
setScore(score + delta);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Implement the isVoting check to prevent race conditions from multiple clicks. Additionally, use a functional update for setScore to ensure the score is updated correctly based on the most recent state, and wrap the API call in a try...finally block to ensure the loading state is reset even if the request fails.

    async function submitVote(isUpvote: boolean) {
        if (isVoting) return;
        if (review.sender.discordID === Auth.user?.discordID) {
            return showToast("You cannot vote on your own review.");
        }

        if (localVote === isUpvote) {
            return showToast(`You already ${isUpvote ? "upvoted" : "downvoted"} this review.`);
        }

        setIsVoting(true);
        try {
            if (await voteReview(review.id, isUpvote)) {
                const delta = localVote == null
                    ? isUpvote ? 1 : -1
                    : isUpvote ? 2 : -2;

                setLocalVote(isUpvote);
                setScore(s => s + delta);
            }
        } finally {
            setIsVoting(false);
        }
    }

@Vendicated
Copy link
Copy Markdown
Owner

@copilot fix whatever gemini said

@mantikafasi
Copy link
Copy Markdown
Contributor Author

@copilot retard fix the issues gemini said

@mantikafasi
Copy link
Copy Markdown
Contributor Author

sugoooooi I think this should be merged

@mantikafasi
Copy link
Copy Markdown
Contributor Author

@gemini FIX: review merge

@gemini-code-assist
Copy link
Copy Markdown

I have reviewed the changes for adding voting functionality to ReviewDB. The implementation correctly introduces the VoteButton component, updates the Review entity, and integrates the necessary API calls for fetching and submitting votes. The state management in ReviewComponent correctly handles optimistic updates and error feedback.

One minor improvement for maintainability: In src/plugins/reviewDB/components/ReviewComponent.tsx, the submitVote function could be slightly simplified by extracting the vote delta calculation logic. Otherwise, the code looks solid and follows the existing patterns in the repository. I have no further issues to report.

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.

2 participants