Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
fix 1 case
Browse files Browse the repository at this point in the history
  • Loading branch information
sballesteros committed Nov 8, 2019
1 parent 9bb85e6 commit 9158ecd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/components/animated-score.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ export function Demo() {
</div>
);
}

export function OneCase() {
const {
nRequests,
nReviews,
now,
onStartAnim,
onStopAnim,
dateFirstActivity
} = useAnimatedScore(actions.slice(0, 1));

return (
<div
onMouseEnter={onStartAnim}
onMouseLeave={onStopAnim}
style={{ display: 'flex', alignItems: 'center' }}
>
<ScoreBadge
now={now}
nRequests={nRequests}
nReviews={nReviews}
dateFirstActivity={dateFirstActivity}
/>
<span style={{ marginLeft: '4px' }}>
reviews {nReviews} + requests {nRequests}
</span>
</div>
);
}
26 changes: 18 additions & 8 deletions src/hooks/score-hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useMemo, useState, useEffect, useCallback } from 'react';

/**
* Note actions should always have a length of at least 1 as only preprint
* with reviews or requests for reviews are listed
*/
export function useAnimatedScore(actions) {
const sorted = useMemo(() => {
return actions.slice().sort((a, b) => {
Expand All @@ -17,13 +21,19 @@ export function useAnimatedScore(actions) {
const tmax = new Date(sorted[sorted.length - 1].startTime).getTime();

const t = new Date(sorted[Math.max(index, 0)].startTime).getTime();
const nextT = new Date(
sorted[Math.max(index + 1, 0)].startTime
).getTime();

const rT = ((t - tmin) / (tmax - tmin)) * totalAnimTime;
const rNextT = ((nextT - tmin) / (tmax - tmin)) * totalAnimTime;
const timeout = rNextT - rT;
let timeout;
if (sorted.length > 1) {
const nextT = new Date(
sorted[Math.max(index + 1, 0)].startTime
).getTime();

const rT = ((t - tmin) / (tmax - tmin)) * totalAnimTime;
const rNextT = ((nextT - tmin) / (tmax - tmin)) * totalAnimTime;
timeout = rNextT - rT;
} else {
timeout = totalAnimTime;
}

const timeoutId = setTimeout(() => {
setIndex(index + 1);
Expand Down Expand Up @@ -59,14 +69,14 @@ export function useAnimatedScore(actions) {
index === null
? undefined
: index === -1
? sorted[0].startTime
? sorted[0] && sorted[0].startTime
: sorted[index].startTime;

return {
nRequests,
nReviews,
now,
dateFirstActivity: sorted[0].startTime,
dateFirstActivity: sorted[0] && sorted[0].startTime,
onStartAnim: handleStartAnim,
onStopAnim: handleStopAnim
};
Expand Down

0 comments on commit 9158ecd

Please sign in to comment.