1- import type { PostHogAPIClient } from "@posthog/api-client/posthog-client" ;
21import { useServiceOptional } from "@posthog/di/react" ;
3- import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events" ;
4- import type { UserBasic } from "@posthog/shared/domain-types" ;
5- import { useOptionalAuthenticatedClient } from "@posthog/ui/features/auth/authClient" ;
62import {
73 type CanvasTerminalStatus ,
84 hasCanvasGenerationStarted ,
95 isCanvasGenerating ,
106 resolveCanvasGenerationStatus ,
117} from "@posthog/ui/features/canvas/freeform/canvasGenerationStatus" ;
12- import { buildCanvasGenerationThreadPosts } from "@posthog/ui/features/canvas/freeform/canvasThreadAutoPost" ;
13- import { taskThreadQueryKey } from "@posthog/ui/features/canvas/hooks/useTaskThread" ;
14- import {
15- type TrackedCanvasGeneration ,
16- useCanvasGenerationTrackerStore ,
17- } from "@posthog/ui/features/canvas/stores/canvasGenerationTrackerStore" ;
8+ import { useCanvasGenerationTrackerStore } from "@posthog/ui/features/canvas/stores/canvasGenerationTrackerStore" ;
189import { NotificationBus } from "@posthog/ui/features/notifications/notifications" ;
1910import { useSessionStore } from "@posthog/ui/features/sessions/sessionStore" ;
2011import { taskDetailQuery } from "@posthog/ui/features/tasks/queries" ;
21- import { track } from "@posthog/ui/shell/analytics" ;
22- import {
23- type QueryClient ,
24- useQueries ,
25- useQueryClient ,
26- } from "@tanstack/react-query" ;
12+ import { useQueries } from "@tanstack/react-query" ;
2713import { useEffect , useMemo , useRef } from "react" ;
2814
2915// Poll cadence for the run status of a tracked generation task. Matches the
@@ -66,38 +52,6 @@ function emitCanvasGenerationNotification(
6652 // "cancelled" is user-initiated — stay silent.
6753}
6854
69- // Drop the finished generation's updates into the task's thread: the one-time
70- // "[name](link) has been created" comment and a turn-complete note tagging the
71- // task creator. Posted by the client that started the generation (the thread
72- // API has no agent author), best-effort per message.
73- async function postThreadUpdates (
74- client : PostHogAPIClient ,
75- queryClient : QueryClient ,
76- entry : TrackedCanvasGeneration ,
77- status : CanvasTerminalStatus ,
78- creator : UserBasic | null | undefined ,
79- ) : Promise < void > {
80- for ( const post of buildCanvasGenerationThreadPosts ( entry , status , creator ) ) {
81- let success = true ;
82- try {
83- await client . createTaskThreadMessage ( entry . taskId , post . content ) ;
84- } catch {
85- success = false ;
86- }
87- track ( ANALYTICS_EVENTS . CHANNEL_ACTION , {
88- action_type : "thread_auto_post" ,
89- surface : "canvas" ,
90- channel_id : entry . channelId ,
91- task_id : entry . taskId ,
92- auto_post_kind : post . kind ,
93- success,
94- } ) ;
95- }
96- void queryClient . invalidateQueries ( {
97- queryKey : taskThreadQueryKey ( entry . taskId ) ,
98- } ) ;
99- }
100-
10155// Watches every canvas generation started in this client (registered in the
10256// tracker store) and fires a toast — with a link to the canvas — the moment each
10357// one stops generating. Mounted on the persistent channel layout so it keeps
@@ -116,10 +70,6 @@ export function useCanvasGenerationToasts(): void {
11670 const bus = useServiceOptional < NotificationBus > ( NotificationBus ) ;
11771 const busRef = useRef ( bus ) ;
11872 busRef . current = bus ;
119- const client = useOptionalAuthenticatedClient ( ) ;
120- const clientRef = useRef ( client ) ;
121- clientRef . current = client ;
122- const queryClient = useQueryClient ( ) ;
12373
12474 const taskIds = useMemo ( ( ) => Object . keys ( tracked ) , [ tracked ] ) ;
12575
@@ -146,7 +96,7 @@ export function useCanvasGenerationToasts(): void {
14696 latestRun,
14797 session,
14898 } ) ;
149- return { id, generating, latestRun, session, task : details [ i ] ?. data } ;
99+ return { id, generating, latestRun, session } ;
150100 } ) ;
151101
152102 // A stable signature so the transition effect only runs on real changes.
@@ -189,23 +139,15 @@ export function useCanvasGenerationToasts(): void {
189139
190140 toastedRef . current . add ( st . id ) ;
191141 const entry = useCanvasGenerationTrackerStore . getState ( ) . tracked [ st . id ] ;
192- if ( entry ) {
193- const status = resolveCanvasGenerationStatus ( {
194- latestRun : st . latestRun ,
195- session : st . session ,
196- } ) ;
197- if ( busRef . current ) {
198- emitCanvasGenerationNotification ( busRef . current , entry , status ) ;
199- }
200- if ( clientRef . current ) {
201- void postThreadUpdates (
202- clientRef . current ,
203- queryClient ,
204- entry ,
205- status ,
206- st . task ?. created_by ,
207- ) ;
208- }
142+ if ( entry && busRef . current ) {
143+ emitCanvasGenerationNotification (
144+ busRef . current ,
145+ entry ,
146+ resolveCanvasGenerationStatus ( {
147+ latestRun : st . latestRun ,
148+ session : st . session ,
149+ } ) ,
150+ ) ;
209151 }
210152 // Stop tracking (and polling) this task now that it's done.
211153 untrack ( st . id ) ;
0 commit comments