Skip to content

Commit

Permalink
Fix reaction burst skin tone variations
Browse files Browse the repository at this point in the history
  • Loading branch information
ayumi-signal committed Apr 24, 2024
1 parent ae4fa04 commit 1834c09
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 45 deletions.
6 changes: 3 additions & 3 deletions ts/components/CallReactionBurst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CallReactionBurstEmoji } from './CallReactionBurstEmoji';
const LIFETIME = 3000;

export type CallReactionBurstType = {
value: string;
values: Array<string>;
};

type CallReactionBurstStateType = CallReactionBurstType & {
Expand Down Expand Up @@ -124,10 +124,10 @@ export function CallReactionBurstProvider({
<CallReactionBurstContext.Provider value={contextValue}>
{createPortal(
<div className="CallReactionBursts">
{bursts.map(({ value, key }) => (
{bursts.map(({ values, key }) => (
<CallReactionBurstEmoji
key={key}
value={value}
values={values}
onAnimationEnd={() => hideBurst(key)}
/>
))}
Expand Down
59 changes: 32 additions & 27 deletions ts/components/CallReactionBurstEmoji.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { v4 as uuid } from 'uuid';
import { Emojify } from './conversation/Emojify';

export type PropsType = {
value: string;
values: Array<string>;
onAnimationEnd?: () => unknown;
};

Expand All @@ -25,31 +25,36 @@ type AnimationConfig = {
velocity: number;
};

export function CallReactionBurstEmoji({ value }: PropsType): JSX.Element {
// values is an array of emojis, which is useful when bursting multi skin tone set of
// emojis to get the correct representation
export function CallReactionBurstEmoji({ values }: PropsType): JSX.Element {
const [toY, setToY] = React.useState<number>(0);
const fromY = -50;

const generateEmojiProps = React.useCallback(() => {
return {
key: uuid(),
value,
springConfig: {
mass: random(10, 20),
tension: random(45, 60),
friction: random(20, 60),
clamp: true,
precision: 0,
velocity: -0.01,
},
fromX: random(0, 20),
toX: random(-30, 300),
fromY,
toY,
toScale: random(1, 2.5, true),
fromRotate: random(-45, 45),
toRotate: random(-45, 45),
};
}, [fromY, toY, value]);
const generateEmojiProps = React.useCallback(
(index: number) => {
return {
key: uuid(),
value: values[index % values.length],
springConfig: {
mass: random(10, 20),
tension: random(45, 60),
friction: random(20, 60),
clamp: true,
precision: 0,
velocity: -0.01,
},
fromX: random(0, 20),
toX: random(-30, 300),
fromY,
toY,
toScale: random(1, 2.5, true),
fromRotate: random(-45, 45),
toRotate: random(-45, 45),
};
},
[fromY, toY, values]
);

// Calculate target Y position before first render. Emojis need to animate Y upwards
// by the value of the container's top, plus the emoji's maximum height.
Expand All @@ -59,12 +64,12 @@ export function CallReactionBurstEmoji({ value }: PropsType): JSX.Element {
const { top } = containerRef.current.getBoundingClientRect();
const calculatedToY = -top;
setToY(calculatedToY);
setEmojis([{ ...generateEmojiProps(), toY: calculatedToY }]);
setEmojis([{ ...generateEmojiProps(0), toY: calculatedToY }]);
}
}, [generateEmojiProps]);

const [emojis, setEmojis] = React.useState<Array<AnimatedEmojiProps>>([
generateEmojiProps(),
generateEmojiProps(0),
]);

React.useEffect(() => {
Expand All @@ -74,14 +79,14 @@ export function CallReactionBurstEmoji({ value }: PropsType): JSX.Element {
if (emojiCount + 1 >= NUM_EMOJIS) {
clearInterval(timer);
}
return [...curEmojis, generateEmojiProps()];
return [...curEmojis, generateEmojiProps(emojiCount)];
});
}, DELAY_BETWEEN_EMOJIS);

return () => {
clearInterval(timer);
};
}, [fromY, toY, value, generateEmojiProps]);
}, [fromY, toY, values, generateEmojiProps]);

return (
<div className="CallReactionBurstEmoji" ref={containerRef}>
Expand Down
49 changes: 37 additions & 12 deletions ts/components/CallScreen.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,9 @@ export function GroupCallReactions(): JSX.Element {
})
);

const activeCall = useReactionsEmitter(
props.activeCall as ActiveGroupCallType
);
const activeCall = useReactionsEmitter({
activeCall: props.activeCall as ActiveGroupCallType,
});

return <CallScreen {...props} activeCall={activeCall} />;
}
Expand All @@ -670,11 +670,30 @@ export function GroupCallReactionsSpam(): JSX.Element {
})
);

