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.

fix(error-tracking): keep selection when linking an existing issue (#88130)

Catalinf5610dd5ed50
diff --git a/products/error_tracking/frontend/components/ExternalReferences.tsx b/products/error_tracking/frontend/components/ExternalReferences.tsx
index 029dac742c2e..8fb22fd4e167 100644
--- a/products/error_tracking/frontend/components/ExternalReferences.tsx
+++ b/products/error_tracking/frontend/components/ExternalReferences.tsx
@@ -381,27 +381,25 @@ function linkExistingIssueForm(integration: ErrorTrackingIntegration, onSubmit:
LemonDialog.openForm({
title: `Link existing ${label} issue`,
shouldAwaitSubmit: true,
- initialValues: { externalContext: null as ErrorTrackingExternalIssueResultApiExternalContext | null },
+ initialValues: { externalIssue: null as ErrorTrackingExternalIssueResultApi | null },
content: (
- <LemonField name="externalContext" label="Issue">
+ <LemonField name="externalIssue" label="Issue">
<ExistingIssueSelect integrationId={integration.id} kind={integration.kind} />
</LemonField>
),
errors: {
- externalContext: (externalContext) => (!externalContext ? 'You must select an issue' : undefined),
+ externalIssue: (externalIssue) => (!externalIssue ? 'You must select an issue' : undefined),
},
- onSubmit: ({ externalContext }) => {
- if (externalContext) {
- onSubmit(integration.id, externalContext)
+ onSubmit: ({ externalIssue }) => {
+ if (externalIssue) {
+ onSubmit(integration.id, externalIssue.external_context)
}
},
})
}
-// Searchable picker of existing provider issues. Designed to sit inside a LemonField, so it takes the
-// field's value/onChange and emits the selected issue's external_context (the payload the backend stores).
// Searchable picker of existing provider issues. Search state lives in externalIssueSearchLogic;
-// this component only bridges the LemonField value/onChange to the selected issue's external_context.
+// this component only bridges the LemonField value/onChange to the selected issue.
function ExistingIssueSelect({
integrationId,
kind,
@@ -410,8 +408,8 @@ function ExistingIssueSelect({
}: {
integrationId: number
kind: ErrorTrackingIntegrationKind
- value?: ErrorTrackingExternalIssueResultApiExternalContext | null
- onChange?: (value: ErrorTrackingExternalIssueResultApiExternalContext | null) => void
+ value?: ErrorTrackingExternalIssueResultApi | null
+ onChange?: (value: ErrorTrackingExternalIssueResultApi | null) => void
}): JSX.Element {
const requiresRepository = kind === 'github'
const logic = externalIssueSearchLogic({ integrationId, requiresRepository })
@@ -419,11 +417,16 @@ function ExistingIssueSelect({
const { setRepository, inputChanged, issueSelected } = useActions(logic)
const optionKey = (result: ErrorTrackingExternalIssueResultApi): string => result.url || `${result.id}`
+ const selectedKey = value ? optionKey(value) : null
const options = results.map((result) => ({
key: optionKey(result),
label: result.title,
}))
- const selectedResult = value ? results.find((result) => result.external_context === value) : undefined
+ // A results refresh must not visually drop a valid selection, so the selected
+ // issue stays in the options even when the fresh results no longer include it.
+ if (value && selectedKey && !options.some((option) => option.key === selectedKey)) {
+ options.push({ key: selectedKey, label: value.title })
+ }
return (
<div className="flex flex-col gap-y-2">
@@ -449,7 +452,7 @@ function ExistingIssueSelect({
// would hide valid matches whose titles don't contain the raw query text.
disableFiltering
options={options}
- value={selectedResult ? [optionKey(selectedResult)] : []}
+ value={selectedKey ? [selectedKey] : []}
onInputChange={(query) => {
inputChanged(query)
// Typing a new query invalidates the current pick - submitting while results
@@ -460,11 +463,13 @@ function ExistingIssueSelect({
}}
onChange={(selection) => {
const key = selection[0] ?? null
- const selected = results.find((result) => optionKey(result) === key)
+ const selected =
+ results.find((result) => optionKey(result) === key) ??
+ (key !== null && key === selectedKey ? value : null)
if (selected) {
issueSelected()
}
- onChange?.(selected ? selected.external_context : null)
+ onChange?.(selected ?? null)
}}
/>
</div>
diff --git a/products/error_tracking/frontend/components/externalIssueSearchLogic.test.ts b/products/error_tracking/frontend/components/externalIssueSearchLogic.test.ts
new file mode 100644
index 000000000000..e4dc38d2b697
--- /dev/null
+++ b/products/error_tracking/frontend/components/externalIssueSearchLogic.test.ts
@@ -0,0 +1,66 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { initKeaTests } from '~/test/init'
+
+import { errorTrackingExternalReferencesSearchIssuesRetrieve } from '../generated/api'
+import { externalIssueSearchLogic } from './externalIssueSearchLogic'
+
+jest.mock('../generated/api', () => ({
+ errorTrackingExternalReferencesSearchIssuesRetrieve: jest.fn(),
+}))
+
+const mockSearchIssues = jest.mocked(errorTrackingExternalReferencesSearchIssuesRetrieve)
+
+const RECENT_ISSUES = [
+ { id: 'ISS-1', title: 'First issue', url: 'https://example.com/ISS-1', external_context: { id: 'ISS-1' } },
+ { id: 'ISS-2', title: 'Second issue', url: 'https://example.com/ISS-2', external_context: { id: 'ISS-2' } },
+]
+
+describe('externalIssueSearchLogic', () => {
+ let logic: ReturnType<typeof externalIssueSearchLogic.build>
+
+ beforeEach(async () => {
+ initKeaTests()
+ mockSearchIssues.mockResolvedValue({ issues: RECENT_ISSUES })
+ logic = externalIssueSearchLogic({ integrationId: 1, requiresRepository: false })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['searchIssuesSuccess'])
+ mockSearchIssues.mockClear()
+ })
+
+ afterEach(() => logic.unmount())
+
+ it('only a genuine input clear refetches, not the clear caused by selecting an issue', async () => {
+ // LemonInputSelect clears its input BEFORE it reports the selection, so the
+ // blank inputChanged lands before issueSelected. It must not trigger a search
+ // that replaces the results the selection was made from.
+ await expectLogic(logic, () => {
+ logic.actions.inputChanged('')
+ logic.actions.issueSelected()
+ }).toDispatchActions(['searchIssuesSuccess'])
+ expect(mockSearchIssues).not.toHaveBeenCalled()
+ expect(logic.values.results).toEqual(RECENT_ISSUES)
+
+ // The suppression is consumed: the user clearing the search afterwards refetches.
+ await expectLogic(logic, () => {
+ logic.actions.inputChanged('')
+ }).toDispatchActions(['searchIssuesSuccess'])
+ expect(mockSearchIssues).toHaveBeenCalledTimes(1)
+ })
+
+ it('typing right after a selection does not suppress the next genuine clear', async () => {
+ // Typing within the debounce cancels the selection-clear search before it can
+ // consume the suppression, so the flag must be reset by the nonblank search.
+ await expectLogic(logic, () => {
+ logic.actions.inputChanged('')
+ logic.actions.issueSelected()
+ logic.actions.inputChanged('crash')
+ }).toDispatchActions(['searchIssuesSuccess'])
+ expect(mockSearchIssues).toHaveBeenCalledTimes(1)
+
+ await expectLogic(logic, () => {
+ logic.actions.inputChanged('')
+ }).toDispatchActions(['searchIssuesSuccess'])
+ expect(mockSearchIssues).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/products/error_tracking/frontend/components/externalIssueSearchLogic.ts b/products/error_tracking/frontend/components/externalIssueSearchLogic.ts
index 07bd6314f4fb..1cdc9ce0d297 100644
--- a/products/error_tracking/frontend/components/externalIssueSearchLogic.ts
+++ b/products/error_tracking/frontend/components/externalIssueSearchLogic.ts
@@ -67,18 +67,32 @@ export const externalIssueSearchLogic: LogicWrapper<externalIssueSearchLogicType
inputChanged: (query: string) => ({ query }),
issueSelected: true,
}),
- loaders(({ props, values }) => ({
+ loaders(({ props, values, cache }) => ({
results: [
[] as ErrorTrackingExternalIssueResultApi[],
{
searchIssues: async (query, breakpoint) => {
const search = query.trim()
+ if (search) {
+ // A nonblank search cancels any pending selection-clear search at the
+ // breakpoint below, so its suppression must not survive to swallow a
+ // later genuine clear.
+ cache.suppressNextBlankSearch = false
+ }
if (props.requiresRepository && !values.repository) {
return values.results
}
// The breakpoint debounces typing and cancels superseded searches, so a slow
// response can never overwrite the results of a newer query.
await breakpoint(300)
+ // Selecting an option makes LemonInputSelect clear its input, which is
+ // indistinguishable from the user clearing the search. The clear can land
+ // before issueSelected (option click) or after it (input blur), so the flag
+ // is only reliable here, after the debounce, not at dispatch time.
+ if (!search && cache.suppressNextBlankSearch) {
+ cache.suppressNextBlankSearch = false
+ return values.results
+ }
const response = await errorTrackingExternalReferencesSearchIssuesRetrieve(
String(teamLogic.values.currentTeamId),
{
@@ -107,20 +121,15 @@ export const externalIssueSearchLogic: LogicWrapper<externalIssueSearchLogicType
},
}),
listeners(({ actions, cache }) => ({
- setRepository: () => actions.searchIssues(''),
+ setRepository: () => {
+ // A repository change clears the results, so the reload must not be suppressed.
+ cache.suppressNextBlankSearch = false
+ actions.searchIssues('')
+ },
issueSelected: () => {
cache.suppressNextBlankSearch = true
},
- inputChanged: ({ query }) => {
- // Selecting an option makes LemonInputSelect clear its input, which is
- // indistinguishable from the user clearing a search - except that it directly
- // follows issueSelected. Only a genuine clear reloads the recent issues.
- if (!query.trim() && cache.suppressNextBlankSearch) {
- cache.suppressNextBlankSearch = false
- return
- }
- actions.searchIssues(query)
- },
+ inputChanged: ({ query }) => actions.searchIssues(query),
})),
afterMount(({ actions }) => {
actions.searchIssues('')