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

Button.tsx

109 lines3.4 KB

1import { useState } from 'react'
2import type { ButtonProps, UIUpdate } from '@/lib/reports/types'
3import { useEventDispatch } from '../../useEventDispatch'
4import { useEffectsHandler } from '../../useEffectsHandler'
5import { useReportRuntime } from '../../ReportRuntimeProvider'
6​
7type ButtonRendererProps = ButtonProps & {
8 handlers?: Record<string, string>
9}
10​
11const variantClasses: Record<NonNullable<ButtonProps['variant']>, string> = {
12 primary: 'bg-cyan-600 text-white hover:bg-cyan-700',
13 secondary: 'bg-gray-700 text-white hover:bg-gray-600',
14 danger: 'bg-red-600 text-white hover:bg-red-700',
15 ghost: 'bg-transparent text-[var(--report-text)] hover:bg-white/10',
17​
18export function Button({
19 id,
20 label,
21 variant = 'primary',
22 disabled = false,
23 handlers,
24}: ButtonRendererProps) {
25 const { dispatch } = useEventDispatch()
26 const { handleEffects } = useEffectsHandler()
27 const { applyUIUpdates } = useReportRuntime()
28 const [isLoading, setIsLoading] = useState(false)
29​
30 const hasHandler = !!handlers?.onPress
31​
32 const handleClick = async () => {
33 if (!hasHandler || disabled || isLoading) return
34​
35 console.log('[Button] Clicked:', id)
36 setIsLoading(true)
37 try {
38 const result = await dispatch(id, 'onPress')
39 console.log('[Button] Handler result:', result)
40​
41 if (result.effects?.length) {
42 console.log('[Button] Processing effects:', result.effects)
43 handleEffects(result.effects)
44 }
45 if (result.uiUpdates?.length) {
46 console.log('[Button] Processing UI updates:', result.uiUpdates)
47 applyUIUpdates(result.uiUpdates)
48 }
49 // Apply refresh results from subscription invalidation
50 if (result.refreshResults?.length) {
51 console.log(
52 '[Button] Processing refresh results:',
53 JSON.stringify(result.refreshResults, null, 2),
54 )
55 const refreshUpdates: UIUpdate[] = result.refreshResults
56 .filter((r) => r.success && r.props)
57 .map((r) => ({
58 type: 'update' as const,
59 componentId: r.componentId,
60 props: r.props,
61 }))
62 console.log(
63 '[Button] Converted to UI updates:',
64 JSON.stringify(refreshUpdates, null, 2),
65 )
66 if (refreshUpdates.length > 0) {
67 console.log(
68 '[Button] Calling applyUIUpdates with',
69 refreshUpdates.length,
70 'updates',
71 )
72 applyUIUpdates(refreshUpdates)
73 } else {
74 console.log('[Button] No valid refresh updates to apply')
75 }
76 } else {
77 console.log('[Button] No refresh results in response')
78 }
79 if (!result.success) {
80 console.log('[Button] Handler failed:', result.error)
81 handleEffects([
82 {
83 type: 'toast',
84 params: {
85 message: result.error || 'Action failed',
86 variant: 'error',
87 },
88 },
89 ])
90 }
91 } catch (error) {
92 console.error('[Button] Exception during handler:', error)
93 } finally {
94 setIsLoading(false)
95 }
96 }
97​
98 return (
99 <button
100 onClick={handleClick}
101 disabled={disabled || isLoading}
102 className={`inline-flex items-center justify-center rounded-lg px-4 py-2 text-sm font-medium transition-colors ${
103 variantClasses[variant]
104 } ${isLoading ? 'opacity-60 cursor-wait' : ''}`}
105 >
106 {isLoading ? 'Working...' : label}
107 </button>
108 )