MCPcopy Create free account
hub / github.com/dailydotdev/apps / toggle

Function toggle

packages/shared/src/features/messages/hooks/useReactToMessage.ts:39–61  ·  view source on GitHub ↗
(add: boolean)

Source from the content-addressed store, hash-verified

37 // Adds or removes just this emoji on the cached copy, so undoing it
38 // keeps any other reaction made while this one was in flight.
39 const toggle = (add: boolean): string[] => {
40 const threadKey = dmThreadQueryKey(user, peer.id);
41 const cached = queryClient
42 .getQueryData<DmMessage[]>(threadKey)
43 ?.find(({ id }) => id === message.id);
44 const current = getOwnReactions(cached ?? message, user.id);
45 const emojis = add
46 ? Array.from(new Set([...current, emoji]))
47 : current.filter((value) => value !== emoji);
48 queryClient.setQueryData<DmMessage[]>(
49 threadKey,
50 (messages) =>
51 messages &&
52 applyReaction(messages, {
53 peerId: peer.id,
54 senderId: user.id,
55 messageId: message.id,
56 emojis,
57 }),
58 );
59
60 return emojis;
61 };
62
63 getDmTransport(user.id)
64 .react(peer, message.id, toggle(!isRemoving))

Callers 1

useReactToMessageFunction · 0.70

Calls 3

dmThreadQueryKeyFunction · 0.90
getOwnReactionsFunction · 0.90
applyReactionFunction · 0.90

Tested by

no test coverage detected