Skip to content

Commit 8e2ffd8

Browse files
authored
Shuffling around where the search_update event is fired so it happens in more places (#1679)
- Changed HeroSearch to use the SearchField component instead of SearchInput, so we get the event capture stuff - Updated SearchInput to not require setPage (for use on the homepage) - Moved facet events out of the SearchPage and into SearchDisplay so they get called on other pages that use it - Also trigger the facet events on more things - should also now get sorting changes, choosing between categories, and clearing facets
1 parent f8a5b1e commit 8e2ffd8

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

frontends/mit-learn/src/page-components/SearchDisplay/SearchDisplay.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import type { TabConfig } from "./ResourceCategoryTabs"
5151
import { ResourceCard } from "../ResourceCard/ResourceCard"
5252
import { useSearchParams } from "@mitodl/course-search-utils/react-router"
5353
import { useUserMe } from "api/hooks/user"
54+
import { usePostHog } from "posthog-js/react"
5455

5556
const StyledResourceTabs = styled(ResourceCategoryTabs.TabList)`
5657
margin-top: 0 px;
@@ -532,11 +533,11 @@ const SearchDisplay: React.FC<SearchDisplayProps> = ({
532533
constantSearchParams,
533534
hasFacets,
534535
requestParams,
535-
setParamValue,
536-
clearAllFacets,
537-
toggleParamValue,
536+
setParamValue: actuallySetParamValue,
537+
clearAllFacets: actuallyClearAllFacets,
538+
toggleParamValue: actuallyToggleParamValue,
538539
showProfessionalToggle,
539-
setSearchParams,
540+
setSearchParams: actuallySetSearchParams,
540541
resultsHeadingEl,
541542
filterHeadingEl,
542543
}) => {
@@ -588,10 +589,45 @@ const SearchDisplay: React.FC<SearchDisplayProps> = ({
588589

589590
const [mobileDrawerOpen, setMobileDrawerOpen] = React.useState(false)
590591

592+
const posthog = usePostHog()
593+
const { POSTHOG } = APP_SETTINGS
594+
591595
const toggleMobileDrawer = (newOpen: boolean) => () => {
592596
setMobileDrawerOpen(newOpen)
593597
}
594598

599+
const captureSearchEvent = () => {
600+
if (!(!POSTHOG?.api_key || POSTHOG.api_key.length < 1)) {
601+
posthog.capture("search_update")
602+
}
603+
}
604+
605+
const setParamValue = (value: string, prev: string | string[]) => {
606+
captureSearchEvent()
607+
actuallySetParamValue(value, prev)
608+
}
609+
610+
const clearAllFacets = () => {
611+
captureSearchEvent()
612+
actuallyClearAllFacets()
613+
}
614+
615+
const setSearchParams = (
616+
value: URLSearchParams | ((prev: URLSearchParams) => URLSearchParams),
617+
) => {
618+
captureSearchEvent()
619+
actuallySetSearchParams(value)
620+
}
621+
622+
const toggleParamValue = (
623+
name: string,
624+
rawValue: string,
625+
checked: boolean,
626+
) => {
627+
captureSearchEvent()
628+
actuallyToggleParamValue(name, rawValue, checked)
629+
}
630+
595631
const searchModeDropdown = (
596632
<SimpleSelect
597633
size="small"

frontends/mit-learn/src/page-components/SearchField/SearchField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { usePostHog } from "posthog-js/react"
55

66
type SearchFieldProps = SearchInputProps & {
77
onSubmit: (event: SearchSubmissionEvent) => void
8-
setPage: (page: number) => void
8+
setPage?: (page: number) => void
99
}
1010

1111
const { POSTHOG } = APP_SETTINGS
@@ -26,7 +26,7 @@ const SearchField: React.FC<SearchFieldProps> = ({
2626
{ isEnter } = {},
2727
) => {
2828
onSubmit(event)
29-
setPage(1)
29+
setPage && setPage(1)
3030
if (POSTHOG?.api_key) {
3131
posthog.capture("search_update", { isEnter: isEnter })
3232
}

frontends/mit-learn/src/pages/HomePage/HeroSearch.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
styled,
66
ChipLink,
77
Link,
8-
SearchInput,
98
SearchInputProps,
109
} from "ol-components"
1110
import type { ChipLinkProps } from "ol-components"
@@ -27,6 +26,7 @@ import {
2726
RiVerifiedBadgeLine,
2827
} from "@remixicon/react"
2928
import _ from "lodash"
29+
import { SearchField } from "@/page-components/SearchField/SearchField"
3030

3131
type SearchChip = {
3232
label: string
@@ -236,7 +236,7 @@ const HeroSearch: React.FC = () => {
236236
</BoldLink>
237237
</Typography>
238238
<ControlsContainer>
239-
<SearchInput
239+
<SearchField
240240
size="hero"
241241
fullWidth
242242
value={searchText}

frontends/mit-learn/src/pages/SearchPage/SearchPage.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type { LearningResourceOfferor } from "api"
1515
import { useOfferorsList } from "api/hooks/learningResources"
1616
import { capitalize } from "ol-utilities"
1717
import MetaTags from "@/page-components/MetaTags/MetaTags"
18-
import { usePostHog } from "posthog-js/react"
1918

2019
const cssGradient = `
2120
linear-gradient(
@@ -171,8 +170,6 @@ const useFacetManifest = (resourceCategory: string | null) => {
171170
const SearchPage: React.FC = () => {
172171
const [searchParams, setSearchParams] = useSearchParams()
173172
const facetManifest = useFacetManifest(searchParams.get("resource_category"))
174-
const posthog = usePostHog()
175-
const { POSTHOG } = APP_SETTINGS
176173

177174
const setPage = useCallback(
178175
(newPage: number) => {
@@ -189,11 +186,8 @@ const SearchPage: React.FC = () => {
189186
[setSearchParams],
190187
)
191188
const onFacetsChange = useCallback(() => {
192-
if (!(!POSTHOG?.api_key || POSTHOG.api_key.length < 1)) {
193-
posthog.capture("search_update")
194-
}
195189
setPage(1)
196-
}, [setPage, posthog, POSTHOG])
190+
}, [setPage])
197191

198192
const {
199193
params,

0 commit comments

Comments
 (0)