Skip to content

Commit

Permalink
Fix: useUserAvatars hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rdig committed Nov 26, 2023
1 parent e9e4baa commit c1dcfb6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 61 deletions.

This file was deleted.

1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export { default as useEnoughTokensForStaking } from './useEnoughTokensForStakin
export { default as useCurrentBlockTime } from './useCurrentBlockTime';
export { default as useClipboardCopy } from './useClipboardCopy';
export { default as useInvitationLink } from './useInvitationLink';
export { default as useUsersByAddresses } from './useUsersByAddresses';

export {
default as useSafeTransactionStatus,
Expand Down
24 changes: 18 additions & 6 deletions src/hooks/useUserAvatars/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { useMemo } from 'react';

import { UserFragment } from '~gql';
import { useAppContext, useUsersByAddresses } from '~hooks';
import { notNull } from '~utils/arrays';
import { calculateRemainingItems } from '~utils/avatars';
import { useGetUsers } from '~common/ColonyActions/ActionDetailsPage/DefaultMotion/MotionPhaseWidget/VoteOutcome/VoteResults/helpers';

import { UserAvatarsItem } from '~v5/shared/UserAvatars/types';
import useAppContext from '../useAppContext';
import { UseUserAvatarsReturnType } from './types';

export const useUserAvatars = (
maxAvatars: number,
items: UserAvatarsItem[],
): UseUserAvatarsReturnType => {
): {
remainingAvatarsCount: number;
registeredUsers: UserFragment[];
} => {
const remainingAvatarsCount = calculateRemainingItems(maxAvatars, items);
const { user } = useAppContext();

const voterAddresses = useMemo(
() =>
items.reduce<string[]>((acc, { address }) => {
Expand All @@ -24,10 +30,16 @@ export const useUserAvatars = (
[items, user],
);

const registeredUsers = useGetUsers(voterAddresses);
const { users: registeredUsers = [] } = useUsersByAddresses(
/*
* @NOTE Due to how the or filter works
* If the array would be empty it would return all users
*/
voterAddresses.length ? voterAddresses : [''],
);

return {
remainingAvatarsCount,
registeredUsers,
registeredUsers: registeredUsers.filter(notNull),
};
};
6 changes: 0 additions & 6 deletions src/hooks/useUserAvatars/types.ts

This file was deleted.

0 comments on commit c1dcfb6

Please sign in to comment.