Skip to content

Commit

Permalink
Use createVersionChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Aug 20, 2024
1 parent e53f4b7 commit cdeb470
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
12 changes: 11 additions & 1 deletion frontend/src/lib/utils/semver.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { highestVersion, lowestVersion, parseVersion, versionToString } from './semver'
import { createVersionChecker, highestVersion, lowestVersion, parseVersion, versionToString } from './semver'

describe('semver', () => {
describe('parseVersion', () => {
Expand Down Expand Up @@ -52,4 +52,14 @@ describe('semver', () => {
expect(versionToString({ major: 1 })).toEqual('1')
})
})
describe('createVersionChecker', () => {
it('should create a version checker that checks that a version is above or equal to a specified version', () => {
const isSupportedVersion = createVersionChecker('4.5.6')
expect(isSupportedVersion('1.2.3')).toEqual(false)
expect(isSupportedVersion('4.5.6')).toEqual(true)
expect(isSupportedVersion('4.5.7')).toEqual(true)
expect(isSupportedVersion('7.8.9')).toEqual(true)
expect(isSupportedVersion('4.5.6-alpha')).toEqual(false)
})
})
})
7 changes: 7 additions & 0 deletions frontend/src/lib/utils/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,10 @@ export function versionToString(version: SemanticVersion): string {
}
return versionPart
}

export function createVersionChecker(requiredVersion: string | SemanticVersion) {
return (version: string | SemanticVersion): boolean => {
const diff = diffVersions(version, requiredVersion)
return !diff || diff.diff > 0
}
}
14 changes: 4 additions & 10 deletions frontend/src/toolbar/elements/heatmapLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'lib/components/heatmaps/types'
import { calculateViewportRange, DEFAULT_HEATMAP_FILTERS } from 'lib/components/heatmaps/utils'
import { dateFilterToText } from 'lib/utils'
import { createVersionChecker } from 'lib/utils/semver'
import { PostHog } from 'posthog-js'
import { collectAllElementsDeep, querySelectorAllDeep } from 'query-selector-shadow-dom'

Expand All @@ -28,7 +29,7 @@ import { FilterType, PropertyFilterType, PropertyOperator } from '~/types'

import type { heatmapLogicType } from './heatmapLogicType'

export const SCROLL_DEPTH_JS_VERSION = [1, 99]
export const doesSupportScrollDepth = createVersionChecker('1.99')

const emptyElementsStatsPages: PaginatedResponse<ElementsEventType> = {
next: undefined,
Expand Down Expand Up @@ -439,20 +440,13 @@ export const heatmapLogic = kea<heatmapLogicType>([
(posthog: PostHog): 'version' | 'disabled' | null => {
// Later SDKs have the version in the posthog object, but we need to check for the old way of doing it too
const posthogVersion: string =
posthog.version ??
(posthog as any)?._calculate_event_properties?.('test', {}, new Date())?.['$lib_version'] ??
'0.0.0'
const majorMinorVersion = posthogVersion.split('.')
const majorVersion = parseInt(majorMinorVersion[0], 10)
const minorVersion = parseInt(majorMinorVersion[1], 10)
posthog?.version ?? posthog?._calculate_event_properties?.('test', {})?.['$lib_version'] ?? '0.0.0'

if (!(posthog as any)?.scrollManager?.scrollY) {
return 'version'
}

const isSupported =
majorVersion > SCROLL_DEPTH_JS_VERSION[0] ||
(majorVersion === SCROLL_DEPTH_JS_VERSION[0] && minorVersion >= SCROLL_DEPTH_JS_VERSION[1])
const isSupported = doesSupportScrollDepth(posthogVersion)
const isDisabled = posthog?.config.disable_scroll_properties

return !isSupported ? 'version' : isDisabled ? 'disabled' : null
Expand Down

0 comments on commit cdeb470

Please sign in to comment.