From 60b5c43005bd4c23ad6976d780656c63180d1231 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Thu, 4 Apr 2024 21:13:08 +0100 Subject: [PATCH] add extra tab to search and translate tab names --- src/state/queries/search-posts.ts | 31 +++++++++++++------ src/view/screens/Search/Search.tsx | 49 +++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/state/queries/search-posts.ts b/src/state/queries/search-posts.ts index 9bf3c0f9ec..ef8b083584 100644 --- a/src/state/queries/search-posts.ts +++ b/src/state/queries/search-posts.ts @@ -10,12 +10,19 @@ import {getAgent} from '#/state/session' import {embedViewRecordToPostView, getEmbeddedPost} from './util' const searchPostsQueryKeyRoot = 'search-posts' -const searchPostsQueryKey = ({query}: {query: string}) => [ +const searchPostsQueryKey = ({query, sort}: {query: string; sort?: string}) => [ searchPostsQueryKeyRoot, query, + sort, ] -export function useSearchPostsQuery({query}: {query: string}) { +export function useSearchPostsQuery({ + query, + sort, +}: { + query: string + sort?: 'top' | 'latest' +}) { return useInfiniteQuery< AppBskyFeedSearchPosts.OutputSchema, Error, @@ -23,14 +30,20 @@ export function useSearchPostsQuery({query}: {query: string}) { QueryKey, string | undefined >({ - queryKey: searchPostsQueryKey({query}), + queryKey: searchPostsQueryKey({query, sort}), queryFn: async ({pageParam}) => { - const res = await getAgent().app.bsky.feed.searchPosts({ - q: query, - limit: 25, - cursor: pageParam, - }) - return res.data + // waiting on new APIs + switch (sort) { + // case 'top': + // case 'latest': + default: + const res = await getAgent().app.bsky.feed.searchPosts({ + q: query, + limit: 25, + cursor: pageParam, + }) + return res.data + } }, initialPageParam: undefined, getNextPageParam: lastPage => lastPage.cursor, diff --git a/src/view/screens/Search/Search.tsx b/src/view/screens/Search/Search.tsx index c0f4cf1950..a6a975e6e8 100644 --- a/src/view/screens/Search/Search.tsx +++ b/src/view/screens/Search/Search.tsx @@ -12,6 +12,7 @@ import { FontAwesomeIcon, FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' +import {type I18n} from '@lingui/core' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import AsyncStorage from '@react-native-async-storage/async-storage' @@ -191,7 +192,13 @@ type SearchResultSlice = key: string } -function SearchScreenPostResults({query}: {query: string}) { +function SearchScreenPostResults({ + query, + sort, +}: { + query: string + sort: 'top' | 'latest' +}) { const {_} = useLingui() const {currentAccount} = useSession() const [isPTR, setIsPTR] = React.useState(false) @@ -209,7 +216,7 @@ function SearchScreenPostResults({query}: {query: string}) { fetchNextPage, isFetchingNextPage, hasNextPage, - } = useSearchPostsQuery({query: augmentedQuery}) + } = useSearchPostsQuery({query: augmentedQuery, sort}) const onPullToRefresh = React.useCallback(async () => { setIsPTR(true) @@ -316,8 +323,25 @@ function SearchScreenUserResults({query}: {query: string}) { ) } -const SECTIONS_LOGGEDOUT = ['Users'] -const SECTIONS_LOGGEDIN = ['Posts', 'Users'] +const SECTIONS_LOGGEDOUT = ['PEOPLE'] as const +const SECTIONS_LOGGEDIN = ['TOP', 'LATEST', 'PEOPLE'] as const + +function getSectionName( + i18n: I18n, + section: + | (typeof SECTIONS_LOGGEDIN)[number] + | (typeof SECTIONS_LOGGEDOUT)[number], +) { + switch (section) { + case 'TOP': + return i18n._(msg`Top`) + case 'LATEST': + return i18n._(msg`Latest`) + case 'PEOPLE': + return i18n._(msg`People`) + } +} + export function SearchScreenInner({ query, primarySearch, @@ -330,6 +354,7 @@ export function SearchScreenInner({ const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled() const {hasSession} = useSession() const {isDesktop} = useWebMediaQueries() + const {i18n} = useLingui() const onPageSelected = React.useCallback( (index: number) => { @@ -339,6 +364,13 @@ export function SearchScreenInner({ [setDrawerSwipeDisabled, setMinimalShellMode], ) + const sections = hasSession ? SECTIONS_LOGGEDIN : SECTIONS_LOGGEDOUT + + const tabBarItems = React.useMemo( + () => sections.map(section => getSectionName(i18n, section)), + [sections, i18n], + ) + if (hasSession) { return query ? ( - + )} initialPage={0}> - + + + + @@ -389,7 +424,7 @@ export function SearchScreenInner({ - + )} initialPage={0}>