: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 { LogicWrapper, MakeLogicType, actions, connect, kea, listeners, path, props, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { lemonToast } from '@posthog/lemon-ui'
import type { PaginatedResponse } from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { ExternalDataSource, IntegrationType } from '~/types'
import { captureSignalSourceFiltersChanged } from '../inboxAnalytics'
import { signalSourcesLogic } from '../signalSourcesLogic'
import {
SOURCE_LINEAR_TEAM_IDS_KEY,
SignalSourceConfig,
SignalSourceProduct,
SignalSourceType,
ToggleSignalSourceParams,
linearTeamIdsFromConfig,
} from '../types'
export type LinearTeamsScope = 'all' | 'selected'
export interface LinearTeamsFormValues {
scope: LinearTeamsScope
teamIds: string[]
}
export interface LinearTeamsModalLogicProps {
/** The Linear source row, or null before the source was ever turned on. */
config: SignalSourceConfig | null
/** Saving from the enable flow turns the source on; saving from Filters leaves `enabled` as it is. */
enableOnSave: boolean
viaSetupWizard: boolean
onClose: () => void
}
function formDefaults(config: SignalSourceConfig | null): LinearTeamsFormValues {
const teamIds = linearTeamIdsFromConfig(config?.config)
return teamIds.length > 0 ? { scope: 'selected', teamIds } : { scope: 'all', teamIds: [] }
}
function isLinearIssueToggle(params: ToggleSignalSourceParams): boolean {
return params.sourceProduct === SignalSourceProduct.Linear && params.sourceType === SignalSourceType.Issue
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface linearTeamsModalLogicValues {
integrations: IntegrationType[] | null // integrationsLogic
linearIntegrations: IntegrationType[] // integrationsLogic
dataWarehouseSources: PaginatedResponse<ExternalDataSource> | null // signalSourcesLogic
isLinearIssuesToggling: boolean // signalSourcesLogic
sourceConfigs: SignalSourceConfig[] | null // signalSourcesLogic
integrationsUnknown: boolean
isLinearTeamsSubmitting: boolean
isLinearTeamsValid: boolean
linearIntegration: IntegrationType | null
linearTeams: LinearTeamsFormValues
linearTeamsAllErrors: Record<string, any>
linearTeamsChanged: boolean
linearTeamsErrors: DeepPartialMap<LinearTeamsFormValues, ValidationErrorType>
linearTeamsHasErrors: boolean
linearTeamsManualErrors: Record<string, any>
linearTeamsTouched: boolean
linearTeamsTouches: Record<string, boolean>
linearTeamsValidationErrors: DeepPartialMap<LinearTeamsFormValues, ValidationErrorType>
saveInFlight: boolean
showLinearTeamsErrors: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface linearTeamsModalLogicActions {
toggleSignalSource: (params: ToggleSignalSourceParams) => {
params: ToggleSignalSourceParams
} // signalSourcesLogic
toggleSignalSourceFailure: (
params: ToggleSignalSourceParams,
error: string
) => {
error: string
params: ToggleSignalSourceParams
} // signalSourcesLogic
toggleSignalSourceSuccess: (params: ToggleSignalSourceParams) => {
params: ToggleSignalSourceParams
} // signalSourcesLogic
resetLinearTeams: (values?: LinearTeamsFormValues) => {
values?: LinearTeamsFormValues
}
saveSettled: () => {
value: true
}
setLinearTeamsManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setLinearTeamsValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setLinearTeamsValues: (values: DeepPartial<LinearTeamsFormValues>) => {
values: DeepPartial<LinearTeamsFormValues>
}
submitLinearTeams: () => {
value: boolean
}
submitLinearTeamsFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitLinearTeamsRequest: (linearTeams: LinearTeamsFormValues) => {
linearTeams: LinearTeamsFormValues
}
submitLinearTeamsSuccess: (linearTeams: LinearTeamsFormValues) => {
linearTeams: LinearTeamsFormValues
}
touchLinearTeamsField: (key: string) => {
key: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface linearTeamsModalLogicMeta {
__keaTypeGenInternalSelectorTypes: {
linearIntegration: (
linearIntegrations: IntegrationType[],
dataWarehouseSources: PaginatedResponse<ExternalDataSource> | null
) => IntegrationType | null
integrationsUnknown: (integrations: IntegrationType[] | null) => boolean
}
}
export type linearTeamsModalLogicType = MakeLogicType<
linearTeamsModalLogicValues,
linearTeamsModalLogicActions,
LinearTeamsModalLogicProps,
linearTeamsModalLogicMeta
>
/**
* The form behind the Linear teams modal. Saving goes through `signalSourcesLogic.toggleSignalSource`,
* which already decides between creating the row and updating it, reloads the list, and reports
* the connection, so the enable flow and the Filters button share one save path.
*/
export const linearTeamsModalLogic: LogicWrapper<linearTeamsModalLogicType> = kea<linearTeamsModalLogicType>([
path(['scenes', 'inbox', 'logics', 'linearTeamsModalLogic']),
props({} as LinearTeamsModalLogicProps),
// Not keyed on the row id: the enable flow creates the row mid-save, and a keyed instance would
// be swapped out before its own result arrives.
actions({
saveSettled: true,
}),
reducers({
// Set while this modal's own save is in flight, so a Linear toggle from elsewhere is ignored.
saveInFlight: [
false,
{
submitLinearTeamsRequest: () => true,
saveSettled: () => false,
},
],
}),
connect(() => ({
values: [
signalSourcesLogic,
['sourceConfigs', 'isLinearIssuesToggling', 'dataWarehouseSources'],
integrationsLogic,
['linearIntegrations', 'integrations'],
],
actions: [signalSourcesLogic, ['toggleSignalSource', 'toggleSignalSourceSuccess', 'toggleSignalSourceFailure']],
})),
forms(({ props: logicProps, actions }) => ({
linearTeams: {
defaults: formDefaults(logicProps.config),
// The message hangs off `scope` (a string field) because kea-forms types an array
// field's error as an array, one entry per item, and this is about the list as a whole.
errors: ({ scope, teamIds }: LinearTeamsFormValues) => ({
scope:
scope === 'selected' && teamIds.length === 0
? 'Pick at least one team, or read all teams.'
: undefined,
}),
submit: ({ scope, teamIds }: LinearTeamsFormValues) => {
actions.toggleSignalSource({
sourceProduct: SignalSourceProduct.Linear,
sourceType: SignalSourceType.Issue,
enabled: logicProps.enableOnSave ? true : (logicProps.config?.enabled ?? true),
configSet: { [SOURCE_LINEAR_TEAM_IDS_KEY]: scope === 'all' ? [] : teamIds },
viaSetupWizard: logicProps.viaSetupWizard,
})
},
},
})),
selectors({
// The warehouse source records the workspace it syncs, so the teams come from that connection
// and match the synced issues. Before a source exists, any Linear connection will do.
linearIntegration: [
(s) => [s.linearIntegrations, s.dataWarehouseSources],
(
linearIntegrations: IntegrationType[],
dataWarehouseSources: PaginatedResponse<ExternalDataSource> | null
): IntegrationType | null => {
const syncedIntegrationId = dataWarehouseSources?.results.find(
(source: ExternalDataSource) => source.source_type === 'Linear'
)?.job_inputs?.linear_integration_id
return (
linearIntegrations.find((integration) => String(integration.id) === String(syncedIntegrationId)) ??
linearIntegrations[0] ??
null
)
},
],
// A null list means the first load has not landed, which is not the same as no connection.
// Reading the list rather than the loading flag keeps a later reload from blanking the picker.
integrationsUnknown: [
(s) => [s.integrations],
(integrations: IntegrationType[] | null): boolean => integrations === null,
],
}),
listeners(({ props: logicProps, actions, values }) => ({
toggleSignalSourceSuccess: ({ params }) => {
if (!isLinearIssueToggle(params) || !values.saveInFlight) {
return
}
actions.saveSettled()
const teamCount = linearTeamIdsFromConfig(params.configSet).length
captureSignalSourceFiltersChanged({
sourceProduct: params.sourceProduct,
sourceType: params.sourceType,
filter: SOURCE_LINEAR_TEAM_IDS_KEY,
selectedCount: teamCount,
success: true,
})
lemonToast.success(
teamCount === 0
? 'Linear reads all teams from the next sync.'
: `Linear reads ${teamCount} ${teamCount === 1 ? 'team' : 'teams'} from the next sync.`
)
logicProps.onClose()
},
// signalSourcesLogic already shows the error toast; the modal stays open for a retry.
toggleSignalSourceFailure: ({ params }) => {
if (!isLinearIssueToggle(params) || !values.saveInFlight) {
return
}
actions.saveSettled()
captureSignalSourceFiltersChanged({
sourceProduct: params.sourceProduct,
sourceType: params.sourceType,
filter: SOURCE_LINEAR_TEAM_IDS_KEY,
selectedCount: linearTeamIdsFromConfig(params.configSet).length,
success: false,
})
},
})),
])