@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
22import {
33 useBotId ,
44 RcbUserSubmitTextEvent ,
5+ RcbUserUploadFileEvent ,
56 useToasts ,
67 useFlow ,
78 useStyles ,
@@ -31,67 +32,116 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
3132
3233 useEffect ( ( ) => {
3334 /**
34- * Handles the user submitting input event.
35+ * Handles the user submitting text input event.
3536 *
36- * @param event event emitted when user submits input
37+ * @param event Event emitted when user submits text input.
3738 */
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+ ) ;
4149 if ( ! validator ) {
4250 return ;
4351 }
4452
45- // gets and checks validation result
53+ // Get and check validation result
4654 const validationResult = validator (
47- event . data . inputText
55+ rcbEvent . data . inputText
4856 ) as ValidationResult ;
4957 if ( ! validationResult ?. success ) {
5058 event . preventDefault ( ) ;
5159 }
5260
53- // if nothing to prompt, return
61+ // If nothing to prompt, return
5462 if ( ! validationResult . promptContent ) {
5563 return ;
5664 }
5765
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
5967 if ( numPluginToasts === 0 ) {
60- originalStyles . current = structuredClone ( styles )
68+ originalStyles . current = structuredClone ( styles ) ;
6169 }
6270 const promptStyles = getPromptStyles (
6371 validationResult ,
6472 mergedPluginConfig
6573 ) ;
6674
67- // update toast with prompt styles
75+ // Update styles with prompt styles
6876 updateStyles ( promptStyles ) ;
6977
70- // shows prompt toast to user
78+ // Show prompt toast to user
7179 showToast (
7280 validationResult . promptContent ,
7381 validationResult . promptDuration ?? 3000
7482 ) ;
7583
76- // increases number of plugin toasts by 1
84+ // Increase number of plugin toasts by 1
7785 setNumPluginToasts ( ( prev ) => prev + 1 ) ;
7886 } ;
7987
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+
80127 /**
81128 * Handles the dismiss toast event.
82129 *
83- * @param event event emitted when toast is dismissed
130+ * @param event Event emitted when toast is dismissed.
84131 */
85132 const handleDismissToast = ( ) : void => {
86133 setNumPluginToasts ( ( prev ) => prev - 1 ) ;
87134 } ;
88135
89- // adds required events
136+ // Add required event listeners
90137 window . addEventListener ( "rcb-user-submit-text" , handleUserSubmitText ) ;
138+ window . addEventListener ( "rcb-user-upload-file" , handleUserUploadFile ) ;
91139 window . addEventListener ( "rcb-dismiss-toast" , handleDismissToast ) ;
92140
93141 return ( ) => {
142+ // Remove event listeners
94143 window . removeEventListener ( "rcb-user-submit-text" , handleUserSubmitText ) ;
144+ window . removeEventListener ( "rcb-user-upload-file" , handleUserUploadFile ) ;
95145 window . removeEventListener ( "rcb-dismiss-toast" , handleDismissToast ) ;
96146 } ;
97147 } , [
@@ -101,28 +151,29 @@ const useRcbPlugin = (pluginConfig?: PluginConfig) => {
101151 updateStyles ,
102152 styles ,
103153 mergedPluginConfig ,
104- numPluginToasts
154+ numPluginToasts ,
105155 ] ) ;
106156
107- // restores original styles when plugin toasts are all dismissed
157+ // Restore original styles when all plugin toasts are dismissed
108158 useEffect ( ( ) => {
109159 if ( numPluginToasts === 0 ) {
110160 setTimeout ( ( ) => {
111161 replaceStyles ( originalStyles . current ) ;
112162 } ) ;
113163 }
114- } , [ numPluginToasts , replaceStyles , originalStyles ] ) ;
164+ } , [ numPluginToasts , replaceStyles ] ) ;
115165
116- // initializes plugin metadata with plugin name
166+ // Initialize plugin metadata with plugin name
117167 const pluginMetaData : ReturnType < Plugin > = {
118- name : "@rcb-plugins/input-validator"
168+ name : "@rcb-plugins/input-validator" ,
119169 } ;
120170
121- // adds required events in settings if auto config is true
171+ // Add required events in settings if autoConfig is true
122172 if ( mergedPluginConfig . autoConfig ) {
123173 pluginMetaData . settings = {
124174 event : {
125175 rcbUserSubmitText : true ,
176+ rcbUserUploadFile : true ,
126177 rcbDismissToast : true ,
127178 } ,
128179 } ;
0 commit comments