: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, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import { match } from 'ts-pattern'
import api from 'lib/api'
import { GitMetadataParser, supportedProviders } from 'lib/components/Git/gitMetadataParser'
import { stackFrameLogic } from './stackFrameLogic'
import type { KeyedStackFrameRecords } from './stackFrameLogic'
export interface SourceData {
url: string
provider: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface framesCodeSourceLogicValues {
stackFrameRecords: KeyedStackFrameRecords // stackFrameLogic
frameSourceUrls: Record<string, SourceData | null>
getSourceDataForFrame: (rawId: string) => SourceData | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface framesCodeSourceLogicActions {
computeSourceUrls: () => {
value: true
}
setSourceData: (data: Record<string, SourceData | null>) => {
data: Record<string, SourceData | null>
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface framesCodeSourceLogicMeta {
__keaTypeGenInternalSelectorTypes: {
getSourceDataForFrame: (
frameSourceUrls: Record<string, SourceData | null>
) => (rawId: string) => SourceData | null
}
}
export type framesCodeSourceLogicType = MakeLogicType<
framesCodeSourceLogicValues,
framesCodeSourceLogicActions,
Record<string, any>,
framesCodeSourceLogicMeta
>
export const framesCodeSourceLogic = kea<framesCodeSourceLogicType>([
path(['components', 'Errors', 'framesCodeSourceLogic']),
connect(() => ({
values: [stackFrameLogic, ['stackFrameRecords']],
})),
actions({
setSourceData: (data: Record<string, SourceData | null>) => ({ data }),
computeSourceUrls: true,
}),
reducers({
frameSourceUrls: [
{} as Record<string, SourceData | null>,
{
setSourceData: (state, { data }) => ({
...state,
...data,
}),
},
],
}),
listeners(({ actions, values }) => ({
computeSourceUrls: async () => {
const records = values.stackFrameRecords
const batchData: Record<string, SourceData | null> = {}
const searchPromises = Object.entries(records).map(async ([rawId, record]) => {
if (values.frameSourceUrls[rawId] !== undefined || !record.contents?.in_app) {
return
}
const codeSample = record.context?.line.line
const remoteUrl = record.release?.metadata?.git?.remote_url
const lineNumber = record.context?.line.number
if (!record.contents.source) {
return
}
const fileName = record.contents.source.split('/').pop()
if (!codeSample || !remoteUrl || !fileName) {
return
}
const parsed = GitMetadataParser.parseRemoteUrl(remoteUrl)
if (!parsed || !supportedProviders.includes(parsed.provider)) {
return
}
const resolveMethod = match(parsed.provider)
.with('github', () => api.gitProviderFileLinks.resolveGithub)
.with('gitlab', () => api.gitProviderFileLinks.resolveGitlab)
.otherwise(() => null)
if (!resolveMethod) {
return
}
const result = await resolveMethod(parsed.owner, parsed.repository, codeSample, fileName)
if (!result.url) {
// Nothing to provide here
return
}
let url = result.url ?? null
if (url && lineNumber) {
url = `${url}#L${lineNumber}`
}
batchData[rawId] = {
url,
provider: parsed.provider,
}
})
await Promise.all(searchPromises)
actions.setSourceData(batchData)
},
})),
subscriptions(({ actions }) => ({
stackFrameRecords: (stackFrameRecords) => {
if (Object.keys(stackFrameRecords).length > 0) {
actions.computeSourceUrls()
}
},
})),
selectors({
getSourceDataForFrame: [
(s) => [s.frameSourceUrls],
(frameSourceUrls: Record<string, SourceData | null>) => (rawId: string) => frameSourceUrls[rawId] || null,
],
}),
])