Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/src/toolbar/TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ Fired when the side panel shows a new state.

Fired when the user runs a tool on the page from the extension.

| Property | Type | Description |
| --------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tool` | `string` | `'inspect'`, `'heatmap'`, `'save-heatmap'`, `'actions'`, `'create-action'`, `'save-action'`, `'flags'`, `'override-flag'`, `'clear-flag-overrides'`, or `'impersonate-flags'` |
| `payload` | `boolean \| undefined` | `true` when `'override-flag'` sets a payload |
| Property | Type | Description |
| --------- | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tool` | `string` | `'inspect'`, `'heatmap'`, `'save-heatmap'`, `'actions'`, `'create-action'`, `'save-action'`, `'flags'`, `'override-flag'`, `'clear-flag-overrides'`, `'impersonate-flags'`, `'events'`, `'export-events'`, `'web-vitals'`, `'experiments'`, `'create-experiment'`, `'save-experiment'`, `'more'`, `'pii-masking'`, `'screenshot-for-event'`, or `'view-session-recording'` |
| `payload` | `boolean \| undefined` | `true` when `'override-flag'` sets a payload |

**File:** `extension/extensionTelemetry.ts`

Expand Down
27 changes: 24 additions & 3 deletions frontend/src/toolbar/actions/SelectorCountContext.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import { querySelectorAllDeep } from 'query-selector-shadow-dom'
import { createContext } from 'react'

/**
* `deep` also matches inside shadow roots, like actions do. `document` matches only the document itself, like
* posthog-js applies web experiments with `document.querySelectorAll`.
*/
export type SelectorScope = 'deep' | 'document'

export type SelectorMatchCounts = Record<SelectorScope, number>

export interface SelectorMatchCounter {
/** `undefined` while the count is pending, `null` for an invalid selector. */
count: (selector: string) => number | null | undefined
count: (selector: string, scope?: SelectorScope) => number | null | undefined
request: (selector: string) => void
}

function countInScope(selector: string, scope: SelectorScope): number {
return scope === 'deep' ? querySelectorAllDeep(selector).length : document.querySelectorAll(selector).length
}

/** Counts in both scopes, or returns `null` for an invalid selector. */
export function countSelectorMatches(selector: string): SelectorMatchCounts | null {
try {
return { deep: countInScope(selector, 'deep'), document: countInScope(selector, 'document') }
} catch {
return null
}
}

