-
Notifications
You must be signed in to change notification settings - Fork 11
feat(inbox): Allow sharing Inbox reports with posthog-code://inbox/ URL
#1757
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
Open
sortafreel
wants to merge
15
commits into
main
Choose a base branch
from
signals/share-inbox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3d1576d
refactor(inbox): Fetch selected report by id when off the current lis…
sortafreel c2c5841
feat(inbox): Add resetFilters action to signals filter store
sortafreel cbdaca2
feat(main): Register Inbox deep-link handler
sortafreel fc174ff
feat(inbox): Open reports from posthog-code://inbox/:reportId deep links
sortafreel 792735d
feat(inbox): Add shareable-link button to report detail pane
sortafreel f5fb259
chore: Tests.
sortafreel 470f9c2
fix: Don't cache deeplink results.
sortafreel 6c7f954
fix: Avoid clearing filters if report doesn't exist.
sortafreel 3253850
fix: Process 404/403 properly.
sortafreel 41f7a0c
fix: Add refetch interval for deeplinks.
sortafreel be3e4f8
fix: Avoid snapping the list back on the scroll.
sortafreel ccd41d9
fix: Stale state for failed deeplink.
sortafreel b276f20
fix: Keep in sync.
sortafreel db54279
Extract out useInboxDeepLinkListSync hook
Twixes 6985a06
Move useInboxDeepLinkListSync
Twixes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import type { IMainWindow } from "@posthog/platform/main-window"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("../../utils/logger.js", () => ({ | ||
| logger: { | ||
| scope: () => ({ | ||
| info: vi.fn(), | ||
| error: vi.fn(), | ||
| warn: vi.fn(), | ||
| debug: vi.fn(), | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| import type { DeepLinkHandler, DeepLinkService } from "../deep-link/service"; | ||
| import { InboxLinkEvent, InboxLinkService } from "./service"; | ||
|
|
||
| function makeDeepLinkService() { | ||
| const handlers = new Map<string, DeepLinkHandler>(); | ||
| const service = { | ||
| registerHandler: vi.fn((key: string, handler: DeepLinkHandler) => { | ||
| handlers.set(key, handler); | ||
| }), | ||
| trigger: (key: string, path: string) => { | ||
| const handler = handlers.get(key); | ||
| if (!handler) throw new Error(`No handler for ${key}`); | ||
| return handler(path, new URLSearchParams()); | ||
| }, | ||
| }; | ||
| return service as unknown as DeepLinkService & { | ||
| trigger: (key: string, path: string) => boolean; | ||
| }; | ||
| } | ||
|
|
||
| function makeMainWindow() { | ||
| return { | ||
| focus: vi.fn(), | ||
| restore: vi.fn(), | ||
| isMinimized: vi.fn().mockReturnValue(false), | ||
| } as unknown as IMainWindow & { | ||
| focus: ReturnType<typeof vi.fn>; | ||
| restore: ReturnType<typeof vi.fn>; | ||
| isMinimized: ReturnType<typeof vi.fn>; | ||
| }; | ||
| } | ||
|
|
||
| describe("InboxLinkService", () => { | ||
| let deepLinkService: ReturnType<typeof makeDeepLinkService>; | ||
| let mainWindow: ReturnType<typeof makeMainWindow>; | ||
| let service: InboxLinkService; | ||
|
|
||
| beforeEach(() => { | ||
| deepLinkService = makeDeepLinkService(); | ||
| mainWindow = makeMainWindow(); | ||
| service = new InboxLinkService(deepLinkService, mainWindow); | ||
| }); | ||
|
|
||
| it("registers an 'inbox' handler on the DeepLinkService", () => { | ||
| expect(deepLinkService.registerHandler).toHaveBeenCalledWith( | ||
| "inbox", | ||
| expect.any(Function), | ||
| ); | ||
| }); | ||
|
|
||
| it("emits OpenReport when a listener is attached", () => { | ||
| const listener = vi.fn(); | ||
| service.on(InboxLinkEvent.OpenReport, listener); | ||
|
|
||
| const result = deepLinkService.trigger("inbox", "abc-123"); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(listener).toHaveBeenCalledWith({ reportId: "abc-123" }); | ||
| }); | ||
|
|
||
| it("queues a pending deep link when no listener is attached", () => { | ||
| deepLinkService.trigger("inbox", "pending-id"); | ||
|
|
||
| const pending = service.consumePendingDeepLink(); | ||
| expect(pending).toEqual({ reportId: "pending-id" }); | ||
|
|
||
| // Draining clears it | ||
| expect(service.consumePendingDeepLink()).toBeNull(); | ||
| }); | ||
|
|
||
| it("takes only the first path segment as the report id", () => { | ||
| const listener = vi.fn(); | ||
| service.on(InboxLinkEvent.OpenReport, listener); | ||
|
|
||
| deepLinkService.trigger("inbox", "abc-123/extra/segments"); | ||
|
|
||
| expect(listener).toHaveBeenCalledWith({ reportId: "abc-123" }); | ||
| }); | ||
|
|
||
| it("returns false and does not emit when the path is empty", () => { | ||
| const listener = vi.fn(); | ||
| service.on(InboxLinkEvent.OpenReport, listener); | ||
|
|
||
| const result = deepLinkService.trigger("inbox", ""); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(listener).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("focuses the main window on link arrival", () => { | ||
| deepLinkService.trigger("inbox", "abc-123"); | ||
|
|
||
| expect(mainWindow.focus).toHaveBeenCalledTimes(1); | ||
| expect(mainWindow.restore).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("restores the main window when it is minimized", () => { | ||
| mainWindow.isMinimized.mockReturnValue(true); | ||
|
|
||
| deepLinkService.trigger("inbox", "abc-123"); | ||
|
|
||
| expect(mainWindow.restore).toHaveBeenCalledTimes(1); | ||
| expect(mainWindow.focus).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import type { IMainWindow } from "@posthog/platform/main-window"; | ||
| import { inject, injectable } from "inversify"; | ||
| import { MAIN_TOKENS } from "../../di/tokens"; | ||
| import { logger } from "../../utils/logger"; | ||
| import { TypedEventEmitter } from "../../utils/typed-event-emitter"; | ||
| import type { DeepLinkService } from "../deep-link/service"; | ||
|
|
||
| const log = logger.scope("inbox-link-service"); | ||
|
|
||
| export const InboxLinkEvent = { | ||
| OpenReport: "openReport", | ||
| } as const; | ||
|
|
||
| export interface InboxLinkEvents { | ||
| [InboxLinkEvent.OpenReport]: { reportId: string }; | ||
| } | ||
|
|
||
| export interface PendingInboxDeepLink { | ||
| reportId: string; | ||
| } | ||
|
|
||
| @injectable() | ||
| export class InboxLinkService extends TypedEventEmitter<InboxLinkEvents> { | ||
| private pendingDeepLink: PendingInboxDeepLink | null = null; | ||
|
|
||
| constructor( | ||
| @inject(MAIN_TOKENS.DeepLinkService) | ||
| private readonly deepLinkService: DeepLinkService, | ||
| @inject(MAIN_TOKENS.MainWindow) | ||
| private readonly mainWindow: IMainWindow, | ||
| ) { | ||
| super(); | ||
|
|
||
| this.deepLinkService.registerHandler("inbox", (path) => | ||
| this.handleInboxLink(path), | ||
| ); | ||
| } | ||
|
|
||
| private handleInboxLink(path: string): boolean { | ||
| // path format: "abc123" from posthog-code://inbox/abc123 | ||
| const reportId = path.split("/")[0]; | ||
|
|
||
| if (!reportId) { | ||
| log.warn("Inbox link missing report ID"); | ||
| return false; | ||
| } | ||
|
|
||
| const hasListeners = this.listenerCount(InboxLinkEvent.OpenReport) > 0; | ||
|
|
||
| if (hasListeners) { | ||
| log.info(`Emitting inbox link event: reportId=${reportId}`); | ||
| this.emit(InboxLinkEvent.OpenReport, { reportId }); | ||
| } else { | ||
| log.info( | ||
| `Queueing inbox link (renderer not ready): reportId=${reportId}`, | ||
| ); | ||
| this.pendingDeepLink = { reportId }; | ||
|
sortafreel marked this conversation as resolved.
|
||
| } | ||
|
|
||
| log.info("Deep link focusing window", { reportId }); | ||
| if (this.mainWindow.isMinimized()) { | ||
| this.mainWindow.restore(); | ||
| } | ||
| this.mainWindow.focus(); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public consumePendingDeepLink(): PendingInboxDeepLink | null { | ||
| const pending = this.pendingDeepLink; | ||
| this.pendingDeepLink = null; | ||
| if (pending) { | ||
| log.info(`Consumed pending inbox link: reportId=${pending.reportId}`); | ||
| } | ||
| return pending; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.