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(notebooks): show notebook title when pasting a notebook link (#58039)

posthog[bot]84704fe7f2e3
diff --git a/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.test.tsx b/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.test.tsx
new file mode 100644
index 000000000000..3c80de248231
--- /dev/null
+++ b/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.test.tsx
@@ -0,0 +1,89 @@
+import { BACKLINK_MAP } from './NotebookNodeBacklink'
+
+const stripQueryAndHash = (path: string): string => path.split(/[?#]/)[0]
+
+describe('NotebookNodeBacklink BACKLINK_MAP', () => {
+ const findConfig = (type: string): (typeof BACKLINK_MAP)[number] => {
+ const config = BACKLINK_MAP.find((c) => c.type === type)
+ if (!config) {
+ throw new Error(`No BACKLINK_MAP entry for type "${type}"`)
+ }
+ return config
+ }
+
+ describe('notebooks', () => {
+ const notebooks = findConfig('notebooks')
+
+ it.each([
+ ['no project prefix', '/notebooks/abc123', 'abc123'],
+ ['with project prefix', '/project/2/notebooks/abc123', 'abc123'],
+ ['with query string', '/notebooks/abc123?source=share', 'abc123'],
+ ['with hash', '/notebooks/abc123#section', 'abc123'],
+ ['project prefix and query string', '/project/2/notebooks/abc123?foo=bar', 'abc123'],
+ ])('extracts the short_id from path with %s', (_label, path, expectedId) => {
+ const stripped = stripQueryAndHash(path)
+ expect(notebooks.regex.test(stripped)).toBe(true)
+ expect(notebooks.regex.exec(stripped)?.[1]).toBe(expectedId)
+ })
+ })
+
+ describe('dashboards', () => {
+ const dashboards = findConfig('dashboards')
+
+ it.each([
+ ['no project prefix', '/dashboard/4', '4'],
+ ['with project prefix', '/project/2/dashboard/4', '4'],
+ ])('extracts the dashboard id from path with %s', (_label, path, expectedId) => {
+ const stripped = stripQueryAndHash(path)
+ expect(dashboards.regex.test(stripped)).toBe(true)
+ expect(dashboards.regex.exec(stripped)?.[1]).toBe(expectedId)
+ })
+ })
+
+ describe('feature_flags', () => {
+ const flags = findConfig('feature_flags')
+
+ it.each([
+ ['no project prefix', '/feature_flags/42', '42'],
+ ['with project prefix', '/project/2/feature_flags/42', '42'],
+ ])('extracts the flag id from path with %s', (_label, path, expectedId) => {
+ const stripped = stripQueryAndHash(path)
+ expect(flags.regex.test(stripped)).toBe(true)
+ expect(flags.regex.exec(stripped)?.[1]).toBe(expectedId)
+ })
+ })
+
+ describe('persons', () => {
+ const persons = findConfig('persons')
+
+ it.each([
+ ['no project prefix', '/person/user-distinct-id', 'user-distinct-id'],
+ ['with project prefix', '/project/2/person/user-distinct-id', 'user-distinct-id'],
+ ])('extracts the distinct_id from path with %s', (_label, path, expectedId) => {
+ const stripped = stripQueryAndHash(path)
+ expect(persons.regex.test(stripped)).toBe(true)
+ expect(persons.regex.exec(stripped)?.[1]).toBe(expectedId)
+ })
+ })
+
+ describe('replay', () => {
+ const replay = findConfig('replay')
+
+ it.each([
+ [
+ 'no project prefix',
+ '/replay/0193abcd-ef01-7000-9999-aaaaaaaaaaaa',
+ '0193abcd-ef01-7000-9999-aaaaaaaaaaaa',
+ ],
+ [
+ 'with project prefix',
+ '/project/2/replay/0193abcd-ef01-7000-9999-aaaaaaaaaaaa',
+ '0193abcd-ef01-7000-9999-aaaaaaaaaaaa',
+ ],
+ ])('extracts the session id from path with %s', (_label, path, expectedId) => {
+ const stripped = stripQueryAndHash(path)
+ expect(replay.regex.test(stripped)).toBe(true)
+ expect(replay.regex.exec(stripped)?.[1]).toBe(expectedId)
+ })
+ })
+})
diff --git a/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.tsx b/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.tsx
index 807e469668d1..23c6900bdec7 100644
--- a/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.tsx
+++ b/frontend/src/scenes/notebooks/Nodes/NotebookNodeBacklink.tsx
@@ -35,117 +35,106 @@ type BackLinkMapper = {
regex: RegExp
type: string
icon: JSX.Element
- getTitle: (match: string) => Promise<string>
+ getTitle: (id: string) => Promise<string>
}
-const BACKLINK_MAP: BackLinkMapper[] = [
+export const BACKLINK_MAP: BackLinkMapper[] = [
{
type: 'dashboards',
- regex: new RegExp(urls.dashboard('(.+)')),
+ regex: new RegExp(urls.dashboard('([^/]+)')),
icon: <IconDashboard />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const dashboard = await api.dashboards.get(Number(id))
- return dashboard.name ?? ''
+ return dashboard.name || id
},
},
{
type: 'insights',
- regex: new RegExp(urls.insightView('(.+)' as QueryBasedInsightModel['short_id'])),
+ regex: new RegExp(urls.insightView('([^/]+)' as QueryBasedInsightModel['short_id'])),
icon: <IconGraph />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const insight = await api.insights.loadInsight(id as QueryBasedInsightModel['short_id'])
- return insight.results[0]?.name ?? ''
+ return insight.results[0]?.name || id
},
},
{
type: 'feature_flags',
- regex: new RegExp(urls.featureFlag('(.+)')),
+ regex: new RegExp(urls.featureFlag('([^/]+)')),
icon: <IconFlag />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const flag = await api.featureFlags.get(Number(id))
- return flag.name ?? ''
+ return flag.name || flag.key || id
},
},
{
type: 'experiments',
- regex: new RegExp(urls.experiment('(.+)')),
+ regex: new RegExp(urls.experiment('([^/]+)')),
icon: <IconFlask />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const experiment = await api.experiments.get(Number(id))
- return experiment.name ?? ''
+ return experiment.name || id
},
},
{
type: 'surveys',
- regex: new RegExp(urls.survey('(.+)')),
+ regex: new RegExp(urls.survey('([^/]+)')),
icon: <IconChat />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const survey = await api.surveys.get(id)
- return survey.name ?? ''
+ return survey.name || id
},
},
{
type: 'events',
- regex: new RegExp(urls.eventDefinition('(.+)')),
+ regex: new RegExp(urls.eventDefinition('([^/]+)')),
icon: <IconLive width="1em" height="1em" />,
- getTitle: async (path: string) => {
- const id = path.split('/')[3]
+ getTitle: async (id: string) => {
const event = await api.eventDefinitions.get({ eventDefinitionId: id })
- return event.name ?? ''
+ return event.name || id
},
},
{
type: 'persons',
- regex: new RegExp(urls.personByDistinctId('(.+)')),
+ regex: new RegExp(urls.personByDistinctId('([^/]+)', false)),
icon: <IconPerson />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const response = await api.persons.list({ distinct_id: id })
- return response.results[0]?.name ?? ''
+ return response.results[0]?.name || id
},
},
{
type: 'cohorts',
- regex: new RegExp(urls.cohort('(.+)')),
+ regex: new RegExp(urls.cohort('([^/]+)')),
icon: <IconPeople />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const cohort = await api.cohorts.get(Number(id))
- return cohort.name ?? ''
+ return cohort.name || id
},
},
{
type: 'playlist',
- regex: new RegExp(urls.replayPlaylist('(.+)')),
+ regex: new RegExp(urls.replayPlaylist('([^/]+)')),
icon: <IconPlaylist />,
- getTitle: async (path: string) => {
- const id = path.split('/')[3]
+ getTitle: async (id: string) => {
const playlist = await api.recordings.getPlaylist(id)
- return playlist.name ?? 'None'
+ return playlist.name || id
},
},
{
type: 'replay',
- regex: new RegExp(urls.replaySingle('(.+)')),
+ regex: new RegExp(urls.replaySingle('([^/]+)')),
icon: <IconRewindPlay />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
return id
},
},
{
type: 'notebooks',
- regex: new RegExp(urls.notebook('(.+)')),
+ regex: new RegExp(urls.notebook('([^/]+)')),
icon: <IconNotebook />,
- getTitle: async (path: string) => {
- const id = path.split('/')[2]
+ getTitle: async (id: string) => {
const notebook = await api.notebooks.get(id)
- return notebook.title ?? ''
+ return notebook.title || 'Untitled'
},
},
]
@@ -155,18 +144,20 @@ const Component = (props: NodeViewProps): JSX.Element => {
const { location } = useValues(router)
const href: string = props.node.attrs.href ?? ''
+ const hrefWithoutQuery: string = href.split(/[?#]/)[0]
- const backLinkConfig = BACKLINK_MAP.find((config) => config.regex.test(href))
+ const backLinkConfig = BACKLINK_MAP.find((config) => config.regex.test(hrefWithoutQuery))
+ const matchedId = backLinkConfig?.regex.exec(hrefWithoutQuery)?.[1]
const derivedText: string = props.node.attrs.title || props.node.attrs.href
const isViewing = location.pathname === href
useEffect(() => {
- if (props.node.attrs.title || !backLinkConfig) {
+ if (props.node.attrs.title || !backLinkConfig || !matchedId) {
return
}
void backLinkConfig
- .getTitle(href)
+ .getTitle(matchedId)
.then((title) => {
props.updateAttributes({
title,