-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
add reddit features to reviewdb #4216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,16 +18,16 @@ | |
|
|
||
| import { Auth, getToken } from "@plugins/reviewDB/auth"; | ||
| import { Review, ReviewType } from "@plugins/reviewDB/entities"; | ||
| import { blockUser, deleteReview, reportReview, unblockUser } from "@plugins/reviewDB/reviewDbApi"; | ||
| import { blockUser, deleteReview, reportReview, unblockUser, voteReview } from "@plugins/reviewDB/reviewDbApi"; | ||
| import { settings } from "@plugins/reviewDB/settings"; | ||
| import { canBlockReviewAuthor, canDeleteReview, canReportReview, cl, showToast } from "@plugins/reviewDB/utils"; | ||
| import { openUserProfile } from "@utils/discord"; | ||
| import { classes } from "@utils/misc"; | ||
| import { findCssClassesLazy } from "@webpack"; | ||
| import { Alerts, IconUtils, Parser, Timestamp, useState } from "@webpack/common"; | ||
| import { Alerts, IconUtils, Parser, Timestamp, useEffect, useState } from "@webpack/common"; | ||
|
|
||
| import { openBlockModal } from "./BlockedUserModal"; | ||
| import { BlockButton, DeleteButton, ReportButton } from "./MessageButton"; | ||
| import { BlockButton, DeleteButton, ReportButton, VoteButton } from "./MessageButton"; | ||
| import ReviewBadge from "./ReviewBadge"; | ||
|
|
||
| const MessageClasses = findCssClassesLazy("cozyMessage", "message", "groupStart", "buttons", "buttonsInner"); | ||
|
|
@@ -40,6 +40,14 @@ const dateFormat = new Intl.DateTimeFormat(); | |
|
|
||
| export default function ReviewComponent({ review, refetch, profileId }: { review: Review; refetch(): void; profileId: string; }) { | ||
| const [showAll, setShowAll] = useState(false); | ||
| 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.score, review.userVote]); | ||
|
|
||
| function openModal() { | ||
| openUserProfile(review.sender.discordID); | ||
|
|
@@ -104,6 +112,33 @@ export default function ReviewComponent({ review, refetch, profileId }: { review | |
| }); | ||
| } | ||
|
|
||
| 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(currentScore => currentScore + delta); | ||
| } | ||
| } finally { | ||
| setIsVoting(false); | ||
| } | ||
| } | ||
|
Comment on lines
+115
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement the |
||
|
|
||
| return ( | ||
| <div className={classes(cl("review"), MessageClasses.cozyMessage, AvatarClasses.wrapper, MessageClasses.message, MessageClasses.groupStart, AvatarClasses.cozy)} style={ | ||
| { | ||
|
|
@@ -156,6 +191,11 @@ export default function ReviewComponent({ review, refetch, profileId }: { review | |
| {dateFormat.format(review.timestamp * 1000)} | ||
| </Timestamp>) | ||
| } | ||
| {review.id !== 0 && ( | ||
| <span className={cl("vote-score")}> | ||
| {score} | ||
| </span> | ||
| )} | ||
|
|
||
| <div className={cl("review-comment")}> | ||
| {(review.comment.length > 200 && !showAll) | ||
|
|
@@ -177,6 +217,8 @@ export default function ReviewComponent({ review, refetch, profileId }: { review | |
| {canReportReview(review) && <ReportButton onClick={reportRev} />} | ||
| {canBlockReviewAuthor(profileId, review) && <BlockButton isBlocked={isAuthorBlocked} onClick={blockReviewSender} />} | ||
| {canDeleteReview(profileId, review) && <DeleteButton onClick={delReview} />} | ||
| <VoteButton isUpvote isSelected={localVote === true} disabled={isVoting} onClick={() => submitVote(true)} /> | ||
| <VoteButton isUpvote={false} isSelected={localVote === false} disabled={isVoting} onClick={() => submitVote(false)} /> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce a loading state to prevent concurrent voting requests and use
useEffectto keep the local state in sync with thereviewprop. SinceuseStateonly initializes once, the localscoreandlocalVotewould otherwise become stale if the parent component refetches data.