const activeCall = useReactionsEmitter(
props.activeCall as ActiveGroupCallType,
250
const activeCall = useReactionsEmitter({
activeCall: props.activeCall as ActiveGroupCallType,
frequency: 250,
});

return <CallScreen {...props} activeCall={activeCall} />;
}

export function GroupCallReactionsSkinTones(): JSX.Element {
const remoteParticipants = allRemoteParticipants.slice(0, 3);
const [props] = React.useState(
createProps({
callMode: CallMode.Group,
remoteParticipants,
viewMode: CallViewMode.Overflow,
})
);

const activeCall = useReactionsEmitter({
activeCall: props.activeCall as ActiveGroupCallType,
frequency: 500,
emojis: ['👍', '👍🏻', '👍🏼', '👍🏽', '👍🏾', '👍🏿', '❤️', '😂', '😮', '😢'],
});

return <CallScreen {...props} activeCall={activeCall} />;
}

Expand All @@ -701,11 +720,17 @@ export function GroupCallReactionsManyInOrder(): JSX.Element {
return <CallScreen {...props} />;
}

function useReactionsEmitter(
activeCall: ActiveGroupCallType,
function useReactionsEmitter({
activeCall,
frequency = 2000,
removeAfter = 5000
) {
removeAfter = 5000,
emojis = DEFAULT_PREFERRED_REACTION_EMOJI,
}: {
activeCall: ActiveGroupCallType;
frequency?: number;
removeAfter?: number;
emojis?: Array<string>;
}) {
const [call, setCall] = React.useState(activeCall);
React.useEffect(() => {
const interval = setInterval(() => {
Expand All @@ -725,7 +750,7 @@ function useReactionsEmitter(
{
timestamp: timeNow,
demuxId,
value: sample(DEFAULT_PREFERRED_REACTION_EMOJI) as string,
value: sample(emojis) as string,
},
];

Expand All @@ -736,7 +761,7 @@ function useReactionsEmitter(
});
}, frequency);
return () => clearInterval(interval);
}, [frequency, removeAfter, call]);
}, [emojis, frequency, removeAfter, call]);
return call;
}

Expand Down
20 changes: 17 additions & 3 deletions ts/components/CallScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import {
} from './CallReactionBurst';
import { isGroupOrAdhocActiveCall } from '../util/isGroupOrAdhocCall';
import { assertDev } from '../util/assert';
import { emojiToData } from './emoji/lib';

export type PropsType = {
activeCall: ActiveCallType;
Expand Down Expand Up @@ -1048,7 +1049,13 @@ function useReactionsToast(props: UseReactionsToastType): void {
const reactionsShown = useRef<
Map<
string,
{ value: string; isBursted: boolean; expireAt: number; demuxId: number }
{
value: string;
originalValue: string;
isBursted: boolean;
expireAt: number;
demuxId: number;
}
>
>(new Map());
const burstsShown = useRef<Map<string, number>>(new Map());
Expand Down Expand Up @@ -1094,8 +1101,13 @@ function useReactionsToast(props: UseReactionsToastType): void {
recentBurstTime &&
recentBurstTime + REACTIONS_BURST_TRAILING_WINDOW > time
);
// Normalize skin tone emoji to calculate burst threshold, but save original
// value to show in the burst animation
const emojiData = emojiToData(value);
const normalizedValue = emojiData?.unified ?? value;
reactionsShown.current.set(key, {
value,
value: normalizedValue,
originalValue: value,
isBursted,
expireAt: timestamp + REACTIONS_BURST_WINDOW,
demuxId,
Expand Down Expand Up @@ -1158,15 +1170,17 @@ function useReactionsToast(props: UseReactionsToastType): void {
}

burstsShown.current.set(value, time);
const values: Array<string> = [];
reactionKeys.forEach(key => {
const reactionShown = reactionsShown.current.get(key);
if (!reactionShown) {
return;
}

reactionShown.isBursted = true;
values.push(reactionShown.originalValue);
});
showBurst({ value });
showBurst({ values });

if (burstsShown.current.size >= REACTIONS_BURST_MAX_IN_SHORT_WINDOW) {
break;
Expand Down

0 comments on commit 1834c09

Please sign in to comment.