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.

feat(conversations): show related groups panel on tickets (#66613)

Abigail9c60a489b24e
diff --git a/frontend/src/scenes/groups/RelatedGroups.tsx b/frontend/src/scenes/groups/RelatedGroups.tsx
index 411afe053c88..58ff8633c16c 100644
--- a/frontend/src/scenes/groups/RelatedGroups.tsx
+++ b/frontend/src/scenes/groups/RelatedGroups.tsx
@@ -1,6 +1,7 @@
import { useValues } from 'kea'
import { IconPerson } from '@posthog/icons'
+import { LemonTag, Tooltip } from '@posthog/lemon-ui'
import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { capitalizeFirstLetter } from 'lib/utils/strings'
@@ -17,6 +18,16 @@ export interface RelatedGroupsProps {
type?: 'person' | 'group'
pageSize?: number
embedded?: boolean
+ /** Tag the group row whose key matches this value with `highlightLabel`. */
+ highlightGroupKey?: string | null
+ /** Label for the tag on the highlighted row. Required for the tag to render. */
+ highlightLabel?: string
+ /** Also show a "Stale" tag on the highlighted row. */
+ highlightStale?: boolean
+ /** Tooltip shown on the "Stale" tag (caller supplies the context-specific wording). */
+ highlightStaleTooltip?: string
+ /** Extra group rows to append (deduped by id), e.g. a group not in the live related list. */
+ extraActors?: ActorType[]
}
export function RelatedGroups({
@@ -25,11 +36,18 @@ export function RelatedGroups({
type,
pageSize,
embedded = false,
+ highlightGroupKey,
+ highlightLabel,
+ highlightStale = false,
+ highlightStaleTooltip,
+ extraActors,
}: RelatedGroupsProps): JSX.Element {
const { relatedActors, relatedPeople, relatedActorsLoading } = useValues(relatedGroupsLogic({ groupTypeIndex, id }))
- const dataSource = type === 'person' ? relatedPeople : relatedActors
const { aggregationLabel } = useValues(groupsModel)
+ const extraGroups = (extraActors ?? []).filter((extra) => !relatedActors.some((actor) => actor.id === extra.id))
+ const dataSource = type === 'person' ? relatedPeople : [...relatedActors, ...extraGroups]
+
const columns: LemonTableColumns<ActorType> = [
{
title: 'Type',
@@ -50,7 +68,24 @@ export function RelatedGroups({
key: 'id',
render: function RenderActor(_, actor: ActorType) {
if (actor.type === 'group') {
- return <GroupActorDisplay actor={actor} />
+ const isHighlighted = highlightGroupKey != null && actor.group_key === highlightGroupKey
+ return (
+ <div className="flex items-center gap-2">
+ <GroupActorDisplay actor={actor} />
+ {isHighlighted && highlightLabel && (
+ <LemonTag type="muted" size="small">
+ {highlightLabel}
+ </LemonTag>
+ )}
+ {isHighlighted && highlightStale && (
+ <Tooltip title={highlightStaleTooltip}>
+ <LemonTag type="warning" size="small">
+ Stale
+ </LemonTag>
+ </Tooltip>
+ )}
+ </div>
+ )
}
return <PersonDisplay person={actor} withIcon={false} />
},
diff --git a/products/conversations/frontend/scenes/ticket/RelatedGroupsPanel.tsx b/products/conversations/frontend/scenes/ticket/RelatedGroupsPanel.tsx
new file mode 100644
index 000000000000..e5deb16ec0fd
--- /dev/null
+++ b/products/conversations/frontend/scenes/ticket/RelatedGroupsPanel.tsx
@@ -0,0 +1,100 @@
+import { useValues } from 'kea'
+
+import { LemonCollapse } from '@posthog/lemon-ui'
+
+import { RelatedGroups } from 'scenes/groups/RelatedGroups'
+import { relatedGroupsLogic } from 'scenes/groups/relatedGroupsLogic'
+
+import { groupsModel } from '~/models/groupsModel'
+import { ActorType, GroupActorType } from '~/types'
+
+import { creationGroupLogic } from './creationGroupLogic'
+
+const HIGHLIGHT_LABEL = 'Ticket origin'
+const STALE_TOOLTIP =
+ "This group was active when the ticket was created, but it's no longer in the person's recent related groups."
+
+interface RelatedGroupsPanelProps {
+ personUuid: string
+ // The group the ticket was created with (organization group key), snapshotted at creation.
+ organizationId?: string | null
+}
+
+// Append the creation group as its own row only when it exists but isn't already in the person's live
+// related groups (e.g. it aged out of the related-groups window) — so we never duplicate a highlighted
+// row and never hide the snapshot when it's missing from the live list.
+export function shouldShowCreationFallback(
+ organizationId: string | null | undefined,
+ relatedActors: ActorType[],
+ relatedActorsLoading: boolean,
+ orgGroupTypeIndex: number | null
+): boolean {
+ if (!organizationId || relatedActorsLoading || orgGroupTypeIndex === null) {
+ return false
+ }
+ return !relatedActors.some((actor) => actor.type === 'group' && actor.group_key === organizationId)
+}
+
+export function RelatedGroupsPanel({ personUuid, organizationId }: RelatedGroupsPanelProps): JSX.Element {
+ const { relatedActors, relatedActorsLoading } = useValues(
+ relatedGroupsLogic({ groupTypeIndex: null, id: personUuid })
+ )
+ const { groupTypes } = useValues(groupsModel)
+
+ // `organization_id` is always a key of the "organization" group type (set that way at creation),
+ // so we can resolve its index by name when the group has dropped out of the live related list.
+ const orgGroupTypeIndex =
+ Array.from(groupTypes.values()).find((gt) => gt.group_type === 'organization')?.group_type_index ?? null
+
+ const { group: creationGroup } = useValues(
+ creationGroupLogic({ groupTypeIndex: orgGroupTypeIndex, groupKey: organizationId ?? null })
+ )
+
+ const showFallback = shouldShowCreationFallback(
+ organizationId,
+ relatedActors,
+ relatedActorsLoading,
+ orgGroupTypeIndex
+ )
+
+ // When the snapshot group isn't in the live related list, fetch it and append it as a real row
+ // (type + name + link) rather than a bare key, tagged via highlightGroupKey below.
+ const extraActors: ActorType[] =
+ showFallback && creationGroup
+ ? [
+ {
+ type: 'group',
+ id: creationGroup.group_key,
+ group_key: creationGroup.group_key,
+ group_type_index: creationGroup.group_type_index,
+ properties: creationGroup.group_properties,
+ created_at: creationGroup.created_at,
+ } as GroupActorType,
+ ]
+ : []
+
+ return (
+ <LemonCollapse
+ className="bg-surface-primary"
+ defaultActiveKey="related-groups"
+ panels={[
+ {
+ key: 'related-groups',
+ header: 'Related groups',
+ content: (
+ <RelatedGroups
+ id={personUuid}
+ groupTypeIndex={null}
+ embedded
+ highlightGroupKey={organizationId}
+ highlightLabel={HIGHLIGHT_LABEL}
+ highlightStale={showFallback}
+ highlightStaleTooltip={STALE_TOOLTIP}
+ extraActors={extraActors}
+ />
+ ),
+ },
+ ]}
+ />
+ )
+}
diff --git a/products/conversations/frontend/scenes/ticket/SupportTicketScene.tsx b/products/conversations/frontend/scenes/ticket/SupportTicketScene.tsx
index 1b60d6727a49..e82023560018 100644
--- a/products/conversations/frontend/scenes/ticket/SupportTicketScene.tsx
+++ b/products/conversations/frontend/scenes/ticket/SupportTicketScene.tsx
@@ -36,6 +36,7 @@ import { AIPanel } from './AIPanel'
import { ExceptionsPanel } from './ExceptionsPanel'
import { PreviousTicketsPanel } from './PreviousTicketsPanel'
import { RecentEventsPanel } from './RecentEventsPanel'
+import { RelatedGroupsPanel } from './RelatedGroupsPanel'
import { SessionRecordingPanel } from './SessionRecordingPanel'
import { StaffActionsPanel } from './StaffActionsPanel'
import { supportTicketSceneLogic } from './supportTicketSceneLogic'
@@ -60,6 +61,7 @@ export function SupportTicketScene({ ticketId }: { ticketId: string }): JSX.Elem
const logic = supportTicketSceneLogic({ id: ticketId || 'new' })
const {
ticket,
+ person,
ticketLoading,
status,
priority,
@@ -256,14 +258,6 @@ export function SupportTicketScene({ ticketId }: { ticketId: string }): JSX.Elem
</span>
</div>
)}
- {ticket?.organization_id && (
- <div className="flex justify-between items-start gap-2">
- <span className="text-muted-alt shrink-0">Organization</span>
- <span className="text-xs truncate text-right" title={ticket.organization_id}>
- {ticket.organization_id}
- </span>
- </div>
- )}
{ticket?.channel_source === 'email' && ticket?.email_subject && (
<div className="flex justify-between items-start gap-2">
<span className="text-muted-alt shrink-0">Subject</span>
@@ -405,6 +399,11 @@ export function SupportTicketScene({ ticketId }: { ticketId: string }): JSX.Elem
</div>
</LemonCard>
+ {/* Related Groups Panel */}
+ {person?.uuid && (
+ <RelatedGroupsPanel personUuid={person.uuid} organizationId={ticket?.organization_id} />
+ )}
+
{/* Staff Actions Panel */}
{user?.is_staff && ticket && <StaffActionsPanel />}
diff --git a/products/conversations/frontend/scenes/ticket/creationGroupLogic.ts b/products/conversations/frontend/scenes/ticket/creationGroupLogic.ts
new file mode 100644
index 000000000000..0d3b1985604e
--- /dev/null
+++ b/products/conversations/frontend/scenes/ticket/creationGroupLogic.ts
@@ -0,0 +1,56 @@
+import { afterMount, connect, kea, key, path, props } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import api from 'lib/api'
+import { toParams } from 'lib/utils/url'
+import { teamLogic } from 'scenes/teamLogic'
+
+import { Group } from '~/types'
+
+import type { creationGroupLogicType } from './creationGroupLogicType'
+
+export type CreationGroupLogicProps = {
+ groupTypeIndex: number | null
+ groupKey: string | null
+}
+
+// Fetches the group a ticket was created with (its organization_id snapshot) so the related-groups
+// panel can show its name and a working link even when it has dropped out of the live related list.
+export const creationGroupLogic = kea<creationGroupLogicType>([
+ props({} as CreationGroupLogicProps),
+ key((props) => `${props.groupTypeIndex}-${props.groupKey}`),
+ path((key) => ['products', 'conversations', 'frontend', 'scenes', 'ticket', 'creationGroupLogic', key]),
+ connect(() => ({
+ values: [teamLogic, ['currentTeamId']],
+ })),
+ loaders(({ values, props }) => ({
+ group: [
+ null as Group | null,
+ {
+ loadGroup: async () => {
+ if (props.groupTypeIndex === null || !props.groupKey) {
+ return null
+ }
+ // skip_create_notebook: this is a read-only display lookup; without it `groups/find`
+ // lazily creates the group's CRM notebook as a side effect.
+ const params = {
+ group_type_index: props.groupTypeIndex,
+ group_key: props.groupKey,
+ skip_create_notebook: true,
+ }
+ try {
+ // `groups/find` is a cross-product (groups) endpoint with no conversations-generated client.
+ // nosemgrep: prefer-codegen-api
+ return await api.get(`api/projects/${values.currentTeamId}/groups/find?${toParams(params)}`)
+ } catch {
+ // Group may no longer exist (stale snapshot) — degrade to no row rather than a broken link.
+ return null
+ }
+ },
+ },
+ ],
+ })),
+ afterMount(({ actions }) => {
+ actions.loadGroup()
+ }),
+])
diff --git a/products/conversations/frontend/scenes/ticket/relatedGroupsPanel.test.ts b/products/conversations/frontend/scenes/ticket/relatedGroupsPanel.test.ts
new file mode 100644
index 000000000000..08054558ed0c
--- /dev/null
+++ b/products/conversations/frontend/scenes/ticket/relatedGroupsPanel.test.ts
@@ -0,0 +1,20 @@
+import { ActorType } from '~/types'
+
+import { shouldShowCreationFallback } from './RelatedGroupsPanel'
+
+const group = (groupKey: string): ActorType => ({ type: 'group', group_key: groupKey }) as ActorType
+const person = (): ActorType => ({ type: 'person' }) as ActorType
+
+describe('shouldShowCreationFallback', () => {
+ it.each<[string, string | null | undefined, ActorType[], boolean, number | null, boolean]>([
+ ['no organization id', null, [], false, 0, false],
+ ['related groups still loading', 'org-1', [], true, 0, false],
+ ['organization group type not resolvable', 'org-1', [], false, null, false],
+ ['snapshot group is already in the related list', 'org-1', [group('org-1')], false, 0, false],
+ ['snapshot group is not in the related list', 'org-1', [group('other')], false, 0, true],
+ ['no related groups at all', 'org-1', [], false, 0, true],
+ ['matching group present alongside a person', 'org-1', [person(), group('org-1')], false, 0, false],
+ ])('%s', (_label, organizationId, relatedActors, loading, orgGroupTypeIndex, expected) => {
+ expect(shouldShowCreationFallback(organizationId, relatedActors, loading, orgGroupTypeIndex)).toBe(expected)
+ })
+})