@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
22import {
33 useBotId ,
44 RcbUserSubmitTextEvent ,
5+ RcbUserUploadFileEvent ,
56 useToasts ,
67 useFlow ,
78 useStyles ,
@@ -17,7 +18,7 @@ import { getValidator } from "../utils/getValidator";
1718/**
1819 * Plugin hook that handles all the core logic.
1920 *
20- * @param pluginConfig configurations for the plugin
21+ * @param pluginConfig Configurations for the plugin.
2122 */
2223const useRcbPlugin = ( pluginConfig ?: PluginConfig ) => {
2324 const { showToast } = useToasts ( ) ;
@@ -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 ( rcbEvent , getBotId ( ) , getFlow ( ) ) ;
4144 if ( ! validator ) {
4245 return ;
4346 }
4447
45- // gets and checks validation result
48+ // Get and check validation result
4649 const validationResult = validator (
47- event . data . inputText
50+ rcbEvent . data . inputText
4851 ) as ValidationResult ;
4952 if ( ! validationResult ?. success ) {
5053 event . preventDefault ( ) ;
5154 }
5255
53- // if nothing to prompt, return
56+ // If nothing to prompt, return
5457 if ( ! validationResult . promptContent ) {
5558 return ;
5659 }
5760
58- // if this is the first plugin toast, preserve original styles for restoration later
61+ // Preserve original styles if this is the first plugin toast
5962 if ( numPluginToasts === 0 ) {
60- originalStyles . current = structuredClone ( styles )
63+ originalStyles . current = structuredClone ( styles ) ;
6164 }
6265 const promptStyles = getPromptStyles (
6366 validationResult ,
6467 mergedPluginConfig
6568 ) ;
6669
67- // update toast with prompt styles
70+ // Update styles with prompt styles
6871 updateStyles ( promptStyles ) ;
6972
70- // shows prompt toast to user
73+ // Show prompt toast to user
7174 showToast (
7275 validationResult . promptContent ,
7376 validationResult . promptDuration ?? 3000
7477 ) ;
7578
76- // increases number of plugin toasts by 1
79+ // Increase number of plugin toasts by 1
7780 setNumPluginToasts ( ( prev ) => prev + 1 ) ;
7881 } ;
7982
8083 /**
81- * Handles the dismiss toast event.
84+ * Handles the user uploading a file event.
8285 *
83- * @param event event emitted when toast is dismissed
86+ * @param event Event emitted when user uploads a file.
87+ */
88+ const handleUserUploadFile = ( event : Event ) : void => {
89+ const rcbEvent = event as RcbUserUploadFileEvent ;
90+ const file : File = rcbEvent . data . files [ 0 ] ;
91+
92+ // Get validator and if no validator, return
93+ const validator = getValidator ( rcbEvent , getBotId ( ) , getFlow ( ) ) ;
94+ if ( ! validator ) {
95+ return ;
96+ }
97+
98+ // Perform validation
99+ const validationResult = validator ( file ) as ValidationResult ;
100+ if ( ! validationResult ?. success ) {
101+ event . preventDefault ( ) ;
102+ }
103+
104+ // Show prompt if necessary
105+ if ( validationResult . promptContent ) {
106+ // Preserve original styles if this is the first plugin toast
107+ if ( numPluginToasts === 0 ) {
108+ originalStyles . current = structuredClone ( styles ) ;
109+ }
110+ const promptStyles = getPromptStyles (
111+ validationResult ,
112+ mergedPluginConfig
113+ ) ;
114+
115+ // Update styles with prompt styles
116+ updateStyles ( promptStyles ) ;
117+
118+ // Show prompt toast to user
119+ showToast (
120+ validationResult . promptContent ,
121+ validationResult . promptDuration ?? 3000
122+ ) ;
123+
124+ // Increase number of plugin toasts by 1
125+ setNumPluginToasts ( ( prev ) => prev + 1 ) ;
126+ }
127+ } ;
128+
129+ /**
130+ * Handles the dismiss toast event.
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