-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Render PostHog URLs as rich preview chips #2355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -600,6 +600,10 @@ export class PostHogAPIClient { | |
| throw new Error("No team found for user"); | ||
| } | ||
|
|
||
| async getDefaultProjectId(): Promise<number> { | ||
| return this.getTeamId(); | ||
| } | ||
|
|
||
| async getCurrentUser() { | ||
| const data = await this.api.get("/api/users/{uuid}/", { | ||
| path: { uuid: "@me" }, | ||
|
|
@@ -2887,4 +2891,180 @@ export class PostHogAPIClient { | |
| } | ||
| return (await response.json()) as SpendAnalysisResponse; | ||
| } | ||
|
|
||
| async getFeatureFlag( | ||
| projectId: string, | ||
| flagId: string, | ||
| ): Promise<{ name: string; key: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/feature_flags/${encodeURIComponent(flagId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string; key?: string }; | ||
| return { name: data.name ?? "", key: data.key ?? "" }; | ||
| } | ||
|
|
||
| async getExperiment( | ||
| projectId: string, | ||
| experimentId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/experiments/${encodeURIComponent(experimentId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getInsight( | ||
| projectId: string, | ||
| insightId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/insights/${encodeURIComponent(insightId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getDashboard( | ||
| projectId: string, | ||
| dashboardId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/dashboards/${encodeURIComponent(dashboardId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getErrorTrackingGroup( | ||
| projectId: string, | ||
| groupId: string, | ||
| ): Promise<{ title: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/error_tracking/${encodeURIComponent(groupId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { title?: string }; | ||
| return { title: data.title ?? "" }; | ||
| } | ||
|
|
||
| async getRecording( | ||
| projectId: string, | ||
| recordingId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/session_recordings/${encodeURIComponent(recordingId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getSurvey( | ||
| projectId: string, | ||
| surveyId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/surveys/${encodeURIComponent(surveyId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getNotebook( | ||
| projectId: string, | ||
| notebookId: string, | ||
| ): Promise<{ title: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/notebooks/${encodeURIComponent(notebookId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { title?: string }; | ||
| return { title: data.title ?? "" }; | ||
| } | ||
|
|
||
| async getCohort( | ||
| projectId: string, | ||
| cohortId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/cohorts/${encodeURIComponent(cohortId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getAction( | ||
| projectId: string, | ||
| actionId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/actions/${encodeURIComponent(actionId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
|
|
||
| async getEarlyAccessFeature( | ||
| projectId: string, | ||
| featureId: string, | ||
| ): Promise<{ name: string } | null> { | ||
| const urlPath = `/api/projects/${encodeURIComponent(projectId)}/early_access_feature/${encodeURIComponent(featureId)}/`; | ||
| const url = new URL(`${this.api.baseUrl}${urlPath}`); | ||
| const response = await this.api.fetcher.fetch({ | ||
| method: "get", | ||
| url, | ||
| path: urlPath, | ||
| }); | ||
| if (!response.ok) return null; | ||
| const data = (await response.json()) as { name?: string }; | ||
| return { name: data.name ?? "" }; | ||
| } | ||
| } | ||
|
Comment on lines
2891
to
3070
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All 11 new methods share an identical 6-line body: build Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/code/src/renderer/api/posthogClient.ts
Line: 2891-3070
Comment:
**Repeated fetch pattern violates OnceAndOnlyOnce**
All 11 new methods share an identical 6-line body: build `urlPath`, construct a `URL`, call `this.api.fetcher.fetch`, return `null` on non-ok, cast the JSON, and return the relevant field. A private helper (e.g. `fetchProjectResource(endpoint, projectId, resourceId): Promise<unknown>`) would let each public method reduce to a one-liner field-pluck, making future endpoint additions or error-handling changes apply in one place.
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { PostHogResourceType } from "@features/message-editor/utils/posthogUrl"; | ||
| import { | ||
| BugIcon, | ||
| ChartLineIcon, | ||
| ClipboardTextIcon, | ||
| FlagIcon, | ||
| FlaskIcon, | ||
| LightningIcon, | ||
| NotebookIcon, | ||
| RocketLaunchIcon, | ||
| SquaresFourIcon, | ||
| UsersThreeIcon, | ||
| VideoIcon, | ||
| } from "@phosphor-icons/react"; | ||
| import { Chip } from "@posthog/quill"; | ||
| import type { ComponentType, ReactNode } from "react"; | ||
|
|
||
| const resourceIconMap: Record< | ||
| PostHogResourceType, | ||
| ComponentType<{ size: number }> | ||
| > = { | ||
| feature_flag: FlagIcon, | ||
| experiment: FlaskIcon, | ||
| insight: ChartLineIcon, | ||
| dashboard: SquaresFourIcon, | ||
| error_tracking: BugIcon, | ||
| recording: VideoIcon, | ||
| survey: ClipboardTextIcon, | ||
| notebook: NotebookIcon, | ||
| cohort: UsersThreeIcon, | ||
| action: LightningIcon, | ||
| early_access_feature: RocketLaunchIcon, | ||
| }; | ||
|
|
||
| export function PostHogRefChip({ | ||
| href, | ||
| resourceType, | ||
| children, | ||
| }: { | ||
| href: string; | ||
| resourceType: PostHogResourceType; | ||
| children: ReactNode; | ||
| }) { | ||
| const Icon = resourceIconMap[resourceType]; | ||
| return ( | ||
| <Chip | ||
| size="xs" | ||
| onClick={() => window.open(href, "_blank")} | ||
| className="cli-file-mention mx-0.5 max-w-full cursor-pointer! whitespace-nowrap pl-1 align-middle active:translate-y-0" | ||
| > | ||
| <Icon size={10} /> | ||
| <span className="min-w-0 truncate">{children}</span> | ||
| </Chip> | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getDefaultProjectIddoes nothing but delegate togetTeamId. The one caller inposthogChip.tscould callgetTeamId()directly (with its ownString()cast), orgetDefaultProjectIdcould at least drop the unnecessaryasynckeyword since it is a singlereturnof an already-Promise-returning method.Prompt To Fix With AI