: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 { CronExpressionParser } from 'cron-parser'
import { MakeLogicType, actions, connect, key, kea, listeners, path, props, reducers, selectors } from 'kea'
import api from 'lib/api'
import { describeCron } from 'lib/cron'
import { Dayjs, dayjs } from 'lib/dayjs'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { RecurrenceInterval, ScheduledChangeOperationType, ScheduledChangeType } from '~/types'
import type { TeamPublicType, TeamType } from '../../types'
import { teamLogic } from '../teamLogic'
import { featureFlagLogic, scheduleDateFromStoredISO, scheduleDateToProjectTzISO } from './featureFlagLogic'
export interface FeatureFlagScheduleEditLogicProps {
id: number | 'new' | 'link'
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagScheduleEditLogicValues {
currentTeam: TeamPublicType | TeamType | null // teamLogic
editCronExpression: string | null
editCronPreview: string | null
editEndDate: Dayjs | null
editIsRecurring: boolean
editOperationType: ScheduledChangeOperationType | null
editPayloadValue: boolean
editRecurrenceInterval: RecurrenceInterval | null
editRepeatsValue: RecurrenceInterval | 'cron' | 'none'
editSaving: boolean
editScheduledAt: Dayjs | null
editValidationErrors: Record<string, string>
editingSchedule: ScheduledChangeType | null
hasEditChanges: boolean
isEditOpen: boolean
projectTimezone: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagScheduleEditLogicActions {
closeEdit: () => {
value: true
}
openEdit: (schedule: ScheduledChangeType) => {
schedule: ScheduledChangeType
timezone: string
}
saveEdit: () => {
value: true
}
saveEditFailure: () => {
value: true
}
saveEditSuccess: () => {
value: true
}
setEditCronExpression: (cron: string | null) => {
cron: string | null
}
setEditEndDate: (date: Dayjs | null) => {
date: Dayjs | null
}
setEditIsRecurring: (isRecurring: boolean) => {
isRecurring: boolean
}
setEditPayloadValue: (value: boolean) => {
value: boolean
}
setEditRecurrenceInterval: (interval: RecurrenceInterval | null) => {
interval: RecurrenceInterval | null
}
setEditRepeatsValue: (value: RecurrenceInterval | 'cron' | 'none') => {
value: RecurrenceInterval | 'cron' | 'none'
}
setEditScheduledAt: (date: Dayjs | null) => {
date: Dayjs | null
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface featureFlagScheduleEditLogicMeta {
key: number | 'link' | 'new'
__keaTypeGenInternalSelectorTypes: {
isEditOpen: (editingSchedule: ScheduledChangeType | null) => boolean
projectTimezone: (currentTeam: TeamPublicType | TeamType | null) => string
editRepeatsValue: (
editIsRecurring: boolean,
editCronExpression: string | null,
editRecurrenceInterval: RecurrenceInterval | null
) => RecurrenceInterval | 'cron' | 'none'
editCronPreview: (editCronExpression: string | null) => string | null
editOperationType: (editingSchedule: ScheduledChangeType | null) => ScheduledChangeOperationType | null
hasEditChanges: (
editingSchedule: ScheduledChangeType | null,
editScheduledAt: Dayjs | null,
editCronExpression: string | null,
editRecurrenceInterval: RecurrenceInterval | null,
editEndDate: Dayjs | null,
editIsRecurring: boolean,
editPayloadValue: boolean,
projectTimezone: string
) => boolean
editValidationErrors: (
editScheduledAt: Dayjs | null,
editCronExpression: string | null,
editEndDate: Dayjs | null,
editIsRecurring: boolean,
projectTimezone: string
) => Record<string, string>
}
}
export type featureFlagScheduleEditLogicType = MakeLogicType<
featureFlagScheduleEditLogicValues,
featureFlagScheduleEditLogicActions,
FeatureFlagScheduleEditLogicProps,
featureFlagScheduleEditLogicMeta
>
export const featureFlagScheduleEditLogic = kea<featureFlagScheduleEditLogicType>([
path((id) => ['scenes', 'feature-flags', 'featureFlagScheduleEditLogic', id]),
props({} as FeatureFlagScheduleEditLogicProps),
key(({ id }) => id),
connect(() => ({
values: [teamLogic, ['currentTeam']],
})),
actions(({ values }) => ({
// The reducers below convert the stored dates into the project timezone, but a reducer may
// not read the store while Redux is dispatching. Resolve the timezone into the payload here,
// which runs before the dispatch starts.
openEdit: (schedule: ScheduledChangeType) => ({ schedule, timezone: values.projectTimezone }),
closeEdit: true,
setEditScheduledAt: (date: Dayjs | null) => ({ date }),
setEditCronExpression: (cron: string | null) => ({ cron }),
setEditRecurrenceInterval: (interval: RecurrenceInterval | null) => ({ interval }),
setEditEndDate: (date: Dayjs | null) => ({ date }),
setEditIsRecurring: (isRecurring: boolean) => ({ isRecurring }),
setEditPayloadValue: (value: boolean) => ({ value }),
setEditRepeatsValue: (value: RecurrenceInterval | 'none' | 'cron') => ({ value }),
saveEdit: true,
saveEditSuccess: true,
saveEditFailure: true,
})),
reducers({
editingSchedule: [
null as ScheduledChangeType | null,
{
openEdit: (_, { schedule }) => schedule,
closeEdit: () => null,
saveEditSuccess: () => null,
},
],
editScheduledAt: [
null as Dayjs | null,
{
openEdit: (_, { schedule, timezone }) =>
schedule.scheduled_at ? scheduleDateFromStoredISO(schedule.scheduled_at, timezone) : null,
setEditScheduledAt: (_, { date }) => date,
closeEdit: () => null,
},
],
editCronExpression: [
null as string | null,
{
openEdit: (_, { schedule }) => schedule.cron_expression,
setEditCronExpression: (_, { cron }) => cron,
closeEdit: () => null,
},
],
editRecurrenceInterval: [
null as RecurrenceInterval | null,
{
openEdit: (_, { schedule }) => schedule.recurrence_interval,
setEditRecurrenceInterval: (_, { interval }) => interval,
closeEdit: () => null,
},
],
editEndDate: [
null as Dayjs | null,
{
openEdit: (_, { schedule, timezone }) =>
schedule.end_date ? scheduleDateFromStoredISO(schedule.end_date, timezone) : null,
setEditEndDate: (_, { date }) => date,
closeEdit: () => null,
},
],
editIsRecurring: [
false,
{
openEdit: (_, { schedule }) =>
schedule.is_recurring || !!schedule.recurrence_interval || !!schedule.cron_expression,
setEditIsRecurring: (_, { isRecurring }) => isRecurring,
closeEdit: () => false,
},
],
editPayloadValue: [
true as boolean,
{
openEdit: (_, { schedule }) => {
if (schedule.payload.operation === ScheduledChangeOperationType.UpdateStatus) {
return schedule.payload.value
}
return true
},
setEditPayloadValue: (_, { value }) => value,
closeEdit: () => true,
},
],
editSaving: [
false,
{
saveEdit: () => true,
saveEditSuccess: () => false,
saveEditFailure: () => false,
},
],
}),
selectors({
isEditOpen: [(s) => [s.editingSchedule], (schedule: ScheduledChangeType | null): boolean => schedule !== null],
// Reactive timezone input — selectors that depend on it recompute when the project
// timezone changes, which matters if the user switches projects while the edit dialog is open.
projectTimezone: [
(s) => [s.currentTeam],
(currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType): string =>
currentTeam?.timezone || 'UTC',
],
editRepeatsValue: [
(s) => [s.editIsRecurring, s.editCronExpression, s.editRecurrenceInterval],
(
isRecurring: boolean,
cron: string | null,
interval: RecurrenceInterval | null
): RecurrenceInterval | 'none' | 'cron' =>
isRecurring ? (cron !== null ? 'cron' : (interval ?? 'none')) : 'none',
],
editCronPreview: [(s) => [s.editCronExpression], (cron: string | null): string | null => describeCron(cron)],
editOperationType: [
(s) => [s.editingSchedule],
(schedule: ScheduledChangeType | null): ScheduledChangeOperationType | null =>
schedule?.payload?.operation ?? null,
],
hasEditChanges: [
(s) => [
s.editingSchedule,
s.editScheduledAt,
s.editCronExpression,
s.editRecurrenceInterval,
s.editEndDate,
s.editIsRecurring,
s.editPayloadValue,
s.projectTimezone,
],
(
schedule: ScheduledChangeType | null,
scheduledAt: Dayjs | null,
cron: string | null,
interval: RecurrenceInterval | null,
endDate: Dayjs | null,
isRecurring: boolean,
payloadValue: boolean,
tz: string
): boolean => {
if (!schedule) {
return false
}
const origScheduledAt = schedule.scheduled_at ? dayjs(schedule.scheduled_at).toISOString() : null
const newScheduledAt = scheduledAt ? scheduleDateToProjectTzISO(scheduledAt, tz) : null
if (origScheduledAt !== newScheduledAt) {
return true
}
if (schedule.cron_expression !== cron) {
return true
}
if (schedule.recurrence_interval !== interval) {
return true
}
const origEndDate = schedule.end_date ? dayjs(schedule.end_date).toISOString() : null
const newEndDate = endDate ? endDate.tz(tz, true).endOf('day').toISOString() : null
if (origEndDate !== newEndDate) {
return true
}
// For paused schedules the effective recurring state may differ
const origIsRecurring =
schedule.is_recurring || !!schedule.recurrence_interval || !!schedule.cron_expression
if (origIsRecurring !== isRecurring) {
return true
}
if (
schedule.payload.operation === ScheduledChangeOperationType.UpdateStatus &&
schedule.payload.value !== payloadValue
) {
return true
}
return false
},
],
editValidationErrors: [
(s) => [s.editScheduledAt, s.editCronExpression, s.editEndDate, s.editIsRecurring, s.projectTimezone],
(
scheduledAt: Dayjs | null,
cron: string | null,
endDate: Dayjs | null,
isRecurring: boolean,
tz: string
): Record<string, string> => {
const errors: Record<string, string> = {}
// editScheduledAt is a browser-local Dayjs whose wall clock mirrors the project
// timezone, so we reinterpret it in project tz before comparing to "now".
const scheduledAtInstant = scheduledAt ? scheduledAt.tz(tz, true) : null
if (!scheduledAt) {
errors.scheduledAt = 'Scheduled date is required'
} else if (!isRecurring && scheduledAtInstant!.isBefore(dayjs())) {
errors.scheduledAt = 'Scheduled date must be in the future'
}
if (cron) {
const fields = cron.trim().split(/\s+/)
if (fields.length !== 5) {
errors.cronExpression = 'Only 5-field cron expressions are supported'
} else {
try {
CronExpressionParser.parse(cron)
} catch {
errors.cronExpression = 'Invalid cron expression'
}
}
} else if (isRecurring && cron !== null) {
// User is in cron mode (cron is '' rather than null) but hasn't entered an expression
errors.cronExpression = 'Enter a cron expression'
}
const normalizedEndDate = endDate ? endDate.tz(tz, true).endOf('day') : null
if (normalizedEndDate && scheduledAtInstant && normalizedEndDate.isBefore(scheduledAtInstant)) {
errors.endDate = 'End date must be after the scheduled start date'
}
return errors
},
],
}),
listeners(({ actions, values, props: logicProps }) => ({
setEditRepeatsValue: ({ value }) => {
if (value === 'none') {
actions.setEditIsRecurring(false)
actions.setEditRecurrenceInterval(null)
actions.setEditCronExpression(null)
actions.setEditEndDate(null)
} else if (value === 'cron') {
actions.setEditIsRecurring(true)
actions.setEditRecurrenceInterval(null)
actions.setEditCronExpression(values.editCronExpression ?? '')
} else {
actions.setEditIsRecurring(true)
actions.setEditRecurrenceInterval(value)
actions.setEditCronExpression(null)
}
},
setEditCronExpression: ({ cron }) => {
if (!cron) {
return
}
const fields = cron.trim().split(/\s+/)
if (fields.length !== 5) {
return
}
try {
const timezone = values.projectTimezone
// Use the later of now or the existing date so that paused/old
// schedules don't snap to an already-elapsed time. The existing
// date is a browser-local Dayjs whose wall clock mirrors the
// project timezone — reinterpret it in project tz to get the real instant.
const now = new Date()
const existing = values.editScheduledAt?.tz(timezone, true).toDate()
const currentDate = existing && existing > now ? existing : now
const interval = CronExpressionParser.parse(cron, { currentDate, tz: timezone })
const nextDate = interval.next().toDate()
actions.setEditScheduledAt(scheduleDateFromStoredISO(nextDate.toISOString(), timezone))
} catch {
// Invalid — don't update
}
},
saveEdit: async () => {
const { editingSchedule, editValidationErrors } = values
if (!editingSchedule || Object.keys(editValidationErrors).length > 0) {
if (Object.keys(editValidationErrors).length > 0) {
const firstError = Object.values(editValidationErrors)[0] as string
lemonToast.error(firstError)
}
actions.saveEditFailure()
return
}
const patch: Record<string, unknown> = {}
const timezone = values.projectTimezone
// Compare and build patch with only changed fields. The calendar emits browser-local
// Dayjs values whose wall clock matches the project timezone; convert them back to a
// project-timezone UTC ISO string before sending.
const newScheduledAt = values.editScheduledAt
? scheduleDateToProjectTzISO(values.editScheduledAt, timezone)
: null
const origScheduledAt = editingSchedule.scheduled_at
? dayjs(editingSchedule.scheduled_at).toISOString()
: null
if (newScheduledAt !== origScheduledAt) {
patch.scheduled_at = newScheduledAt
}
if (editingSchedule.cron_expression !== values.editCronExpression) {
patch.cron_expression = values.editCronExpression
}
if (editingSchedule.recurrence_interval !== values.editRecurrenceInterval) {
patch.recurrence_interval = values.editRecurrenceInterval
}
const newEndDate = values.editEndDate
? values.editEndDate.tz(timezone, true).endOf('day').toISOString()
: null
const origEndDate = editingSchedule.end_date ? dayjs(editingSchedule.end_date).toISOString() : null
if (newEndDate !== origEndDate) {
patch.end_date = newEndDate
}
// Use the same derived comparison as hasEditChanges so that
// editing a paused schedule doesn't silently unpause it.
const origIsRecurring =
editingSchedule.is_recurring ||
!!editingSchedule.recurrence_interval ||
!!editingSchedule.cron_expression
if (origIsRecurring !== values.editIsRecurring) {
patch.is_recurring = values.editIsRecurring
}
if (
editingSchedule.payload.operation === ScheduledChangeOperationType.UpdateStatus &&
editingSchedule.payload.value !== values.editPayloadValue
) {
patch.payload = {
operation: ScheduledChangeOperationType.UpdateStatus,
value: values.editPayloadValue,
}
}
if (Object.keys(patch).length === 0) {
lemonToast.info('No changes to save')
actions.closeEdit()
return
}
try {
await api.featureFlags.updateScheduledChange(editingSchedule.team_id, editingSchedule.id, patch)
lemonToast.success('Schedule updated')
actions.saveEditSuccess()
// Reload the schedule list in the parent logic
featureFlagLogic.findMounted({ id: logicProps.id })?.actions.loadScheduledChanges()
} catch {
lemonToast.error('Failed to update schedule')
actions.saveEditFailure()
}
},
})),
])