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

add SuggestedUser card row in mobile feed #2417

Merged
merged 12 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
10 changes: 9 additions & 1 deletion apps/mobile/src/components/Feed/FeedVirtualizedRow.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { Suspense, useMemo } from 'react';

import { FeedListItemType } from '~/components/Feed/createVirtualizedFeedEventItems';
import { FeedListCaption } from '~/components/Feed/FeedListCaption';
Expand All @@ -7,8 +7,10 @@ import { FeedListSectionHeader } from '~/components/Feed/FeedListSectionHeader';
import { FeedEventSocializeSection } from '~/components/Feed/Socialize/FeedEventSocializeSection';
import { ReportingErrorBoundary } from '~/shared/errors/ReportingErrorBoundary';

import { SearchDefaultCardRowFallback } from '../Search/SearchDefaultFallback';
import { FeedFilter } from './FeedFilter';
import { FeedPostSocializeSection } from './Posts/FeedPostSocializeSection';
import { FeedSuggestedProfileRow } from './Posts/FeedSuggestedProfileRow';
import { PostListCaption } from './Posts/PostListCaption';
import { PostListItem } from './Posts/PostListItem';
import { PostListMintButtonSection } from './Posts/PostListMintButtonSection';
Expand Down Expand Up @@ -64,6 +66,12 @@ export function FeedVirtualizedRow({ onFailure, item }: Props) {
);
case 'post-item-mint-link':
return <PostListMintButtonSection postRef={item.post} />;
case 'suggested-profile-row':
return (
<Suspense fallback={<SearchDefaultCardRowFallback />}>
<FeedSuggestedProfileRow queryRef={item.queryRef} />
</Suspense>
);
}
}, [item]);

Expand Down
87 changes: 87 additions & 0 deletions apps/mobile/src/components/Feed/Posts/FeedSuggestedProfileRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useMemo } from 'react';
import { View } from 'react-native';
import { graphql, useFragment } from 'react-relay';
import { removeNullValues } from 'shared/relay/removeNullValues';

import { TrendingUserCard } from '~/components/Trending/TrendingUserCard';
import { FeedSuggestedProfileRowFragment$key } from '~/generated/FeedSuggestedProfileRowFragment.graphql';

import { Typography } from '../../Typography';

const CARD_HEIGHT = 180;
const CARD_WIDTH = 180;

type FeedSuggestedProfileRowProps = {
queryRef: FeedSuggestedProfileRowFragment$key;
};

export function FeedSuggestedProfileRow({ queryRef }: FeedSuggestedProfileRowProps) {
const query = useFragment(
graphql`
fragment FeedSuggestedProfileRowFragment on Query {
viewer @required(action: THROW) {
... on Viewer {
suggestedUsers(first: 4) @required(action: THROW) {
__typename
edges {
node {
__typename
... on GalleryUser {
galleries {
tokenPreviews {
__typename
}
}
...TrendingUserCardFragment
}
}
}
}
}
}
...TrendingUserCardQueryFragment
}
`,
queryRef
);

const suggestedUsers = useMemo(() => {
const users = [];
if (query.viewer?.suggestedUsers?.__typename === 'UsersConnection') {
for (const edge of query.viewer?.suggestedUsers?.edges ?? []) {
if (edge?.node) {
users.push(edge.node);
}
}
}
if (users) {
const usersWithTokenPreviews = users.filter((user) => {
return user?.galleries?.find(
(gallery) => removeNullValues(gallery?.tokenPreviews).length > 0
);
});
return usersWithTokenPreviews?.slice(0, 2);
}
return users;
}, [query.viewer?.suggestedUsers]);

return (
<View className="pl-4 mt-2 mb-12 space-y-3">
<Typography
className="text-sm text-offBlack dark:text-offBlack"
font={{ family: 'ABCDiatype', weight: 'Bold' }}
>
Suggested creators and collectors
</Typography>
<View className="space-x-1 flex flex-row">
{suggestedUsers?.map((user, idx) => {
return (
<View className="mb-1" style={{ width: CARD_WIDTH, height: CARD_HEIGHT }} key={idx}>
<TrendingUserCard userRef={user} queryRef={query} />
</View>
);
})}
</View>
</View>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { removeNullValues } from '~/shared/relay/removeNullValues';
export type FeedItemTypes = 'Post' | 'FeedEvent';
type itemType = FeedItemTypes | null;

const SUGGESTED_PROFILE_ROW_IDX = 5;

export type FeedListItemType = { key: string } & (
| {
kind: 'feed-item-navigation';
Expand Down Expand Up @@ -107,6 +109,14 @@ export type FeedListItemType = { key: string } & (
postId: string;
itemType: itemType;
}
| {
kind: 'suggested-profile-row';
event: null;
post: null;
eventId: string;
queryRef: createVirtualizedFeedEventItemsQueryFragment$data;
itemType: itemType;
}
);

export type createVirtualizedItemsFromFeedEventsArgs = {
Expand Down Expand Up @@ -149,6 +159,8 @@ export function createVirtualizedFeedEventItems({
...FeedPostSocializeSectionQueryFragment
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...PostListItemQueryFragment
# eslint-disable-next-line relay/must-colocate-fragment-spreads
...FeedSuggestedProfileRowFragment
}
`,
queryRef
Expand Down Expand Up @@ -370,5 +382,28 @@ export function createVirtualizedFeedEventItems({
}
}

return { items };
let postCount = 0;
const itemsWithSuggestedProfileRow = [];
let insertedRow = false;

for (const item of items) {
if (!insertedRow && item.kind === 'post-item-header') {
postCount++;
if (postCount === SUGGESTED_PROFILE_ROW_IDX) {
itemsWithSuggestedProfileRow.push({
kind: 'suggested-profile-row',
event: null,
post: null,
queryRef: query,
key: 'suggested-profile-row',
eventId: 'suggested-profile-row',
itemType: null,
} as FeedListItemType);
insertedRow = true;
}
}
itemsWithSuggestedProfileRow.push(item);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. could we add an explainer / comment block for what's going on here?
  2. do we need to mark insertedRow as true? i assume postCount === SUGGESTED_PROFILE_ROW_IDX would only be true a single time anyway?

overall i'm wondering if there could be a simpler way to do this without the loop, like:

const itemsWithSuggestedProfileRow = [
  ...items.slice(0, 8),
  { kind: 'suggested-profile-row', /* rest of the object */ }
  ...items.slice(8)
]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yo @Robinnnnn, you're right we don't need the insertedRow variable, removed that now.

Regarding using the splice, I think we can't use the hardcoded value for the index because the items of the list are feed-post-header, feed-post-socialize-section, etc (basically subsections of a single post in the feed for all the posts) so we want to add the row only when it is before a feed-post-header

Or it might end up after a post and before its mint button for example.

Screenshot 2024-04-12 at 00 28 45


return { items: itemsWithSuggestedProfileRow };
}
Loading