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(experiments): Add delete action to the shared metric form (#84027)

Juraj Majerik0ed5f9f37972
diff --git a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
index 0e1173d14501..f45ff30e6f1d 100644
--- a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
+++ b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
@@ -7,6 +7,7 @@ import { AccessControlAction } from 'lib/components/AccessControlAction'
import { SceneMenuBarFileItems } from 'lib/components/Scenes/SceneMenuBarFileItems'
import { SceneTags } from 'lib/components/Scenes/SceneTags'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
+import { More } from 'lib/lemon-ui/LemonButton/More'
import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'
import { SceneExport } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
@@ -45,6 +46,24 @@ export const scene: SceneExport<SharedMetricLogicProps> = {
}),
}
+function openDeleteSharedMetricDialog(onDelete: () => void): void {
+ LemonDialog.open({
+ title: 'Delete this metric?',
+ content: <div className="text-sm text-secondary">This action cannot be undone.</div>,
+ primaryButton: {
+ children: 'Delete',
+ type: 'primary',
+ onClick: onDelete,
+ size: 'small',
+ },
+ secondaryButton: {
+ children: 'Cancel',
+ type: 'tertiary',
+ size: 'small',
+ },
+ })
+}
+
export function SharedMetric(): JSX.Element {
const { sharedMetric, action } = useValues(sharedMetricLogic)
const sceneMenuBarEnabled = useFeatureFlag('SCENE_MENU_BAR')
@@ -128,27 +147,7 @@ export function SharedMetric(): JSX.Element {
variant="destructive"
opensFloatingUi
disabled={!!disabledReason}
- onClick={() => {
- LemonDialog.open({
- title: 'Delete this metric?',
- content: (
- <div className="text-sm text-secondary">
- This action cannot be undone.
- </div>
- ),
- primaryButton: {
- children: 'Delete',
- type: 'primary',
- onClick: () => deleteSharedMetric(),
- size: 'small',
- },
- secondaryButton: {
- children: 'Cancel',
- type: 'tertiary',
- size: 'small',
- },
- })
- }}
+ onClick={() => openDeleteSharedMetricDialog(deleteSharedMetric)}
data-attr="shared-metric-menubar-delete"
>
<IconTrash />
@@ -187,25 +186,7 @@ export function SharedMetric(): JSX.Element {
<ButtonPrimitive
variant="danger"
menuItem
- onClick={() => {
- LemonDialog.open({
- title: 'Delete this metric?',
- content: (
- <div className="text-sm text-secondary">This action cannot be undone.</div>
- ),
- primaryButton: {
- children: 'Delete',
- type: 'primary',
- onClick: () => deleteSharedMetric(),
- size: 'small',
- },
- secondaryButton: {
- children: 'Cancel',
- type: 'tertiary',
- size: 'small',
- },
- })
- }}
+ onClick={() => openDeleteSharedMetricDialog(deleteSharedMetric)}
>
<IconTrash /> Delete
</ButtonPrimitive>
@@ -236,27 +217,51 @@ export function SharedMetric(): JSX.Element {
key: ExperimentsTabs.SharedMetrics,
}}
actions={
- <AccessControlAction
- resourceType={AccessControlResourceType.ExperimentSavedMetric}
- minAccessLevel={AccessControlLevel.Editor}
- userAccessLevel={sharedMetric.user_access_level}
- >
- <LemonButton
- disabledReason={sharedMetric.name ? undefined : 'You must give your metric a name'}
- size="small"
- type="primary"
- onClick={() => {
- if (['create', 'duplicate'].includes(action)) {
- createSharedMetric()
- return
+ <>
+ {action === 'update' && (
+ <More
+ overlay={
+ <AccessControlAction
+ resourceType={AccessControlResourceType.ExperimentSavedMetric}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={sharedMetric.user_access_level}
+ >
+ <LemonButton
+ fullWidth
+ size="small"
+ icon={<IconTrash />}
+ status="danger"
+ data-attr="shared-metric-delete"
+ onClick={() => openDeleteSharedMetricDialog(deleteSharedMetric)}
+ >
+ Delete
+ </LemonButton>
+ </AccessControlAction>
}
-
- updateSharedMetric()
- }}
+ />
+ )}
+ <AccessControlAction
+ resourceType={AccessControlResourceType.ExperimentSavedMetric}
+ minAccessLevel={AccessControlLevel.Editor}
+ userAccessLevel={sharedMetric.user_access_level}
>
- Save
- </LemonButton>
- </AccessControlAction>
+ <LemonButton
+ disabledReason={sharedMetric.name ? undefined : 'You must give your metric a name'}
+ size="small"
+ type="primary"
+ onClick={() => {
+ if (['create', 'duplicate'].includes(action)) {
+ createSharedMetric()
+ return
+ }
+
+ updateSharedMetric()
+ }}
+ >
+ Save
+ </LemonButton>
+ </AccessControlAction>
+ </>
}
/>
@@ -278,32 +283,6 @@ export function SharedMetric(): JSX.Element {
<LegacySharedFunnelsMetricForm />
)}
<div className="flex justify-between">
- {/* {action === 'update' && (
- <LemonButton
- size="medium"
- type="primary"
- status="danger"
- onClick={() => {
- LemonDialog.open({
- title: 'Delete this metric?',
- content: <div className="text-sm text-secondary">This action cannot be undone.</div>,
- primaryButton: {
- children: 'Delete',
- type: 'primary',
- onClick: () => deleteSharedMetric(),
- size: 'small',
- },
- secondaryButton: {
- children: 'Cancel',
- type: 'tertiary',
- size: 'small',
- },
- })
- }}
- >
- Delete
- </LemonButton>
- )} */}
<AccessControlAction
resourceType={AccessControlResourceType.ExperimentSavedMetric}
minAccessLevel={AccessControlLevel.Editor}