: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 clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { IconCopy, IconPencil, IconPlus, IconTrash } from '@posthog/icons'
import { IconOpenInApp } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
import { LemonTag } from 'lib/lemon-ui/LemonTag/LemonTag'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { ExperimentIdType, ToolbarUserIntent } from '~/types'
import { AuthorizedUrlForm } from './AuthorizedUrlForm'
import { AuthorizedUrlListType, authorizedUrlListLogic } from './authorizedUrlListLogic'
import { EmptyState } from './EmptyState'
export interface AuthorizedUrlListProps {
type: AuthorizedUrlListType
actionId?: number
experimentId?: ExperimentIdType
productTourId?: string | null
userIntent?: ToolbarUserIntent
query?: string | null
allowWildCards?: boolean
displaySuggestions?: boolean
showLaunch?: boolean
allowAdd?: boolean
allowDelete?: boolean
launchInSameTab?: boolean
/** Drop the verbose "there are no authorized URLs" explainer box (e.g. compact onboarding). */
hideEmptyState?: boolean
}
export function AuthorizedUrlList({
actionId,
experimentId,
productTourId,
userIntent,
query,
type,
addText = 'Add new authorized URL',
allowWildCards,
displaySuggestions = true,
allowAdd = true,
allowDelete = true,
showLaunch = true,
launchInSameTab = false,
hideEmptyState = false,
}: AuthorizedUrlListProps & { addText?: string }): JSX.Element {
const logic = authorizedUrlListLogic({
experimentId: experimentId ?? null,
productTourId: productTourId ?? null,
actionId: actionId ?? null,
userIntent,
type,
query,
allowWildCards,
})
const { urlsKeyed, launchUrl, editUrlIndex, isAddUrlFormVisible, onlyAllowDomains } = useValues(logic)
const { addUrl, removeUrl, newUrl, setEditUrlIndex, copyLaunchCode } = useActions(logic)
const noAuthorizedUrls = !urlsKeyed.some((url) => url.type === 'authorized')
// toolbar and web analytics urls are sent through the backend to be validated and have toolbar auth
// information added; other urls are simply opened directly
const isLaunchableType = type === AuthorizedUrlListType.TOOLBAR_URLS || type === AuthorizedUrlListType.WEB_ANALYTICS
return (
<div className="flex flex-col gap-2" data-attr="authorized-urls-table">
{!hideEmptyState && (
<EmptyState
experimentId={experimentId}
productTourId={productTourId}
actionId={actionId}
type={type}
displaySuggestions={displaySuggestions}
/>
)}
{isAddUrlFormVisible ? (
<div className="border rounded p-2 bg-surface-primary">
<AuthorizedUrlForm
type={type}
actionId={actionId}
experimentId={experimentId}
productTourId={productTourId}
allowWildCards={allowWildCards}
/>
</div>
) : allowAdd ? (
<LemonButton
className="w-full"
onClick={newUrl}
type="secondary"
icon={<IconPlus />}
data-attr="toolbar-add-url"
>
{addText}
</LemonButton>
) : null}
{urlsKeyed.map((keyedURL, index) => {
// If there are no authorized urls, highlight the first suggestion
const isFirstSuggestion = keyedURL.originalIndex === 0 && keyedURL.type === 'suggestion'
const isHighlighted = noAuthorizedUrls && isFirstSuggestion
if (!displaySuggestions && keyedURL.type === 'suggestion') {
return null
}
const launchTarget = isLaunchableType ? launchUrl(keyedURL.url) : `${keyedURL.url}${query ?? ''}`
// The whole row mirrors the Launch button, so the card-like row is a real click target instead of
// a dead click. Suggestions (handled separately) and wildcard domains can't be launched.
const isRowLaunchable = showLaunch && keyedURL.type === 'authorized' && !keyedURL.url.includes('*')
const launchFromRow = (): void => {
if (launchInSameTab) {
window.location.href = launchTarget
} else {
window.open(launchTarget, '_blank', 'noopener,noreferrer')
}
}
return editUrlIndex === index ? (
<div key={keyedURL.url} className="border rounded p-2 bg-surface-primary">
<AuthorizedUrlForm
type={type}
actionId={actionId}
experimentId={experimentId}
productTourId={productTourId}
allowWildCards={allowWildCards}
/>
</div>
) : (
<div
key={keyedURL.url}
className={clsx(
'border rounded flex items-center p-2 pl-4 bg-surface-primary',
isRowLaunchable ? 'cursor-pointer hover:bg-surface-secondary' : 'cursor-default'
)}
onClick={isRowLaunchable ? launchFromRow : undefined}
onKeyDown={
isRowLaunchable
? (e) => {
// Only react to the row itself, not key events bubbling up from the buttons
if (e.currentTarget === e.target && (e.key === 'Enter' || e.key === ' ')) {
e.preventDefault()
launchFromRow()
}
}
: undefined
}
role={isRowLaunchable ? 'button' : undefined}
tabIndex={isRowLaunchable ? 0 : undefined}
>
{keyedURL.type === 'suggestion' && (
<Tooltip title={'Seen in ' + keyedURL.count + ' events in the last 3 days'}>
<LemonTag type="highlight" className="mr-4 uppercase cursor-pointer">
Suggestion
</LemonTag>
</Tooltip>
)}
<span title={keyedURL.url} className="flex-1 truncate">
{keyedURL.url}
</span>
<div
className="Actions flex deprecated-space-x-2 shrink-0"
onClick={(e) => e.stopPropagation()}
>
{keyedURL.type === 'suggestion' && allowAdd ? (
<LemonButton
onClick={() => addUrl(keyedURL.url)}
icon={<IconPlus />}
data-attr="toolbar-apply-suggestion"
type={isHighlighted ? 'primary' : undefined}
active={isHighlighted}
>
Apply suggestion
</LemonButton>
) : (
<>
{showLaunch && (
<LemonButton
icon={<IconOpenInApp />}
to={launchTarget}
targetBlank={!launchInSameTab}
tooltip={
isLaunchableType
? 'Open the toolbar on this page'
: 'Open the browser at this page'
}
center
data-attr="toolbar-open"
type="secondary"
disabledReason={
keyedURL.url.includes('*')
? 'Wildcard domains cannot be launched'
: undefined
}
sideAction={{
dropdown: {
placement: 'bottom-start',
overlay: (
<div className="px-2 py-1">
<h3>If launching the toolbar didn't work, </h3>
<p>
You can copy the launch code and paste it into the
browser console on your site.
</p>
<p>NB you need to have added posthog to the `window`</p>
<LemonButton
icon={<IconCopy />}
size="small"
className="float-right"
type="primary"
data-attr="copy-manual-toolbar-launch-code"
onClick={() => {
copyLaunchCode()
}}
>
Copy launch code
</LemonButton>
</div>
),
},
'data-attr': 'launch-toolbar-sideaction-dropdown',
}}
>
Launch
</LemonButton>
)}
{allowAdd && (
<LemonButton
icon={<IconPencil />}
onClick={() => setEditUrlIndex(keyedURL.originalIndex)}
tooltip="Edit"
center
/>
)}
{allowDelete && (
<LemonButton
icon={<IconTrash />}
tooltip={`Remove ${onlyAllowDomains ? 'domain' : 'URL'}`}
center
onClick={() => {
LemonDialog.open({
title: <>Remove {keyedURL.url} ?</>,
description: `Are you sure you want to remove this authorized ${
onlyAllowDomains ? 'domain' : 'URL'
}?`,
primaryButton: {
status: 'danger',
children: 'Remove',
onClick: () => removeUrl(index),
},
secondaryButton: {
children: 'Cancel',
},
})
}}
/>
)}
</>
)}
</div>
</div>
)
})}
</div>
)
}