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(dashboards): optimistically remove tiles on delete (#63614)

Matt Pua72a78cdffae5
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index cea344cac332..ee708c1166e5 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -1549,6 +1549,32 @@ describe('dashboardLogic', () => {
const { container } = render(toastContent)
expect(container.textContent).toBe('Text card has been removed from the dashboard')
})
+
+ it('removes the tile from state optimistically before the API call resolves', () => {
+ expect(logic.values.textTiles).toHaveLength(1)
+
+ // Dispatch without awaiting listeners — the reducer drops the tile synchronously.
+ logic.actions.removeTile(TEXT_TILE)
+
+ expect(logic.values.textTiles).toEqual([])
+ })
+
+ it('restores the tile and suppresses the undo toast when the API call fails', async () => {
+ const updateSpy = jest.spyOn(api, 'update').mockRejectedValueOnce(new Error('boom'))
+ const lemonToastErrorSpy = jest.spyOn(lemonToast, 'error').mockImplementation(() => 'toast-id')
+
+ await expectLogic(logic, () => {
+ logic.actions.removeTile(TEXT_TILE)
+ }).toFinishAllListeners()
+
+ // Tile is back in place, the error toast fired, and no undo toast was shown.
+ expect(logic.values.textTiles).toHaveLength(1)
+ expect(lemonToastErrorSpy).toHaveBeenCalled()
+ expect(lemonToastInfoSpy).not.toHaveBeenCalled()
+
+ updateSpy.mockRestore()
+ lemonToastErrorSpy.mockRestore()
+ })
})
describe('widget tiles', () => {
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index b2d5db94c953..398424a2158d 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -560,6 +560,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
}
},
removeTile: async ({ tile }) => {
+ // The reducer drops the tile optimistically; here we only persist and roll back on failure.
try {
await api.update(`api/environments/${values.currentTeamId}/dashboards/${props.id}`, {
tiles: [{ id: tile.id, deleted: true }],
@@ -569,13 +570,15 @@ export const dashboardLogic = kea<dashboardLogicType>([
dashboardId: props.id,
})
+ return values.dashboard
+ } catch (e) {
+ lemonToast.error('Could not remove tile from dashboard: ' + String(e))
+ // Re-insert the tile (its layout puts it back in place) and suppress the undo toast.
+ cache.removedTileForUndo = undefined
return {
...values.dashboard,
- tiles: values.tiles.filter((t) => t.id !== tile.id),
+ tiles: [...(values.tiles || []), tile],
} as DashboardType<QueryBasedInsightModel>
- } catch (e) {
- lemonToast.error('Could not remove tile from dashboard: ' + String(e))
- return values.dashboard
}
},
setDashboardMode: async ({ mode, source }) => {
@@ -878,6 +881,13 @@ export const dashboardLogic = kea<dashboardLogicType>([
tiles: state?.tiles?.map((tile) => (tile.id === tileId ? { ...tile, ...properties } : tile)),
} as DashboardType<QueryBasedInsightModel>
},
+ removeTile: (state, { tile }) => {
+ // Optimistically drop the tile so the grid reflows immediately; the loader rolls back on failure.
+ return {
+ ...state,
+ tiles: state?.tiles?.filter((t) => t.id !== tile.id),
+ } as DashboardType<QueryBasedInsightModel>
+ },
[dashboardsModel.actionTypes.tileMovedToDashboard]: (state, { tile, dashboardId }) => {
if (state?.id === dashboardId) {
return {