@@ -136,15 +136,74 @@ export const AiFeedbackActions: FC<AiFeedbackActionsProps> = props => {
136136 }
137137
138138 try {
139+ const itemsKey = `${ EnvironmentConfig . API . V6 } /workflows/${ workflowId } /runs/${ workflowRun . id } /items`
140+ mutate ( itemsKey , ( items : any ) => {
141+ if ( ! items || ! Array . isArray ( items ) ) return items
142+
143+ return items . map ( ( it : any ) => {
144+ if ( String ( it . id ) !== String ( props . feedback ?. id ) ) return it
145+
146+ const newItem = { ...it }
147+
148+ const prevUpCount = Number ( newItem . upVotes
149+ ?? ( newItem . votes ? newItem . votes . filter ( ( v : any ) => String ( v . voteType )
150+ . toLowerCase ( )
151+ . includes ( 'up' ) ) . length : 0 ) )
152+ const prevDownCount = Number ( newItem . downVotes
153+ ?? ( newItem . votes ? newItem . votes . filter ( ( v : any ) => String ( v . voteType )
154+ . toLowerCase ( )
155+ . includes ( 'down' ) ) . length : 0 ) )
156+
157+ let nextUp = prevUpCount
158+ let nextDown = prevDownCount
159+
160+ if ( current === type ) {
161+ // user removed their existing vote
162+ if ( type === VOTE_TYPE . UPVOTE ) nextUp = Math . max ( 0 , prevUpCount - 1 )
163+ else nextDown = Math . max ( 0 , prevDownCount - 1 )
164+ } else if ( ! current ) {
165+ // user added a new vote
166+ if ( type === VOTE_TYPE . UPVOTE ) nextUp = prevUpCount + 1
167+ else nextDown = prevDownCount + 1
168+ } else if ( type === VOTE_TYPE . UPVOTE ) {
169+ nextUp = prevUpCount + 1
170+ nextDown = Math . max ( 0 , prevDownCount - 1 )
171+ } else {
172+ nextDown = prevDownCount + 1
173+ nextUp = Math . max ( 0 , prevUpCount - 1 )
174+ }
175+
176+ newItem . upVotes = nextUp
177+ newItem . downVotes = nextDown
178+
179+ const votesArrLocal = Array . isArray ( newItem . votes ) ? [ ...newItem . votes ] : [ ]
180+ const filtered = votesArrLocal
181+ . filter ( ( v : any ) => String ( v . createdBy ) !== String ( loginUserInfo ?. userId ) )
182+
183+ if ( up ) {
184+ filtered . push ( { createdBy : loginUserInfo ?. userId , voteType : VOTE_TYPE . UPVOTE } )
185+ } else if ( down ) {
186+ filtered . push ( { createdBy : loginUserInfo ?. userId , voteType : VOTE_TYPE . DOWNVOTE } )
187+ }
188+
189+ newItem . votes = filtered
190+
191+ return newItem
192+ } )
193+ } , false )
194+
139195 await updateLikesOrDislikesOnRunItem ( workflowId , workflowRun . id , props . feedback . id , {
140196 downVote : down ,
141197 upVote : up ,
142198 } )
199+ await mutate ( itemsKey )
143200 } catch ( err ) {
144- // rollback
145201 setUserVote ( prevUserVote )
146202 setUpVotes ( prevUp )
147203 setDownVotes ( prevDown )
204+
205+ const itemsKey = `${ EnvironmentConfig . API . V6 } /workflows/${ workflowId } /runs/${ workflowRun . id } /items`
206+ await mutate ( itemsKey )
148207 } finally {
149208 setVotingInprogress ( false )
150209 }
0 commit comments