Skip to content

Use PaginationTrigger to avoid loading all org members #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/client/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { styled } from 'styled-components';
import { thread, user } from '@cord-sdk/react';
import type { Channel } from 'src/client/consts/Channel';
Expand All @@ -18,16 +18,10 @@ interface ChatProps {
}

export function Chat({ channel, onOpenThread }: ChatProps) {
const { orgMembers, loading, hasMore, fetchMore } = user.useOrgMembers({
const orgMembers = user.useOrgMembers({
organizationID: channel.org ?? EVERYONE_ORG_ID,
});

useEffect(() => {
if (!loading && hasMore) {
void fetchMore(50);
}
}, [orgMembers, hasMore, loading, fetchMore]);

const { threads: pinnedThreads } = thread.useLocationData(
{ channel: channel.id },
{
Expand All @@ -50,8 +44,8 @@ export function Chat({ channel, onOpenThread }: ChatProps) {
<ChannelDetailsBar>
<PageHeaderWrapper>
<PageHeader># {channel.id}</PageHeader>
{orgMembers && (
<PageUsersLabel users={orgMembers} channel={channel} />
{orgMembers.orgMembers && (
<PageUsersLabel orgMembers={orgMembers} channel={channel} />
)}
</PageHeaderWrapper>
</ChannelDetailsBar>
Expand Down
30 changes: 18 additions & 12 deletions src/client/components/PageUsersLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import React, { useMemo } from 'react';
import React from 'react';
import { UserIcon } from '@heroicons/react/24/outline';
import { Tooltip } from 'react-tooltip';
import styled from 'styled-components';
import { Facepile } from '@cord-sdk/react';
import type { ClientUserData } from '@cord-sdk/types';
import type { OrgMembersData } from '@cord-sdk/types';
import type { Channel } from 'src/client/consts/Channel';
import { Colors } from 'src/client/consts/Colors';
import { combine } from 'src/client/utils';
import { combine, isDefined } from 'src/client/utils';
import { UsersInChannelModal } from 'src/client/components/UsersInChannelModal';

export function PageUsersLabel({
users,
orgMembers,
channel,
}: {
users: ClientUserData[];
orgMembers: OrgMembersData;
channel: Channel;
}) {
const [showModal, setShowModal] = React.useState(false);

const userIDs = useMemo(() => users.map((u) => u.id), [users]);
const previewUsers = userIDs.slice(1, 4);

return (
<>
<Tooltip
Expand All @@ -34,7 +31,10 @@ export function PageUsersLabel({
<span>View all members of this channel.</span>
<span>{`Includes ${combine(
'and',
users.map((user) => user?.name ?? ''),
[
...orgMembers.orgMembers.map((user) => user?.name),
orgMembers.hasMore ? 'others' : null,
].filter(isDefined),
)}`}</span>
</TooltipText>
</Tooltip>
Expand All @@ -47,15 +47,21 @@ export function PageUsersLabel({
{/* notes: type of users coming from cord
api not matching what is expected by cord component
*/}
<StyledFacepile users={previewUsers} enableTooltip={false} />
<StyledFacepile
users={orgMembers.orgMembers.slice(1, 4).map((u) => u.id)}
enableTooltip={false}
/>
<StyledUserIcon width={18} height={18} />
<UserCount>{users.length}</UserCount>
<UserCount>
{orgMembers.orgMembers.length}
{orgMembers.hasMore ? '+' : ''}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yeo-yeo this is IMO an omission in the new API -- we should send a total count so you know how many before you paginate through. @maylynn-ng is working on the first API we have now which combines paginated and non-paginated data, so we should follow the pattern there.

</UserCount>
</UsersLabel>
{showModal && (
<UsersInChannelModal
onClose={() => setShowModal(false)}
channel={channel}
users={users}
orgMembers={orgMembers}
/>
)}
</>
Expand Down
12 changes: 8 additions & 4 deletions src/client/components/UsersInChannelModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import {
Avatar as DefaultAvatar,
presence,
} from '@cord-sdk/react';
import type { ClientUserData } from '@cord-sdk/types';
import type { OrgMembersData } from '@cord-sdk/types';
import type { Channel } from 'src/client/consts/Channel';
import { Colors } from 'src/client/consts/Colors';
import { ActiveBadge } from 'src/client/components/ActiveBadge';
import { Name } from 'src/client/components/Name';
import { XIcon } from 'src/client/components/Buttons';
import { PaginationTrigger } from 'src/client/components/PaginationTrigger';

interface UsersInChannelModalProps {
onClose: () => void;
channel: Channel;
users: ClientUserData[];
orgMembers: OrgMembersData;
}

export function UsersInChannelModal({
onClose,
users,
orgMembers,
channel,
}: UsersInChannelModalProps) {
const usersPresent = presence.useLocationData(
Expand All @@ -40,7 +41,7 @@ export function UsersInChannelModal({
</Header>

<UsersList>
{users.map((user) => {
{orgMembers.orgMembers.map((user) => {
const isUserPresent = usersPresent?.some(
(presence) => presence.id === user.id,
);
Expand All @@ -57,6 +58,9 @@ export function UsersInChannelModal({
</UserDetails>
);
})}
<UserDetails>
<PaginationTrigger {...orgMembers} />
</UserDetails>
</UsersList>
</Box>
</Modal>
Expand Down
4 changes: 4 additions & 0 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function combine(combiner: string, items: string[]): string {
}`;
}
}

export function isDefined<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined;
}