Skip to content

Commit

Permalink
Reduce type
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 9, 2024
1 parent 99e92b5 commit aec0967
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/extensions/surveys/action-matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="dom" />

import { ActionType, ActionStepStringMatching } from '../../../posthog-surveys-types'
import { SurveyActionType, ActionStepStringMatching } from '../../../posthog-surveys-types'
import { PostHogPersistence } from '../../../posthog-persistence'
import { PostHog } from '../../../posthog-core'
import { CaptureResult, PostHogConfig } from '../../../types'
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('action-matcher', () => {
eventName: string,
currentUrl?: string,
urlMatch?: ActionStepStringMatching
): ActionType => {
): SurveyActionType => {
return {
id: id,
name: `${eventName || 'user defined '} action`,
Expand All @@ -68,7 +68,7 @@ describe('action-matcher', () => {
}

it('can match action on event name', () => {
const pageViewAction = createAction(3, '$mypageview') as unknown as ActionType
const pageViewAction = createAction(3, '$mypageview') as unknown as SurveyActionType
const actionMatcher = new ActionMatcher(instance)
actionMatcher.register([pageViewAction])
let pageViewActionMatched = false
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/utils/survey-event-receiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
SurveyType,
SurveyQuestionType,
Survey,
ActionType,
SurveyActionType,
ActionStepStringMatching,
} from '../../posthog-surveys-types'
import { PostHogPersistence } from '../../posthog-persistence'
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('survey-event-receiver', () => {
eventName: string,
currentUrl?: string,
urlMatch?: ActionStepStringMatching
): ActionType => {
): SurveyActionType => {
return {
id: id,
name: `${eventName || 'user defined '} action`,
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('survey-event-receiver', () => {
type: SurveyType.Popover,
questions: [{ type: SurveyQuestionType.Open, question: 'what is a bokoblin?' }],
conditions: {
actions: [createAction(2, '$autocapture') as unknown as ActionType],
actions: [createAction(2, '$autocapture') as unknown as SurveyActionType],
},
} as unknown as Survey

Expand All @@ -249,7 +249,7 @@ describe('survey-event-receiver', () => {
type: SurveyType.Popover,
questions: [{ type: SurveyQuestionType.Open, question: 'what is a bokoblin?' }],
conditions: {
actions: [createAction(3, '$pageview') as unknown as ActionType],
actions: [createAction(3, '$pageview') as unknown as SurveyActionType],
},
} as unknown as Survey

Expand All @@ -262,7 +262,7 @@ describe('survey-event-receiver', () => {
questions: [{ type: SurveyQuestionType.Open, question: 'what is a bokoblin?' }],
conditions: {
actions: {
values: [createAction(3, '$mypageview') as unknown as ActionType],
values: [createAction(3, '$mypageview') as unknown as SurveyActionType],
},
},
} as unknown as Survey
Expand Down
10 changes: 5 additions & 5 deletions src/extensions/surveys/action-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { PostHog } from '../../posthog-core'
import { ActionStepStringMatching, ActionStepType, ActionType, SurveyElement } from '../../posthog-surveys-types'
import { ActionStepStringMatching, ActionStepType, SurveyActionType, SurveyElement } from '../../posthog-surveys-types'
import { SimpleEventEmitter } from '../../utils/simple-event-emitter'
import { CaptureResult } from '../../types'
import { isUndefined } from '../../utils/type-utils'
import { window } from '../../utils/globals'
import { isUrlMatchingRegex } from '../../utils/request-utils'

export class ActionMatcher {
private readonly actionRegistry?: Set<ActionType>
private readonly actionRegistry?: Set<SurveyActionType>
private readonly instance?: PostHog
private readonly actionEvents: Set<string>
private _debugEventEmitter = new SimpleEventEmitter()

constructor(instance?: PostHog) {
this.instance = instance
this.actionEvents = new Set<string>()
this.actionRegistry = new Set<ActionType>()
this.actionRegistry = new Set<SurveyActionType>()
}

init() {
Expand All @@ -27,7 +27,7 @@ export class ActionMatcher {
}
}

register(actions: ActionType[]): void {
register(actions: SurveyActionType[]): void {
if (isUndefined(this.instance?._addCaptureHook)) {
return
}
Expand Down Expand Up @@ -74,7 +74,7 @@ export class ActionMatcher {
this.onAction('actionCaptured', (data) => callback(data))
}

private checkAction(event?: CaptureResult, action?: ActionType): boolean {
private checkAction(event?: CaptureResult, action?: SurveyActionType): boolean {
if (action?.steps == null) {
return false
}
Expand Down
10 changes: 2 additions & 8 deletions src/posthog-surveys-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface Survey {
}[]
} | null
actions: {
values: ActionType[]
values: SurveyActionType[]
} | null
} | null
start_date: string | null
Expand All @@ -181,16 +181,10 @@ export interface Survey {
current_iteration_start_date: string | null
}

export interface ActionType {
count?: number
created_at: string
deleted?: boolean
export interface SurveyActionType {
id: number
name: string | null
steps?: ActionStepType[]
tags?: string[]
is_action?: true
action_id?: number // alias of id to make it compatible with event definitions uuid
}

/** Sync with plugin-server/src/types.ts */
Expand Down

0 comments on commit aec0967

Please sign in to comment.