: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, actions, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { actionToUrl, urlToAction } from 'kea-router'
import api, { CountedPaginatedResponse } from 'lib/api'
import { objectsEqual } from 'lib/utils/objects'
import { toParams } from 'lib/utils/url'
import { DataManagementTab } from 'scenes/data-management/DataManagementScene'
import { urls } from 'scenes/urls'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { ActionType, ActivityScope, Breadcrumb } from '~/types'
export const ACTIONS_PER_PAGE = 50
export type ActionsResponse = CountedPaginatedResponse<ActionType>
export interface ActionsFilters {
createdBy: number[]
tags: string[]
ordering: string
}
const DEFAULT_FILTERS: ActionsFilters = {
createdBy: [],
tags: [],
ordering: '-created_by',
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicValues {
actionCount: number
actionsList: ActionType[]
actionsResponse: ActionsResponse
actionsResponseLoading: boolean
apiParams: string
breadcrumbs: Breadcrumb[]
filters: ActionsFilters
hasActiveFilters: boolean
page: number
searchTerm: string
shouldShowEmptyState: boolean
sidePanelContext: SidePanelSceneContext
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicActions {
loadActions: () => any
loadActionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadActionsSuccess: (
actionsResponse: {
count: number
results: ActionType[]
},
payload?: any
) => {
actionsResponse: {
count: number
results: ActionType[]
}
payload?: any
}
pinAction: (action: ActionType) => ActionType
pinActionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
pinActionSuccess: (
actionsResponse: {
count: number
next?: string | null | undefined
previous?: string | null | undefined
results: ActionType[]
},
payload?: ActionType
) => {
actionsResponse: {
count: number
next?: string | null | undefined
previous?: string | null | undefined
results: ActionType[]
}
payload?: ActionType
}
setFilters: (filters: Partial<ActionsFilters>) => {
filters: Partial<ActionsFilters>
}
setPage: (page: number) => {
page: number
}
setSearchTerm: (searchTerm: string) => {
searchTerm: string
}
unpinAction: (action: ActionType) => ActionType
unpinActionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
unpinActionSuccess: (
actionsResponse: {
count: number
next?: string | null | undefined
previous?: string | null | undefined
results: ActionType[]
},
payload?: ActionType
) => {
actionsResponse: {
count: number
next?: string | null | undefined
previous?: string | null | undefined
results: ActionType[]
}
payload?: ActionType
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
actionsList: (actionsResponse: ActionsResponse) => ActionType[]
actionCount: (actionsResponse: ActionsResponse) => number
apiParams: (searchTerm: string, filters: ActionsFilters, page: number) => string
hasActiveFilters: (searchTerm: string, filters: ActionsFilters) => boolean
shouldShowEmptyState: (
actionCount: number,
actionsResponseLoading: boolean,
hasActiveFilters: boolean
) => boolean
}
}
export type actionsLogicType = MakeLogicType<
actionsLogicValues,
actionsLogicActions,
Record<string, any>,
actionsLogicMeta
>
export const actionsLogic = kea<actionsLogicType>([
path(['products', 'actions', 'actionsLogic']),
actions({
setSearchTerm: (searchTerm: string) => ({ searchTerm }),
setFilters: (filters: Partial<ActionsFilters>) => ({ filters }),
setPage: (page: number) => ({ page }),
}),
reducers({
searchTerm: [
'',
{
setSearchTerm: (_, { searchTerm }) => searchTerm,
},
],
filters: [
DEFAULT_FILTERS,
{
setFilters: (state, { filters }) => ({ ...state, ...filters }),
},
],
page: [
1,
{
setPage: (_, { page }) => page,
setFilters: () => 1,
setSearchTerm: () => 1,
},
],
}),
loaders(({ values, actions }) => ({
actionsResponse: [
{ count: 0, results: [] } as ActionsResponse,
{
loadActions: async () => {
const response = await api.actions.list(values.apiParams)
return { count: response.count ?? 0, results: response.results ?? [] }
},
pinAction: async (action: ActionType) => {
const updated = await api.actions.update(action.id, {
pinned_at: new Date().toISOString(),
})
// Patch the row for instant icon feedback, then reload so server-side
// ordering (e.g. by pinned_at) reflects the change.
actions.loadActions()
return {
...values.actionsResponse,
results: values.actionsResponse.results.map((a) => (a.id === updated.id ? updated : a)),
}
},
unpinAction: async (action: ActionType) => {
const updated = await api.actions.update(action.id, {
pinned_at: null,
})
actions.loadActions()
return {
...values.actionsResponse,
results: values.actionsResponse.results.map((a) => (a.id === updated.id ? updated : a)),
}
},
},
],
})),
selectors({
actionsList: [(s) => [s.actionsResponse], (response: ActionsResponse): ActionType[] => response.results],
actionCount: [(s) => [s.actionsResponse], (response: ActionsResponse): number => response.count],
apiParams: [
(s) => [s.searchTerm, s.filters, s.page],
(searchTerm: string, filters: ActionsFilters, page: number): string => {
const params: Record<string, any> = {
limit: ACTIONS_PER_PAGE,
offset: (page - 1) * ACTIONS_PER_PAGE,
ordering: filters.ordering,
include_count: 1,
}
if (searchTerm.trim()) {
params.search = searchTerm.trim()
}
if (filters.createdBy.length > 0) {
params.created_by = filters.createdBy.join(',')
}
if (filters.tags.length > 0) {
params.tags = JSON.stringify(filters.tags)
}
return toParams(params)
},
],
hasActiveFilters: [
(s) => [s.searchTerm, s.filters],
(searchTerm: string, filters: ActionsFilters): boolean =>
!!searchTerm.trim() || filters.createdBy.length > 0 || filters.tags.length > 0,
],
shouldShowEmptyState: [
(s) => [s.actionCount, s.actionsResponseLoading, s.hasActiveFilters],
(actionCount: number, actionsResponseLoading: boolean, hasActiveFilters: boolean): boolean =>
actionCount === 0 && !actionsResponseLoading && !hasActiveFilters,
],
breadcrumbs: [
() => [],
(): Breadcrumb[] => [
{
key: DataManagementTab.Actions,
name: 'Actions',
path: urls.actions(),
iconType: 'action',
},
],
],
[SIDE_PANEL_CONTEXT_KEY]: [
() => [],
(): SidePanelSceneContext => ({
activity_scope: ActivityScope.ACTION,
}),
],
}),
listeners(({ actions }) => ({
setFilters: () => actions.loadActions(),
setPage: () => actions.loadActions(),
setSearchTerm: async (_, breakpoint) => {
await breakpoint(300)
actions.loadActions()
},
})),
urlToAction(({ actions, values }) => ({
[urls.actions()]: (_, searchParams) => {
const urlFilters: ActionsFilters = {
...DEFAULT_FILTERS,
...(searchParams.ordering !== undefined && { ordering: searchParams.ordering }),
...(Array.isArray(searchParams.tags) && { tags: searchParams.tags }),
...(Array.isArray(searchParams.created_by) && { createdBy: searchParams.created_by.map(Number) }),
}
if (!objectsEqual(values.filters, urlFilters)) {
actions.setFilters(urlFilters)
} else if (!values.actionsResponse.results.length && !values.actionsResponseLoading) {
actions.loadActions()
}
},
})),
// Search term and page are intentionally not URL-synced; only the shareable filters are.
actionToUrl(({ values }) => {
const buildUrl = (): [string, Record<string, any>] => [
urls.actions(),
{
...(values.filters.ordering !== DEFAULT_FILTERS.ordering && { ordering: values.filters.ordering }),
...(values.filters.tags.length > 0 && { tags: values.filters.tags }),
...(values.filters.createdBy.length > 0 && { created_by: values.filters.createdBy }),
},
]
return {
setFilters: buildUrl,
}
}),
])