Skip to content

Commit

Permalink
fix(insights): fix save as with query based insight (#19325)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Dec 14, 2023
1 parent d9faf41 commit 3e9aad2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
Binary file modified frontend/__snapshots__/scenes-app-saved-insights--card-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions frontend/src/scenes/insights/InsightPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
hasDashboardItemId,
exporterResourceParams,
} = useValues(logic)
const { setInsightMetadata, saveAs } = useActions(logic)
const { setInsightMetadata } = useActions(logic)

// savedInsightsLogic
const { duplicateInsight, loadInsights } = useActions(savedInsightsLogic)

// insightDataLogic
const { query, queryChanged, showQueryEditor, hogQL } = useValues(insightDataLogic(insightProps))
const { saveInsight: saveQueryBasedInsight, toggleQueryEditorPanel } = useActions(insightDataLogic(insightProps))
const { saveInsight, saveAs, toggleQueryEditorPanel } = useActions(insightDataLogic(insightProps))

// other logics
useMountedLogic(insightCommandLogic(insightProps))
Expand Down Expand Up @@ -309,7 +309,7 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
) : (
<InsightSaveButton
saveAs={saveAs}
saveInsight={saveQueryBasedInsight}
saveInsight={saveInsight}
isSaved={hasDashboardItemId}
addingToDashboard={!!insight.dashboards?.length && !insight.id}
insightSaving={insightSaving}
Expand Down
46 changes: 44 additions & 2 deletions frontend/src/scenes/insights/insightDataLogic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { actions, connect, kea, key, listeners, path, props, propsChanged, reducers, selectors } from 'kea'
import { promptLogic } from 'lib/logic/promptLogic'
import { objectsEqual } from 'lib/utils'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
import { filterTestAccountsDefaultsLogic } from 'scenes/settings/project/filterTestAccountDefaultsLogic'
Expand Down Expand Up @@ -54,15 +55,22 @@ export const insightDataLogic = kea<insightDataLogicType>([
],
actions: [
insightLogic,
['setInsight', 'loadInsightSuccess', 'saveInsight as insightLogicSaveInsight'],
[
'setInsight',
'loadInsightSuccess',
'saveInsight as insightLogicSaveInsight',
'saveAsNamingSuccess as insightLogicSaveAsNamingSuccess',
],
dataNodeLogic({ key: insightVizDataNodeKey(props) } as DataNodeLogicProps),
['loadData', 'loadDataSuccess', 'loadDataFailure', 'setResponse as setInsightData'],
],
logic: [insightDataTimingLogic(props)],
logic: [insightDataTimingLogic(props), promptLogic({ key: `save-as-insight` })],
})),

actions({
setQuery: (query: Node | null) => ({ query }),
saveAs: true,
saveAsNamingSuccess: (name: string) => ({ name }),
saveInsight: (redirectToViewMode = true) => ({ redirectToViewMode }),
toggleQueryEditorPanel: true,
cancelChanges: true,
Expand Down Expand Up @@ -213,6 +221,40 @@ export const insightDataLogic = kea<insightDataLogicType>([

actions.insightLogicSaveInsight(redirectToViewMode)
},
saveAs: async () => {
promptLogic({ key: `save-as-insight` }).actions.prompt({
title: 'Save as new insight',
placeholder: 'Please enter the new name',
value: `${values.insight.name || values.insight.derived_name} (copy)`,
error: 'You must enter a name',
success: actions.saveAsNamingSuccess,
})
},
saveAsNamingSuccess: ({ name }) => {
let filters = values.insight.filters
if (isInsightVizNode(values.query)) {
const querySource = values.query.source
filters = queryNodeToFilter(querySource)
} else if (values.isQueryBasedInsight) {
filters = {}
}

let query = undefined
if (values.isQueryBasedInsight) {
query = values.query
}

actions.setInsight(
{
...values.insight,
filters: filters,
query: query ?? undefined,
},
{ overrideFilter: true, fromPersistentApi: false }
)

actions.insightLogicSaveAsNamingSuccess(name)
},
cancelChanges: () => {
const savedFilters = values.savedInsight.filters
const savedResult = values.savedInsight.result
Expand Down
13 changes: 1 addition & 12 deletions frontend/src/scenes/insights/insightLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { TriggerExportProps } from 'lib/components/ExportButton/exporter'
import { parseProperties } from 'lib/components/PropertyFilters/utils'
import { DashboardPrivilegeLevel } from 'lib/constants'
import { lemonToast } from 'lib/lemon-ui/lemonToast'
import { promptLogic } from 'lib/logic/promptLogic'
import { getEventNamesForAction, objectsEqual, sum, toParams } from 'lib/utils'
import { eventUsageLogic, InsightEventSource } from 'lib/utils/eventUsageLogic'
import { transformLegacyHiddenLegendKeys } from 'scenes/funnels/funnelUtils'
Expand Down Expand Up @@ -95,7 +94,7 @@ export const insightLogic = kea<insightLogicType>([
['user'],
],
actions: [tagsModel, ['loadTags']],
logic: [eventUsageLogic, dashboardsModel, promptLogic({ key: `save-as-insight` })],
logic: [eventUsageLogic, dashboardsModel],
})),

actions({
Expand All @@ -121,7 +120,6 @@ export const insightLogic = kea<insightLogicType>([
insight,
options,
}),
saveAs: true,
saveAsNamingSuccess: (name: string) => ({ name }),
cancelChanges: true,
setInsightDescription: (description: string) => ({ description }),
Expand Down Expand Up @@ -683,15 +681,6 @@ export const insightLogic = kea<insightLogicType>([
router.actions.push(urls.insightEdit(savedInsight.short_id))
}
},
saveAs: async () => {
promptLogic({ key: `save-as-insight` }).actions.prompt({
title: 'Save as new insight',
placeholder: 'Please enter the new name',
value: `${values.insight.name || values.insight.derived_name} (copy)`,
error: 'You must enter a name',
success: actions.saveAsNamingSuccess,
})
},
saveAsNamingSuccess: async ({ name }) => {
const insight: InsightModel = await api.create(`api/projects/${teamLogic.values.currentTeamId}/insights/`, {
name,
Expand Down

0 comments on commit 3e9aad2

Please sign in to comment.