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

chore: Clean AREnableNewSearchModal ff related code #11347

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useSearch } from "app/Components/GlobalSearchInput/useSearch"
import { RecentSearches } from "app/Scenes/Search/RecentSearches"
import { SEARCH_INPUT_PLACEHOLDER, shouldStartSearching } from "app/Scenes/Search/Search"
import { SearchContext } from "app/Scenes/Search/SearchContext"
import { MAX_SHOWN_RECENT_SEARCHES, useRecentSearches } from "app/Scenes/Search/SearchModel"
import { useRecentSearches } from "app/Scenes/Search/SearchModel"
import { SearchPills } from "app/Scenes/Search/SearchPills"
import { SearchResults } from "app/Scenes/Search/SearchResults"
import { SEARCH_PILLS } from "app/Scenes/Search/constants"
Expand Down Expand Up @@ -40,7 +40,7 @@ const GlobalSearchInputOverlayContent: React.FC<{ query: string }> = ({ query })
refetch,
} = useSearch({ query })

const recentSearches = useRecentSearches(MAX_SHOWN_RECENT_SEARCHES)
const recentSearches = useRecentSearches()

return (
<SearchContext.Provider value={searchProviderValues}>
Expand Down
53 changes: 12 additions & 41 deletions src/app/Scenes/HomeView/Components/HomeHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OwnerType } from "@artsy/cohesion"
import { ArtsyLogoBlackIcon, Box, Flex } from "@artsy/palette-mobile"
import { Flex } from "@artsy/palette-mobile"
import { GlobalSearchInput } from "app/Components/GlobalSearchInput/GlobalSearchInput"
import { PaymentFailureBanner } from "app/Scenes/HomeView/Components/PaymentFailureBanner"
import { GlobalStore } from "app/store/GlobalStore"
Expand All @@ -8,58 +8,29 @@ import { Suspense } from "react"
import { ActivityIndicator } from "./ActivityIndicator"

export const HomeHeader: React.FC = () => {
const enableNewSearchModal = useFeatureFlag("AREnableNewSearchModal")
const showPaymentFailureBanner = useFeatureFlag("AREnablePaymentFailureBanner")
const hasUnseenNotifications = GlobalStore.useAppState(
(state) => state.bottomTabs.hasUnseenNotifications
)

if (enableNewSearchModal) {
return (
<Flex backgroundColor="white100">
{!!showPaymentFailureBanner && (
<Suspense fallback={null}>
<PaymentFailureBanner />
</Suspense>
)}
<Flex pb={1} pt={2}>
<Flex
flexDirection="row"
px={2}
gap={1}
justifyContent="space-around"
alignItems="center"
>
<Flex flex={1}>
<GlobalSearchInput ownerType={OwnerType.home} />
</Flex>
<Flex alignItems="flex-end">
<ActivityIndicator hasUnseenNotifications={hasUnseenNotifications} />
</Flex>
</Flex>
</Flex>
</Flex>
)
}

return (
<>
<Flex backgroundColor="white100">
{!!showPaymentFailureBanner && (
<Suspense fallback={null}>
<PaymentFailureBanner />
</Suspense>
)}
<Box py={2} backgroundColor="white100">
<Flex flexDirection="row" px={2} justifyContent="space-between" alignItems="center">
<Box flex={1} />
<Box>
<ArtsyLogoBlackIcon scale={0.75} />
</Box>
<Box flex={1} alignItems="flex-end">

<Flex pb={1} pt={2}>
<Flex flexDirection="row" px={2} gap={1} justifyContent="space-around" alignItems="center">
<Flex flex={1}>
<GlobalSearchInput ownerType={OwnerType.home} />
</Flex>
<Flex alignItems="flex-end">
<ActivityIndicator hasUnseenNotifications={hasUnseenNotifications} />
</Box>
</Flex>
</Flex>
</Box>
</>
</Flex>
</Flex>
)
}
4 changes: 2 additions & 2 deletions src/app/Scenes/Search/RecentSearches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { SimpleMessage } from "@artsy/palette-mobile"
import { SectionTitle } from "app/Components/SectionTitle"
import { GlobalStore } from "app/store/GlobalStore"
import { LayoutAnimation } from "react-native"
import { MAX_SHOWN_RECENT_SEARCHES, useRecentSearches } from "./SearchModel"
import { useRecentSearches } from "./SearchModel"
import { AutosuggestSearchResult } from "./components/AutosuggestSearchResult"
import { SearchResultList } from "./components/SearchResultList"

