Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

useEventDispatch.ts

40 lines1012 Bytes

1'use client'
2​
3import { useCallback } from 'react'
4import type { HandlerResult } from '@/lib/reports/types'
5import { useReportRuntime } from './ReportRuntimeProvider'
6​
7export function useEventDispatch() {
8 const { reportId } = useReportRuntime()
9​
10 const dispatch = useCallback(
11 async (
12 componentId: string,
13 handlerName: string,
14 eventData?: Record<string, unknown>,
15 ): Promise<HandlerResult> => {
16 try {
17 const response = await fetch('/api/report-event', {
18 method: 'POST',
19 headers: { 'Content-Type': 'application/json' },
20 body: JSON.stringify({
21 reportId,
22 componentId,
23 handlerName,
24 eventData,
25 }),
26 })
27 const result = await response.json()
28 return result
29 } catch (error) {
30 return {
31 success: false,
32 error: error instanceof Error ? error.message : 'Unknown error',
33 }
34 }
35 },
36 [reportId],
37 )
38​
39 return { dispatch }