/** Counts in the document the component renders in, which is the page for the floating toolbar. */
const pageDocumentCounter: SelectorMatchCounter = {
count: (selector) => {
count: (selector, scope = 'deep') => {
try {
return querySelectorAllDeep(selector).length
return countInScope(selector, scope)
} catch {
return null
}
Expand Down
37 changes: 12 additions & 25 deletions frontend/src/toolbar/bar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import { ToolbarLockedFeature } from '~/toolbar/bar/ToolbarLockedFeature'
import { toolbarLogic } from '~/toolbar/bar/toolbarLogic'
import { UiHostConfigModal } from '~/toolbar/bar/UiHostConfigModal'
import { readPosthogDebugInfo } from '~/toolbar/debug/posthogDebugInfo'
import { fieldNotesLogic } from '~/toolbar/field-notes/fieldNotesLogic'
import { productToursLogic } from '~/toolbar/product-tours/productToursLogic'
import { screenshotUploadLogic } from '~/toolbar/screenshot-upload/screenshotUploadLogic'
Expand Down Expand Up @@ -147,7 +148,7 @@
loadingSurveys: boolean,
surveysCount: number
): LemonMenuItem {
const isAutocaptureEnabled = posthog?.autocapture?.isEnabled
const info = readPosthogDebugInfo(posthog)

return {
icon: <IconStethoscope />,
Expand All @@ -157,44 +158,30 @@
label: (
<div className="flex justify-between items-center w-full">
<div>version: </div>
<div>{posthog?.version || 'posthog not available'}</div>
<div>{info.version || 'posthog not available'}</div>
</div>
),
},
{
label: (
<div className="flex justify-between items-center w-full">
<div>api host: </div>
<div>{posthog?.config.api_host}</div>
<div>{info.apiHost}</div>
</div>
),
},
{
label: (
<div className="flex justify-between items-center w-full">
<div>ui host: </div>
<div>{posthog?.config.ui_host || 'not set'}</div>
<div>{info.uiHost || 'not set'}</div>
</div>
),
},
{ label: <EnabledStatusItem label="autocapture" value={!!isAutocaptureEnabled} /> },
{
label: (
<EnabledStatusItem
label="rageclicks"
value={!!(isAutocaptureEnabled && posthog?.config.rageclick)}
/>
),
},
{
label: (
<EnabledStatusItem
label="dead clicks"
value={!!posthog?.deadClicksAutocapture?.lazyLoadedDeadClicksAutocapture}
/>
),
},
{ label: <EnabledStatusItem label="heatmaps" value={!!posthog?.heatmaps?.isEnabled} /> },
{ label: <EnabledStatusItem label="autocapture" value={info.autocapture} /> },
{ label: <EnabledStatusItem label="rageclicks" value={info.rageclicks} /> },
{ label: <EnabledStatusItem label="dead clicks" value={info.deadClicks} /> },
{ label: <EnabledStatusItem label="heatmaps" value={info.heatmaps} /> },
{
label: (
<div className="flex justify-between items-center w-full">
Expand All @@ -205,19 +192,19 @@
</div>
),
},
{ label: <EnabledStatusItem label="session recording" value={!!posthog?.sessionRecording?.started} /> },
{ label: <EnabledStatusItem label="session recording" value={info.sessionRecordingStarted} /> },
{
label: (
<div className="flex justify-between items-center w-full">
<div>session recording status: </div>
<div>{posthog?.sessionRecording?.status || 'unknown'}</div>
<div>{info.sessionRecordingStatus || 'unknown'}</div>
</div>
),
},
{
label: (
<div className="flex items-center w-full">
<Link to={posthog?.get_session_replay_url()} target="_blank">
<Link to={info.sessionReplayUrl ?? undefined} target="_blank">
View current session recording
</Link>
</div>
Expand Down Expand Up @@ -380,7 +367,7 @@
)
}

export function ToolbarInfoMenu(): JSX.Element | null {

Check warning on line 370 in frontend/src/toolbar/bar/Toolbar.tsx

View workflow job for this annotation

GitHub Actions / Frontend formatting

lint:complexity

`ToolbarInfoMenu` has cyclomatic complexity 24 (warn >10)
const ref = useRef<HTMLDivElement | null>(null)
const { visibleMenu, isDragging, menuProperties, minimized, isBlurred } = useValues(toolbarLogic)
const { setMenu } = useActions(toolbarLogic)
Expand Down Expand Up @@ -489,7 +476,7 @@
)
}

export function Toolbar(): JSX.Element | null {

Check warning on line 479 in frontend/src/toolbar/bar/Toolbar.tsx

View workflow job for this annotation

GitHub Actions / Frontend formatting

lint:complexity

`Toolbar` has cyclomatic complexity 31 (warn >10)
const ref = useRef<HTMLDivElement | null>(null)
const { minimized, position, isDragging, hedgehogMode, isEmbeddedInApp, isExiting, isLoading } =
useValues(toolbarLogic)
Expand Down
54 changes: 54 additions & 0 deletions frontend/src/toolbar/bar/piiMaskingStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,57 @@ export const PII_MASKING_PRESET_COLORS = [
{ label: 'Purple', value: '#9988aa' },
{ label: 'Orange', value: '#aa9988' },
] as const

export const getPiiMaskingWarnings = (posthog: PostHog): string[] => {
const warnings: string[] = []
if (posthog.sessionRecording?.status === 'active') {
if (
posthog.config.session_recording?.blockClass !== undefined &&
typeof posthog.config.session_recording?.blockClass !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.blockClass`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
if (
posthog.config.session_recording?.maskTextClass !== undefined &&
typeof posthog.config.session_recording?.maskTextClass !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.maskTextClass`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
if (
posthog.config.session_recording?.maskTextSelector !== undefined &&
typeof posthog.config.session_recording?.maskTextSelector !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.maskTextSelector`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
}

return warnings
}

export const DEFAULT_PII_MASKING_COLOR = '#888888'

/** Adds, updates or removes the masking preview stylesheet in the page's head. */
export function applyPiiMaskingStylesheet(
styleId: string,
enabled: boolean,
color: string,
posthog: PostHog | null
): void {
const existing = document.getElementById(styleId)
if (!enabled) {
existing?.remove()
return
}
const styleElement = existing ?? document.createElement('style')
styleElement.id = styleId
styleElement.textContent = generatePiiMaskingCSS(color, posthog)
if (!existing) {
document.head.appendChild(styleElement)
}
}
89 changes: 18 additions & 71 deletions frontend/src/toolbar/bar/toolbarLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import type { CommonFilters, HeatmapFilters, HeatmapFixedPositionMode } from '../../lib/components/heatmaps/types'
import type { HedgehogModeInterface } from '../../lib/components/HedgehogMode/types'
import type { ActionType } from '../../types'
import { generatePiiMaskingCSS } from './piiMaskingStyles'
import { DEFAULT_PII_MASKING_COLOR, applyPiiMaskingStylesheet, getPiiMaskingWarnings } from './piiMaskingStyles'

const MARGIN = 2
const HEDGEHOG_OFFSET = 80
Expand Down Expand Up @@ -521,7 +521,7 @@
},
],
piiMaskingColor: [
'#888888' as string,
DEFAULT_PII_MASKING_COLOR as string,
{ persist: true },
{
setPiiMaskingColor: (_, { color }) => color,
Expand Down Expand Up @@ -699,36 +699,7 @@
if (!posthog || !piiMaskingEnabled) {
return null
}

const warnings: string[] = []
if (posthog.sessionRecording?.status === 'active') {
if (
posthog.config.session_recording?.blockClass !== undefined &&
typeof posthog.config.session_recording?.blockClass !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.blockClass`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
if (
posthog.config.session_recording?.maskTextClass !== undefined &&
typeof posthog.config.session_recording?.maskTextClass !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.maskTextClass`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
if (
posthog.config.session_recording?.maskTextSelector !== undefined &&
typeof posthog.config.session_recording?.maskTextSelector !== 'string'
) {
warnings.push(
"The toolbar's PII masking tool doesn't support non-string `session_recording.maskTextSelector`. If you want to use PII masking, please set it to a string, or file a feature request at https://posthog.com/roadmap."
)
}
}

return warnings
return getPiiMaskingWarnings(posthog)
},
],
isLoading: [
Expand Down Expand Up @@ -791,7 +762,7 @@
actions.toggleMinimized(false)
}
},
setVisibleMenu: ({ visibleMenu }, _, __, previousState) => {

Check warning on line 765 in frontend/src/toolbar/bar/toolbarLogic.ts

View workflow job for this annotation

GitHub Actions / Frontend formatting

lint:complexity

`setVisibleMenu` has cyclomatic complexity 11 (warn >10)
const previousMenu = toolbarLogic.selectors.visibleMenu(previousState)
if (visibleMenu !== 'none') {
toolbarPosthogJS.capture('toolbar menu opened', { menu: visibleMenu, previous_menu: previousMenu })
Expand Down Expand Up @@ -950,30 +921,15 @@
})
},
togglePiiMasking: () => {
const styleElement = document.getElementById(PII_MASKING_STYLESHEET_ID) as HTMLStyleElement | null

if (values.piiMaskingEnabled) {
const css = generatePiiMaskingCSS(values.piiMaskingColor, values.posthog)
if (!styleElement) {
const newStyleElement = document.createElement('style')
newStyleElement.id = PII_MASKING_STYLESHEET_ID
document.head.appendChild(newStyleElement)
newStyleElement.textContent = css
} else {
styleElement.textContent = css
}
} else {
if (styleElement) {
styleElement.remove()
}
}
applyPiiMaskingStylesheet(
PII_MASKING_STYLESHEET_ID,
values.piiMaskingEnabled,
values.piiMaskingColor,
values.posthog
)
},
setPiiMaskingColor: async ({ color }) => {
const styleElement = document.getElementById(PII_MASKING_STYLESHEET_ID) as HTMLStyleElement | null

if (styleElement && values.piiMaskingEnabled) {
styleElement.textContent = generatePiiMaskingCSS(color, values.posthog)
}
setPiiMaskingColor: ({ color }) => {
applyPiiMaskingStylesheet(PII_MASKING_STYLESHEET_ID, values.piiMaskingEnabled, color, values.posthog)
},
startGracefulExit: () => {
actions.setVisibleMenu('none')
Expand Down Expand Up @@ -1036,22 +992,13 @@
// Initialize PII masking if already enabled
// Remove stylesheet on unmount
cache.disposables.add(() => {
if (values.piiMaskingEnabled) {
let styleElement = document.getElementById(PII_MASKING_STYLESHEET_ID) as HTMLStyleElement | null
if (!styleElement) {
styleElement = document.createElement('style')
styleElement.id = PII_MASKING_STYLESHEET_ID
document.head.appendChild(styleElement)
}
styleElement.textContent = generatePiiMaskingCSS(values.piiMaskingColor, values.posthog)
}

return () => {
const styleElement = document.getElementById(PII_MASKING_STYLESHEET_ID)
if (styleElement) {
styleElement.remove()
}
}
applyPiiMaskingStylesheet(
PII_MASKING_STYLESHEET_ID,
values.piiMaskingEnabled,
values.piiMaskingColor,
values.posthog
)
return () => applyPiiMaskingStylesheet(PII_MASKING_STYLESHEET_ID, false, values.piiMaskingColor, null)
}, 'piiMasking')

// the toolbar can be run within the posthog parent app
Expand All @@ -1060,7 +1007,7 @@

if (isInIframe) {
cache.disposables.add(() => {
const iframeEventListener = (e: MessageEvent): void => {

Check warning on line 1010 in frontend/src/toolbar/bar/toolbarLogic.ts

View workflow job for this annotation

GitHub Actions / Frontend formatting

lint:complexity

`iframeEventListener` has cyclomatic complexity 16 (warn >10)
// Only the PostHog app at uiHost should be driving these actions.
const expectedOrigin = toolbarConfigLogic.findMounted()?.values.uiHost
if (!expectedOrigin || e.origin !== expectedOrigin) {
Expand Down
Loading
Loading