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.

5.4 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { LemonButton, LemonModal } from '@posthog/lemon-ui'

import { LemonRadio, LemonRadioOption } from 'lib/lemon-ui/LemonRadio'

import { deleteDisabledReason, isDeprecatePayload, noReadsLabel, refreshCostLabel } from '../suggestionCopy'
import { catalogEditBlockedReason, warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'

type RefreshChoice = 'stop' | 'keep'
type ActionChoice = 'deprecate' | 'delete'

const ACTION_CHOICES: LemonRadioOption<ActionChoice>[] = [
    {
        value: 'deprecate',
        label: 'Mark it deprecated',
        description: 'It stays in the data catalog with a deprecated label, so people know not to use it.',
        'data-attr': 'warehouse-suggestions-deprecate-mark',
    },
    {
        value: 'delete',
        label: 'Delete it',
        description: "Deletes the view and its stored table. You can't undo this.",
        'data-attr': 'warehouse-suggestions-deprecate-delete',
    },
]

const REFRESH_CHOICES: LemonRadioOption<RefreshChoice>[] = [
    {
        value: 'stop',
        label: 'Stop refreshing it',
        description:
            'Deletes its stored table and stops its refreshes, if it still has them. PostHog keeps them when another view or a sync reads it. You can materialize it again later.',
        'data-attr': 'warehouse-suggestions-deprecate-stop-refreshing',
    },
    {
        value: 'keep',
        label: 'Keep refreshing it',
        description: 'It stays materialized, and its refreshes keep their cost.',
        'data-attr': 'warehouse-suggestions-deprecate-keep-refreshing',
    },
]

export function DeprecateSuggestionModal(): JSX.Element | null {
    const { acceptConfirmSuggestion, actionsInFlight, deleteView, keepMaterialized, status } =
        useValues(warehouseSuggestionsLogic)
    const { confirmAccept, closeAcceptConfirm, setDeleteView, setKeepMaterialized } =
        useActions(warehouseSuggestionsLogic)
    const payload = acceptConfirmSuggestion?.payload

    if (!acceptConfirmSuggestion || !payload || !isDeprecatePayload(payload)) {
        return null
    }
    const inFlight = !!actionsInFlight[acceptConfirmSuggestion.id]
    const materialized = payload.materialized !== false
    const windowDays = status?.window_days ?? 0
    const noReads = noReadsLabel(windowDays)
    const blockedReasons: Record<ActionChoice, string | undefined> = {
        deprecate: catalogEditBlockedReason(),
        delete: deleteDisabledReason(acceptConfirmSuggestion.evidence, windowDays),
    }
    const actionChoices = ACTION_CHOICES.map((choice) => ({ ...choice, disabledReason: blockedReasons[choice.value] }))
    const chosen: ActionChoice = deleteView ? 'delete' : 'deprecate'

    return (
        <LemonModal
            isOpen
            closable={!inFlight}
            onClose={closeAcceptConfirm}
            title={deleteView ? `Delete ${payload.subject_name}?` : `Mark ${payload.subject_name} as deprecated?`}
            footer={
                <>
                    <LemonButton
                        type="secondary"
                        onClick={closeAcceptConfirm}
                        disabledReason={inFlight ? 'Working' : undefined}
                    >
                        Cancel
                    </LemonButton>
                    <LemonButton
                        type="primary"
                        status={deleteView ? 'danger' : undefined}
                        loading={inFlight}
                        disabledReason={blockedReasons[chosen]}
                        onClick={() => confirmAccept()}
                        data-attr={
                            deleteView
                                ? 'warehouse-suggestions-delete-confirm'
                                : 'warehouse-suggestions-deprecate-confirm'
                        }
                    >
                        {deleteView ? 'Delete view' : 'Mark deprecated'}
                    </LemonButton>
                </>
            }
        >
            <div className="flex max-w-xl flex-col gap-4">
                <p className="m-0 text-sm text-secondary">
                    {materialized ? `${noReads}. ${refreshCostLabel(payload)}.` : `${noReads}.`}
                </p>
                <fieldset disabled={inFlight} className="flex flex-col gap-2">
                    <h4 className="m-0 text-sm font-semibold">What to do</h4>
                    <LemonRadio
                        aria-label="What to do"
                        value={chosen}
                        onChange={(choice) => setDeleteView(choice === 'delete')}
                        options={actionChoices}
                        radioPosition="top"
                    />
                </fieldset>
                {materialized && !deleteView && (
                    <fieldset disabled={inFlight} className="flex flex-col gap-2">
                        <h4 className="m-0 text-sm font-semibold">Its materialization</h4>
                        <LemonRadio
                            aria-label="Its materialization"
                            value={keepMaterialized ? 'keep' : 'stop'}
                            onChange={(choice) => setKeepMaterialized(choice === 'keep')}
                            options={REFRESH_CHOICES}
                            radioPosition="top"
                        />
                    </fieldset>
                )}
            </div>
        </LemonModal>
    )
}