export const RecentSearches: React.FC = () => {
const recentSearches = useRecentSearches(MAX_SHOWN_RECENT_SEARCHES)
const recentSearches = useRecentSearches()
return (
<>
<SectionTitle title="Recent Searches" />
Expand Down
65 changes: 4 additions & 61 deletions src/app/Scenes/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@ import { StackScreenProps } from "@react-navigation/stack"
import { withProfiler } from "@sentry/react-native"
import { SearchQuery, SearchQuery$variables } from "__generated__/SearchQuery.graphql"
import { GlobalSearchInput } from "app/Components/GlobalSearchInput/GlobalSearchInput"
import { SearchInput } from "app/Components/SearchInput"
import { SearchPills } from "app/Scenes/Search/SearchPills"
import { useRefetchWhenQueryChanged } from "app/Scenes/Search/useRefetchWhenQueryChanged"
import { useSearchQuery } from "app/Scenes/Search/useSearchQuery"
import { ArtsyKeyboardAvoidingView } from "app/utils/ArtsyKeyboardAvoidingView"
import { useBottomTabsScrollToTop } from "app/utils/bottomTabsHelper"
import { useFeatureFlag } from "app/utils/hooks/useFeatureFlag"
import { Schema } from "app/utils/track"
import { throttle } from "lodash"
import { memo, Suspense, useEffect, useMemo, useRef, useState } from "react"
import { memo, Suspense, useEffect, useRef, useState } from "react"
import { Platform, ScrollView } from "react-native"
import { isTablet } from "react-native-device-info"
import { graphql } from "react-relay"
import { useTracking } from "react-tracking"
import styled from "styled-components/native"
import { CuratedCollections } from "./CuratedCollections"
import { RecentSearches } from "./RecentSearches"
import { SearchContext, useSearchProviderValues } from "./SearchContext"
import { SearchResults } from "./SearchResults"
import { TrendingArtists } from "./TrendingArtists"
import { CityGuideCTA } from "./components/CityGuideCTA"
import { SearchPlaceholder } from "./components/placeholders/SearchPlaceholder"
import { SEARCH_PILLS, SEARCH_THROTTLE_INTERVAL, TOP_PILL } from "./constants"
import { SEARCH_PILLS, TOP_PILL } from "./constants"
import { getContextModuleByPillName } from "./helpers"
import { PillType } from "./types"

Expand All @@ -45,7 +41,6 @@ export const searchQueryDefaultVariables: SearchQuery$variables = {
}

export const Search: React.FC = () => {
const enableNewSearchModal = useFeatureFlag("AREnableNewSearchModal")
const searchPillsRef = useRef<ScrollView>(null)
const [searchQuery, setSearchQuery] = useState<string>("")
const [selectedPill, setSelectedPill] = useState<PillType>(TOP_PILL)
Expand Down Expand Up @@ -84,41 +79,6 @@ export const Search: React.FC = () => {
return selectedPill.key === pill.key
}

const handleResetSearchInput = () => {
searchPillsRef?.current?.scrollTo({ x: 0, y: 0, animated: true })
setSelectedPill(TOP_PILL)
}

const handleThrottledTextChange = useMemo(
() =>
throttle((value) => {
setSearchQuery(value)
}, SEARCH_THROTTLE_INTERVAL),
[]
)

const onSearchTextChanged = (queryText: string) => {
queryText = queryText.trim()

handleThrottledTextChange(queryText)

if (queryText.length === 0) {
trackEvent({
action_type: Schema.ActionNames.ARAnalyticsSearchCleared,
})
handleResetSearchInput()

handleThrottledTextChange.flush()

return
}

trackEvent({
action_type: Schema.ActionNames.ARAnalyticsSearchStartedQuery,
query: queryText,
})
}

useEffect(() => {
if (searchProviderValues.inputRef?.current && isAndroid) {
const unsubscribe = navigation?.addListener("focus", () => {
Expand All @@ -134,16 +94,8 @@ export const Search: React.FC = () => {
return (
<SearchContext.Provider value={searchProviderValues}>
<ArtsyKeyboardAvoidingView>
<Flex p={2} pb={enableNewSearchModal ? 1 : 0}>
{enableNewSearchModal ? (
<GlobalSearchInput ownerType={OwnerType.search} />
) : (
<SearchInput
ref={searchProviderValues?.inputRef}
placeholder={SEARCH_INPUT_PLACEHOLDER}
onChangeText={onSearchTextChanged}
/>
)}
<Flex p={2} pb={1}>
<GlobalSearchInput ownerType={OwnerType.search} />
</Flex>
<Flex flex={1} collapsable={false}>
{shouldStartSearching(searchQuery) && !!queryData.viewer ? (
Expand All @@ -167,15 +119,6 @@ export const Search: React.FC = () => {
</>
) : (
<Scrollable ref={scrollableRef}>
{!enableNewSearchModal && (
<>
<HorizontalPadding>
<RecentSearches />
</HorizontalPadding>
<Spacer y={4} />
</>
)}

<TrendingArtists data={queryData} mb={4} />
<CuratedCollections collections={queryData} mb={4} />

Expand Down
42 changes: 13 additions & 29 deletions src/app/Scenes/Search/SearchModel.tests.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { waitFor } from "@testing-library/react-native"
import { __globalStoreTestUtils__, GlobalStore, GlobalStoreProvider } from "app/store/GlobalStore"
import { flushPromiseQueue } from "app/utils/tests/flushPromiseQueue"
import { renderWithWrappersLEGACY } from "app/utils/tests/renderWithWrappers"
import { times } from "lodash"
import {
Expand Down Expand Up @@ -94,31 +94,26 @@ describe("Recent Searches", () => {
})

describe(useRecentSearches, () => {
beforeEach(() => {
__globalStoreTestUtils__?.injectFeatureFlags({
AREnableNewSearchModal: true,
})
})

it("truncates the list of recent searches", async () => {
let localRecentSearches: SearchModel["recentSearches"] = []
let globalRecentSearches: SearchModel["recentSearches"] = []
const TestComponent: React.FC<{ numSearches: number }> = ({ numSearches }) => {
localRecentSearches = useRecentSearches(numSearches)

const TestComponent: React.FC = () => {
localRecentSearches = useRecentSearches()
globalRecentSearches = __globalStoreTestUtils__?.getCurrentState().search.recentSearches!

return null
}

const tree = renderWithWrappersLEGACY(
expect(localRecentSearches.length).toBe(0)
expect(globalRecentSearches.length).toBe(0)

renderWithWrappersLEGACY(
<GlobalStoreProvider>
<TestComponent numSearches={5} />
<TestComponent />
</GlobalStoreProvider>
)

expect(localRecentSearches.length).toBe(0)
expect(globalRecentSearches.length).toBe(0)

times(10).forEach((i) => {
GlobalStore.actions.search.addRecentSearch({
type: "AUTOSUGGEST_RESULT_TAPPED",
Expand All @@ -132,20 +127,9 @@ describe(useRecentSearches, () => {
})
})

await flushPromiseQueue()

expect(localRecentSearches.length).toBe(10)
expect(globalRecentSearches.length).toBe(10)

tree.update(
<GlobalStoreProvider>
<TestComponent numSearches={8} />
</GlobalStoreProvider>
)

await flushPromiseQueue()

expect(localRecentSearches.length).toBe(10)
expect(globalRecentSearches.length).toBe(10)
await waitFor(() => {
expect(localRecentSearches.length).toBe(10)
expect(globalRecentSearches.length).toBe(10)
})
})
})
7 changes: 2 additions & 5 deletions src/app/Scenes/Search/SearchModel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AutosuggestResult } from "app/Components/AutosuggestResults/AutosuggestResults"
import { GlobalStore } from "app/store/GlobalStore"
import { useFeatureFlag } from "app/utils/hooks/useFeatureFlag"
import { Action, action } from "easy-peasy"

export const MAX_SAVED_RECENT_SEARCHES = 100
Expand Down Expand Up @@ -40,10 +39,8 @@ export const getSearchModel = (): SearchModel => ({
}),
})

export const useRecentSearches = (numSearches: number = MAX_SHOWN_RECENT_SEARCHES) => {
const enableNewSearchModal = useFeatureFlag("AREnableNewSearchModal")

export const useRecentSearches = () => {
return GlobalStore.useAppState((state) => {
return state.search.recentSearches
}).slice(0, enableNewSearchModal ? MAX_SAVED_RECENT_SEARCHES : numSearches)
}).slice(0, MAX_SAVED_RECENT_SEARCHES)
}
Loading