: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(notebooks): stabilize widget insertion flow (#95105)
Marius Andra4094fa2ef3a4
diff --git a/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx b/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsxindex 3035323f94cf..24573a4c2d9c 100644--- a/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx+++ b/frontend/src/lib/components/MarkdownNotebook/InsertMenu.tsx@@ -182,11 +182,11 @@ export function renderHighlightedInsertCommandLabel(label: string, query: stringexport function getFilteredInsertCommands(commands: InsertCommand[], query: string): InsertCommand[] {const normalizedQuery = query.trim().toLowerCase()- if (!normalizedQuery) {- return commands- }+ const filteredCommands = normalizedQuery+ ? commands.filter((command) => getInsertCommandSearchText(command).includes(normalizedQuery))+ : commands- return commands.filter((command) => getInsertCommandSearchText(command).includes(normalizedQuery))+ return Object.values(groupInsertCommandsByCategory(filteredCommands)).flat()}export function getInsertCommandSearchText(command: InsertCommand): string {diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.tsindex 06e4dbe37da2..e93e6d1571bf 100644--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts@@ -3481,7 +3481,23 @@ ${queryMarkdown}`)it('moves slash menu selection with arrow keys', () => {const onChange = jest.fn()- const { container } = render(createElement(MarkdownNotebook, { value: withNotebookTitle(' '), onChange }))+ const registry = createMarkdownNotebookRegistry([+ {+ tagName: 'Widget',+ label: 'Widget',+ category: 'Code',+ insertCommand: { category: 'Common' },+ ViewComponent: () => createElement('div'),+ },+ ])+ const { container } = render(+ createElement(MarkdownNotebook, {+ value: withNotebookTitle(' '),+ registry,+ onAskAI: jest.fn(),+ onChange,+ })+ )const row = getBodyTextBlock(container).closest('.MarkdownNotebook__row')expect(row).toBeInstanceOf(HTMLElement)@@ -3492,9 +3508,9 @@ ${queryMarkdown}`)const getSelectedLabel = (): string | null =>container.querySelector('.MarkdownNotebook__insert-item[aria-selected="true"]')?.textContent ?? null- expect(getSelectedLabel()).toEqual('Text')+ expect(getSelectedLabel()).toEqual('Ask AI')- for (const label of ['SQL', 'Trend', 'Funnel']) {+ for (const label of ['Text', 'SQL', 'Widget', 'Trend', 'Funnel']) {fireEvent.keyDown(textBlock, { key: 'ArrowDown' })expect(getSelectedLabel()).toEqual(label)}diff --git a/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.test.ts b/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.test.tsindex b7131fab99ca..eaa835f036d2 100644--- a/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.test.ts+++ b/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.test.ts@@ -105,6 +105,25 @@ describe('notebookNodeGeneratedWidgetLogic', () => {expect(notebooksWidgetGenerate).not.toHaveBeenCalled()})+ it('persists a newly inserted widget before retrying its initial status', async () => {+ jest.mocked(notebooksWidgetStatus)+ .mockRejectedValueOnce(+ new ApiError('Not found', 404, undefined, {+ code: 'node_not_found',+ detail: 'This generated widget is no longer in the notebook.',+ })+ )+ .mockResolvedValueOnce(status())+ logic = notebookNodeGeneratedWidgetLogic(props)+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()++ expect(props.persistNotebook).toHaveBeenCalledTimes(1)+ expect(notebooksWidgetStatus).toHaveBeenCalledTimes(2)+ expect(logic.values.status?.lifecycle_status).toBe('awaiting_generation')+ expect(logic.values.statusLoadError).toBeNull()+ })+it('times out a status request instead of leaving the widget loading forever', async () => {jest.useFakeTimers()jest.mocked(notebooksWidgetStatus).mockImplementation(diff --git a/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.ts b/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.tsindex dcc5d03e6394..06211daaa78d 100644--- a/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.ts+++ b/products/notebooks/frontend/NotebookNodeGeneratedWidget/notebookNodeGeneratedWidgetLogic.ts@@ -1088,11 +1088,22 @@ export const notebookNodeGeneratedWidgetLogic: LogicWrapper<notebookNodeGenerateconst requestId = nextStatusRequestId()actions.statusRequestStarted()try {- const loadedStatus = await requestWithTimeout((signal) =>- notebooksWidgetStatus(String(props.projectId), props.notebookShortId, props.nodeId, {- signal,- })- )+ const requestStatus = async (): Promise<WidgetStatusApi> =>+ await requestWithTimeout((signal) =>+ notebooksWidgetStatus(String(props.projectId), props.notebookShortId, props.nodeId, {+ signal,+ })+ )+ let loadedStatus: WidgetStatusApi+ try {+ loadedStatus = await requestStatus()+ } catch (error) {+ if (!props.isEditable || !isMissingNodeError(error)) {+ throw error+ }+ await props.persistNotebook()+ loadedStatus = await requestStatus()+ }if (isCurrentStatusRequest(requestId)) {actions.statusReceived(loadedStatus)}