: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 { MakeLogicType, afterMount, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import { getCurrentTeamId } from 'lib/utils/getAppContext'
import { conversationsTicketsMessagesList } from '../../generated/api'
import type { TicketMessageApi } from '../../generated/api.schemas'
export interface TicketPreviewLogicProps {
ticketId: string
}
export interface TicketPreview {
firstMessages: TicketMessageApi[]
// The newest message in the thread, shown after the truncation divider. Null when the
// whole thread already fits in firstMessages.
lastMessage: TicketMessageApi | null
// How many messages sit between firstMessages and lastMessage (the "+N more" count).
hiddenCount: number
// True when the fetch failed, so the card can distinguish an error from an empty thread.
error: boolean
}
/** How many of the ticket's first messages the hover preview shows. */
export const TICKET_PREVIEW_MESSAGE_COUNT = 3
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketPreviewLogicValues {
preview: TicketPreview | null
previewLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketPreviewLogicActions {
loadPreview: () => any
loadPreviewFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPreviewSuccess: (
preview: TicketPreview,
payload?: any
) => {
preview: TicketPreview
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketPreviewLogicMeta {
key: string
}
export type ticketPreviewLogicType = MakeLogicType<
ticketPreviewLogicValues,
ticketPreviewLogicActions,
TicketPreviewLogicProps,
ticketPreviewLogicMeta
>
export const ticketPreviewLogic = kea<ticketPreviewLogicType>([
props({} as TicketPreviewLogicProps),
key((props) => props.ticketId),
path((key) => ['products', 'conversations', 'frontend', 'components', 'TicketPreview', 'ticketPreviewLogic', key]),
loaders(({ props }) => ({
preview: [
null as TicketPreview | null,
{
loadPreview: async (): Promise<TicketPreview> => {
try {
const teamId = String(getCurrentTeamId())
const first = await conversationsTicketsMessagesList(teamId, props.ticketId, {
limit: TICKET_PREVIEW_MESSAGE_COUNT,
})
// Short threads fit entirely in the first batch, so there's no tail to fetch.
if (first.count <= TICKET_PREVIEW_MESSAGE_COUNT) {
return { firstMessages: first.results, lastMessage: null, hiddenCount: 0, error: false }
}
// The thread is oldest-first with no reverse ordering, so the newest message is
// the last page. Fetch a short window at the tail and take its last item: if a
// reply lands between the two requests, the stale offset lands one short and the
// window still includes the real newest message. Truncation uses the tail
// response's own (fresher) count, not the first request's.
const tail = await conversationsTicketsMessagesList(teamId, props.ticketId, {
limit: 2,
offset: first.count - 1,
})
const lastMessage = tail.results[tail.results.length - 1] ?? null
const hiddenCount = Math.max(0, tail.count - first.results.length - (lastMessage ? 1 : 0))
return { firstMessages: first.results, lastMessage, hiddenCount, error: false }
} catch (e) {
posthog.captureException(e)
return { firstMessages: [], lastMessage: null, hiddenCount: 0, error: true }
}
},
},
],
})),
afterMount(({ actions, values }) => {
if (!values.preview && !values.previewLoading) {
actions.loadPreview()
}
}),
])