-
Notifications
You must be signed in to change notification settings - Fork 228
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
feat: onboarding follow sources #3867
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
96e505e
feat: onboarding follow sources
rebelchris d893d74
fix: new format
rebelchris 6de40b8
fix: placeholder
rebelchris 8ca1b53
fix: PR Feedback
rebelchris 3ef0e59
Update packages/shared/src/components/onboarding/Sources/Sources.tsx
rebelchris f6ee305
fix: remove pending state
rebelchris 43ff29b
Merge remote-tracking branch 'origin/AS-678-follow-sources-onboarding…
rebelchris f56b7e4
Merge branch 'main' into AS-678-follow-sources-onboarding
rebelchris 8a5f5bb
fix: remove pending state
rebelchris cff8c8a
Merge remote-tracking branch 'origin/AS-678-follow-sources-onboarding…
rebelchris 596c972
fix: copy feedback
rebelchris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
packages/shared/src/components/onboarding/Sources/Sources.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import React, { ReactElement, useCallback, useMemo } from 'react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import classNames from 'classnames'; | ||
import { | ||
Typography, | ||
TypographyColor, | ||
TypographyType, | ||
} from '../../typography/Typography'; | ||
import { SearchField } from '../../fields/SearchField'; | ||
import { useViewSize, ViewSize } from '../../../hooks'; | ||
import useDebounceFn from '../../../hooks/useDebounceFn'; | ||
import { useSourceSearch } from '../../../hooks/useSourceSearch'; | ||
import { Origin } from '../../../lib/log'; | ||
import { gqlClient } from '../../../graphql/common'; | ||
import { disabledRefetch } from '../../../lib/func'; | ||
import { generateQueryKey, RequestKey } from '../../../lib/query'; | ||
import { ONBOARDING_SOURCES_QUERY, Source } from '../../../graphql/sources'; | ||
import { ElementPlaceholder } from '../../ElementPlaceholder'; | ||
import { ProfileImageSize, ProfilePicture } from '../../ProfilePicture'; | ||
import { PlusIcon, VIcon } from '../../icons'; | ||
import { IconSize } from '../../Icon'; | ||
import useFeedSettings from '../../../hooks/useFeedSettings'; | ||
import useTagAndSource from '../../../hooks/useTagAndSource'; | ||
|
||
const placeholderSources = new Array(5).fill(null).map((_, index) => index); | ||
|
||
export const Sources = (): ReactElement => { | ||
const isMobile = useViewSize(ViewSize.MobileL); | ||
|
||
const [searchQuery, setSearchQuery] = React.useState<string>(); | ||
const [onSearch] = useDebounceFn(setSearchQuery, 200); | ||
|
||
const { feedSettings } = useFeedSettings(); | ||
|
||
const { onFollowSource, onUnfollowSource } = useTagAndSource({ | ||
origin: Origin.Onboarding, | ||
}); | ||
|
||
const selectedTags = feedSettings?.includeTags || []; | ||
|
||
const selectedSources = useMemo(() => { | ||
return feedSettings?.includeSources.map(({ id }) => id); | ||
}, [feedSettings?.includeSources]); | ||
|
||
const onToggleSource = useCallback( | ||
(source) => { | ||
if (selectedSources?.includes(source.id)) { | ||
onUnfollowSource({ source }); | ||
} else { | ||
onFollowSource({ source }); | ||
} | ||
}, | ||
[onFollowSource, onUnfollowSource, selectedSources], | ||
); | ||
|
||
const { data: searchResult, isPending: isPendingSearch } = useSourceSearch({ | ||
value: searchQuery, | ||
}); | ||
|
||
const { data: onboardingSources, isPending } = useQuery({ | ||
queryKey: generateQueryKey(RequestKey.OnboardingSources), | ||
|
||
queryFn: async () => { | ||
const result = await gqlClient.request<{ | ||
sourceRecommendationByTags: Source[]; | ||
}>(ONBOARDING_SOURCES_QUERY, { tags: selectedTags }); | ||
|
||
return result.sourceRecommendationByTags; | ||
}, | ||
...disabledRefetch, | ||
staleTime: Infinity, | ||
}); | ||
|
||
const sources = | ||
searchQuery && !isPendingSearch ? searchResult : onboardingSources; | ||
|
||
const SourceTag = ({ source }: { source: Source }): ReactElement => { | ||
const checked = selectedSources?.includes(source.id); | ||
return ( | ||
<div | ||
key={source.id} | ||
className={classNames( | ||
'flex w-full rounded-12 p-px', | ||
checked | ||
? 'bg-gradient-to-b from-accent-onion-subtlest to-accent-cabbage-default' | ||
: 'bg-transparent', | ||
)} | ||
> | ||
<div className="w-full rounded-11 bg-background-default"> | ||
<button | ||
type="button" | ||
className={classNames( | ||
'flex w-full gap-2 rounded-11 bg-surface-float px-3 py-2', | ||
checked | ||
? undefined | ||
: 'hover:bg-surface-hover active:bg-surface-active', | ||
)} | ||
onClick={() => onToggleSource(source)} | ||
> | ||
<ProfilePicture | ||
size={ProfileImageSize.Medium} | ||
rounded="full" | ||
className="mt-2" | ||
user={{ | ||
id: source.id, | ||
image: source.image, | ||
username: source.handle, | ||
}} | ||
nativeLazyLoading | ||
/> | ||
<div className="flex-1 text-left"> | ||
<Typography | ||
type={TypographyType.Title3} | ||
bold | ||
color={TypographyColor.Primary} | ||
> | ||
{source.name} | ||
</Typography> | ||
<Typography | ||
type={TypographyType.Body} | ||
color={TypographyColor.Secondary} | ||
> | ||
{source.handle} | ||
</Typography> | ||
<Typography | ||
type={TypographyType.Body} | ||
color={TypographyColor.Tertiary} | ||
className="multi-truncate line-clamp-2" | ||
> | ||
{source.description} | ||
</Typography> | ||
</div> | ||
<div className="mr-2 flex size-8 items-center justify-center self-center"> | ||
{checked ? ( | ||
<VIcon | ||
className="text-brand-default" | ||
size={IconSize.Small} | ||
secondary | ||
/> | ||
) : ( | ||
<PlusIcon size={IconSize.Small} /> | ||
)} | ||
</div> | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="flex max-w-screen-laptop flex-col tablet:px-10"> | ||
<Typography | ||
type={TypographyType.LargeTitle} | ||
bold | ||
className="mb-10 text-center" | ||
> | ||
Things you would care to follow | ||
</Typography> | ||
<div className="w-full max-w-[35rem]"> | ||
<SearchField | ||
aria-label="Pick tags that are relevant to you" | ||
autoFocus={!isMobile} | ||
className="mb-10 w-full" | ||
inputId="search-filters" | ||
placeholder="TechCrunch, Hacker News, GitHub, etc" | ||
valueChanged={onSearch} | ||
/> | ||
<div | ||
role="list" | ||
aria-busy={isPending} | ||
className="flex flex-row flex-wrap justify-center gap-4" | ||
> | ||
{isPending && | ||
placeholderSources.map((item) => ( | ||
<ElementPlaceholder key={item} className="h-16 w-full rounded-12"> | ||
<span className="invisible">{item}</span> | ||
</ElementPlaceholder> | ||
))} | ||
{!isPending && | ||
sources?.map((source) => ( | ||
<SourceTag source={source} key={source.id} /> | ||
))} | ||
{/* render leftover tags not rendered in initial recommendations but selected */} | ||
{!isPending && | ||
!searchQuery && | ||
feedSettings?.includeSources?.map((source) => { | ||
if (sources.find(({ id }) => id === source.id)) { | ||
return null; | ||
} | ||
|
||
return <SourceTag source={source} key={source.id} />; | ||
})} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { gqlClient } from '../graphql/common'; | ||
import { SEARCH_SOURCES_QUERY, Source } from '../graphql/sources'; | ||
import { generateQueryKey, RequestKey } from '../lib/query'; | ||
|
||
export type UseSourceSearchProps = { | ||
value: string; | ||
}; | ||
|
||
export type UseSourceSearch = { | ||
data?: Source[]; | ||
isPending: boolean; | ||
}; | ||
|
||
export const MIN_SEARCH_QUERY_LENGTH = 2; | ||
|
||
export const useSourceSearch = ({ | ||
value, | ||
}: UseSourceSearchProps): UseSourceSearch => { | ||
const { data, isPending } = useQuery({ | ||
queryKey: generateQueryKey(RequestKey.SearchSources, null, value), | ||
queryFn: async () => { | ||
const result = await gqlClient.request<{ | ||
searchSources: Source[]; | ||
}>(SEARCH_SOURCES_QUERY, { | ||
query: value, | ||
}); | ||
|
||
return result.searchSources; | ||
}, | ||
enabled: value?.length >= MIN_SEARCH_QUERY_LENGTH, | ||
}); | ||
|
||
return { | ||
data, | ||
isPending, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ export default { | |
6: '0.375rem', | ||
8: '0.5rem', | ||
10: '0.625rem', | ||
11: '0.6875rem', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed for inner border, else @omBratteng would get angry There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😂 |
||
12: '0.75rem', | ||
14: '0.875rem', | ||
16: '1rem', | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More readable in my opinion