@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
2
2
import {
3
3
useBotId ,
4
4
RcbUserSubmitTextEvent ,
5
+ RcbUserUploadFileEvent ,
5
6
useToasts ,
6
7
useFlow ,
7
8
useStyles ,
@@ -31,67 +32,116 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
31
32
32
33
useEffect ( ( ) => {
33
34
/**
34
- * Handles the user submitting input event.
35
+ * Handles the user submitting text input event.
35
36
*
36
- * @param event event emitted when user submits input
37
+ * @param event Event emitted when user submits text input.
37
38
*/
38
- const handleUserSubmitText = ( event : RcbUserSubmitTextEvent ) : void => {
39
- // gets validator and if no validator, return
40
- const validator = getValidator ( event , getBotId ( ) , getFlow ( ) ) ;
39
+ const handleUserSubmitText = ( event : Event ) : void => {
40
+ const rcbEvent = event as RcbUserSubmitTextEvent ;
41
+
42
+ // Get validator and if no validator, return
43
+ const validator = getValidator < string > (
44
+ rcbEvent ,
45
+ getBotId ( ) ,
46
+ getFlow ( ) ,
47
+ "validateTextInput"
48
+ ) ;
41
49
if ( ! validator ) {
42
50
return ;
43
51
}
44
52
45
- // gets and checks validation result
53
+ // Get and check validation result
46
54
const validationResult = validator (
47
- event . data . inputText
55
+ rcbEvent . data . inputText
48
56
) as ValidationResult ;
49
57
if ( ! validationResult ?. success ) {
50
58
event . preventDefault ( ) ;
51
59
}
52
60
53
- // if nothing to prompt, return
61
+ // If nothing to prompt, return
54
62
if ( ! validationResult . promptContent ) {
55
63
return ;
56
64
}
57
65
58
- // if this is the first plugin toast, preserve original styles for restoration later
66
+ // Preserve original styles if this is the first plugin toast
59
67
if ( numPluginToasts === 0 ) {
60
- originalStyles . current = structuredClone ( styles )
68
+ originalStyles . current = structuredClone ( styles ) ;
61
69
}
62
70
const promptStyles = getPromptStyles (
63
71
validationResult ,
64
72
mergedPluginConfig
65
73
) ;
66
74
67
- // update toast with prompt styles
75
+ // Update styles with prompt styles
68
76
updateStyles ( promptStyles ) ;
69
77
70
- // shows prompt toast to user
78
+ // Show prompt toast to user
71
79
showToast (
72
80
validationResult . promptContent ,
73
81
validationResult . promptDuration ?? 3000
74
82
) ;
75
83
76
- // increases number of plugin toasts by 1
84
+ // Increase number of plugin toasts by 1
77
85
setNumPluginToasts ( ( prev ) => prev + 1 ) ;
78
86
} ;
79
87
88
+ const handleUserUploadFile = ( event : Event ) : void => {
89
+ const rcbEvent = event as RcbUserUploadFileEvent ;
90
+ const file : File | undefined = rcbEvent . data ?. files ?. [ 0 ] ;
91
+
92
+ if ( ! file ) {
93
+ console . error ( "No file uploaded." ) ;
94
+ event . preventDefault ( ) ;
95
+ return ;
96
+ }
97
+
98
+ const validator = getValidator < File > (
99
+ rcbEvent ,
100
+ getBotId ( ) ,
101
+ getFlow ( ) ,
102
+ "validateFileInput"
103
+ ) ;
104
+
105
+ if ( ! validator ) {
106
+ console . error ( "Validator not found for file input." ) ;
107
+ return ;
108
+ }
109
+
110
+ const validationResult = validator ( file ) ;
111
+
112
+ if ( ! validationResult . success ) {
113
+ console . error ( "Validation failed:" , validationResult ) ;
114
+ if ( validationResult . promptContent ) {
115
+ showToast (
116
+ validationResult . promptContent ,
117
+ validationResult . promptDuration ?? 3000
118
+ ) ;
119
+ }
120
+ event . preventDefault ( ) ;
121
+ return ;
122
+ }
123
+
124
+ console . log ( "Validation successful:" , validationResult ) ;
125
+ } ;
126
+
80
127
/**
81
128
* Handles the dismiss toast event.
82
129
*
83
- * @param event event emitted when toast is dismissed
130
+ * @param event Event emitted when toast is dismissed.
84
131
*/
85
132
const handleDismissToast = ( ) : void => {
86
133
setNumPluginToasts ( ( prev ) => prev - 1 ) ;
87
134
} ;
88
135
89
- // adds required events
136
+ // Add required event listeners
90
137
window . addEventListener ( "rcb-user-submit-text" , handleUserSubmitText ) ;
138
+ window . addEventListener ( "rcb-user-upload-file" , handleUserUploadFile ) ;
91
139
window . addEventListener ( "rcb-dismiss-toast" , handleDismissToast ) ;
92
140
93
141
return ( ) => {
142
+ // Remove event listeners
94
143
window . removeEventListener ( "rcb-user-submit-text" , handleUserSubmitText ) ;
144
+ window . removeEventListener ( "rcb-user-upload-file" , handleUserUploadFile ) ;
95
145
window . removeEventListener ( "rcb-dismiss-toast" , handleDismissToast ) ;
96
146
} ;
97
147
} , [
@@ -101,28 +151,29 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
101
151
updateStyles ,
102
152
styles ,
103
153
mergedPluginConfig ,
104
- numPluginToasts
154
+ numPluginToasts ,
105
155
] ) ;
106
156
107
- // restores original styles when plugin toasts are all dismissed
157
+ // Restore original styles when all plugin toasts are dismissed
108
158
useEffect ( ( ) => {
109
159
if ( numPluginToasts === 0 ) {
110
160
setTimeout ( ( ) => {
111
161
replaceStyles ( originalStyles . current ) ;
112
162
} ) ;
113
163
}
114
- } , [ numPluginToasts , replaceStyles , originalStyles ] ) ;
164
+ } , [ numPluginToasts , replaceStyles ] ) ;
115
165
116
- // initializes plugin metadata with plugin name
166
+ // Initialize plugin metadata with plugin name
117
167
const pluginMetaData : ReturnType < Plugin > = {
118
- name : "@rcb-plugins/input-validator"
168
+ name : "@rcb-plugins/input-validator" ,
119
169
} ;
120
170
121
- // adds required events in settings if auto config is true
171
+ // Add required events in settings if autoConfig is true
122
172
if ( mergedPluginConfig . autoConfig ) {
123
173
pluginMetaData . settings = {
124
174
event : {
125
175
rcbUserSubmitText : true ,
176
+ rcbUserUploadFile : true ,
126
177
rcbDismissToast : true ,
127
178
} ,
128
179
} ;
0 commit comments