Skip to content

: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.

fix(settings): summarize personal API key scopes by product area in the tooltip (#127601)

Reece Jones492091158ff4
diff --git a/frontend/src/lib/scopes.test.ts b/frontend/src/lib/scopes.test.ts
index 22940bae611c..a8367cea5055 100644
--- a/frontend/src/lib/scopes.test.ts
+++ b/frontend/src/lib/scopes.test.ts
@@ -11,6 +11,7 @@ import {
scopeGroupLevel,
scopeGroupTooltip,
scopeMatchesSearch,
+ summarizeScopesByGroup,
} from 'lib/scopes'
import { ScopeObjectEnumApi } from 'products/access_control/frontend/generated/api.schemas'
@@ -232,3 +233,24 @@ describe('API_KEY_SCOPE_PRESETS', () => {
}
)
})
+
+describe('summarizeScopesByGroup', () => {
+ it('counts each object once at its highest level, grouped in picker order', () => {
+ expect(
+ summarizeScopesByGroup([
+ 'feature_flag:read',
+ 'feature_flag:write',
+ 'session_recording:read',
+ 'insight:write',
+ 'insight:read',
+ 'dashboard:read',
+ 'not_a_real_object:read',
+ ])
+ ).toEqual([
+ { label: 'Product analytics', write: 1, read: 1 },
+ { label: 'Session replay', write: 0, read: 1 },
+ { label: 'Feature flags, experiments & surveys', write: 1, read: 0 },
+ { label: 'Other', write: 0, read: 1 },
+ ])
+ })
+})
diff --git a/frontend/src/lib/scopes.tsx b/frontend/src/lib/scopes.tsx
index 36439d4ea5f5..71cebbfc795f 100644
--- a/frontend/src/lib/scopes.tsx
+++ b/frontend/src/lib/scopes.tsx
@@ -698,6 +698,31 @@ export const groupScopeRows = <Row extends ScopePickerRow>(rows: Row[]): ScopePi
.map((label) => ({ label, rows: rowsByLabel.get(label) ?? [] }))
}
+export type ScopeGroupSummary = { label: string; write: number; read: number }
+
+// Counts the objects in each product area that a key can write, or only read, in API_SCOPE_GROUPS order.
+// `insight:read` and `insight:write` together count once, as write.
+export const summarizeScopesByGroup = (scopes: readonly string[]): ScopeGroupSummary[] => {
+ const levelByObject = new Map<string, 'read' | 'write'>()
+ for (const scope of scopes) {
+ const [object, action] = scope.split(':')
+ if (levelByObject.get(object) !== 'write') {
+ levelByObject.set(object, action === 'write' ? 'write' : 'read')
+ }
+ }
+ const summaryByLabel = new Map<string, ScopeGroupSummary>()
+ for (const [object, level] of levelByObject) {
+ const label = getScopeGroupLabel(object)
+ const summary = summaryByLabel.get(label) ?? { label, write: 0, read: 0 }
+ summary[level] += 1
+ summaryByLabel.set(label, summary)
+ }
+ return [...API_SCOPE_GROUPS.map(({ label }) => label), OTHER_SCOPE_GROUP_LABEL].flatMap((label) => {
+ const summary = summaryByLabel.get(label)
+ return summary ? [summary] : []
+ })
+}
+
export const DEFAULT_OAUTH_SCOPES = ['openid', 'email', 'profile']
export const getScopeDescription = (scope: string): string | undefined => {
diff --git a/frontend/src/scenes/settings/organization/OrganizationPersonalAPIKeysTable.tsx b/frontend/src/scenes/settings/organization/OrganizationPersonalAPIKeysTable.tsx
index 7825e90ea9cb..0bad3c797b8c 100644
--- a/frontend/src/scenes/settings/organization/OrganizationPersonalAPIKeysTable.tsx
+++ b/frontend/src/scenes/settings/organization/OrganizationPersonalAPIKeysTable.tsx
@@ -6,6 +6,7 @@ import { TZLabel } from 'lib/components/TZLabel'
import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { fullName } from 'lib/utils/strings'
import { TagList } from 'scenes/settings/user/PersonalAPIKeys'
+import { ScopeGroupSummary } from 'scenes/settings/user/scopes/ScopeGroupSummary'
import type { OrganizationPersonalAPIKeyApi } from 'products/platform_features/frontend/generated/api.schemas'
@@ -50,7 +51,7 @@ export function OrganizationPersonalAPIKeysTable(): JSX.Element {
{
title: 'Scopes',
key: 'scopes',
- render: (_, key) => <TagList tags={[...key.scopes]} />,
+ render: (_, key) => <TagList tags={[...key.scopes]} tooltip={<ScopeGroupSummary scopes={key.scopes} />} />,
},
{
title: 'Access scope',
diff --git a/frontend/src/scenes/settings/user/PersonalAPIKeys.tsx b/frontend/src/scenes/settings/user/PersonalAPIKeys.tsx
index a294dca4be65..4371703e356e 100644
--- a/frontend/src/scenes/settings/user/PersonalAPIKeys.tsx
+++ b/frontend/src/scenes/settings/user/PersonalAPIKeys.tsx
@@ -1,6 +1,6 @@
import { useActions, useValues } from 'kea'
import { Form } from 'kea-forms'
-import { useEffect } from 'react'
+import { ReactNode, useEffect } from 'react'
import { IconInfo, IconPlus } from '@posthog/icons'
import {
@@ -34,6 +34,7 @@ import { PersonalAPIKeyType } from '~/types'
import { APIKeyTable } from '../shared/APIKeyTable'
import { KeyExpirationOption, expiresAtForOption, isExpired, personalAPIKeysLogic } from './personalAPIKeysLogic'
import ScopeAccessSelector from './scopes/ScopeAccessSelector'
+import { ScopeGroupSummary } from './scopes/ScopeGroupSummary'
interface EditKeyModalProps {
zIndex?: LemonModalProps['zIndex']
@@ -312,9 +313,14 @@ function openRollKeyDialog(
})
}
-type TagListProps = { onMoreClick?: () => void; tags: string[] }
+type TagListProps = {
+ onMoreClick?: () => void
+ tags: string[]
+ /** Content of the "+N more" tooltip. Defaults to the hidden tags, comma separated. */
+ tooltip?: ReactNode
+}
-export function TagList({ tags, onMoreClick }: TagListProps): JSX.Element {
+export function TagList({ tags, onMoreClick, tooltip }: TagListProps): JSX.Element {
return (
<span className="flex flex-wrap gap-1">
{tags.slice(0, 4).map((x) => (
@@ -323,7 +329,7 @@ export function TagList({ tags, onMoreClick }: TagListProps): JSX.Element {
</>
))}
{tags.length > 4 && (
- <Tooltip title={tags.slice(4).join(', ')}>
+ <Tooltip title={tooltip ?? tags.slice(4).join(', ')}>
<LemonTag onClick={onMoreClick} forceClickable={!!onMoreClick}>
+{tags.length - 4} more
</LemonTag>
@@ -606,7 +612,11 @@ function PersonalAPIKeysTable(): JSX.Element {
key.scopes[0] === '*' ? (
<LemonTag type="warning">All access</LemonTag>
) : (
- <TagList tags={key.scopes} onMoreClick={() => setEditingKeyId(key.id)} />
+ <TagList
+ tags={key.scopes}
+ tooltip={<ScopeGroupSummary scopes={key.scopes} />}
+ onMoreClick={() => setEditingKeyId(key.id)}
+ />
)
}
extraColumnsAfterLabel={[statusColumn]}
diff --git a/frontend/src/scenes/settings/user/UserDangerZone.tsx b/frontend/src/scenes/settings/user/UserDangerZone.tsx
index 284004d5c376..198eb6d193b9 100644
--- a/frontend/src/scenes/settings/user/UserDangerZone.tsx
+++ b/frontend/src/scenes/settings/user/UserDangerZone.tsx
@@ -23,6 +23,7 @@ import { userLogic } from 'scenes/userLogic'
import { DeleteOrganizationModal } from '../organization/OrganizationDangerZone'
import { TagList } from './PersonalAPIKeys'
import { personalAPIKeysLogic } from './personalAPIKeysLogic'
+import { ScopeGroupSummary } from './scopes/ScopeGroupSummary'
import { userDangerZoneLogic } from './userDangerZoneLogic'
const DELETE_CONFIRMATION_TEXT = 'permanently delete data'
@@ -209,7 +210,11 @@ export function DeleteUserModal({
key.scopes[0] === '*' ? (
<LemonTag type="warning">All access</LemonTag>
) : (
- <TagList tags={key.scopes} onMoreClick={() => {}} />
+ <TagList
+ tags={key.scopes}
+ tooltip={<ScopeGroupSummary scopes={key.scopes} />}
+ onMoreClick={() => {}}
+ />
),
},
]}
diff --git a/frontend/src/scenes/settings/user/scopes/ScopeGroupSummary.tsx b/frontend/src/scenes/settings/user/scopes/ScopeGroupSummary.tsx
new file mode 100644
index 000000000000..5c0effddc6b7
--- /dev/null
+++ b/frontend/src/scenes/settings/user/scopes/ScopeGroupSummary.tsx
@@ -0,0 +1,18 @@
+import { Fragment } from 'react'
+
+import { summarizeScopesByGroup } from 'lib/scopes'
+
+export function ScopeGroupSummary({ scopes }: { scopes: readonly string[] }): JSX.Element {
+ return (
+ <div className="grid grid-cols-[auto_auto] gap-x-4 gap-y-0.5">
+ {summarizeScopesByGroup(scopes).map(({ label, write, read }) => (
+ <Fragment key={label}>
+ <span>{label}</span>
+ <span className="text-right whitespace-nowrap opacity-75">
+ {[write ? `${write} write` : null, read ? `${read} read` : null].filter(Boolean).join(' · ')}
+ </span>
+ </Fragment>
+ ))}
+ </div>
+ )
+}