@cobuildlab/8base-chat
Version:
Chat component that uses 8base
64 lines (51 loc) • 1.59 kB
text/typescript
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 extends ChannelIdentityFragment | DmIdentityFragment> = T & {
channel: NonNullable<T['channel']> & {
members: NonNullable<NonNullable<T['channel']>['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<T>,
transform?: (item: ChannelItem, originalItem: OriginalItem<T>) => 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<T>) : entry,
);
}
return result;
}