-
Notifications
You must be signed in to change notification settings - Fork 21
feat(PM-2177): likes and dislikes on run items and comments #1322
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3fae0af
feat: likes and dislikes on run items and comments
hentrymartin d383431
feat: likes and dislikes on run items and comments
hentrymartin 5dd1dc4
feat: likes and dislikes on run items and comments
hentrymartin 564381f
fix: moved setInitialVotesForFeedback into useEffect
hentrymartin 7909129
fix: moved setInitialVotesForFeedback into useEffect
hentrymartin f9c98ad
fix: review comments
hentrymartin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/apps/review/src/lib/assets/icons/icon-thumb-up-filled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
src/apps/review/src/lib/assets/icons/icon-thumbs-down-filled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...orecard/ScorecardViewer/ScorecardQuestion/AiFeedbackActions/AiFeedbackActions.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| @import '@libs/ui/styles/includes'; | ||
|
|
||
| .actions { | ||
| display: flex; | ||
| gap: $sp-2; | ||
| margin-top: $sp-3; | ||
| .count { | ||
| font-family: $font-roboto; | ||
| color: #0D61BF; | ||
| font-weight: 700; | ||
| font-size: 14px; | ||
| } | ||
| } | ||
|
|
||
| .actionBtn { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| gap: $sp-2; | ||
| background: transparent; | ||
| border: none; | ||
| cursor: pointer; | ||
| color: black; | ||
| padding: $sp-1 $sp-2; | ||
| &.active { | ||
| svg { | ||
| path { | ||
| fill: $link-blue-dark; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| svg { | ||
| width: 20px; | ||
| height: 20px; | ||
| } | ||
|
|
||
| &:hover { | ||
| opacity: 0.85; | ||
| } | ||
| } | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
227 changes: 227 additions & 0 deletions
227
...nents/Scorecard/ScorecardViewer/ScorecardQuestion/AiFeedbackActions/AiFeedbackActions.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| /* eslint-disable react/jsx-no-bind */ | ||
| import { FC, useCallback, useContext, useEffect, useState } from 'react' | ||
| import { mutate } from 'swr' | ||
|
|
||
| import { | ||
| IconThumbsDown, | ||
| IconThumbsDownFilled, | ||
| IconThumbsUp, | ||
| IconThumbsUpFilled, | ||
| } from '~/apps/review/src/lib/assets/icons' | ||
| import { ReviewAppContext } from '~/apps/review/src/lib/contexts' | ||
| import { useReviewsContext } from '~/apps/review/src/pages/reviews/ReviewsContext' | ||
| import { updateLikesOrDislikesOnRunItem, updateLikesOrDislikesOnRunItemComment } from '~/apps/review/src/lib/services' | ||
| import { EnvironmentConfig } from '~/config' | ||
| import { ReviewAppContextModel, ReviewsContextModel } from '~/apps/review/src/lib/models' | ||
|
|
||
| import { AiFeedbackComment } from '../AiFeedbackComments/AiFeedbackComments' | ||
|
|
||
| import styles from './AiFeedbackActions.module.scss' | ||
|
|
||
| export enum VOTE_TYPE { | ||
| UPVOTE = 'UPVOTE', | ||
| DOWNVOTE = 'DOWNVOTE' | ||
| } | ||
|
|
||
| interface AiFeedbackActionsProps { | ||
| actionType: 'comment' | 'runItem' | ||
| comment?: AiFeedbackComment | ||
| feedback?: any | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export const AiFeedbackActions: FC<AiFeedbackActionsProps> = props => { | ||
|
|
||
| const [userVote, setUserVote] = useState<string | undefined>(undefined) | ||
| const [upVotes, setUpVotes] = useState<number>(0) | ||
| const [downVotes, setDownVotes] = useState<number>(0) | ||
|
|
||
| const { loginUserInfo }: ReviewAppContextModel = useContext(ReviewAppContext) | ||
| const { workflowId, workflowRun }: ReviewsContextModel = useReviewsContext() | ||
|
|
||
| const votesArr: any[] = (props.actionType === 'runItem' ? (props.feedback?.votes) : (props.comment?.votes)) || [] | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const setInitialVotesForFeedback = useCallback((): void => { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const initialUp = props.feedback?.upVotes ?? votesArr.filter(v => String(v.voteType) | ||
| .toLowerCase() | ||
| .includes('up')).length | ||
| const initialDown = props.feedback?.downVotes ?? votesArr.filter(v => String(v.voteType) | ||
| .toLowerCase() | ||
| .includes('down')).length | ||
|
|
||
| const myVote = votesArr.find(v => String(v.createdBy) === String(loginUserInfo?.userId)) | ||
| setUpVotes(initialUp) | ||
| setDownVotes(initialDown) | ||
| setUserVote(myVote?.voteType ?? undefined) | ||
| }, [votesArr, props.feedback]) | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const setInitialVotesForComment = useCallback((): void => { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const initialUp = votesArr.filter(v => String(v.voteType) | ||
| .toLowerCase() | ||
| .includes('up')).length | ||
| const initialDown = votesArr.filter(v => String(v.voteType) | ||
| .toLowerCase() | ||
| .includes('down')).length | ||
|
|
||
| const myVote = votesArr.find(v => String(v.createdBy) === String(loginUserInfo?.userId)) | ||
| setUpVotes(initialUp) | ||
| setDownVotes(initialDown) | ||
| setUserVote(myVote?.voteType ?? undefined) | ||
| }, [votesArr]) | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| useEffect(() => { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (props.actionType === 'runItem') { | ||
| setInitialVotesForFeedback() | ||
| } else { | ||
| setInitialVotesForComment() | ||
| } | ||
| }, [props.actionType, props.feedback?.id, votesArr.length, loginUserInfo?.userId]) | ||
|
|
||
| const voteOnItem = useCallback(async (type: VOTE_TYPE) => { | ||
| if (!workflowId || !workflowRun?.id) return | ||
| const current = userVote | ||
| let up = false | ||
| let down = false | ||
|
|
||
| if (current === type) { | ||
| // remove vote | ||
| up = false | ||
| down = false | ||
| } else if (!current) { | ||
| up = type === VOTE_TYPE.UPVOTE | ||
| down = type === VOTE_TYPE.DOWNVOTE | ||
| } else { | ||
| // switch vote | ||
| up = type === VOTE_TYPE.UPVOTE | ||
| down = type === VOTE_TYPE.DOWNVOTE | ||
| } | ||
|
|
||
| // optimistic update | ||
| const prevUserVote = userVote | ||
| const prevUp = upVotes | ||
| const prevDown = downVotes | ||
|
|
||
| if (current === type) { | ||
| // removing | ||
| if (type === VOTE_TYPE.UPVOTE) setUpVotes(Math.max(0, upVotes - 1)) | ||
| else setDownVotes(Math.max(0, downVotes - 1)) | ||
| setUserVote(undefined) | ||
| } else if (!current) { | ||
| if (type === VOTE_TYPE.UPVOTE) setUpVotes(upVotes + 1) | ||
| else setDownVotes(downVotes + 1) | ||
| setUserVote(type) | ||
| } else { | ||
| // switch | ||
| if (type === VOTE_TYPE.UPVOTE) { | ||
| setUpVotes(upVotes + 1) | ||
| setDownVotes(Math.max(0, downVotes - 1)) | ||
| } else { | ||
| setDownVotes(downVotes + 1) | ||
| setUpVotes(Math.max(0, upVotes - 1)) | ||
| } | ||
|
|
||
| setUserVote(type) | ||
| } | ||
|
|
||
| try { | ||
| await updateLikesOrDislikesOnRunItem(workflowId, workflowRun.id, props.feedback.id, { | ||
| downVote: down, | ||
| upVote: up, | ||
| }) | ||
| } catch (err) { | ||
| // rollback | ||
| setUserVote(prevUserVote) | ||
| setUpVotes(prevUp) | ||
| setDownVotes(prevDown) | ||
| } | ||
| }, [workflowId, workflowRun, props.feedback?.id, userVote, upVotes, downVotes]) | ||
|
|
||
| const voteOnComment = useCallback(async (c: any, type: VOTE_TYPE) => { | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!workflowId || !workflowRun?.id) return | ||
| const votes = (c.votes || []) | ||
hentrymartin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const my = votes.find((v: any) => String(v.createdBy) === String(loginUserInfo?.userId)) | ||
| const current = my?.voteType ?? undefined | ||
|
|
||
| let up = false | ||
| let down = false | ||
|
|
||
| if (current === type) { | ||
| up = false | ||
| down = false | ||
| } else if (!current) { | ||
| up = type === VOTE_TYPE.UPVOTE | ||
| down = type === VOTE_TYPE.DOWNVOTE | ||
| } else { | ||
| up = type === VOTE_TYPE.UPVOTE | ||
| down = type === VOTE_TYPE.DOWNVOTE | ||
| } | ||
|
|
||
| const prevUserVote = userVote | ||
| const prevUp = upVotes | ||
| const prevDown = downVotes | ||
|
|
||
| if (current === type) { | ||
| // removing | ||
| if (type === VOTE_TYPE.UPVOTE) setUpVotes(Math.max(0, upVotes - 1)) | ||
| else setDownVotes(Math.max(0, downVotes - 1)) | ||
| setUserVote(undefined) | ||
| } else if (!current) { | ||
| if (type === VOTE_TYPE.UPVOTE) setUpVotes(upVotes + 1) | ||
| else setDownVotes(downVotes + 1) | ||
| setUserVote(type) | ||
| } else { | ||
| // switch | ||
| if (type === VOTE_TYPE.UPVOTE) { | ||
| setUpVotes(upVotes + 1) | ||
| setDownVotes(Math.max(0, downVotes - 1)) | ||
| } else { | ||
| setDownVotes(downVotes + 1) | ||
| setUpVotes(Math.max(0, upVotes - 1)) | ||
| } | ||
|
|
||
| setUserVote(type) | ||
|
|
||
| } | ||
|
|
||
| try { | ||
| await updateLikesOrDislikesOnRunItemComment(workflowId, workflowRun.id, props.feedback.id, c.id, { | ||
| downVote: down, | ||
| upVote: up, | ||
| }) | ||
| await mutate(`${EnvironmentConfig.API.V6}/workflows/${workflowId}/runs/${workflowRun.id}/items`) | ||
| } catch (err) { | ||
| setUserVote(prevUserVote) | ||
| setUpVotes(prevUp) | ||
| setDownVotes(prevDown) | ||
| } | ||
| }, [workflowId, workflowRun, props.feedback?.id, loginUserInfo]) | ||
|
|
||
| const onVote = (action: VOTE_TYPE): void => { | ||
| if (props.actionType === 'comment') { | ||
| voteOnComment(props.comment as AiFeedbackComment, action) | ||
| } else { | ||
| voteOnItem(action) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className={styles.actions}> | ||
| <button | ||
| type='button' | ||
| className={styles.actionBtn} | ||
| onClick={() => onVote(VOTE_TYPE.UPVOTE)} | ||
| > | ||
| {userVote === 'UPVOTE' ? <IconThumbsUpFilled /> : <IconThumbsUp />} | ||
| <span className={styles.count}>{upVotes}</span> | ||
| </button> | ||
|
|
||
| <button | ||
| type='button' | ||
| className={styles.actionBtn} | ||
| onClick={() => onVote(VOTE_TYPE.DOWNVOTE)} | ||
| > | ||
| {userVote === 'DOWNVOTE' ? <IconThumbsDownFilled /> : <IconThumbsDown />} | ||
| <span className={styles.count}>{downVotes}</span> | ||
| </button> | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.