: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
import { MakeLogicType, connect, kea, key, listeners, path, props } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
import { dashboardsModel } from '~/models/dashboardsModel'
import { DashboardTileIdOrNew, DashboardType, NewDashboardTile } from '~/types'
export interface ButtonTileForm {
url: string
text: string
placement: 'left' | 'right'
style: 'primary' | 'secondary'
transparent_background: boolean
}
export interface ButtonTileCardModalProps {
dashboard: DashboardType
buttonTileId: DashboardTileIdOrNew
onClose: () => void
/** Creates the new tile. Without it, the tile is created on the dashboard as is. */
onCreateTile?: (tile: NewDashboardTile) => void
}
const getExistingButtonTile = (dashboard: DashboardType, buttonTileId: number): ButtonTileForm => {
const tile = dashboard.tiles?.find((t) => t.id === buttonTileId)
if (tile?.button_tile) {
return {
url: tile.button_tile.url,
text: tile.button_tile.text,
placement: tile.button_tile.placement,
style: tile.button_tile.style,
transparent_background: tile.transparent_background ?? false,
}
}
return { url: '', text: '', placement: 'left', style: 'primary', transparent_background: false }
}
const ALLOWED_URL_PROTOCOLS = ['http:', 'https:']
const isValidUrl = (value: string): boolean => {
try {
const url = new URL(value)
return ALLOWED_URL_PROTOCOLS.includes(url.protocol)
} catch {
return false
}
}
const isValidPathname = (value: string): boolean => {
return /^\/[a-zA-Z0-9._~:/?#[\]@!$&'()*+,;=%-]*$/.test(value)
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface buttonTileCardModalLogicValues {
buttonTile: ButtonTileForm
buttonTileAllErrors: Record<string, any>
buttonTileChanged: boolean
buttonTileErrors: DeepPartialMap<ButtonTileForm, ValidationErrorType>
buttonTileHasErrors: boolean
buttonTileManualErrors: Record<string, any>
buttonTileTouched: boolean
buttonTileTouches: Record<string, boolean>
buttonTileValidationErrors: DeepPartialMap<ButtonTileForm, ValidationErrorType>
isButtonTileSubmitting: boolean
isButtonTileValid: boolean
showButtonTileErrors: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface buttonTileCardModalLogicActions {
updateDashboard: (args_0: any) => any // dashboardsModel
resetButtonTile: (values?: ButtonTileForm) => {
values?: ButtonTileForm
}
setButtonTileManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setButtonTileValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setButtonTileValues: (values: DeepPartial<ButtonTileForm>) => {
values: DeepPartial<ButtonTileForm>
}
submitButtonTile: () => {
value: boolean
}
submitButtonTileFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitButtonTileRequest: (buttonTile: ButtonTileForm) => {
buttonTile: ButtonTileForm
}
submitButtonTileSuccess: (buttonTile: ButtonTileForm) => {
buttonTile: ButtonTileForm
}
touchButtonTileField: (key: string) => {
key: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface buttonTileCardModalLogicMeta {
key: string
}
export type buttonTileCardModalLogicType = MakeLogicType<
buttonTileCardModalLogicValues,
buttonTileCardModalLogicActions,
ButtonTileCardModalProps,
buttonTileCardModalLogicMeta
>
export const buttonTileCardModalLogic = kea<buttonTileCardModalLogicType>([
path(['scenes', 'dashboard', 'buttonTileCardModal', 'logic']),
props({} as ButtonTileCardModalProps),
key((props) => `buttonTileCardModalLogic-${props.dashboard.id}-${props.buttonTileId}`),
connect(() => ({ actions: [dashboardsModel, ['updateDashboard']] })),
listeners(({ props, actions }) => ({
submitButtonTileFailure: (error) => {
if (props.dashboard && props.buttonTileId) {
lemonToast.error(`Could not save button: ${error.error} (${JSON.stringify(error.errors)})`)
}
actions.resetButtonTile()
props?.onClose?.()
},
submitButtonTileSuccess: ({ buttonTile }: { buttonTile: ButtonTileForm }) => {
actions.resetButtonTile()
props?.onClose?.()
posthog.capture('dashboard button tile saved', {
dashboard_id: props.dashboard.id,
button_tile_id: props.buttonTileId,
is_new: props.buttonTileId === null,
url_type: buttonTile.url.startsWith('/') ? 'pathname' : 'full_url',
})
},
})),
forms(({ props, actions }) => ({
buttonTile: {
defaults: (props.buttonTileId !== null
? getExistingButtonTile(props.dashboard, props.buttonTileId)
: {
url: '',
text: '',
placement: 'left',
style: 'primary',
transparent_background: false,
}) as ButtonTileForm,
errors: ({ url, text }) => ({
url: !url
? 'URL is required'
: url.startsWith('/')
? !isValidPathname(url)
? 'Must be a valid pathname starting with /'
: null
: !isValidUrl(url)
? 'Must be a valid URL (e.g. https://example.com) or a pathname starting with /'
: null,
text: !text ? 'Button text is required' : null,
}),
submit: (formValues) => {
const { transparent_background, ...buttonTileFields } = formValues
const tiles = (props.dashboard.tiles || []).map((t) => ({
id: t.id,
button_tile: t.button_tile,
transparent_background: t.transparent_background,
}))
if (props.buttonTileId === null) {
const tile = { button_tile: buttonTileFields, transparent_background }
if (props.onCreateTile) {
props.onCreateTile(tile)
} else {
actions.updateDashboard({ id: props.dashboard.id, tiles: [tile] })
}
} else {
const updatedTiles = tiles.map((tile) => {
if (tile.id === props.buttonTileId && tile.button_tile) {
return {
...tile,
button_tile: { ...tile.button_tile, ...buttonTileFields },
transparent_background,
}
}
return tile
})
actions.updateDashboard({ id: props.dashboard.id, tiles: updatedTiles })
}
},
},
})),
])