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

update Search Default state on mobile #2414

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
129 changes: 114 additions & 15 deletions apps/mobile/src/components/Search/SearchDefault.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { FlashList, ListRenderItem } from '@shopify/flash-list';
import { useCallback, useMemo } from 'react';
import { Suspense, useCallback, useMemo } from 'react';
import { View } from 'react-native';
import { graphql, useFragment } from 'react-relay';

import { SearchDefaultFragment$key } from '~/generated/SearchDefaultFragment.graphql';
import { TrendingUserCardFragment$key } from '~/generated/TrendingUserCardFragment.graphql';
import { UserSearchResultFragment$key } from '~/generated/UserSearchResultFragment.graphql';
import { removeNullValues } from '~/shared/relay/removeNullValues';

import { TrendingUserCard } from '../Trending/TrendingUserCard';
import { Typography } from '../Typography';
import { UserFollowListFallback } from '../UserFollowList/UserFollowListFallback';
import { UserSearchResult } from './User/UserSearchResult';

type Props = {
Expand All @@ -17,17 +21,54 @@ type Props = {

type ListItemType =
| { kind: 'header'; title: string }
| { kind: 'user'; user: UserSearchResultFragment$key };
| { kind: 'user'; user: UserSearchResultFragment$key }
| {
kind: 'userCardRow';
users: Array<TrendingUserCardFragment$key>;
};

const CARD_HEIGHT = 145;
const CARD_WIDTH = 185;

export function SearchDefault({ queryRef, blurInputFocus, keyword }: Props) {
const query = useFragment(
graphql`
fragment SearchDefaultFragment on Query {
...TrendingUserCardQueryFragment
trendingUsers5Days: trendingUsers(input: { report: LAST_5_DAYS }) {
... on TrendingUsersPayload {
__typename
users {
galleries {
tokenPreviews {
__typename
}
}

...UserSearchResultFragment
...TrendingUserCardFragment
}
}
}
viewer @required(action: THROW) {
... on Viewer {
suggestedUsers(first: 2) @required(action: THROW) {
__typename
edges {
node {
__typename
... on GalleryUser {
galleries {
tokenPreviews {
__typename
}
}

...UserSearchResultFragment
...TrendingUserCardFragment
}
}
}
}
}
}
Expand All @@ -52,24 +93,80 @@ export function SearchDefault({ queryRef, blurInputFocus, keyword }: Props) {
</Typography>
</View>
);
} else if (item.kind === 'userCardRow') {
return (
<View className="flex flex-row justify-around">
{item?.users?.map((user, idx) => {
return (
<View className="mb-1 " style={{ width: CARD_WIDTH, height: CARD_HEIGHT }}>
<TrendingUserCard
key={idx}
userRef={user}
queryRef={query}
showFollowButton={false}
/>
</View>
);
})}
</View>
);
} else {
return <UserSearchResult userRef={item.user} keyword={keyword} />;
}
},
[keyword]
[query, keyword]
);

const items = useMemo((): ListItemType[] => {
const items: ListItemType[] = [];
items.push({
kind: 'header',
title: 'Trending Curators',
});

const suggestedUsers = [];
if (query.viewer?.suggestedUsers?.__typename === 'UsersConnection') {
for (const edge of query.viewer?.suggestedUsers?.edges ?? []) {
if (edge?.node) {
suggestedUsers.push(edge.node);
}
}
}
if (suggestedUsers) {
items.push({
kind: 'header',
title: 'Suggested Collectors and Creators',
});
items.push({
kind: 'userCardRow',
users: suggestedUsers?.slice(0, 2),
});
}

if (
query.trendingUsers5Days?.__typename === 'TrendingUsersPayload' &&
query.trendingUsers5Days.users
) {
for (const user of query.trendingUsers5Days.users) {
const { users } = query.trendingUsers5Days;

const usersWithTokenPreviews = users.filter((user) => {
return user?.galleries?.find(
(gallery) => removeNullValues(gallery?.tokenPreviews).length > 0
);
});

if (usersWithTokenPreviews) {
items.push({
kind: 'header',
title: 'Trending Collectors and Creators',
});
items.push({
kind: 'userCardRow',
users: usersWithTokenPreviews?.slice(0, 2),
});
}

items.push({
kind: 'header',
title: 'Trending Curators',
});
for (const user of users) {
items.push({
kind: 'user',
user,
Expand All @@ -81,13 +178,15 @@ export function SearchDefault({ queryRef, blurInputFocus, keyword }: Props) {

return (
<View className="flex-grow">
<FlashList
keyboardShouldPersistTaps="always"
data={items}
estimatedItemSize={25}
renderItem={renderItem}
onTouchStart={blurInputFocus}
/>
<Suspense fallback={<UserFollowListFallback />}>
Rohan-cp marked this conversation as resolved.
Show resolved Hide resolved
<FlashList
keyboardShouldPersistTaps="always"
data={items}
estimatedItemSize={25}
renderItem={renderItem}
onTouchStart={blurInputFocus}
/>
</Suspense>
</View>
);
}
6 changes: 3 additions & 3 deletions apps/mobile/src/components/Trending/TrendingUserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Typography } from '../Typography';
type Props = {
userRef: TrendingUserCardFragment$key;
queryRef: TrendingUserCardQueryFragment$key;
showFollowButton?: boolean;
style?: ViewProps['style'];
};

Expand All @@ -32,7 +33,7 @@ const markdownStyle = {
},
};

export function TrendingUserCard({ style, userRef, queryRef }: Props) {
export function TrendingUserCard({ style, userRef, queryRef, showFollowButton = true }: Props) {
const user = useFragment(
graphql`
fragment TrendingUserCardFragment on GalleryUser {
Expand Down Expand Up @@ -159,8 +160,7 @@ export function TrendingUserCard({ style, userRef, queryRef }: Props) {
</Markdown>
</View>
</View>

<FollowButton queryRef={query} userRef={user} width="grow" />
{showFollowButton && <FollowButton queryRef={query} userRef={user} width="grow" />}
</GalleryTouchableOpacity>
);
}
Loading