Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(surveys): sync survey search bar with url query param (#65833)

Jon McCallum11f4c6c9a4e6
diff --git a/frontend/src/scenes/surveys/surveysLogic.test.ts b/frontend/src/scenes/surveys/surveysLogic.test.ts
index 8ae87debc5f6..44c93e9efb44 100644
--- a/frontend/src/scenes/surveys/surveysLogic.test.ts
+++ b/frontend/src/scenes/surveys/surveysLogic.test.ts
@@ -134,6 +134,64 @@ describe('surveysLogic', () => {
})
})
+ describe('url syncing', () => {
+ let logic: ReturnType<typeof surveysLogic.build>
+
+ beforeEach(async () => {
+ initKeaTests()
+
+ useMocks({
+ get: {
+ '/api/projects/:team/surveys/': () => [200, { count: 0, results: [], next: null, previous: null }],
+ '/api/projects/:team/surveys/responses_count': () => [200, {}],
+ },
+ })
+
+ logic = surveysLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ })
+
+ it('writes the search term to the search query param', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setSearchTerm('checkout')
+ }).toFinishAllListeners()
+
+ expect(router.values.searchParams.search).toEqual('checkout')
+ })
+
+ it('removes the search query param when the term is cleared', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setSearchTerm('checkout')
+ }).toFinishAllListeners()
+ await expectLogic(logic, () => {
+ logic.actions.setSearchTerm('')
+ }).toFinishAllListeners()
+
+ expect(router.values.searchParams.search).toBeUndefined()
+ })
+
+ it('reads the search term from the search query param on navigation', async () => {
+ router.actions.push('/surveys', { search: 'onboarding' })
+
+ await expectLogic(logic).toFinishAllListeners().toMatchValues({
+ searchTerm: 'onboarding',
+ })
+ })
+
+ it('clears a stale search term when navigating to surveys without a search param', async () => {
+ await expectLogic(logic, () => {
+ logic.actions.setSearchTerm('onboarding')
+ }).toFinishAllListeners()
+
+ router.actions.push('/surveys')
+
+ await expectLogic(logic).toFinishAllListeners().toMatchValues({
+ searchTerm: '',
+ })
+ })
+ })
+
describe('product intent tracking', () => {
let logic: ReturnType<typeof surveysLogic.build>
let capturedIntentRequests: any[]
diff --git a/frontend/src/scenes/surveys/surveysLogic.tsx b/frontend/src/scenes/surveys/surveysLogic.tsx
index 1eb0b53f6f74..2a0c9adf6edb 100644
--- a/frontend/src/scenes/surveys/surveysLogic.tsx
+++ b/frontend/src/scenes/surveys/surveysLogic.tsx
@@ -473,14 +473,31 @@ export const surveysLogic = kea<surveysLogicType>([
}),
actionToUrl(({ values }) => ({
setTab: () => {
- return [router.values.location.pathname, { ...router.values.searchParams, tab: values.tab }]
+ return [
+ router.values.location.pathname,
+ { ...router.values.searchParams, tab: values.tab },
+ router.values.hashParams,
+ ]
+ },
+ setSearchTerm: () => {
+ const searchParams = { ...router.values.searchParams }
+ if (values.searchTerm) {
+ searchParams.search = values.searchTerm
+ } else {
+ delete searchParams.search
+ }
+ return [router.values.location.pathname, searchParams, router.values.hashParams, { replace: true }]
},
})),
- urlToAction(({ actions }) => ({
- [urls.surveys()]: (_, { tab }) => {
+ urlToAction(({ actions, values }) => ({
+ [urls.surveys()]: (_, { tab, search }) => {
if (tab) {
actions.setTab(tab)
}
+ const nextSearchTerm = search ?? ''
+ if (nextSearchTerm !== values.searchTerm) {
+ actions.setSearchTerm(nextSearchTerm)
+ }
actions.addProductIntent({
product_type: ProductKey.SURVEYS,
intent_context: ProductIntentContext.SURVEYS_VIEWED,