Skip to content
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

Fix avatar wall #4790

Merged
merged 2 commits into from
Aug 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/components/Drawer/Header/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

& h2 {
font-size: var(--text20);
font-weight: var(--font-semibold);
font-weight: var(--font-medium);
line-height: 1.5;
}

Expand Down
20 changes: 9 additions & 11 deletions src/views/CampaignDetail/Participants/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ export const ParticipantsDrawer: React.FC<DrawerProps> = ({
<Drawer.Header
title={
<>
<span className={styles.drawerTitle}>
{intl.formatMessage({
defaultMessage: 'Writers',
description:
'src/views/CampaignDetail/SideParticipants/index.tsx',
id: 'xl95XN',
})}{' '}
{totalParticipants > 0 && (
<span className={styles.count}>{totalParticipants}</span>
)}
</span>
{intl.formatMessage({
defaultMessage: 'Writers',
description:
'src/views/CampaignDetail/SideParticipants/index.tsx',
id: 'xl95XN',
})}{' '}
{totalParticipants > 0 && (
<span className={styles.count}>{totalParticipants}</span>
)}
</>
}
closeDrawer={onClose}
Expand Down
4 changes: 0 additions & 4 deletions src/views/CampaignDetail/Participants/styles.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.drawerTitle {
font-weight: var(--font-medium);
}

.count {
font-weight: var(--font-normal);
color: var(--color-grey);
Expand Down
2 changes: 1 addition & 1 deletion src/views/CampaignDetail/SideParticipants/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const fragments = {
fragment SideParticipantsCampaignPublic on WritingChallenge {
id
shortHash
sideParticipants: participants(input: { first: 60 }) {
sideParticipants: participants(input: { first: null }) {
totalCount
edges {
cursor
Expand Down
59 changes: 42 additions & 17 deletions src/views/CampaignDetail/SideParticipants/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _shuffle from 'lodash/shuffle'
import dynamic from 'next/dynamic'
import { useContext, useState } from 'react'
import { useContext, useMemo, useState } from 'react'
import { FormattedMessage } from 'react-intl'

import { ReactComponent as IconRight } from '@/public/static/icons/24px/right.svg'
Expand Down Expand Up @@ -80,12 +80,42 @@ const SideParticipants = ({ campaign }: SideParticipantsProps) => {
setOpenDrawer((prevState) => !prevState)
}

if (edges && edges.length <= 0) {
const getFilteredEdges = () => {
if (!edges) {
return []
}

const withAvatars = edges.filter(
({ node }) => node.avatar && node.id !== viewer.id
)
const withoutAvatars = edges.filter(
({ node }) => !node.avatar && node.id !== viewer.id
)

const shuffledAvatars = _shuffle(withAvatars).slice(
0,
isViewerApplySucceeded ? maxAvatarCount - 2 : maxAvatarCount - 1
)
const remainingSlots = isViewerApplySucceeded
? maxAvatarCount - shuffledAvatars.length - 2
: maxAvatarCount - shuffledAvatars.length - 1

const additionalEdges = _shuffle(withoutAvatars).slice(0, remainingSlots)

return shuffledAvatars.concat(additionalEdges)
}

const filteredEdges = useMemo(getFilteredEdges, [
edges,
isViewerApplySucceeded,
])

if (filteredEdges.length <= 0) {
return null
}

return (
<DrawerProvider>
<>
<section className={styles.participants}>
<section className={styles.header}>
<h2>
Expand Down Expand Up @@ -120,14 +150,7 @@ const SideParticipants = ({ campaign }: SideParticipantsProps) => {
<section className={styles.avatars}>
{isViewerApplySucceeded && <Participant user={viewer} />}

{_shuffle(
edges
?.slice(
0,
isViewerApplySucceeded ? maxAvatarCount - 2 : maxAvatarCount - 1
)
?.filter((u) => u.node.id !== viewer.id)
).map(({ node, cursor }, i) => (
{filteredEdges.map(({ node, cursor }, i) => (
<Participant
key={cursor}
user={node}
Expand Down Expand Up @@ -158,12 +181,14 @@ const SideParticipants = ({ campaign }: SideParticipantsProps) => {
)}
</section>
</section>
<DynamicParticipantsDrawer
isOpen={openDrawer}
onClose={toggleDrawer}
totalParticipants={totalCount}
/>
</DrawerProvider>
<DrawerProvider>
<DynamicParticipantsDrawer
isOpen={openDrawer}
onClose={toggleDrawer}
totalParticipants={totalCount}
/>
</DrawerProvider>
</>
)
}

Expand Down
Loading