import { ChannelIdentityFragment, DmIdentityFragment, } from 'shared/graphql/__generated__'; import { IChannelItemProps } from './components/channel-item'; // -- TYPES interface IChannelIdentities< T extends ChannelIdentityFragment | DmIdentityFragment = ChannelIdentityFragment > { items: T[]; } type OriginalItem = T & { channel: NonNullable & { members: NonNullable['members']>; }; }; type ChannelItem = { id: string; channelIdentityId: string } & Pick< IChannelItemProps, 'name' | 'membersCount' | 'hasUnreads' >; // -- UTILS /** * Takes user's channel identities and converts them to fit ChannelItemProps * @param channelIdentities - User's channel identities */ export function toChannelItemProps< T extends ChannelIdentityFragment | DmIdentityFragment >( channelIdentities: IChannelIdentities, transform?: (item: ChannelItem, originalItem: OriginalItem) => ChannelItem, ) { const result: ChannelItem[] = []; const { items } = channelIdentities; for (const item of items) { const { channel, hasUnreads, id } = item; if (!channel || !channel.members || !channel.type) { continue; } const entry = { id: channel.id as string, name: channel.name as string, membersCount: channel.members.count, hasUnreads, channelIdentityId: id as string, }; result.push( typeof transform === 'function' ? transform(entry, item as OriginalItem) : entry, ); } return result; }